blob: fd9cf1544361be82ba10830dcf44470abf8c3fcb [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 Gohman69de1932008-02-06 22:27:42 +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
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000489 // double. This shrinks FP constants and canonicalizes them for targets where
490 // an FP extending load is the same cost as a normal load (such as on the x87
491 // fp stack or PPC FP unit).
Evan Cheng00495212006-12-12 21:32:44 +0000492 MVT::ValueType VT = CFP->getValueType(0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +0000493 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000494 CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000495 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000496 if (VT!=MVT::f64 && VT!=MVT::f32)
497 assert(0 && "Invalid type expansion");
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000498 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
Evan Chengef120572008-03-04 08:05:30 +0000499 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000500 }
501
Evan Chengef120572008-03-04 08:05:30 +0000502 MVT::ValueType OrigVT = VT;
503 MVT::ValueType SVT = VT;
504 while (SVT != MVT::f32) {
505 SVT = (unsigned)SVT - 1;
506 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
507 // Only do this if the target has a native EXTLOAD instruction from
508 // smaller type.
Evan Cheng6fd599f2008-03-05 01:30:59 +0000509 TLI.isLoadXLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000510 TLI.ShouldShrinkFPConstant(OrigVT)) {
Evan Chengef120572008-03-04 08:05:30 +0000511 const Type *SType = MVT::getTypeForValueType(SVT);
512 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
513 VT = SVT;
514 Extend = true;
515 }
Evan Cheng00495212006-12-12 21:32:44 +0000516 }
517
518 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Chengef120572008-03-04 08:05:30 +0000519 if (Extend)
520 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000521 CPIdx, PseudoSourceValue::getConstantPool(),
Evan Chengef120572008-03-04 08:05:30 +0000522 0, VT);
523 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
524 PseudoSourceValue::getConstantPool(), 0);
Evan Cheng00495212006-12-12 21:32:44 +0000525}
526
Chris Lattner6831a812006-02-13 09:18:02 +0000527
Evan Cheng912095b2007-01-04 21:56:39 +0000528/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
529/// operations.
530static
531SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
532 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000533 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000534 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000535 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
536 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000537 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000538
Evan Cheng912095b2007-01-04 21:56:39 +0000539 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000540 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000541 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
542 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000543 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000544 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000545 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000546 // Shift right or sign-extend it if the two operands have different types.
547 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
548 if (SizeDiff > 0) {
549 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
550 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
551 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
552 } else if (SizeDiff < 0)
553 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000554
555 // Clear the sign bit of first operand.
556 SDOperand Mask2 = (VT == MVT::f64)
557 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
558 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
559 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000560 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000561 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
562
563 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000564 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
565 return Result;
566}
567
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000568/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
569static
570SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
571 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000572 SDOperand Chain = ST->getChain();
573 SDOperand Ptr = ST->getBasePtr();
574 SDOperand Val = ST->getValue();
575 MVT::ValueType VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000576 int Alignment = ST->getAlignment();
577 int SVOffset = ST->getSrcValueOffset();
Dale Johannesen8155d642008-02-27 22:36:00 +0000578 if (MVT::isFloatingPoint(ST->getMemoryVT()) ||
579 MVT::isVector(ST->getMemoryVT())) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000580 // Expand to a bitconvert of the value to the integer type of the
581 // same size, then a (misaligned) int store.
582 MVT::ValueType intVT;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000583 if (MVT::is128BitVector(VT) || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000584 intVT = MVT::i128;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000585 else if (MVT::is64BitVector(VT) || VT==MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000586 intVT = MVT::i64;
587 else if (VT==MVT::f32)
588 intVT = MVT::i32;
589 else
Dale Johannesencd9f1742008-02-28 18:36:51 +0000590 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000591
592 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
593 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
594 SVOffset, ST->isVolatile(), Alignment);
595 }
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000596 assert(MVT::isInteger(ST->getMemoryVT()) &&
Dale Johannesen8155d642008-02-27 22:36:00 +0000597 !MVT::isVector(ST->getMemoryVT()) &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000598 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000599 // Get the half-size VT
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000600 MVT::ValueType NewStoredVT = ST->getMemoryVT() - 1;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000601 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000602 int IncrementSize = NumBits / 8;
603
604 // Divide the stored value in two parts.
605 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
606 SDOperand Lo = Val;
607 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
608
609 // Store the two parts
610 SDOperand Store1, Store2;
611 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
612 ST->getSrcValue(), SVOffset, NewStoredVT,
613 ST->isVolatile(), Alignment);
614 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
615 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000616 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000617 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
618 ST->getSrcValue(), SVOffset + IncrementSize,
619 NewStoredVT, ST->isVolatile(), Alignment);
620
621 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
622}
623
624/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
625static
626SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
627 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000628 int SVOffset = LD->getSrcValueOffset();
629 SDOperand Chain = LD->getChain();
630 SDOperand Ptr = LD->getBasePtr();
631 MVT::ValueType VT = LD->getValueType(0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000632 MVT::ValueType LoadedVT = LD->getMemoryVT();
Dale Johannesen8155d642008-02-27 22:36:00 +0000633 if (MVT::isFloatingPoint(VT) || MVT::isVector(VT)) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000634 // Expand to a (misaligned) integer load of the same size,
Dale Johannesen8155d642008-02-27 22:36:00 +0000635 // then bitconvert to floating point or vector.
Dale Johannesen907f28c2007-09-08 19:29:23 +0000636 MVT::ValueType intVT;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000637 if (MVT::is128BitVector(LoadedVT) ||
638 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000639 intVT = MVT::i128;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000640 else if (MVT::is64BitVector(LoadedVT) || LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000641 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000642 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000643 intVT = MVT::i32;
644 else
Dale Johannesen8155d642008-02-27 22:36:00 +0000645 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000646
647 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
648 SVOffset, LD->isVolatile(),
649 LD->getAlignment());
650 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Dale Johannesen8155d642008-02-27 22:36:00 +0000651 if (MVT::isFloatingPoint(VT) && LoadedVT != VT)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000652 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
653
654 SDOperand Ops[] = { Result, Chain };
655 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
656 Ops, 2);
657 }
Dale Johannesen8155d642008-02-27 22:36:00 +0000658 assert(MVT::isInteger(LoadedVT) && !MVT::isVector(LoadedVT) &&
Chris Lattnere400af82007-11-19 21:38:03 +0000659 "Unaligned load of unsupported type.");
660
Dale Johannesen8155d642008-02-27 22:36:00 +0000661 // Compute the new VT that is half the size of the old one. This is an
662 // integer MVT.
Chris Lattnere400af82007-11-19 21:38:03 +0000663 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
664 MVT::ValueType NewLoadedVT;
Dale Johannesen8155d642008-02-27 22:36:00 +0000665 NewLoadedVT = MVT::getIntegerType(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000666 NumBits >>= 1;
667
668 unsigned Alignment = LD->getAlignment();
669 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000670 ISD::LoadExtType HiExtType = LD->getExtensionType();
671
672 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
673 if (HiExtType == ISD::NON_EXTLOAD)
674 HiExtType = ISD::ZEXTLOAD;
675
676 // Load the value in two parts
677 SDOperand Lo, Hi;
678 if (TLI.isLittleEndian()) {
679 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
680 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
681 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
682 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
683 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
684 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000685 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000686 } else {
687 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
688 NewLoadedVT,LD->isVolatile(), Alignment);
689 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
690 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
691 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
692 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000693 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000694 }
695
696 // aggregate the two parts
697 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
698 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
699 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
700
701 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
702 Hi.getValue(1));
703
704 SDOperand Ops[] = { Result, TF };
705 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
706}
Evan Cheng912095b2007-01-04 21:56:39 +0000707
Dan Gohman82669522007-10-11 23:57:53 +0000708/// UnrollVectorOp - We know that the given vector has a legal type, however
709/// the operation it performs is not legal and is an operation that we have
710/// no way of lowering. "Unroll" the vector, splitting out the scalars and
711/// operating on each element individually.
712SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
713 MVT::ValueType VT = Op.getValueType();
714 assert(isTypeLegal(VT) &&
715 "Caller should expand or promote operands that are not legal!");
716 assert(Op.Val->getNumValues() == 1 &&
717 "Can't unroll a vector with multiple results!");
718 unsigned NE = MVT::getVectorNumElements(VT);
719 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
720
721 SmallVector<SDOperand, 8> Scalars;
722 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
723 for (unsigned i = 0; i != NE; ++i) {
724 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
725 SDOperand Operand = Op.getOperand(j);
726 MVT::ValueType OperandVT = Operand.getValueType();
727 if (MVT::isVector(OperandVT)) {
728 // A vector operand; extract a single element.
729 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
730 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
731 OperandEltVT,
732 Operand,
733 DAG.getConstant(i, MVT::i32));
734 } else {
735 // A scalar operand; just use it as is.
736 Operands[j] = Operand;
737 }
738 }
739 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
740 &Operands[0], Operands.size()));
741 }
742
743 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
744}
745
Duncan Sands007f9842008-01-10 10:28:30 +0000746/// GetFPLibCall - Return the right libcall for the given floating point type.
747static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
748 RTLIB::Libcall Call_F32,
749 RTLIB::Libcall Call_F64,
750 RTLIB::Libcall Call_F80,
751 RTLIB::Libcall Call_PPCF128) {
752 return
753 VT == MVT::f32 ? Call_F32 :
754 VT == MVT::f64 ? Call_F64 :
755 VT == MVT::f80 ? Call_F80 :
756 VT == MVT::ppcf128 ? Call_PPCF128 :
757 RTLIB::UNKNOWN_LIBCALL;
758}
759
Dan Gohmana3466152007-07-13 20:14:11 +0000760/// LegalizeOp - We know that the specified value has a legal type, and
761/// that its operands are legal. Now ensure that the operation itself
762/// is legal, recursively ensuring that the operands' operations remain
763/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000764SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000765 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
766 return Op;
767
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000768 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000769 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000770 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000771
Chris Lattner3e928bb2005-01-07 07:47:09 +0000772 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000773 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000774 if (Node->getNumValues() > 1) {
775 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000776 if (getTypeAction(Node->getValueType(i)) != Legal) {
777 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000778 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000779 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000780 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000781 }
782 }
783
Chris Lattner45982da2005-05-12 16:53:42 +0000784 // Note that LegalizeOp may be reentered even from single-use nodes, which
785 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000786 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000787 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000788
Nate Begeman9373a812005-08-10 20:51:12 +0000789 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000790 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000791 bool isCustom = false;
792
Chris Lattner3e928bb2005-01-07 07:47:09 +0000793 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000794 case ISD::FrameIndex:
795 case ISD::EntryToken:
796 case ISD::Register:
797 case ISD::BasicBlock:
798 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000799 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000800 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000801 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000802 case ISD::TargetConstantPool:
803 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000804 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000805 case ISD::TargetExternalSymbol:
806 case ISD::VALUETYPE:
807 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +0000808 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +0000809 case ISD::STRING:
810 case ISD::CONDCODE:
811 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000812 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000813 "This must be legal!");
814 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000815 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000816 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
817 // If this is a target node, legalize it by legalizing the operands then
818 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000819 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000820 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000821 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000822
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000823 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000824
825 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
826 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
827 return Result.getValue(Op.ResNo);
828 }
829 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000830#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000831 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000832#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000833 assert(0 && "Do not know how to legalize this operator!");
834 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000835 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000836 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000837 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000838 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000839 case ISD::ConstantPool:
840 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000841 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
842 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000843 case TargetLowering::Custom:
844 Tmp1 = TLI.LowerOperation(Op, DAG);
845 if (Tmp1.Val) Result = Tmp1;
846 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000847 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000848 break;
849 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000850 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000851 case ISD::FRAMEADDR:
852 case ISD::RETURNADDR:
853 // The only option for these nodes is to custom lower them. If the target
854 // does not custom lower them, then return zero.
855 Tmp1 = TLI.LowerOperation(Op, DAG);
856 if (Tmp1.Val)
857 Result = Tmp1;
858 else
859 Result = DAG.getConstant(0, TLI.getPointerTy());
860 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000861 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000862 MVT::ValueType VT = Node->getValueType(0);
863 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
864 default: assert(0 && "This action is not supported yet!");
865 case TargetLowering::Custom:
866 Result = TLI.LowerOperation(Op, DAG);
867 if (Result.Val) break;
868 // Fall Thru
869 case TargetLowering::Legal:
870 Result = DAG.getConstant(0, VT);
871 break;
872 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000873 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000874 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000875 case ISD::EXCEPTIONADDR: {
876 Tmp1 = LegalizeOp(Node->getOperand(0));
877 MVT::ValueType VT = Node->getValueType(0);
878 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
879 default: assert(0 && "This action is not supported yet!");
880 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000881 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000882 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000883 }
884 break;
885 case TargetLowering::Custom:
886 Result = TLI.LowerOperation(Op, DAG);
887 if (Result.Val) break;
888 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000889 case TargetLowering::Legal: {
890 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
891 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000892 Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000893 break;
894 }
895 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000896 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000897 if (Result.Val->getNumValues() == 1) break;
898
899 assert(Result.Val->getNumValues() == 2 &&
900 "Cannot return more than two values!");
901
902 // Since we produced two values, make sure to remember that we
903 // legalized both of them.
904 Tmp1 = LegalizeOp(Result);
905 Tmp2 = LegalizeOp(Result.getValue(1));
906 AddLegalizedOperand(Op.getValue(0), Tmp1);
907 AddLegalizedOperand(Op.getValue(1), Tmp2);
908 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000909 case ISD::EHSELECTION: {
910 Tmp1 = LegalizeOp(Node->getOperand(0));
911 Tmp2 = LegalizeOp(Node->getOperand(1));
912 MVT::ValueType VT = Node->getValueType(0);
913 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
914 default: assert(0 && "This action is not supported yet!");
915 case TargetLowering::Expand: {
916 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000917 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000918 }
919 break;
920 case TargetLowering::Custom:
921 Result = TLI.LowerOperation(Op, DAG);
922 if (Result.Val) break;
923 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000924 case TargetLowering::Legal: {
925 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
926 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000927 Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +0000928 break;
929 }
930 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000931 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000932 if (Result.Val->getNumValues() == 1) break;
933
934 assert(Result.Val->getNumValues() == 2 &&
935 "Cannot return more than two values!");
936
937 // Since we produced two values, make sure to remember that we
938 // legalized both of them.
939 Tmp1 = LegalizeOp(Result);
940 Tmp2 = LegalizeOp(Result.getValue(1));
941 AddLegalizedOperand(Op.getValue(0), Tmp1);
942 AddLegalizedOperand(Op.getValue(1), Tmp2);
943 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000944 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000945 MVT::ValueType VT = Node->getValueType(0);
946 // The only "good" option for this node is to custom lower it.
947 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
948 default: assert(0 && "This action is not supported at all!");
949 case TargetLowering::Custom:
950 Result = TLI.LowerOperation(Op, DAG);
951 if (Result.Val) break;
952 // Fall Thru
953 case TargetLowering::Legal:
954 // Target does not know, how to lower this, lower to noop
955 Result = LegalizeOp(Node->getOperand(0));
956 break;
957 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000958 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000959 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000960 case ISD::AssertSext:
961 case ISD::AssertZext:
962 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000963 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000964 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000965 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000966 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000967 Result = Node->getOperand(Op.ResNo);
968 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000969 case ISD::CopyFromReg:
970 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000971 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000972 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000973 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000974 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000975 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000976 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000977 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000978 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
979 } else {
980 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
981 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000982 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
983 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000984 // Since CopyFromReg produces two values, make sure to remember that we
985 // legalized both of them.
986 AddLegalizedOperand(Op.getValue(0), Result);
987 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
988 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000989 case ISD::UNDEF: {
990 MVT::ValueType VT = Op.getValueType();
991 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000992 default: assert(0 && "This action is not supported yet!");
993 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000994 if (MVT::isInteger(VT))
995 Result = DAG.getConstant(0, VT);
996 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000997 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
998 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000999 else
1000 assert(0 && "Unknown value type!");
1001 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001002 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001003 break;
1004 }
1005 break;
1006 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001007
Chris Lattner48b61a72006-03-28 00:40:33 +00001008 case ISD::INTRINSIC_W_CHAIN:
1009 case ISD::INTRINSIC_WO_CHAIN:
1010 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001011 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001012 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1013 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001014 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001015
1016 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001017 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001018 TargetLowering::Custom) {
1019 Tmp3 = TLI.LowerOperation(Result, DAG);
1020 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001021 }
1022
1023 if (Result.Val->getNumValues() == 1) break;
1024
1025 // Must have return value and chain result.
1026 assert(Result.Val->getNumValues() == 2 &&
1027 "Cannot return more than two values!");
1028
1029 // Since loads produce two values, make sure to remember that we
1030 // legalized both of them.
1031 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1032 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1033 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001034 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001035
1036 case ISD::LOCATION:
1037 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1038 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1039
1040 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1041 case TargetLowering::Promote:
1042 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001043 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001044 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001045 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +00001046 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001047
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001048 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001049 const std::string &FName =
1050 cast<StringSDNode>(Node->getOperand(3))->getValue();
1051 const std::string &DirName =
1052 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001053 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001054
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001055 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +00001056 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +00001057 SDOperand LineOp = Node->getOperand(1);
1058 SDOperand ColOp = Node->getOperand(2);
1059
1060 if (useDEBUG_LOC) {
1061 Ops.push_back(LineOp); // line #
1062 Ops.push_back(ColOp); // col #
1063 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001064 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001065 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +00001066 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1067 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Evan Chenga647c922008-02-01 02:05:57 +00001068 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001069 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Evan Chengbb81d972008-01-31 09:59:15 +00001070 Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label
1071 Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001072 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001073 } else {
1074 Result = Tmp1; // chain
1075 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001076 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001077 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001078 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +00001079 if (Tmp1 != Node->getOperand(0) ||
1080 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001081 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001082 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +00001083 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1084 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1085 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1086 } else {
1087 // Otherwise promote them.
1088 Ops.push_back(PromoteOp(Node->getOperand(1)));
1089 Ops.push_back(PromoteOp(Node->getOperand(2)));
1090 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001091 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1092 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001093 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001094 }
1095 break;
1096 }
1097 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001098
1099 case ISD::DECLARE:
1100 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1101 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1102 default: assert(0 && "This action is not supported yet!");
1103 case TargetLowering::Legal:
1104 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1105 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1106 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1107 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1108 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001109 case TargetLowering::Expand:
1110 Result = LegalizeOp(Node->getOperand(0));
1111 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001112 }
1113 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001114
1115 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001116 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001117 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001118 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001119 case TargetLowering::Legal:
1120 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1121 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1122 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1123 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001124 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001125 break;
1126 }
1127 break;
1128
Jim Laskey1ee29252007-01-26 14:34:52 +00001129 case ISD::LABEL:
Evan Chengbb81d972008-01-31 09:59:15 +00001130 assert(Node->getNumOperands() == 3 && "Invalid LABEL node!");
Jim Laskey1ee29252007-01-26 14:34:52 +00001131 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001132 default: assert(0 && "This action is not supported yet!");
1133 case TargetLowering::Legal:
1134 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1135 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Evan Chengbb81d972008-01-31 09:59:15 +00001136 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand.
1137 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001138 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001139 case TargetLowering::Expand:
1140 Result = LegalizeOp(Node->getOperand(0));
1141 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001142 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001143 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001144
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001145 case ISD::MEMBARRIER: {
1146 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001147 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1148 default: assert(0 && "This action is not supported yet!");
1149 case TargetLowering::Legal: {
1150 SDOperand Ops[6];
1151 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001152 for (int x = 1; x < 6; ++x) {
1153 Ops[x] = Node->getOperand(x);
1154 if (!isTypeLegal(Ops[x].getValueType()))
1155 Ops[x] = PromoteOp(Ops[x]);
1156 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001157 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1158 break;
1159 }
1160 case TargetLowering::Expand:
1161 //There is no libgcc call for this op
1162 Result = Node->getOperand(0); // Noop
1163 break;
1164 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001165 break;
1166 }
1167
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001168 case ISD::ATOMIC_LCS:
1169 case ISD::ATOMIC_LAS:
1170 case ISD::ATOMIC_SWAP: {
1171 assert(((Node->getNumOperands() == 4 && Node->getOpcode() == ISD::ATOMIC_LCS) ||
1172 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_LAS) ||
1173 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_SWAP)) &&
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001174 "Invalid Atomic node!");
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001175 int num = Node->getOpcode() == ISD::ATOMIC_LCS ? 4 : 3;
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001176 SDOperand Ops[4];
1177 for (int x = 0; x < num; ++x)
1178 Ops[x] = LegalizeOp(Node->getOperand(x));
1179 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num);
1180
1181 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001182 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001183 case TargetLowering::Custom:
1184 Result = TLI.LowerOperation(Result, DAG);
1185 break;
1186 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001187 break;
1188 }
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001189 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1190 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1191 return Result.getValue(Op.ResNo);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001192 }
1193
Scott Michelc1513d22007-08-08 23:23:31 +00001194 case ISD::Constant: {
1195 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1196 unsigned opAction =
1197 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1198
Chris Lattner3e928bb2005-01-07 07:47:09 +00001199 // We know we don't need to expand constants here, constants only have one
1200 // value and we check that it is fine above.
1201
Scott Michelc1513d22007-08-08 23:23:31 +00001202 if (opAction == TargetLowering::Custom) {
1203 Tmp1 = TLI.LowerOperation(Result, DAG);
1204 if (Tmp1.Val)
1205 Result = Tmp1;
1206 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001207 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001208 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001209 case ISD::ConstantFP: {
1210 // Spill FP immediates to the constant pool if the target cannot directly
1211 // codegen them. Targets often have some immediate values that can be
1212 // efficiently generated into an FP register without a load. We explicitly
1213 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001214 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1215
Chris Lattner3181a772006-01-29 06:26:56 +00001216 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1217 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001218 case TargetLowering::Legal:
1219 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001220 case TargetLowering::Custom:
1221 Tmp3 = TLI.LowerOperation(Result, DAG);
1222 if (Tmp3.Val) {
1223 Result = Tmp3;
1224 break;
1225 }
1226 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001227 case TargetLowering::Expand: {
1228 // Check to see if this FP immediate is already legal.
1229 bool isLegal = false;
1230 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1231 E = TLI.legal_fpimm_end(); I != E; ++I) {
1232 if (CFP->isExactlyValue(*I)) {
1233 isLegal = true;
1234 break;
1235 }
1236 }
1237 // If this is a legal constant, turn it into a TargetConstantFP node.
1238 if (isLegal)
1239 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001240 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001241 }
Nate Begemane1795842008-02-14 08:57:00 +00001242 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001243 break;
1244 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001245 case ISD::TokenFactor:
1246 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001247 Tmp1 = LegalizeOp(Node->getOperand(0));
1248 Tmp2 = LegalizeOp(Node->getOperand(1));
1249 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1250 } else if (Node->getNumOperands() == 3) {
1251 Tmp1 = LegalizeOp(Node->getOperand(0));
1252 Tmp2 = LegalizeOp(Node->getOperand(1));
1253 Tmp3 = LegalizeOp(Node->getOperand(2));
1254 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001255 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001256 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001257 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001258 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1259 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001260 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001261 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001262 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001263
1264 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001265 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001266 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001267 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1268 assert(Tmp3.Val && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001269 // A call within a calling sequence must be legalized to something
1270 // other than the normal CALLSEQ_END. Violating this gets Legalize
1271 // into an infinite loop.
1272 assert ((!IsLegalizingCall ||
1273 Node->getOpcode() != ISD::CALL ||
1274 Tmp3.Val->getOpcode() != ISD::CALLSEQ_END) &&
1275 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001276
1277 // The number of incoming and outgoing values should match; unless the final
1278 // outgoing value is a flag.
1279 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1280 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1281 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1282 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001283 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001284
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001285 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001286 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001287 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001288 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1289 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001290 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001291 if (Op.ResNo == i)
1292 Tmp2 = Tmp1;
1293 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1294 }
1295 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001296 case ISD::EXTRACT_SUBREG: {
1297 Tmp1 = LegalizeOp(Node->getOperand(0));
1298 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1299 assert(idx && "Operand must be a constant");
1300 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1301 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1302 }
1303 break;
1304 case ISD::INSERT_SUBREG: {
1305 Tmp1 = LegalizeOp(Node->getOperand(0));
1306 Tmp2 = LegalizeOp(Node->getOperand(1));
1307 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1308 assert(idx && "Operand must be a constant");
1309 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1310 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1311 }
1312 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001313 case ISD::BUILD_VECTOR:
1314 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001315 default: assert(0 && "This action is not supported yet!");
1316 case TargetLowering::Custom:
1317 Tmp3 = TLI.LowerOperation(Result, DAG);
1318 if (Tmp3.Val) {
1319 Result = Tmp3;
1320 break;
1321 }
1322 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001323 case TargetLowering::Expand:
1324 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001325 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001326 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001327 break;
1328 case ISD::INSERT_VECTOR_ELT:
1329 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001330 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001331
1332 // The type of the value to insert may not be legal, even though the vector
1333 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1334 // here.
1335 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1336 default: assert(0 && "Cannot expand insert element operand");
1337 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1338 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1339 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001340 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1341
1342 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1343 Node->getValueType(0))) {
1344 default: assert(0 && "This action is not supported yet!");
1345 case TargetLowering::Legal:
1346 break;
1347 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001348 Tmp4 = TLI.LowerOperation(Result, DAG);
1349 if (Tmp4.Val) {
1350 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001351 break;
1352 }
1353 // FALLTHROUGH
1354 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001355 // If the insert index is a constant, codegen this as a scalar_to_vector,
1356 // then a shuffle that inserts it into the right position in the vector.
1357 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001358 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1359 // match the element type of the vector being created.
1360 if (Tmp2.getValueType() ==
1361 MVT::getVectorElementType(Op.getValueType())) {
1362 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1363 Tmp1.getValueType(), Tmp2);
1364
1365 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1366 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1367 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1368
1369 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1370 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1371 // elt 0 of the RHS.
1372 SmallVector<SDOperand, 8> ShufOps;
1373 for (unsigned i = 0; i != NumElts; ++i) {
1374 if (i != InsertPos->getValue())
1375 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1376 else
1377 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1378 }
1379 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1380 &ShufOps[0], ShufOps.size());
1381
1382 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1383 Tmp1, ScVec, ShufMask);
1384 Result = LegalizeOp(Result);
1385 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001386 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001387 }
1388
Chris Lattner2332b9f2006-03-19 01:17:20 +00001389 // If the target doesn't support this, we have to spill the input vector
1390 // to a temporary stack slot, update the element, then reload it. This is
1391 // badness. We could also load the value into a vector register (either
1392 // with a "move to register" or "extload into register" instruction, then
1393 // permute it into place, if the idx is a constant and if the idx is
1394 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001395 MVT::ValueType VT = Tmp1.getValueType();
Nate Begeman0325d902008-02-13 06:43:04 +00001396 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Chengf6bf87f2006-04-08 01:46:37 +00001397 MVT::ValueType IdxVT = Tmp3.getValueType();
1398 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner85dd3be2007-10-15 17:48:57 +00001399 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohman69de1932008-02-06 22:27:42 +00001400
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00001401 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val);
Dan Gohman69de1932008-02-06 22:27:42 +00001402 int SPFI = StackPtrFI->getIndex();
1403
Evan Chengeb0b4612006-03-31 01:27:51 +00001404 // Store the vector.
Dan Gohman69de1932008-02-06 22:27:42 +00001405 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00001406 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00001407 SPFI);
Evan Chengeb0b4612006-03-31 01:27:51 +00001408
1409 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001410 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1411 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001412 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001413 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1414 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1415 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001416 // Store the scalar value.
Nate Begeman0325d902008-02-13 06:43:04 +00001417 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
1418 PseudoSourceValue::getFixedStack(), SPFI, EltVT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001419 // Load the updated vector.
Dan Gohman69de1932008-02-06 22:27:42 +00001420 Result = DAG.getLoad(VT, Ch, StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00001421 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001422 break;
1423 }
1424 }
1425 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001426 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001427 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1428 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1429 break;
1430 }
1431
Chris Lattnerce872152006-03-19 06:31:19 +00001432 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1433 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1434 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1435 Node->getValueType(0))) {
1436 default: assert(0 && "This action is not supported yet!");
1437 case TargetLowering::Legal:
1438 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001439 case TargetLowering::Custom:
1440 Tmp3 = TLI.LowerOperation(Result, DAG);
1441 if (Tmp3.Val) {
1442 Result = Tmp3;
1443 break;
1444 }
1445 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001446 case TargetLowering::Expand:
1447 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001448 break;
1449 }
Chris Lattnerce872152006-03-19 06:31:19 +00001450 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001451 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001452 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1453 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1454 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1455
1456 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001457 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1458 default: assert(0 && "Unknown operation action!");
1459 case TargetLowering::Legal:
1460 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1461 "vector shuffle should not be created if not legal!");
1462 break;
1463 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001464 Tmp3 = TLI.LowerOperation(Result, DAG);
1465 if (Tmp3.Val) {
1466 Result = Tmp3;
1467 break;
1468 }
1469 // FALLTHROUGH
1470 case TargetLowering::Expand: {
1471 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001472 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001473 MVT::ValueType PtrVT = TLI.getPointerTy();
1474 SDOperand Mask = Node->getOperand(2);
1475 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001476 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001477 for (unsigned i = 0; i != NumElems; ++i) {
1478 SDOperand Arg = Mask.getOperand(i);
1479 if (Arg.getOpcode() == ISD::UNDEF) {
1480 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1481 } else {
1482 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1483 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1484 if (Idx < NumElems)
1485 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1486 DAG.getConstant(Idx, PtrVT)));
1487 else
1488 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1489 DAG.getConstant(Idx - NumElems, PtrVT)));
1490 }
1491 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001492 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001493 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001494 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001495 case TargetLowering::Promote: {
1496 // Change base type to a different vector type.
1497 MVT::ValueType OVT = Node->getValueType(0);
1498 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1499
1500 // Cast the two input vectors.
1501 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1502 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1503
1504 // Convert the shuffle mask to the right # elements.
1505 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1506 assert(Tmp3.Val && "Shuffle not legal?");
1507 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1508 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1509 break;
1510 }
Chris Lattner87100e02006-03-20 01:52:29 +00001511 }
1512 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001513
1514 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001515 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001516 Tmp2 = LegalizeOp(Node->getOperand(1));
1517 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001518 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001519 break;
1520
Dan Gohman7f321562007-06-25 16:23:39 +00001521 case ISD::EXTRACT_SUBVECTOR:
1522 Tmp1 = Node->getOperand(0);
1523 Tmp2 = LegalizeOp(Node->getOperand(1));
1524 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1525 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001526 break;
1527
Chris Lattner6831a812006-02-13 09:18:02 +00001528 case ISD::CALLSEQ_START: {
1529 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1530
1531 // Recursively Legalize all of the inputs of the call end that do not lead
1532 // to this call start. This ensures that any libcalls that need be inserted
1533 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001534 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001535 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001536 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1537 NodesLeadingTo);
1538 }
Chris Lattner6831a812006-02-13 09:18:02 +00001539
1540 // Now that we legalized all of the inputs (which may have inserted
1541 // libcalls) create the new CALLSEQ_START node.
1542 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1543
1544 // Merge in the last call, to ensure that this call start after the last
1545 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001546 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001547 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1548 Tmp1 = LegalizeOp(Tmp1);
1549 }
Chris Lattner6831a812006-02-13 09:18:02 +00001550
1551 // Do not try to legalize the target-specific arguments (#1+).
1552 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001553 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001554 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001555 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001556 }
1557
1558 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001559 AddLegalizedOperand(Op.getValue(0), Result);
1560 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1561 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1562
Chris Lattner6831a812006-02-13 09:18:02 +00001563 // Now that the callseq_start and all of the non-call nodes above this call
1564 // sequence have been legalized, legalize the call itself. During this
1565 // process, no libcalls can/will be inserted, guaranteeing that no calls
1566 // can overlap.
1567 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1568 SDOperand InCallSEQ = LastCALLSEQ_END;
1569 // Note that we are selecting this call!
1570 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1571 IsLegalizingCall = true;
1572
1573 // Legalize the call, starting from the CALLSEQ_END.
1574 LegalizeOp(LastCALLSEQ_END);
1575 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1576 return Result;
1577 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001578 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001579 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1580 // will cause this node to be legalized as well as handling libcalls right.
1581 if (LastCALLSEQ_END.Val != Node) {
1582 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001583 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001584 assert(I != LegalizedNodes.end() &&
1585 "Legalizing the call start should have legalized this node!");
1586 return I->second;
1587 }
1588
1589 // Otherwise, the call start has been legalized and everything is going
1590 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001591 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001592 // Do not try to legalize the target-specific arguments (#1+), except for
1593 // an optional flag input.
1594 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1595 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001596 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001597 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001598 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001599 }
1600 } else {
1601 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1602 if (Tmp1 != Node->getOperand(0) ||
1603 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001604 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001605 Ops[0] = Tmp1;
1606 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001607 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001608 }
Chris Lattner6a542892006-01-24 05:48:21 +00001609 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001610 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001611 // This finishes up call legalization.
1612 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001613
1614 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1615 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1616 if (Node->getNumValues() == 2)
1617 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1618 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001619 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001620 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001621 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1622 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1623 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001624 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001625
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001626 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001627 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001628 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001629 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001630 case TargetLowering::Expand: {
1631 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1632 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1633 " not tell us which reg is the stack pointer!");
1634 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001635
1636 // Chain the dynamic stack allocation so that it doesn't modify the stack
1637 // pointer when other instructions are using the stack.
1638 Chain = DAG.getCALLSEQ_START(Chain,
1639 DAG.getConstant(0, TLI.getPointerTy()));
1640
Chris Lattner903d2782006-01-15 08:54:32 +00001641 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001642 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1643 Chain = SP.getValue(1);
1644 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1645 unsigned StackAlign =
1646 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1647 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001648 SP = DAG.getNode(ISD::AND, VT, SP,
1649 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001650 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001651 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1652
1653 Tmp2 =
1654 DAG.getCALLSEQ_END(Chain,
1655 DAG.getConstant(0, TLI.getPointerTy()),
1656 DAG.getConstant(0, TLI.getPointerTy()),
1657 SDOperand());
1658
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001659 Tmp1 = LegalizeOp(Tmp1);
1660 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001661 break;
1662 }
1663 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001664 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1665 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001666 Tmp1 = LegalizeOp(Tmp3);
1667 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001668 }
Chris Lattner903d2782006-01-15 08:54:32 +00001669 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001670 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001671 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001672 }
Chris Lattner903d2782006-01-15 08:54:32 +00001673 // Since this op produce two values, make sure to remember that we
1674 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001675 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1676 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001677 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001678 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001679 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001680 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001681 bool Changed = false;
1682 // Legalize all of the operands of the inline asm, in case they are nodes
1683 // that need to be expanded or something. Note we skip the asm string and
1684 // all of the TargetConstant flags.
1685 SDOperand Op = LegalizeOp(Ops[0]);
1686 Changed = Op != Ops[0];
1687 Ops[0] = Op;
1688
1689 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1690 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1691 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1692 for (++i; NumVals; ++i, --NumVals) {
1693 SDOperand Op = LegalizeOp(Ops[i]);
1694 if (Op != Ops[i]) {
1695 Changed = true;
1696 Ops[i] = Op;
1697 }
1698 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001699 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001700
1701 if (HasInFlag) {
1702 Op = LegalizeOp(Ops.back());
1703 Changed |= Op != Ops.back();
1704 Ops.back() = Op;
1705 }
1706
1707 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001708 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001709
1710 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001711 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001712 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1713 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001714 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001715 case ISD::BR:
1716 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001717 // Ensure that libcalls are emitted before a branch.
1718 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1719 Tmp1 = LegalizeOp(Tmp1);
1720 LastCALLSEQ_END = DAG.getEntryNode();
1721
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001722 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001723 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001724 case ISD::BRIND:
1725 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1726 // Ensure that libcalls are emitted before a branch.
1727 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1728 Tmp1 = LegalizeOp(Tmp1);
1729 LastCALLSEQ_END = DAG.getEntryNode();
1730
1731 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1732 default: assert(0 && "Indirect target must be legal type (pointer)!");
1733 case Legal:
1734 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1735 break;
1736 }
1737 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1738 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001739 case ISD::BR_JT:
1740 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1741 // Ensure that libcalls are emitted before a branch.
1742 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1743 Tmp1 = LegalizeOp(Tmp1);
1744 LastCALLSEQ_END = DAG.getEntryNode();
1745
1746 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1747 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1748
1749 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1750 default: assert(0 && "This action is not supported yet!");
1751 case TargetLowering::Legal: break;
1752 case TargetLowering::Custom:
1753 Tmp1 = TLI.LowerOperation(Result, DAG);
1754 if (Tmp1.Val) Result = Tmp1;
1755 break;
1756 case TargetLowering::Expand: {
1757 SDOperand Chain = Result.getOperand(0);
1758 SDOperand Table = Result.getOperand(1);
1759 SDOperand Index = Result.getOperand(2);
1760
1761 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001762 MachineFunction &MF = DAG.getMachineFunction();
1763 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001764 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1765 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001766
1767 SDOperand LD;
1768 switch (EntrySize) {
1769 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001770 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001771 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001772 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001773 PseudoSourceValue::getJumpTable(), 0); break;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001774 }
1775
Evan Chengcc415862007-11-09 01:32:10 +00001776 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001777 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001778 // For PIC, the sequence is:
1779 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001780 // RelocBase can be JumpTable, GOT or some sort of global base.
1781 if (PTy != MVT::i32)
1782 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1783 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1784 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001785 }
Evan Chengcc415862007-11-09 01:32:10 +00001786 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001787 }
1788 }
1789 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001790 case ISD::BRCOND:
1791 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001792 // Ensure that libcalls are emitted before a return.
1793 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1794 Tmp1 = LegalizeOp(Tmp1);
1795 LastCALLSEQ_END = DAG.getEntryNode();
1796
Chris Lattner47e92232005-01-18 19:27:06 +00001797 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1798 case Expand: assert(0 && "It's impossible to expand bools");
1799 case Legal:
1800 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1801 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001802 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00001803 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001804
1805 // The top bits of the promoted condition are not necessarily zero, ensure
1806 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001807 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001808 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001809 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00001810 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001811 break;
1812 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001813 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001814
1815 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001816 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001817
1818 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1819 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001820 case TargetLowering::Legal: break;
1821 case TargetLowering::Custom:
1822 Tmp1 = TLI.LowerOperation(Result, DAG);
1823 if (Tmp1.Val) Result = Tmp1;
1824 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001825 case TargetLowering::Expand:
1826 // Expand brcond's setcc into its constituent parts and create a BR_CC
1827 // Node.
1828 if (Tmp2.getOpcode() == ISD::SETCC) {
1829 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1830 Tmp2.getOperand(0), Tmp2.getOperand(1),
1831 Node->getOperand(2));
1832 } else {
1833 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1834 DAG.getCondCode(ISD::SETNE), Tmp2,
1835 DAG.getConstant(0, Tmp2.getValueType()),
1836 Node->getOperand(2));
1837 }
1838 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001839 }
1840 break;
1841 case ISD::BR_CC:
1842 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001843 // Ensure that libcalls are emitted before a branch.
1844 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1845 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001846 Tmp2 = Node->getOperand(2); // LHS
1847 Tmp3 = Node->getOperand(3); // RHS
1848 Tmp4 = Node->getOperand(1); // CC
1849
1850 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001851 LastCALLSEQ_END = DAG.getEntryNode();
1852
Nate Begeman750ac1b2006-02-01 07:19:44 +00001853 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1854 // the LHS is a legal SETCC itself. In this case, we need to compare
1855 // the result against zero to select between true and false values.
1856 if (Tmp3.Val == 0) {
1857 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1858 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001859 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001860
1861 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1862 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001863
Chris Lattner181b7a32005-12-17 23:46:46 +00001864 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1865 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001866 case TargetLowering::Legal: break;
1867 case TargetLowering::Custom:
1868 Tmp4 = TLI.LowerOperation(Result, DAG);
1869 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001870 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001871 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001872 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001873 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001874 LoadSDNode *LD = cast<LoadSDNode>(Node);
1875 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1876 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001877
Evan Cheng466685d2006-10-09 20:57:25 +00001878 ISD::LoadExtType ExtType = LD->getExtensionType();
1879 if (ExtType == ISD::NON_EXTLOAD) {
1880 MVT::ValueType VT = Node->getValueType(0);
1881 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1882 Tmp3 = Result.getValue(0);
1883 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001884
Evan Cheng466685d2006-10-09 20:57:25 +00001885 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1886 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001887 case TargetLowering::Legal:
1888 // If this is an unaligned load and the target doesn't support it,
1889 // expand it.
1890 if (!TLI.allowsUnalignedMemoryAccesses()) {
1891 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001892 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001893 if (LD->getAlignment() < ABIAlignment){
1894 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1895 TLI);
1896 Tmp3 = Result.getOperand(0);
1897 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001898 Tmp3 = LegalizeOp(Tmp3);
1899 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001900 }
1901 }
1902 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001903 case TargetLowering::Custom:
1904 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1905 if (Tmp1.Val) {
1906 Tmp3 = LegalizeOp(Tmp1);
1907 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001908 }
Evan Cheng466685d2006-10-09 20:57:25 +00001909 break;
1910 case TargetLowering::Promote: {
1911 // Only promote a load of vector type to another.
1912 assert(MVT::isVector(VT) && "Cannot promote this load!");
1913 // Change base type to a different vector type.
1914 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1915
1916 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001917 LD->getSrcValueOffset(),
1918 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001919 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1920 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001921 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001922 }
Evan Cheng466685d2006-10-09 20:57:25 +00001923 }
1924 // Since loads produce two values, make sure to remember that we
1925 // legalized both of them.
1926 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1927 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1928 return Op.ResNo ? Tmp4 : Tmp3;
1929 } else {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001930 MVT::ValueType SrcVT = LD->getMemoryVT();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001931 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1932 int SVOffset = LD->getSrcValueOffset();
1933 unsigned Alignment = LD->getAlignment();
1934 bool isVolatile = LD->isVolatile();
1935
1936 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1937 // Some targets pretend to have an i1 loading operation, and actually
1938 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1939 // bits are guaranteed to be zero; it helps the optimizers understand
1940 // that these bits are zero. It is also useful for EXTLOAD, since it
1941 // tells the optimizers that those bits are undefined. It would be
1942 // nice to have an effective generic way of getting these benefits...
1943 // Until such a way is found, don't insist on promoting i1 here.
1944 (SrcVT != MVT::i1 ||
1945 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1946 // Promote to a byte-sized load if not loading an integral number of
1947 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1948 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1949 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1950 SDOperand Ch;
1951
1952 // The extra bits are guaranteed to be zero, since we stored them that
1953 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1954
1955 ISD::LoadExtType NewExtType =
1956 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1957
1958 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1959 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1960 NVT, isVolatile, Alignment);
1961
1962 Ch = Result.getValue(1); // The chain.
1963
1964 if (ExtType == ISD::SEXTLOAD)
1965 // Having the top bits zero doesn't help when sign extending.
1966 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1967 Result, DAG.getValueType(SrcVT));
1968 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1969 // All the top bits are guaranteed to be zero - inform the optimizers.
1970 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1971 DAG.getValueType(SrcVT));
1972
1973 Tmp1 = LegalizeOp(Result);
1974 Tmp2 = LegalizeOp(Ch);
1975 } else if (SrcWidth & (SrcWidth - 1)) {
1976 // If not loading a power-of-2 number of bits, expand as two loads.
1977 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1978 "Unsupported extload!");
1979 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1980 assert(RoundWidth < SrcWidth);
1981 unsigned ExtraWidth = SrcWidth - RoundWidth;
1982 assert(ExtraWidth < RoundWidth);
1983 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1984 "Load size not an integral number of bytes!");
1985 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
1986 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
1987 SDOperand Lo, Hi, Ch;
1988 unsigned IncrementSize;
1989
1990 if (TLI.isLittleEndian()) {
1991 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1992 // Load the bottom RoundWidth bits.
1993 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
1994 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1995 Alignment);
1996
1997 // Load the remaining ExtraWidth bits.
1998 IncrementSize = RoundWidth / 8;
1999 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2000 DAG.getIntPtrConstant(IncrementSize));
2001 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2002 LD->getSrcValue(), SVOffset + IncrementSize,
2003 ExtraVT, isVolatile,
2004 MinAlign(Alignment, IncrementSize));
2005
2006 // Build a factor node to remember that this load is independent of the
2007 // other one.
2008 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2009 Hi.getValue(1));
2010
2011 // Move the top bits to the right place.
2012 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2013 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2014
2015 // Join the hi and lo parts.
2016 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002017 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002018 // Big endian - avoid unaligned loads.
2019 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2020 // Load the top RoundWidth bits.
2021 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2022 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2023 Alignment);
2024
2025 // Load the remaining ExtraWidth bits.
2026 IncrementSize = RoundWidth / 8;
2027 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2028 DAG.getIntPtrConstant(IncrementSize));
2029 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2030 LD->getSrcValue(), SVOffset + IncrementSize,
2031 ExtraVT, isVolatile,
2032 MinAlign(Alignment, IncrementSize));
2033
2034 // Build a factor node to remember that this load is independent of the
2035 // other one.
2036 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2037 Hi.getValue(1));
2038
2039 // Move the top bits to the right place.
2040 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2041 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2042
2043 // Join the hi and lo parts.
2044 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2045 }
2046
2047 Tmp1 = LegalizeOp(Result);
2048 Tmp2 = LegalizeOp(Ch);
2049 } else {
2050 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2051 default: assert(0 && "This action is not supported yet!");
2052 case TargetLowering::Custom:
2053 isCustom = true;
2054 // FALLTHROUGH
2055 case TargetLowering::Legal:
2056 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2057 Tmp1 = Result.getValue(0);
2058 Tmp2 = Result.getValue(1);
2059
2060 if (isCustom) {
2061 Tmp3 = TLI.LowerOperation(Result, DAG);
2062 if (Tmp3.Val) {
2063 Tmp1 = LegalizeOp(Tmp3);
2064 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2065 }
2066 } else {
2067 // If this is an unaligned load and the target doesn't support it,
2068 // expand it.
2069 if (!TLI.allowsUnalignedMemoryAccesses()) {
2070 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002071 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002072 if (LD->getAlignment() < ABIAlignment){
2073 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2074 TLI);
2075 Tmp1 = Result.getOperand(0);
2076 Tmp2 = Result.getOperand(1);
2077 Tmp1 = LegalizeOp(Tmp1);
2078 Tmp2 = LegalizeOp(Tmp2);
2079 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002080 }
2081 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002082 break;
2083 case TargetLowering::Expand:
2084 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2085 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2086 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2087 LD->getSrcValueOffset(),
2088 LD->isVolatile(), LD->getAlignment());
2089 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2090 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2091 Tmp2 = LegalizeOp(Load.getValue(1));
2092 break;
2093 }
2094 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2095 // Turn the unsupported load into an EXTLOAD followed by an explicit
2096 // zero/sign extend inreg.
2097 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2098 Tmp1, Tmp2, LD->getSrcValue(),
2099 LD->getSrcValueOffset(), SrcVT,
2100 LD->isVolatile(), LD->getAlignment());
2101 SDOperand ValRes;
2102 if (ExtType == ISD::SEXTLOAD)
2103 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2104 Result, DAG.getValueType(SrcVT));
2105 else
2106 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2107 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2108 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002109 break;
2110 }
Evan Cheng466685d2006-10-09 20:57:25 +00002111 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002112
Evan Cheng466685d2006-10-09 20:57:25 +00002113 // Since loads produce two values, make sure to remember that we legalized
2114 // both of them.
2115 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2116 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2117 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002118 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002119 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002120 case ISD::EXTRACT_ELEMENT: {
2121 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2122 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002123 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002124 case Legal:
2125 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2126 // 1 -> Hi
2127 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2128 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2129 TLI.getShiftAmountTy()));
2130 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2131 } else {
2132 // 0 -> Lo
2133 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2134 Node->getOperand(0));
2135 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002136 break;
2137 case Expand:
2138 // Get both the low and high parts.
2139 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2140 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2141 Result = Tmp2; // 1 -> Hi
2142 else
2143 Result = Tmp1; // 0 -> Lo
2144 break;
2145 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002146 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002147 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002148
2149 case ISD::CopyToReg:
2150 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002151
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002152 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002153 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002154 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002155 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002156 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002157 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002158 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002159 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002160 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002161 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002162 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2163 Tmp3);
2164 } else {
2165 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002166 }
2167
2168 // Since this produces two values, make sure to remember that we legalized
2169 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002170 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00002171 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002172 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002173 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002174 break;
2175
2176 case ISD::RET:
2177 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002178
2179 // Ensure that libcalls are emitted before a return.
2180 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2181 Tmp1 = LegalizeOp(Tmp1);
2182 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002183
Chris Lattner3e928bb2005-01-07 07:47:09 +00002184 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002185 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002186 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002187 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002188 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002189 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002190 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002191 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002192 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00002193 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00002194 SDOperand Lo, Hi;
2195 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002196
2197 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002198 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002199 std::swap(Lo, Hi);
2200
Evan Cheng13acce32006-12-11 19:27:14 +00002201 if (Hi.Val)
2202 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2203 else
2204 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002205 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002206 } else {
2207 SDNode *InVal = Tmp2.Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002208 int InIx = Tmp2.ResNo;
2209 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2210 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattnerf87324e2006-04-11 01:31:51 +00002211
Dan Gohman7f321562007-06-25 16:23:39 +00002212 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002213 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00002214 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002215 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002216 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002217 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002218 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002219 } else if (NumElems == 1) {
2220 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002221 Tmp2 = ScalarizeVectorOp(Tmp2);
2222 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002223 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002224
2225 // FIXME: Returns of gcc generic vectors smaller than a legal type
2226 // should be returned in integer registers!
2227
Chris Lattnerf87324e2006-04-11 01:31:51 +00002228 // The scalarized value type may not be legal, e.g. it might require
2229 // promotion or expansion. Relegalize the return.
2230 Result = LegalizeOp(Result);
2231 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002232 // FIXME: Returns of gcc generic vectors larger than a legal vector
2233 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00002234 SDOperand Lo, Hi;
2235 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002236 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002237 Result = LegalizeOp(Result);
2238 }
2239 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002240 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002241 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002242 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002243 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002244 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002245 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002246 }
2247 break;
2248 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002249 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002250 break;
2251 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002252 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002253 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002254 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002255 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2256 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002257 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002258 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002259 break;
2260 case Expand: {
2261 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00002262 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002263 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002264 ExpandOp(Node->getOperand(i), Lo, Hi);
2265 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002266 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00002267 if (Hi.Val) {
2268 NewValues.push_back(Hi);
2269 NewValues.push_back(Node->getOperand(i+1));
2270 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002271 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002272 }
2273 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002274 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002275 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002276
2277 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002278 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002279 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002280 Result = DAG.getNode(ISD::RET, MVT::Other,
2281 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002282 break;
2283 }
2284 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002285
Chris Lattner6862dbc2006-01-29 21:02:23 +00002286 if (Result.getOpcode() == ISD::RET) {
2287 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2288 default: assert(0 && "This action is not supported yet!");
2289 case TargetLowering::Legal: break;
2290 case TargetLowering::Custom:
2291 Tmp1 = TLI.LowerOperation(Result, DAG);
2292 if (Tmp1.Val) Result = Tmp1;
2293 break;
2294 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002295 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002296 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002297 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002298 StoreSDNode *ST = cast<StoreSDNode>(Node);
2299 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2300 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002301 int SVOffset = ST->getSrcValueOffset();
2302 unsigned Alignment = ST->getAlignment();
2303 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002304
Evan Cheng8b2794a2006-10-13 21:14:26 +00002305 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002306 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2307 // FIXME: We shouldn't do this for TargetConstantFP's.
2308 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2309 // to phase ordering between legalized code and the dag combiner. This
2310 // probably means that we need to integrate dag combiner and legalizer
2311 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002312 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002313 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002314 if (CFP->getValueType(0) == MVT::f32 &&
2315 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00002316 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2317 convertToAPInt().getZExtValue(),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002318 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002319 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2320 SVOffset, isVolatile, Alignment);
2321 break;
2322 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002323 // If this target supports 64-bit registers, do a single 64-bit store.
2324 if (getTypeAction(MVT::i64) == Legal) {
2325 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2326 getZExtValue(), MVT::i64);
2327 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2328 SVOffset, isVolatile, Alignment);
2329 break;
2330 } else if (getTypeAction(MVT::i32) == Legal) {
2331 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2332 // stores. If the target supports neither 32- nor 64-bits, this
2333 // xform is certainly not worth it.
2334 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2335 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2336 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002337 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002338
2339 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2340 SVOffset, isVolatile, Alignment);
2341 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002342 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002343 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002344 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002345
2346 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2347 break;
2348 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002349 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002350 }
2351
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002352 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002353 case Legal: {
2354 Tmp3 = LegalizeOp(ST->getValue());
2355 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2356 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002357
Evan Cheng8b2794a2006-10-13 21:14:26 +00002358 MVT::ValueType VT = Tmp3.getValueType();
2359 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2360 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002361 case TargetLowering::Legal:
2362 // If this is an unaligned store and the target doesn't support it,
2363 // expand it.
2364 if (!TLI.allowsUnalignedMemoryAccesses()) {
2365 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002366 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002367 if (ST->getAlignment() < ABIAlignment)
2368 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2369 TLI);
2370 }
2371 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002372 case TargetLowering::Custom:
2373 Tmp1 = TLI.LowerOperation(Result, DAG);
2374 if (Tmp1.Val) Result = Tmp1;
2375 break;
2376 case TargetLowering::Promote:
2377 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2378 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2379 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2380 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002381 ST->getSrcValue(), SVOffset, isVolatile,
2382 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002383 break;
2384 }
2385 break;
2386 }
2387 case Promote:
2388 // Truncate the value and store the result.
2389 Tmp3 = PromoteOp(ST->getValue());
2390 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002391 SVOffset, ST->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002392 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002393 break;
2394
2395 case Expand:
2396 unsigned IncrementSize = 0;
2397 SDOperand Lo, Hi;
2398
2399 // If this is a vector type, then we have to calculate the increment as
2400 // the product of the element size in bytes, and the number of elements
2401 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002402 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002403 SDNode *InVal = ST->getValue().Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002404 int InIx = ST->getValue().ResNo;
Chris Lattner0bd48932008-01-17 07:00:52 +00002405 MVT::ValueType InVT = InVal->getValueType(InIx);
2406 unsigned NumElems = MVT::getVectorNumElements(InVT);
2407 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002408
Dan Gohman7f321562007-06-25 16:23:39 +00002409 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002410 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002411 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002412 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002413 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002414 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002415 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002416 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002417 Result = LegalizeOp(Result);
2418 break;
2419 } else if (NumElems == 1) {
2420 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002421 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002422 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002423 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002424 // The scalarized value type may not be legal, e.g. it might require
2425 // promotion or expansion. Relegalize the scalar store.
2426 Result = LegalizeOp(Result);
2427 break;
2428 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002429 SplitVectorOp(ST->getValue(), Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00002430 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2431 MVT::getSizeInBits(EVT)/8;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002432 }
2433 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002434 ExpandOp(ST->getValue(), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002435 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002436
Duncan Sands0753fc12008-02-11 10:37:04 +00002437 if (TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002438 std::swap(Lo, Hi);
2439 }
2440
2441 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002442 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002443
2444 if (Hi.Val == NULL) {
2445 // Must be int <-> float one-to-one expansion.
2446 Result = Lo;
2447 break;
2448 }
2449
Evan Cheng8b2794a2006-10-13 21:14:26 +00002450 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002451 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002452 assert(isTypeLegal(Tmp2.getValueType()) &&
2453 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002454 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002455 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002456 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002457 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002458 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2459 break;
2460 }
2461 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002462 switch (getTypeAction(ST->getValue().getValueType())) {
2463 case Legal:
2464 Tmp3 = LegalizeOp(ST->getValue());
2465 break;
2466 case Promote:
2467 // We can promote the value, the truncstore will still take care of it.
2468 Tmp3 = PromoteOp(ST->getValue());
2469 break;
2470 case Expand:
2471 // Just store the low part. This may become a non-trunc store, so make
2472 // sure to use getTruncStore, not UpdateNodeOperands below.
2473 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2474 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2475 SVOffset, MVT::i8, isVolatile, Alignment);
2476 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002477
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002478 MVT::ValueType StVT = ST->getMemoryVT();
Duncan Sands7e857202008-01-22 07:17:34 +00002479 unsigned StWidth = MVT::getSizeInBits(StVT);
2480
2481 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2482 // Promote to a byte-sized store with upper bits zero if not
2483 // storing an integral number of bytes. For example, promote
2484 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2485 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2486 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2487 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2488 SVOffset, NVT, isVolatile, Alignment);
2489 } else if (StWidth & (StWidth - 1)) {
2490 // If not storing a power-of-2 number of bits, expand as two stores.
2491 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2492 "Unsupported truncstore!");
2493 unsigned RoundWidth = 1 << Log2_32(StWidth);
2494 assert(RoundWidth < StWidth);
2495 unsigned ExtraWidth = StWidth - RoundWidth;
2496 assert(ExtraWidth < RoundWidth);
2497 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2498 "Store size not an integral number of bytes!");
2499 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2500 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2501 SDOperand Lo, Hi;
2502 unsigned IncrementSize;
2503
2504 if (TLI.isLittleEndian()) {
2505 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2506 // Store the bottom RoundWidth bits.
2507 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2508 SVOffset, RoundVT,
2509 isVolatile, Alignment);
2510
2511 // Store the remaining ExtraWidth bits.
2512 IncrementSize = RoundWidth / 8;
2513 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2514 DAG.getIntPtrConstant(IncrementSize));
2515 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2516 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2517 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2518 SVOffset + IncrementSize, ExtraVT, isVolatile,
2519 MinAlign(Alignment, IncrementSize));
2520 } else {
2521 // Big endian - avoid unaligned stores.
2522 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2523 // Store the top RoundWidth bits.
2524 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2525 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2526 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2527 RoundVT, isVolatile, Alignment);
2528
2529 // Store the remaining ExtraWidth bits.
2530 IncrementSize = RoundWidth / 8;
2531 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2532 DAG.getIntPtrConstant(IncrementSize));
2533 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2534 SVOffset + IncrementSize, ExtraVT, isVolatile,
2535 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002536 }
Duncan Sands7e857202008-01-22 07:17:34 +00002537
2538 // The order of the stores doesn't matter.
2539 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2540 } else {
2541 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2542 Tmp2 != ST->getBasePtr())
2543 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2544 ST->getOffset());
2545
2546 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2547 default: assert(0 && "This action is not supported yet!");
2548 case TargetLowering::Legal:
2549 // If this is an unaligned store and the target doesn't support it,
2550 // expand it.
2551 if (!TLI.allowsUnalignedMemoryAccesses()) {
2552 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002553 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Duncan Sands7e857202008-01-22 07:17:34 +00002554 if (ST->getAlignment() < ABIAlignment)
2555 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2556 TLI);
2557 }
2558 break;
2559 case TargetLowering::Custom:
2560 Result = TLI.LowerOperation(Result, DAG);
2561 break;
2562 case Expand:
2563 // TRUNCSTORE:i16 i32 -> STORE i16
2564 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2565 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2566 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2567 isVolatile, Alignment);
2568 break;
2569 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002570 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002571 }
2572 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002573 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002574 case ISD::PCMARKER:
2575 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002576 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002577 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002578 case ISD::STACKSAVE:
2579 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002580 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2581 Tmp1 = Result.getValue(0);
2582 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002583
Chris Lattner140d53c2006-01-13 02:50:02 +00002584 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2585 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002586 case TargetLowering::Legal: break;
2587 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002588 Tmp3 = TLI.LowerOperation(Result, DAG);
2589 if (Tmp3.Val) {
2590 Tmp1 = LegalizeOp(Tmp3);
2591 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002592 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002593 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002594 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002595 // Expand to CopyFromReg if the target set
2596 // StackPointerRegisterToSaveRestore.
2597 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002598 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002599 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002600 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002601 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002602 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2603 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002604 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002605 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002606 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002607
2608 // Since stacksave produce two values, make sure to remember that we
2609 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002610 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2611 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2612 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002613
Chris Lattner140d53c2006-01-13 02:50:02 +00002614 case ISD::STACKRESTORE:
2615 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2616 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002617 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002618
2619 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2620 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002621 case TargetLowering::Legal: break;
2622 case TargetLowering::Custom:
2623 Tmp1 = TLI.LowerOperation(Result, DAG);
2624 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002625 break;
2626 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002627 // Expand to CopyToReg if the target set
2628 // StackPointerRegisterToSaveRestore.
2629 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2630 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2631 } else {
2632 Result = Tmp1;
2633 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002634 break;
2635 }
2636 break;
2637
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002638 case ISD::READCYCLECOUNTER:
2639 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002640 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002641 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2642 Node->getValueType(0))) {
2643 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002644 case TargetLowering::Legal:
2645 Tmp1 = Result.getValue(0);
2646 Tmp2 = Result.getValue(1);
2647 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002648 case TargetLowering::Custom:
2649 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002650 Tmp1 = LegalizeOp(Result.getValue(0));
2651 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002652 break;
2653 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002654
2655 // Since rdcc produce two values, make sure to remember that we legalized
2656 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002657 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2658 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002659 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002660
Chris Lattner2ee743f2005-01-14 22:08:15 +00002661 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002662 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2663 case Expand: assert(0 && "It's impossible to expand bools");
2664 case Legal:
2665 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2666 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002667 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00002668 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002669 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002670 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002671 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002672 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002673 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002674 break;
2675 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002676 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002677 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002678 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002679
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002680 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002681
Nate Begemanb942a3d2005-08-23 04:29:48 +00002682 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002683 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002684 case TargetLowering::Legal: break;
2685 case TargetLowering::Custom: {
2686 Tmp1 = TLI.LowerOperation(Result, DAG);
2687 if (Tmp1.Val) Result = Tmp1;
2688 break;
2689 }
Nate Begeman9373a812005-08-10 20:51:12 +00002690 case TargetLowering::Expand:
2691 if (Tmp1.getOpcode() == ISD::SETCC) {
2692 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2693 Tmp2, Tmp3,
2694 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2695 } else {
2696 Result = DAG.getSelectCC(Tmp1,
2697 DAG.getConstant(0, Tmp1.getValueType()),
2698 Tmp2, Tmp3, ISD::SETNE);
2699 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002700 break;
2701 case TargetLowering::Promote: {
2702 MVT::ValueType NVT =
2703 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2704 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002705 if (MVT::isVector(Tmp2.getValueType())) {
2706 ExtOp = ISD::BIT_CONVERT;
2707 TruncOp = ISD::BIT_CONVERT;
2708 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002709 ExtOp = ISD::ANY_EXTEND;
2710 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002711 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002712 ExtOp = ISD::FP_EXTEND;
2713 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002714 }
2715 // Promote each of the values to the new type.
2716 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2717 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2718 // Perform the larger operation, then round down.
2719 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002720 if (TruncOp != ISD::FP_ROUND)
2721 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2722 else
2723 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2724 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002725 break;
2726 }
2727 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002728 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002729 case ISD::SELECT_CC: {
2730 Tmp1 = Node->getOperand(0); // LHS
2731 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002732 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2733 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002734 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002735
Nate Begeman750ac1b2006-02-01 07:19:44 +00002736 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2737
2738 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2739 // the LHS is a legal SETCC itself. In this case, we need to compare
2740 // the result against zero to select between true and false values.
2741 if (Tmp2.Val == 0) {
2742 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2743 CC = DAG.getCondCode(ISD::SETNE);
2744 }
2745 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2746
2747 // Everything is legal, see if we should expand this op or something.
2748 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2749 default: assert(0 && "This action is not supported yet!");
2750 case TargetLowering::Legal: break;
2751 case TargetLowering::Custom:
2752 Tmp1 = TLI.LowerOperation(Result, DAG);
2753 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002754 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002755 }
2756 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002757 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002758 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002759 Tmp1 = Node->getOperand(0);
2760 Tmp2 = Node->getOperand(1);
2761 Tmp3 = Node->getOperand(2);
2762 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2763
2764 // If we had to Expand the SetCC operands into a SELECT node, then it may
2765 // not always be possible to return a true LHS & RHS. In this case, just
2766 // return the value we legalized, returned in the LHS
2767 if (Tmp2.Val == 0) {
2768 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002769 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002770 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002771
Chris Lattner73e142f2006-01-30 22:43:50 +00002772 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002773 default: assert(0 && "Cannot handle this action for SETCC yet!");
2774 case TargetLowering::Custom:
2775 isCustom = true;
2776 // FALLTHROUGH.
2777 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002778 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002779 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002780 Tmp4 = TLI.LowerOperation(Result, DAG);
2781 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002782 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002783 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002784 case TargetLowering::Promote: {
2785 // First step, figure out the appropriate operation to use.
2786 // Allow SETCC to not be supported for all legal data types
2787 // Mostly this targets FP
2788 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002789 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002790
2791 // Scan for the appropriate larger type to use.
2792 while (1) {
2793 NewInTy = (MVT::ValueType)(NewInTy+1);
2794
2795 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2796 "Fell off of the edge of the integer world");
2797 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2798 "Fell off of the edge of the floating point world");
2799
2800 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002801 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002802 break;
2803 }
2804 if (MVT::isInteger(NewInTy))
2805 assert(0 && "Cannot promote Legal Integer SETCC yet");
2806 else {
2807 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2808 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2809 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002810 Tmp1 = LegalizeOp(Tmp1);
2811 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002812 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002813 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002814 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002815 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002816 case TargetLowering::Expand:
2817 // Expand a setcc node into a select_cc of the same condition, lhs, and
2818 // rhs that selects between const 1 (true) and const 0 (false).
2819 MVT::ValueType VT = Node->getValueType(0);
2820 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2821 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002822 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002823 break;
2824 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002825 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002826 case ISD::MEMSET:
2827 case ISD::MEMCPY:
2828 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002829 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002830 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2831
2832 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2833 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2834 case Expand: assert(0 && "Cannot expand a byte!");
2835 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002836 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002837 break;
2838 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002839 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002840 break;
2841 }
2842 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002843 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002844 }
Chris Lattner272455b2005-02-02 03:44:41 +00002845
2846 SDOperand Tmp4;
2847 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002848 case Expand: {
2849 // Length is too big, just take the lo-part of the length.
2850 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002851 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002852 break;
2853 }
Chris Lattnere5605212005-01-28 22:29:18 +00002854 case Legal:
2855 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002856 break;
2857 case Promote:
2858 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002859 break;
2860 }
2861
2862 SDOperand Tmp5;
2863 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2864 case Expand: assert(0 && "Cannot expand this yet!");
2865 case Legal:
2866 Tmp5 = LegalizeOp(Node->getOperand(4));
2867 break;
2868 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002869 Tmp5 = PromoteOp(Node->getOperand(4));
2870 break;
2871 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002872
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002873 SDOperand Tmp6;
2874 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2875 case Expand: assert(0 && "Cannot expand this yet!");
2876 case Legal:
2877 Tmp6 = LegalizeOp(Node->getOperand(5));
2878 break;
2879 case Promote:
2880 Tmp6 = PromoteOp(Node->getOperand(5));
2881 break;
2882 }
2883
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002884 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2885 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002886 case TargetLowering::Custom:
2887 isCustom = true;
2888 // FALLTHROUGH
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002889 case TargetLowering::Legal: {
2890 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2891 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner456a93a2006-01-28 07:39:30 +00002892 if (isCustom) {
2893 Tmp1 = TLI.LowerOperation(Result, DAG);
2894 if (Tmp1.Val) Result = Tmp1;
2895 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002896 break;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002897 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002898 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002899 // Otherwise, the target does not support this operation. Lower the
2900 // operation to an explicit libcall as appropriate.
2901 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002902 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002903 TargetLowering::ArgListTy Args;
2904 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002905
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002906 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002907 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002908 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002909 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002910 // Extend the (previously legalized) ubyte argument to be an int value
2911 // for the call.
2912 if (Tmp3.getValueType() > MVT::i32)
2913 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2914 else
2915 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002916 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002917 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002918 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002919 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002920
2921 FnName = "memset";
2922 } else if (Node->getOpcode() == ISD::MEMCPY ||
2923 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002924 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002925 Entry.Node = Tmp2; Args.push_back(Entry);
2926 Entry.Node = Tmp3; Args.push_back(Entry);
2927 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002928 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2929 } else {
2930 assert(0 && "Unknown op!");
2931 }
Chris Lattner45982da2005-05-12 16:53:42 +00002932
Chris Lattnere1bd8222005-01-11 05:57:22 +00002933 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00002934 TLI.LowerCallTo(Tmp1, Type::VoidTy,
2935 false, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002936 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002937 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002938 break;
2939 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002940 }
2941 break;
2942 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002943
Chris Lattner5b359c62005-04-02 04:00:59 +00002944 case ISD::SHL_PARTS:
2945 case ISD::SRA_PARTS:
2946 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002947 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002948 bool Changed = false;
2949 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2950 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2951 Changed |= Ops.back() != Node->getOperand(i);
2952 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002953 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002954 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002955
Evan Cheng05a2d562006-01-09 18:31:59 +00002956 switch (TLI.getOperationAction(Node->getOpcode(),
2957 Node->getValueType(0))) {
2958 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002959 case TargetLowering::Legal: break;
2960 case TargetLowering::Custom:
2961 Tmp1 = TLI.LowerOperation(Result, DAG);
2962 if (Tmp1.Val) {
2963 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002964 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002965 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002966 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2967 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002968 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002969 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002970 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002971 return RetVal;
2972 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002973 break;
2974 }
2975
Chris Lattner2c8086f2005-04-02 05:00:07 +00002976 // Since these produce multiple values, make sure to remember that we
2977 // legalized all of them.
2978 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2979 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2980 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002981 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002982
2983 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002984 case ISD::ADD:
2985 case ISD::SUB:
2986 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002987 case ISD::MULHS:
2988 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002989 case ISD::UDIV:
2990 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002991 case ISD::AND:
2992 case ISD::OR:
2993 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002994 case ISD::SHL:
2995 case ISD::SRL:
2996 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002997 case ISD::FADD:
2998 case ISD::FSUB:
2999 case ISD::FMUL:
3000 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00003001 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003002 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00003003 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3004 case Expand: assert(0 && "Not possible");
3005 case Legal:
3006 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3007 break;
3008 case Promote:
3009 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3010 break;
3011 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003012
3013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003014
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003015 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003016 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003017 case TargetLowering::Legal: break;
3018 case TargetLowering::Custom:
3019 Tmp1 = TLI.LowerOperation(Result, DAG);
3020 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00003021 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003022 case TargetLowering::Expand: {
Dan Gohman525178c2007-10-08 18:33:35 +00003023 MVT::ValueType VT = Op.getValueType();
3024
3025 // See if multiply or divide can be lowered using two-result operations.
3026 SDVTList VTs = DAG.getVTList(VT, VT);
3027 if (Node->getOpcode() == ISD::MUL) {
3028 // We just need the low half of the multiply; try both the signed
3029 // and unsigned forms. If the target supports both SMUL_LOHI and
3030 // UMUL_LOHI, form a preference by checking which forms of plain
3031 // MULH it supports.
3032 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3033 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3034 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3035 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3036 unsigned OpToUse = 0;
3037 if (HasSMUL_LOHI && !HasMULHS) {
3038 OpToUse = ISD::SMUL_LOHI;
3039 } else if (HasUMUL_LOHI && !HasMULHU) {
3040 OpToUse = ISD::UMUL_LOHI;
3041 } else if (HasSMUL_LOHI) {
3042 OpToUse = ISD::SMUL_LOHI;
3043 } else if (HasUMUL_LOHI) {
3044 OpToUse = ISD::UMUL_LOHI;
3045 }
3046 if (OpToUse) {
3047 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
3048 break;
3049 }
3050 }
3051 if (Node->getOpcode() == ISD::MULHS &&
3052 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
3053 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3054 break;
3055 }
3056 if (Node->getOpcode() == ISD::MULHU &&
3057 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
3058 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3059 break;
3060 }
3061 if (Node->getOpcode() == ISD::SDIV &&
3062 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3063 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3064 break;
3065 }
3066 if (Node->getOpcode() == ISD::UDIV &&
3067 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3068 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3069 break;
3070 }
3071
Dan Gohman82669522007-10-11 23:57:53 +00003072 // Check to see if we have a libcall for this operator.
3073 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3074 bool isSigned = false;
3075 switch (Node->getOpcode()) {
3076 case ISD::UDIV:
3077 case ISD::SDIV:
3078 if (VT == MVT::i32) {
3079 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00003080 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003081 isSigned = Node->getOpcode() == ISD::SDIV;
3082 }
3083 break;
3084 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003085 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3086 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003087 break;
3088 default: break;
3089 }
3090 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3091 SDOperand Dummy;
3092 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003093 break;
3094 }
3095
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003096 assert(MVT::isVector(Node->getValueType(0)) &&
3097 "Cannot expand this binary operator!");
3098 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003099 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003100 break;
3101 }
Evan Chengcc987612006-04-12 21:20:24 +00003102 case TargetLowering::Promote: {
3103 switch (Node->getOpcode()) {
3104 default: assert(0 && "Do not know how to promote this BinOp!");
3105 case ISD::AND:
3106 case ISD::OR:
3107 case ISD::XOR: {
3108 MVT::ValueType OVT = Node->getValueType(0);
3109 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3110 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3111 // Bit convert each of the values to the new type.
3112 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3113 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3114 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3115 // Bit convert the result back the original type.
3116 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3117 break;
3118 }
3119 }
3120 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003121 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003122 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003123
Dan Gohmane14ea862007-10-05 14:17:22 +00003124 case ISD::SMUL_LOHI:
3125 case ISD::UMUL_LOHI:
3126 case ISD::SDIVREM:
3127 case ISD::UDIVREM:
3128 // These nodes will only be produced by target-specific lowering, so
3129 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003130 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003131 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003132
3133 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3134 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3135 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003136 break;
3137
Chris Lattnera09f8482006-03-05 05:09:38 +00003138 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3139 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3140 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3141 case Expand: assert(0 && "Not possible");
3142 case Legal:
3143 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3144 break;
3145 case Promote:
3146 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3147 break;
3148 }
3149
3150 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3151
3152 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3153 default: assert(0 && "Operation not supported");
3154 case TargetLowering::Custom:
3155 Tmp1 = TLI.LowerOperation(Result, DAG);
3156 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003157 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003158 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003159 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003160 // If this target supports fabs/fneg natively and select is cheap,
3161 // do this efficiently.
3162 if (!TLI.isSelectExpensive() &&
3163 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3164 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003165 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003166 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003167 // Get the sign bit of the RHS.
3168 MVT::ValueType IVT =
3169 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3170 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
3171 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
3172 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3173 // Get the absolute value of the result.
3174 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3175 // Select between the nabs and abs value based on the sign bit of
3176 // the input.
3177 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3178 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3179 AbsVal),
3180 AbsVal);
3181 Result = LegalizeOp(Result);
3182 break;
3183 }
3184
3185 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00003186 MVT::ValueType NVT =
3187 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3188 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3189 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3190 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003191 break;
3192 }
Evan Cheng912095b2007-01-04 21:56:39 +00003193 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003194 break;
3195
Nate Begeman551bf3f2006-02-17 05:43:56 +00003196 case ISD::ADDC:
3197 case ISD::SUBC:
3198 Tmp1 = LegalizeOp(Node->getOperand(0));
3199 Tmp2 = LegalizeOp(Node->getOperand(1));
3200 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3201 // Since this produces two values, make sure to remember that we legalized
3202 // both of them.
3203 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3204 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3205 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003206
Nate Begeman551bf3f2006-02-17 05:43:56 +00003207 case ISD::ADDE:
3208 case ISD::SUBE:
3209 Tmp1 = LegalizeOp(Node->getOperand(0));
3210 Tmp2 = LegalizeOp(Node->getOperand(1));
3211 Tmp3 = LegalizeOp(Node->getOperand(2));
3212 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3213 // Since this produces two values, make sure to remember that we legalized
3214 // both of them.
3215 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3216 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3217 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003218
Nate Begeman419f8b62005-10-18 00:27:41 +00003219 case ISD::BUILD_PAIR: {
3220 MVT::ValueType PairTy = Node->getValueType(0);
3221 // TODO: handle the case where the Lo and Hi operands are not of legal type
3222 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3223 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3224 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003225 case TargetLowering::Promote:
3226 case TargetLowering::Custom:
3227 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003228 case TargetLowering::Legal:
3229 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3230 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3231 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003232 case TargetLowering::Expand:
3233 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3234 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3235 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3236 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3237 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003238 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003239 break;
3240 }
3241 break;
3242 }
3243
Nate Begemanc105e192005-04-06 00:23:54 +00003244 case ISD::UREM:
3245 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003246 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003247 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3248 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003249
Nate Begemanc105e192005-04-06 00:23:54 +00003250 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003251 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3252 case TargetLowering::Custom:
3253 isCustom = true;
3254 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003255 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003256 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003257 if (isCustom) {
3258 Tmp1 = TLI.LowerOperation(Result, DAG);
3259 if (Tmp1.Val) Result = Tmp1;
3260 }
Nate Begemanc105e192005-04-06 00:23:54 +00003261 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003262 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003263 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003264 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman525178c2007-10-08 18:33:35 +00003265 MVT::ValueType VT = Node->getValueType(0);
3266
3267 // See if remainder can be lowered using two-result operations.
3268 SDVTList VTs = DAG.getVTList(VT, VT);
3269 if (Node->getOpcode() == ISD::SREM &&
3270 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3271 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3272 break;
3273 }
3274 if (Node->getOpcode() == ISD::UREM &&
3275 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3276 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3277 break;
3278 }
3279
3280 if (MVT::isInteger(VT)) {
3281 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003282 TargetLowering::Legal) {
3283 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003284 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003285 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3286 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman80176312007-11-05 23:35:22 +00003287 } else if (MVT::isVector(VT)) {
3288 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003289 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003290 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003291 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003292 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3293 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003294 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003295 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003296 }
Dan Gohman0d974262007-11-06 22:11:54 +00003297 } else {
3298 assert(MVT::isFloatingPoint(VT) &&
3299 "remainder op must have integer or floating-point type");
Dan Gohman80176312007-11-05 23:35:22 +00003300 if (MVT::isVector(VT)) {
3301 Result = LegalizeOp(UnrollVectorOp(Op));
3302 } else {
3303 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003304 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3305 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman80176312007-11-05 23:35:22 +00003306 SDOperand Dummy;
3307 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3308 false/*sign irrelevant*/, Dummy);
3309 }
Nate Begemanc105e192005-04-06 00:23:54 +00003310 }
3311 break;
3312 }
Dan Gohman525178c2007-10-08 18:33:35 +00003313 }
Nate Begemanc105e192005-04-06 00:23:54 +00003314 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003315 case ISD::VAARG: {
3316 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3317 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3318
Chris Lattner5c62f332006-01-28 07:42:08 +00003319 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003320 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3321 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003322 case TargetLowering::Custom:
3323 isCustom = true;
3324 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003325 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003326 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3327 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003328 Tmp1 = Result.getValue(1);
3329
3330 if (isCustom) {
3331 Tmp2 = TLI.LowerOperation(Result, DAG);
3332 if (Tmp2.Val) {
3333 Result = LegalizeOp(Tmp2);
3334 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3335 }
3336 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003337 break;
3338 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003339 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3340 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003341 // Increment the pointer, VAList, to the next vaarg
3342 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3343 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3344 TLI.getPointerTy()));
3345 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003346 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003347 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003348 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003349 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003350 Result = LegalizeOp(Result);
3351 break;
3352 }
3353 }
3354 // Since VAARG produces two values, make sure to remember that we
3355 // legalized both of them.
3356 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00003357 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3358 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003359 }
3360
3361 case ISD::VACOPY:
3362 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3363 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3364 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3365
3366 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3367 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003368 case TargetLowering::Custom:
3369 isCustom = true;
3370 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003371 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003372 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3373 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003374 if (isCustom) {
3375 Tmp1 = TLI.LowerOperation(Result, DAG);
3376 if (Tmp1.Val) Result = Tmp1;
3377 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003378 break;
3379 case TargetLowering::Expand:
3380 // This defaults to loading a pointer from the input and storing it to the
3381 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003382 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3383 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3384 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VD, 0);
3385 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VS, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003386 break;
3387 }
3388 break;
3389
3390 case ISD::VAEND:
3391 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3392 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3393
3394 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3395 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003396 case TargetLowering::Custom:
3397 isCustom = true;
3398 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003399 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003400 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003401 if (isCustom) {
3402 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3403 if (Tmp1.Val) Result = Tmp1;
3404 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003405 break;
3406 case TargetLowering::Expand:
3407 Result = Tmp1; // Default to a no-op, return the chain
3408 break;
3409 }
3410 break;
3411
3412 case ISD::VASTART:
3413 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3414 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3415
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003416 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3417
Nate Begemanacc398c2006-01-25 18:21:52 +00003418 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3419 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003420 case TargetLowering::Legal: break;
3421 case TargetLowering::Custom:
3422 Tmp1 = TLI.LowerOperation(Result, DAG);
3423 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003424 break;
3425 }
3426 break;
3427
Nate Begeman35ef9132006-01-11 21:21:00 +00003428 case ISD::ROTL:
3429 case ISD::ROTR:
3430 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3431 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003432 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003433 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3434 default:
3435 assert(0 && "ROTL/ROTR legalize operation not supported");
3436 break;
3437 case TargetLowering::Legal:
3438 break;
3439 case TargetLowering::Custom:
3440 Tmp1 = TLI.LowerOperation(Result, DAG);
3441 if (Tmp1.Val) Result = Tmp1;
3442 break;
3443 case TargetLowering::Promote:
3444 assert(0 && "Do not know how to promote ROTL/ROTR");
3445 break;
3446 case TargetLowering::Expand:
3447 assert(0 && "Do not know how to expand ROTL/ROTR");
3448 break;
3449 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003450 break;
3451
Nate Begemand88fc032006-01-14 03:14:10 +00003452 case ISD::BSWAP:
3453 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3454 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003455 case TargetLowering::Custom:
3456 assert(0 && "Cannot custom legalize this yet!");
3457 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003458 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003459 break;
3460 case TargetLowering::Promote: {
3461 MVT::ValueType OVT = Tmp1.getValueType();
3462 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00003463 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00003464
Chris Lattner456a93a2006-01-28 07:39:30 +00003465 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3466 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3467 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3468 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3469 break;
3470 }
3471 case TargetLowering::Expand:
3472 Result = ExpandBSWAP(Tmp1);
3473 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003474 }
3475 break;
3476
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003477 case ISD::CTPOP:
3478 case ISD::CTTZ:
3479 case ISD::CTLZ:
3480 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3481 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003482 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003483 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003484 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003485 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003486 TargetLowering::Custom) {
3487 Tmp1 = TLI.LowerOperation(Result, DAG);
3488 if (Tmp1.Val) {
3489 Result = Tmp1;
3490 }
Scott Michel910b66d2007-07-30 21:00:31 +00003491 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003492 break;
3493 case TargetLowering::Promote: {
3494 MVT::ValueType OVT = Tmp1.getValueType();
3495 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003496
3497 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003498 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3499 // Perform the larger operation, then subtract if needed.
3500 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003501 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003502 case ISD::CTPOP:
3503 Result = Tmp1;
3504 break;
3505 case ISD::CTTZ:
3506 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003507 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003508 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003509 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003510 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00003511 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003512 break;
3513 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003514 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003515 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003516 DAG.getConstant(MVT::getSizeInBits(NVT) -
3517 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003518 break;
3519 }
3520 break;
3521 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003522 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003523 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003524 break;
3525 }
3526 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003527
Chris Lattner2c8086f2005-04-02 05:00:07 +00003528 // Unary operators
3529 case ISD::FABS:
3530 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003531 case ISD::FSQRT:
3532 case ISD::FSIN:
3533 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003534 Tmp1 = LegalizeOp(Node->getOperand(0));
3535 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003536 case TargetLowering::Promote:
3537 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003538 isCustom = true;
3539 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003540 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003541 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003542 if (isCustom) {
3543 Tmp1 = TLI.LowerOperation(Result, DAG);
3544 if (Tmp1.Val) Result = Tmp1;
3545 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003546 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003547 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003548 switch (Node->getOpcode()) {
3549 default: assert(0 && "Unreachable!");
3550 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003551 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3552 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003553 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003554 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003555 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003556 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3557 MVT::ValueType VT = Node->getValueType(0);
3558 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003559 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003560 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3561 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003562 break;
3563 }
3564 case ISD::FSQRT:
3565 case ISD::FSIN:
3566 case ISD::FCOS: {
3567 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003568
3569 // Expand unsupported unary vector operators by unrolling them.
3570 if (MVT::isVector(VT)) {
3571 Result = LegalizeOp(UnrollVectorOp(Op));
3572 break;
3573 }
3574
Evan Cheng56966222007-01-12 02:11:51 +00003575 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003576 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003577 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003578 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3579 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003580 break;
3581 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003582 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3583 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003584 break;
3585 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003586 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3587 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003588 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003589 default: assert(0 && "Unreachable!");
3590 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003591 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003592 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3593 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003594 break;
3595 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003596 }
3597 break;
3598 }
3599 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003600 case ISD::FPOWI: {
Dan Gohman82669522007-10-11 23:57:53 +00003601 MVT::ValueType VT = Node->getValueType(0);
3602
3603 // Expand unsupported unary vector operators by unrolling them.
3604 if (MVT::isVector(VT)) {
3605 Result = LegalizeOp(UnrollVectorOp(Op));
3606 break;
3607 }
3608
3609 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003610 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3611 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003612 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003613 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3614 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003615 break;
3616 }
Chris Lattner35481892005-12-23 00:16:34 +00003617 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003618 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003619 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3620 Node->getValueType(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003621 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3622 // The input has to be a vector type, we have to either scalarize it, pack
3623 // it, or convert it based on whether the input vector type is legal.
3624 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesene5269622007-10-20 00:07:52 +00003625 int InIx = Node->getOperand(0).ResNo;
3626 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3627 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohman7f321562007-06-25 16:23:39 +00003628
3629 // Figure out if there is a simple type corresponding to this Vector
3630 // type. If so, convert to the vector type.
3631 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3632 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003633 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003634 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3635 LegalizeOp(Node->getOperand(0)));
3636 break;
3637 } else if (NumElems == 1) {
3638 // Turn this into a bit convert of the scalar input.
3639 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3640 ScalarizeVectorOp(Node->getOperand(0)));
3641 break;
3642 } else {
3643 // FIXME: UNIMP! Store then reload
3644 assert(0 && "Cast from unsupported vector type not implemented yet!");
3645 }
Chris Lattner67993f72006-01-23 07:30:46 +00003646 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003647 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3648 Node->getOperand(0).getValueType())) {
3649 default: assert(0 && "Unknown operation action!");
3650 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003651 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3652 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003653 break;
3654 case TargetLowering::Legal:
3655 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003656 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003657 break;
3658 }
3659 }
3660 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003661
Chris Lattner2c8086f2005-04-02 05:00:07 +00003662 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003663 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003664 case ISD::UINT_TO_FP: {
3665 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003666 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3667 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003668 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003669 Node->getOperand(0).getValueType())) {
3670 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003671 case TargetLowering::Custom:
3672 isCustom = true;
3673 // FALLTHROUGH
3674 case TargetLowering::Legal:
3675 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003676 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003677 if (isCustom) {
3678 Tmp1 = TLI.LowerOperation(Result, DAG);
3679 if (Tmp1.Val) Result = Tmp1;
3680 }
3681 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003682 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003683 Result = ExpandLegalINT_TO_FP(isSigned,
3684 LegalizeOp(Node->getOperand(0)),
3685 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003686 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003687 case TargetLowering::Promote:
3688 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3689 Node->getValueType(0),
3690 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003691 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003692 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003693 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003694 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003695 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3696 Node->getValueType(0), Node->getOperand(0));
3697 break;
3698 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003699 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003700 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003701 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3702 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003703 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003704 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3705 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003706 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003707 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3708 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003709 break;
3710 }
3711 break;
3712 }
3713 case ISD::TRUNCATE:
3714 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3715 case Legal:
3716 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003717 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003718 break;
3719 case Expand:
3720 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3721
3722 // Since the result is legal, we should just be able to truncate the low
3723 // part of the source.
3724 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3725 break;
3726 case Promote:
3727 Result = PromoteOp(Node->getOperand(0));
3728 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3729 break;
3730 }
3731 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003732
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003733 case ISD::FP_TO_SINT:
3734 case ISD::FP_TO_UINT:
3735 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3736 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003737 Tmp1 = LegalizeOp(Node->getOperand(0));
3738
Chris Lattner1618beb2005-07-29 00:11:56 +00003739 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3740 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003741 case TargetLowering::Custom:
3742 isCustom = true;
3743 // FALLTHROUGH
3744 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003745 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003746 if (isCustom) {
3747 Tmp1 = TLI.LowerOperation(Result, DAG);
3748 if (Tmp1.Val) Result = Tmp1;
3749 }
3750 break;
3751 case TargetLowering::Promote:
3752 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3753 Node->getOpcode() == ISD::FP_TO_SINT);
3754 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003755 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003756 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3757 SDOperand True, False;
3758 MVT::ValueType VT = Node->getOperand(0).getValueType();
3759 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00003760 const uint64_t zero[] = {0, 0};
3761 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003762 APInt x = APInt::getSignBit(MVT::getSizeInBits(NVT));
3763 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003764 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begemand2558e32005-08-14 01:20:53 +00003765 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3766 Node->getOperand(0), Tmp2, ISD::SETLT);
3767 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3768 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003769 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003770 Tmp2));
3771 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003772 DAG.getConstant(x, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003773 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3774 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003775 } else {
3776 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3777 }
3778 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003779 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003780 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003781 case Expand: {
Evan Cheng6af00d52006-12-13 01:57:55 +00003782 MVT::ValueType VT = Op.getValueType();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003783 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003784 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003785 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003786 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3787 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3788 Node->getOperand(0), DAG.getValueType(MVT::f64));
3789 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3790 DAG.getIntPtrConstant(1));
3791 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3792 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003793 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3794 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3795 Tmp2 = DAG.getConstantFP(apf, OVT);
3796 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3797 // FIXME: generated code sucks.
3798 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3799 DAG.getNode(ISD::ADD, MVT::i32,
3800 DAG.getNode(ISD::FP_TO_SINT, VT,
3801 DAG.getNode(ISD::FSUB, OVT,
3802 Node->getOperand(0), Tmp2)),
3803 DAG.getConstant(0x80000000, MVT::i32)),
3804 DAG.getNode(ISD::FP_TO_SINT, VT,
3805 Node->getOperand(0)),
3806 DAG.getCondCode(ISD::SETGE));
3807 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003808 break;
3809 }
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003810 // Convert f32 / f64 to i32 / i64.
Evan Cheng56966222007-01-12 02:11:51 +00003811 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003812 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003813 case ISD::FP_TO_SINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003814 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003815 LC = (VT == MVT::i32)
3816 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003817 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003818 LC = (VT == MVT::i32)
3819 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003820 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003821 assert(VT == MVT::i64);
Dale Johannesen161e8972007-10-05 20:04:43 +00003822 LC = RTLIB::FPTOSINT_F80_I64;
3823 }
3824 else if (OVT == MVT::ppcf128) {
3825 assert(VT == MVT::i64);
3826 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003827 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003828 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003829 }
3830 case ISD::FP_TO_UINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003831 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003832 LC = (VT == MVT::i32)
3833 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003834 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003835 LC = (VT == MVT::i32)
3836 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003837 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003838 LC = (VT == MVT::i32)
Dale Johannesen161e8972007-10-05 20:04:43 +00003839 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3840 }
3841 else if (OVT == MVT::ppcf128) {
3842 assert(VT == MVT::i64);
3843 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003844 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003845 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003846 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003847 default: assert(0 && "Unreachable!");
3848 }
3849 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003850 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3851 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003852 break;
3853 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003854 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003855 Tmp1 = PromoteOp(Node->getOperand(0));
3856 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3857 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003858 break;
3859 }
3860 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003861
Chris Lattnerf2670a82008-01-16 06:57:07 +00003862 case ISD::FP_EXTEND: {
Chris Lattner0bd48932008-01-17 07:00:52 +00003863 MVT::ValueType DstVT = Op.getValueType();
3864 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3865 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3866 // The only other way we can lower this is to turn it into a STORE,
3867 // LOAD pair, targetting a temporary location (a stack slot).
3868 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3869 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00003870 }
3871 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3872 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3873 case Legal:
3874 Tmp1 = LegalizeOp(Node->getOperand(0));
3875 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3876 break;
3877 case Promote:
3878 Tmp1 = PromoteOp(Node->getOperand(0));
3879 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3880 break;
3881 }
3882 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003883 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003884 case ISD::FP_ROUND: {
Chris Lattner0bd48932008-01-17 07:00:52 +00003885 MVT::ValueType DstVT = Op.getValueType();
3886 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3887 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3888 if (SrcVT == MVT::ppcf128) {
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003889 SDOperand Lo;
3890 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00003891 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003892 if (DstVT!=MVT::f64)
3893 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00003894 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00003895 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003896 // The only other way we can lower this is to turn it into a STORE,
3897 // LOAD pair, targetting a temporary location (a stack slot).
3898 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3899 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00003900 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00003901 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3902 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3903 case Legal:
3904 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003905 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003906 break;
3907 case Promote:
3908 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003909 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3910 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003911 break;
3912 }
3913 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003914 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003915 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003916 case ISD::ZERO_EXTEND:
3917 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003918 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003919 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003920 case Legal:
3921 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel0123b7d2008-02-15 23:05:48 +00003922 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3923 TargetLowering::Custom) {
3924 Tmp2 = TLI.LowerOperation(Result, DAG);
3925 if (Tmp2.Val) {
3926 Tmp1 = Tmp2;
3927 }
3928 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003929 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003930 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003931 case Promote:
3932 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003933 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003934 Tmp1 = PromoteOp(Node->getOperand(0));
3935 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003936 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003937 case ISD::ZERO_EXTEND:
3938 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003939 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003940 Result = DAG.getZeroExtendInReg(Result,
3941 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003942 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003943 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003944 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003945 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003946 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003947 Result,
3948 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003949 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003950 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003951 }
3952 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003953 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003954 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003955 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003956 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003957
3958 // If this operation is not supported, convert it to a shl/shr or load/store
3959 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003960 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3961 default: assert(0 && "This action not supported for this op yet!");
3962 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003963 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003964 break;
3965 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003966 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003967 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003968 // NOTE: we could fall back on load/store here too for targets without
3969 // SAR. However, it is doubtful that any exist.
3970 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3971 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003972 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003973 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3974 Node->getOperand(0), ShiftCst);
3975 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3976 Result, ShiftCst);
3977 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003978 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003979 // EXTLOAD pair, targetting a temporary location (a stack slot).
3980
3981 // NOTE: there is a choice here between constantly creating new stack
3982 // slots and always reusing the same one. We currently always create
3983 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00003984 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3985 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00003986 } else {
3987 assert(0 && "Unknown op");
3988 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003989 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003990 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003991 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003992 }
Duncan Sands36397f52007-07-27 12:58:54 +00003993 case ISD::TRAMPOLINE: {
3994 SDOperand Ops[6];
3995 for (unsigned i = 0; i != 6; ++i)
3996 Ops[i] = LegalizeOp(Node->getOperand(i));
3997 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3998 // The only option for this node is to custom lower it.
3999 Result = TLI.LowerOperation(Result, DAG);
4000 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00004001
4002 // Since trampoline produces two values, make sure to remember that we
4003 // legalized both of them.
4004 Tmp1 = LegalizeOp(Result.getValue(1));
4005 Result = LegalizeOp(Result);
4006 AddLegalizedOperand(SDOperand(Node, 0), Result);
4007 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
4008 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00004009 }
Dan Gohman1a024862008-01-31 00:41:03 +00004010 case ISD::FLT_ROUNDS_: {
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004011 MVT::ValueType VT = Node->getValueType(0);
4012 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4013 default: assert(0 && "This action not supported for this op yet!");
4014 case TargetLowering::Custom:
4015 Result = TLI.LowerOperation(Op, DAG);
4016 if (Result.Val) break;
4017 // Fall Thru
4018 case TargetLowering::Legal:
4019 // If this operation is not supported, lower it to constant 1
4020 Result = DAG.getConstant(1, VT);
4021 break;
4022 }
4023 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004024 case ISD::TRAP: {
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004025 MVT::ValueType VT = Node->getValueType(0);
4026 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4027 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004028 case TargetLowering::Legal:
4029 Tmp1 = LegalizeOp(Node->getOperand(0));
4030 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4031 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004032 case TargetLowering::Custom:
4033 Result = TLI.LowerOperation(Op, DAG);
4034 if (Result.Val) break;
4035 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004036 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004037 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004038 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004039 TargetLowering::ArgListTy Args;
4040 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004041 TLI.LowerCallTo(Tmp1, Type::VoidTy,
4042 false, false, false, CallingConv::C, false,
Chris Lattner034f12e2008-01-15 22:09:33 +00004043 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
4044 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004045 Result = CallResult.second;
4046 break;
4047 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004048 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004049 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00004050 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004051
Chris Lattner4ddd2832006-04-08 04:13:17 +00004052 assert(Result.getValueType() == Op.getValueType() &&
4053 "Bad legalization!");
4054
Chris Lattner456a93a2006-01-28 07:39:30 +00004055 // Make sure that the generated code is itself legal.
4056 if (Result != Op)
4057 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004058
Chris Lattner45982da2005-05-12 16:53:42 +00004059 // Note that LegalizeOp may be reentered even from single-use nodes, which
4060 // means that we always must cache transformed nodes.
4061 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004062 return Result;
4063}
4064
Chris Lattner8b6fa222005-01-15 22:16:26 +00004065/// PromoteOp - Given an operation that produces a value in an invalid type,
4066/// promote it to compute the value into a larger type. The produced value will
4067/// have the correct bits for the low portion of the register, but no guarantee
4068/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00004069SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
4070 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004071 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004072 assert(getTypeAction(VT) == Promote &&
4073 "Caller should expand or legalize operands that are not promotable!");
4074 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
4075 "Cannot promote to smaller type!");
4076
Chris Lattner03c85462005-01-15 05:21:40 +00004077 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00004078 SDOperand Result;
4079 SDNode *Node = Op.Val;
4080
Chris Lattner40030bf2007-02-04 01:17:38 +00004081 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004082 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004083
Chris Lattner03c85462005-01-15 05:21:40 +00004084 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004085 case ISD::CopyFromReg:
4086 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004087 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004088#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004089 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004090#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004091 assert(0 && "Do not know how to promote this operator!");
4092 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004093 case ISD::UNDEF:
4094 Result = DAG.getNode(ISD::UNDEF, NVT);
4095 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004096 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004097 if (VT != MVT::i1)
4098 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4099 else
4100 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004101 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4102 break;
4103 case ISD::ConstantFP:
4104 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4105 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4106 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004107
Chris Lattner82fbfb62005-01-18 02:59:52 +00004108 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004109 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004110 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
4111 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004112 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00004113
Chris Lattner03c85462005-01-15 05:21:40 +00004114 case ISD::TRUNCATE:
4115 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4116 case Legal:
4117 Result = LegalizeOp(Node->getOperand(0));
4118 assert(Result.getValueType() >= NVT &&
4119 "This truncation doesn't make sense!");
4120 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4121 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4122 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004123 case Promote:
4124 // The truncation is not required, because we don't guarantee anything
4125 // about high bits anyway.
4126 Result = PromoteOp(Node->getOperand(0));
4127 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004128 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004129 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4130 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004131 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004132 }
4133 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004134 case ISD::SIGN_EXTEND:
4135 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004136 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004137 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4138 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4139 case Legal:
4140 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004141 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004142 break;
4143 case Promote:
4144 // Promote the reg if it's smaller.
4145 Result = PromoteOp(Node->getOperand(0));
4146 // The high bits are not guaranteed to be anything. Insert an extend.
4147 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004148 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004149 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004150 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004151 Result = DAG.getZeroExtendInReg(Result,
4152 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004153 break;
4154 }
4155 break;
Chris Lattner35481892005-12-23 00:16:34 +00004156 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004157 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4158 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004159 Result = PromoteOp(Result);
4160 break;
4161
Chris Lattner8b6fa222005-01-15 22:16:26 +00004162 case ISD::FP_EXTEND:
4163 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4164 case ISD::FP_ROUND:
4165 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4166 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4167 case Promote: assert(0 && "Unreachable with 2 FP types!");
4168 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004169 if (Node->getConstantOperandVal(1) == 0) {
4170 // Input is legal? Do an FP_ROUND_INREG.
4171 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4172 DAG.getValueType(VT));
4173 } else {
4174 // Just remove the truncate, it isn't affecting the value.
4175 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4176 Node->getOperand(1));
4177 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004178 break;
4179 }
4180 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004181 case ISD::SINT_TO_FP:
4182 case ISD::UINT_TO_FP:
4183 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4184 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004185 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004186 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004187 break;
4188
4189 case Promote:
4190 Result = PromoteOp(Node->getOperand(0));
4191 if (Node->getOpcode() == ISD::SINT_TO_FP)
4192 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004193 Result,
4194 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004195 else
Chris Lattner23993562005-04-13 02:38:47 +00004196 Result = DAG.getZeroExtendInReg(Result,
4197 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004198 // No extra round required here.
4199 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004200 break;
4201 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004202 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4203 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004204 // Round if we cannot tolerate excess precision.
4205 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004206 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4207 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004208 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004209 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004210 break;
4211
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004212 case ISD::SIGN_EXTEND_INREG:
4213 Result = PromoteOp(Node->getOperand(0));
4214 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4215 Node->getOperand(1));
4216 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004217 case ISD::FP_TO_SINT:
4218 case ISD::FP_TO_UINT:
4219 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4220 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004221 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004222 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004223 break;
4224 case Promote:
4225 // The input result is prerounded, so we don't have to do anything
4226 // special.
4227 Tmp1 = PromoteOp(Node->getOperand(0));
4228 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004229 }
Nate Begemand2558e32005-08-14 01:20:53 +00004230 // If we're promoting a UINT to a larger size, check to see if the new node
4231 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4232 // we can use that instead. This allows us to generate better code for
4233 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4234 // legal, such as PowerPC.
4235 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004236 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004237 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4238 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004239 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4240 } else {
4241 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4242 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004243 break;
4244
Chris Lattner2c8086f2005-04-02 05:00:07 +00004245 case ISD::FABS:
4246 case ISD::FNEG:
4247 Tmp1 = PromoteOp(Node->getOperand(0));
4248 assert(Tmp1.getValueType() == NVT);
4249 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4250 // NOTE: we do not have to do any extra rounding here for
4251 // NoExcessFPPrecision, because we know the input will have the appropriate
4252 // precision, and these operations don't modify precision at all.
4253 break;
4254
Chris Lattnerda6ba872005-04-28 21:44:33 +00004255 case ISD::FSQRT:
4256 case ISD::FSIN:
4257 case ISD::FCOS:
4258 Tmp1 = PromoteOp(Node->getOperand(0));
4259 assert(Tmp1.getValueType() == NVT);
4260 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004261 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004262 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4263 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004264 break;
4265
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004266 case ISD::FPOWI: {
4267 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4268 // directly as well, which may be better.
4269 Tmp1 = PromoteOp(Node->getOperand(0));
4270 assert(Tmp1.getValueType() == NVT);
4271 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4272 if (NoExcessFPPrecision)
4273 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4274 DAG.getValueType(VT));
4275 break;
4276 }
4277
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004278 case ISD::ATOMIC_LCS: {
4279 Tmp2 = PromoteOp(Node->getOperand(2));
4280 Tmp3 = PromoteOp(Node->getOperand(3));
4281 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4282 Node->getOperand(1), Tmp2, Tmp3,
4283 cast<AtomicSDNode>(Node)->getVT());
4284 // Remember that we legalized the chain.
4285 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4286 break;
4287 }
4288 case ISD::ATOMIC_LAS:
4289 case ISD::ATOMIC_SWAP: {
4290 Tmp2 = PromoteOp(Node->getOperand(2));
4291 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4292 Node->getOperand(1), Tmp2,
4293 cast<AtomicSDNode>(Node)->getVT());
4294 // Remember that we legalized the chain.
4295 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4296 break;
4297 }
4298
Chris Lattner03c85462005-01-15 05:21:40 +00004299 case ISD::AND:
4300 case ISD::OR:
4301 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004302 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004303 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004304 case ISD::MUL:
4305 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004306 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004307 // that too is okay if they are integer operations.
4308 Tmp1 = PromoteOp(Node->getOperand(0));
4309 Tmp2 = PromoteOp(Node->getOperand(1));
4310 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4311 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004312 break;
4313 case ISD::FADD:
4314 case ISD::FSUB:
4315 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004316 Tmp1 = PromoteOp(Node->getOperand(0));
4317 Tmp2 = PromoteOp(Node->getOperand(1));
4318 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4319 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4320
4321 // Floating point operations will give excess precision that we may not be
4322 // able to tolerate. If we DO allow excess precision, just leave it,
4323 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004324 // FIXME: Why would we need to round FP ops more than integer ones?
4325 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004326 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004327 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4328 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004329 break;
4330
Chris Lattner8b6fa222005-01-15 22:16:26 +00004331 case ISD::SDIV:
4332 case ISD::SREM:
4333 // These operators require that their input be sign extended.
4334 Tmp1 = PromoteOp(Node->getOperand(0));
4335 Tmp2 = PromoteOp(Node->getOperand(1));
4336 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004337 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4338 DAG.getValueType(VT));
4339 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4340 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004341 }
4342 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4343
4344 // Perform FP_ROUND: this is probably overly pessimistic.
4345 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004346 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4347 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004348 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004349 case ISD::FDIV:
4350 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004351 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004352 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004353 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004354 case Expand: assert(0 && "not implemented");
4355 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4356 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004357 }
4358 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004359 case Expand: assert(0 && "not implemented");
4360 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4361 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004362 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004363 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4364
4365 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004366 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004367 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4368 DAG.getValueType(VT));
4369 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004370
4371 case ISD::UDIV:
4372 case ISD::UREM:
4373 // These operators require that their input be zero extended.
4374 Tmp1 = PromoteOp(Node->getOperand(0));
4375 Tmp2 = PromoteOp(Node->getOperand(1));
4376 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004377 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4378 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004379 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4380 break;
4381
4382 case ISD::SHL:
4383 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004384 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004385 break;
4386 case ISD::SRA:
4387 // The input value must be properly sign extended.
4388 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004389 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4390 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004391 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004392 break;
4393 case ISD::SRL:
4394 // The input value must be properly zero extended.
4395 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004396 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004397 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004398 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004399
4400 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004401 Tmp1 = Node->getOperand(0); // Get the chain.
4402 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004403 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4404 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4405 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4406 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004407 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4408 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004409 // Increment the pointer, VAList, to the next vaarg
4410 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4411 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4412 TLI.getPointerTy()));
4413 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004414 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004415 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004416 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004417 }
4418 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004419 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004420 break;
4421
Evan Cheng466685d2006-10-09 20:57:25 +00004422 case ISD::LOAD: {
4423 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004424 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4425 ? ISD::EXTLOAD : LD->getExtensionType();
4426 Result = DAG.getExtLoad(ExtType, NVT,
4427 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004428 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004429 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004430 LD->isVolatile(),
4431 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004432 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004433 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004434 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004435 }
Chris Lattner03c85462005-01-15 05:21:40 +00004436 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00004437 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4438 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00004439 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004440 break;
Nate Begeman9373a812005-08-10 20:51:12 +00004441 case ISD::SELECT_CC:
4442 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4443 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4444 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004445 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004446 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004447 case ISD::BSWAP:
4448 Tmp1 = Node->getOperand(0);
4449 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4450 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4451 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004452 DAG.getConstant(MVT::getSizeInBits(NVT) -
4453 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00004454 TLI.getShiftAmountTy()));
4455 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004456 case ISD::CTPOP:
4457 case ISD::CTTZ:
4458 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004459 // Zero extend the argument
4460 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004461 // Perform the larger operation, then subtract if needed.
4462 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004463 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004464 case ISD::CTPOP:
4465 Result = Tmp1;
4466 break;
4467 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004468 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00004469 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004470 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4471 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004472 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004473 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004474 break;
4475 case ISD::CTLZ:
4476 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004477 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004478 DAG.getConstant(MVT::getSizeInBits(NVT) -
4479 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004480 break;
4481 }
4482 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004483 case ISD::EXTRACT_SUBVECTOR:
4484 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004485 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004486 case ISD::EXTRACT_VECTOR_ELT:
4487 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4488 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004489 }
4490
4491 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004492
4493 // Make sure the result is itself legal.
4494 Result = LegalizeOp(Result);
4495
4496 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004497 AddPromotedOperand(Op, Result);
4498 return Result;
4499}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004500
Dan Gohman7f321562007-06-25 16:23:39 +00004501/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4502/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4503/// based on the vector type. The return type of this matches the element type
4504/// of the vector, which may not be legal for the target.
4505SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004506 // We know that operand #0 is the Vec vector. If the index is a constant
4507 // or if the invec is a supported hardware type, we can use it. Otherwise,
4508 // lower to a store then an indexed load.
4509 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004510 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004511
Dan Gohmane40c7b02007-09-24 15:54:53 +00004512 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00004513 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00004514
Dan Gohman7f321562007-06-25 16:23:39 +00004515 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4516 default: assert(0 && "This action is not supported yet!");
4517 case TargetLowering::Custom: {
4518 Vec = LegalizeOp(Vec);
4519 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4520 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4521 if (Tmp3.Val)
4522 return Tmp3;
4523 break;
4524 }
4525 case TargetLowering::Legal:
4526 if (isTypeLegal(TVT)) {
4527 Vec = LegalizeOp(Vec);
4528 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004529 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004530 }
4531 break;
4532 case TargetLowering::Expand:
4533 break;
4534 }
4535
4536 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004537 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004538 Op = ScalarizeVectorOp(Vec);
4539 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00004540 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00004541 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00004542 SDOperand Lo, Hi;
4543 SplitVectorOp(Vec, Lo, Hi);
Nate Begeman55030dc2008-01-29 02:24:00 +00004544 if (CIdx->getValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00004545 Vec = Lo;
4546 } else {
4547 Vec = Hi;
Nate Begeman55030dc2008-01-29 02:24:00 +00004548 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00004549 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004550 }
Dan Gohman7f321562007-06-25 16:23:39 +00004551
Chris Lattner15972212006-03-31 17:55:51 +00004552 // It's now an extract from the appropriate high or low part. Recurse.
4553 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004554 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004555 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004556 // Store the value to a temporary stack slot, then LOAD the scalar
4557 // element back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004558 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00004559 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4560
4561 // Add the offset to the index.
4562 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4563 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4564 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004565
4566 if (MVT::getSizeInBits(Idx.getValueType()) >
4567 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004568 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004569 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004570 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004571
Dan Gohman7f321562007-06-25 16:23:39 +00004572 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4573
4574 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004575 }
Dan Gohman7f321562007-06-25 16:23:39 +00004576 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004577}
4578
Dan Gohman7f321562007-06-25 16:23:39 +00004579/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004580/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00004581SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004582 // We know that operand #0 is the Vec vector. For now we assume the index
4583 // is a constant and that the extracted result is a supported hardware type.
4584 SDOperand Vec = Op.getOperand(0);
4585 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4586
Dan Gohman7f321562007-06-25 16:23:39 +00004587 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004588
4589 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4590 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004591 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004592 }
4593
4594 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4595 SDOperand Lo, Hi;
4596 SplitVectorOp(Vec, Lo, Hi);
4597 if (CIdx->getValue() < NumElems/2) {
4598 Vec = Lo;
4599 } else {
4600 Vec = Hi;
4601 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4602 }
4603
4604 // It's now an extract from the appropriate high or low part. Recurse.
4605 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004606 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004607}
4608
Nate Begeman750ac1b2006-02-01 07:19:44 +00004609/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4610/// with condition CC on the current target. This usually involves legalizing
4611/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4612/// there may be no choice but to create a new SetCC node to represent the
4613/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4614/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4615void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4616 SDOperand &RHS,
4617 SDOperand &CC) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00004618 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004619
4620 switch (getTypeAction(LHS.getValueType())) {
4621 case Legal:
4622 Tmp1 = LegalizeOp(LHS); // LHS
4623 Tmp2 = LegalizeOp(RHS); // RHS
4624 break;
4625 case Promote:
4626 Tmp1 = PromoteOp(LHS); // LHS
4627 Tmp2 = PromoteOp(RHS); // RHS
4628
4629 // If this is an FP compare, the operands have already been extended.
4630 if (MVT::isInteger(LHS.getValueType())) {
4631 MVT::ValueType VT = LHS.getValueType();
4632 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4633
4634 // Otherwise, we have to insert explicit sign or zero extends. Note
4635 // that we could insert sign extends for ALL conditions, but zero extend
4636 // is cheaper on many machines (an AND instead of two shifts), so prefer
4637 // it.
4638 switch (cast<CondCodeSDNode>(CC)->get()) {
4639 default: assert(0 && "Unknown integer comparison!");
4640 case ISD::SETEQ:
4641 case ISD::SETNE:
4642 case ISD::SETUGE:
4643 case ISD::SETUGT:
4644 case ISD::SETULE:
4645 case ISD::SETULT:
4646 // ALL of these operations will work if we either sign or zero extend
4647 // the operands (including the unsigned comparisons!). Zero extend is
4648 // usually a simpler/cheaper operation, so prefer it.
4649 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4650 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4651 break;
4652 case ISD::SETGE:
4653 case ISD::SETGT:
4654 case ISD::SETLT:
4655 case ISD::SETLE:
4656 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4657 DAG.getValueType(VT));
4658 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4659 DAG.getValueType(VT));
4660 break;
4661 }
4662 }
4663 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004664 case Expand: {
4665 MVT::ValueType VT = LHS.getValueType();
4666 if (VT == MVT::f32 || VT == MVT::f64) {
4667 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004668 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004669 switch (cast<CondCodeSDNode>(CC)->get()) {
4670 case ISD::SETEQ:
4671 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004672 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004673 break;
4674 case ISD::SETNE:
4675 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004676 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004677 break;
4678 case ISD::SETGE:
4679 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004680 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004681 break;
4682 case ISD::SETLT:
4683 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004684 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004685 break;
4686 case ISD::SETLE:
4687 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004688 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004689 break;
4690 case ISD::SETGT:
4691 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004692 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004693 break;
4694 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004695 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4696 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004697 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004698 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004699 break;
4700 default:
Evan Cheng56966222007-01-12 02:11:51 +00004701 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004702 switch (cast<CondCodeSDNode>(CC)->get()) {
4703 case ISD::SETONE:
4704 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004705 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004706 // Fallthrough
4707 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004708 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004709 break;
4710 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004711 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004712 break;
4713 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004714 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004715 break;
4716 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004717 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004718 break;
Evan Cheng56966222007-01-12 02:11:51 +00004719 case ISD::SETUEQ:
4720 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004721 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004722 default: assert(0 && "Unsupported FP setcc!");
4723 }
4724 }
4725
4726 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004727 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004728 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004729 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004730 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004731 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004732 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004733 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004734 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004735 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004736 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004737 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004738 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004739 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4740 Tmp2 = SDOperand();
4741 }
4742 LHS = Tmp1;
4743 RHS = Tmp2;
4744 return;
4745 }
4746
Nate Begeman750ac1b2006-02-01 07:19:44 +00004747 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4748 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004749 ExpandOp(RHS, RHSLo, RHSHi);
4750 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4751
4752 if (VT==MVT::ppcf128) {
4753 // FIXME: This generated code sucks. We want to generate
4754 // FCMP crN, hi1, hi2
4755 // BNE crN, L:
4756 // FCMP crN, lo1, lo2
4757 // The following can be improved, but not that much.
4758 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4759 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4760 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4761 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4762 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4763 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4764 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4765 Tmp2 = SDOperand();
4766 break;
4767 }
4768
4769 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004770 case ISD::SETEQ:
4771 case ISD::SETNE:
4772 if (RHSLo == RHSHi)
4773 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4774 if (RHSCST->isAllOnesValue()) {
4775 // Comparison to -1.
4776 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4777 Tmp2 = RHSLo;
4778 break;
4779 }
4780
4781 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4782 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4783 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4784 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4785 break;
4786 default:
4787 // If this is a comparison of the sign bit, just look at the top part.
4788 // X > -1, x < 0
4789 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4790 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4791 CST->getValue() == 0) || // X < 0
4792 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4793 CST->isAllOnesValue())) { // X > -1
4794 Tmp1 = LHSHi;
4795 Tmp2 = RHSHi;
4796 break;
4797 }
4798
4799 // FIXME: This generated code sucks.
4800 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004801 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004802 default: assert(0 && "Unknown integer setcc!");
4803 case ISD::SETLT:
4804 case ISD::SETULT: LowCC = ISD::SETULT; break;
4805 case ISD::SETGT:
4806 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4807 case ISD::SETLE:
4808 case ISD::SETULE: LowCC = ISD::SETULE; break;
4809 case ISD::SETGE:
4810 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4811 }
4812
4813 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4814 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4815 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4816
4817 // NOTE: on targets without efficient SELECT of bools, we can always use
4818 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004819 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4820 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4821 false, DagCombineInfo);
4822 if (!Tmp1.Val)
4823 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4824 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4825 CCCode, false, DagCombineInfo);
4826 if (!Tmp2.Val)
Chris Lattner85dd3be2007-10-15 17:48:57 +00004827 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004828
4829 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4830 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4831 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4832 (Tmp2C && Tmp2C->getValue() == 0 &&
4833 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4834 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4835 (Tmp2C && Tmp2C->getValue() == 1 &&
4836 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4837 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4838 // low part is known false, returns high part.
4839 // For LE / GE, if high part is known false, ignore the low part.
4840 // For LT / GT, if high part is known true, ignore the low part.
4841 Tmp1 = Tmp2;
4842 Tmp2 = SDOperand();
4843 } else {
4844 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4845 ISD::SETEQ, false, DagCombineInfo);
4846 if (!Result.Val)
4847 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4848 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4849 Result, Tmp1, Tmp2));
4850 Tmp1 = Result;
4851 Tmp2 = SDOperand();
4852 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004853 }
4854 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004855 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004856 LHS = Tmp1;
4857 RHS = Tmp2;
4858}
4859
Chris Lattner1401d152008-01-16 07:45:30 +00004860/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4861/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4862/// a load from the stack slot to DestVT, extending it if needed.
4863/// The resultant code need not be legal.
4864SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4865 MVT::ValueType SlotVT,
4866 MVT::ValueType DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00004867 // Create the stack frame object.
Chris Lattner1401d152008-01-16 07:45:30 +00004868 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4869
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00004870 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00004871 int SPFI = StackPtrFI->getIndex();
4872
Chris Lattner1401d152008-01-16 07:45:30 +00004873 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4874 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4875 unsigned DestSize = MVT::getSizeInBits(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004876
Chris Lattner1401d152008-01-16 07:45:30 +00004877 // Emit a store to the stack slot. Use a truncstore if the input value is
4878 // later than DestVT.
4879 SDOperand Store;
4880 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00004881 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004882 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00004883 SPFI, SlotVT);
Chris Lattner1401d152008-01-16 07:45:30 +00004884 else {
4885 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00004886 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004887 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00004888 SPFI, SlotVT);
Chris Lattner1401d152008-01-16 07:45:30 +00004889 }
4890
Chris Lattner35481892005-12-23 00:16:34 +00004891 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00004892 if (SlotSize == DestSize)
4893 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4894
4895 assert(SlotSize < DestSize && "Unknown extension!");
4896 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Chris Lattner35481892005-12-23 00:16:34 +00004897}
4898
Chris Lattner4352cc92006-04-04 17:23:26 +00004899SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4900 // Create a vector sized/aligned stack slot, store the value to element #0,
4901 // then load the whole vector back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004902 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00004903
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00004904 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00004905 int SPFI = StackPtrFI->getIndex();
4906
Evan Cheng786225a2006-10-05 23:01:46 +00004907 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004908 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohman69de1932008-02-06 22:27:42 +00004909 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004910 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner4352cc92006-04-04 17:23:26 +00004911}
4912
4913
Chris Lattnerce872152006-03-19 06:31:19 +00004914/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004915/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004916SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4917
4918 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004919 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004920 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004921 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004922 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004923 std::map<SDOperand, std::vector<unsigned> > Values;
4924 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004925 bool isConstant = true;
4926 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4927 SplatValue.getOpcode() != ISD::UNDEF)
4928 isConstant = false;
4929
Evan Cheng033e6812006-03-24 01:17:21 +00004930 for (unsigned i = 1; i < NumElems; ++i) {
4931 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004932 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004933 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004934 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004935 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004936 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004937
4938 // If this isn't a constant element or an undef, we can't use a constant
4939 // pool load.
4940 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4941 V.getOpcode() != ISD::UNDEF)
4942 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004943 }
4944
4945 if (isOnlyLowElement) {
4946 // If the low element is an undef too, then this whole things is an undef.
4947 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4948 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4949 // Otherwise, turn this into a scalar_to_vector node.
4950 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4951 Node->getOperand(0));
4952 }
4953
Chris Lattner2eb86532006-03-24 07:29:17 +00004954 // If all elements are constants, create a load from the constant pool.
4955 if (isConstant) {
4956 MVT::ValueType VT = Node->getValueType(0);
4957 const Type *OpNTy =
4958 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4959 std::vector<Constant*> CV;
4960 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4961 if (ConstantFPSDNode *V =
4962 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004963 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004964 } else if (ConstantSDNode *V =
4965 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004966 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004967 } else {
4968 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4969 CV.push_back(UndefValue::get(OpNTy));
4970 }
4971 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004972 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004973 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00004974 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004975 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004976 }
4977
Chris Lattner87100e02006-03-20 01:52:29 +00004978 if (SplatValue.Val) { // Splat of one value?
4979 // Build the shuffle constant vector: <0, 0, 0, 0>
4980 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004981 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004982 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004983 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004984 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4985 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004986
4987 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004988 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004989 // Get the splatted value into the low element of a vector register.
4990 SDOperand LowValVec =
4991 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4992
4993 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4994 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4995 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4996 SplatMask);
4997 }
4998 }
4999
Evan Cheng033e6812006-03-24 01:17:21 +00005000 // If there are only two unique elements, we may be able to turn this into a
5001 // vector shuffle.
5002 if (Values.size() == 2) {
5003 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
5004 MVT::ValueType MaskVT =
5005 MVT::getIntVectorWithNumElements(NumElems);
5006 std::vector<SDOperand> MaskVec(NumElems);
5007 unsigned i = 0;
5008 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
5009 E = Values.end(); I != E; ++I) {
5010 for (std::vector<unsigned>::iterator II = I->second.begin(),
5011 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00005012 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00005013 i += NumElems;
5014 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005015 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
5016 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005017
5018 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005019 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5020 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005021 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00005022 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
5023 E = Values.end(); I != E; ++I) {
5024 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5025 I->first);
5026 Ops.push_back(Op);
5027 }
5028 Ops.push_back(ShuffleMask);
5029
5030 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005031 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
5032 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005033 }
5034 }
Chris Lattnerce872152006-03-19 06:31:19 +00005035
5036 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5037 // aligned object on the stack, store each element into it, then load
5038 // the result as a vector.
5039 MVT::ValueType VT = Node->getValueType(0);
5040 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00005041 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005042
5043 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005044 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00005045 unsigned TypeByteSize =
5046 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005047 // Store (in the right endianness) the elements to memory.
5048 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5049 // Ignore undef elements.
5050 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5051
Chris Lattner841c8822006-03-22 01:46:54 +00005052 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005053
5054 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5055 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5056
Evan Cheng786225a2006-10-05 23:01:46 +00005057 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00005058 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00005059 }
5060
5061 SDOperand StoreChain;
5062 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005063 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5064 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005065 else
5066 StoreChain = DAG.getEntryNode();
5067
5068 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00005069 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005070}
5071
Chris Lattner5b359c62005-04-02 04:00:59 +00005072void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5073 SDOperand Op, SDOperand Amt,
5074 SDOperand &Lo, SDOperand &Hi) {
5075 // Expand the subcomponents.
5076 SDOperand LHSL, LHSH;
5077 ExpandOp(Op, LHSL, LHSH);
5078
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005079 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005080 MVT::ValueType VT = LHSL.getValueType();
5081 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005082 Hi = Lo.getValue(1);
5083}
5084
5085
Chris Lattnere34b3962005-01-19 04:19:40 +00005086/// ExpandShift - Try to find a clever way to expand this shift operation out to
5087/// smaller elements. If we can't find a way that is more efficient than a
5088/// libcall on this target, return false. Otherwise, return true with the
5089/// low-parts expanded into Lo and Hi.
5090bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
5091 SDOperand &Lo, SDOperand &Hi) {
5092 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5093 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005094
Chris Lattnere34b3962005-01-19 04:19:40 +00005095 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005096 SDOperand ShAmt = LegalizeOp(Amt);
5097 MVT::ValueType ShTy = ShAmt.getValueType();
Dan Gohman91dc17b2008-02-20 16:57:27 +00005098 unsigned ShBits = MVT::getSizeInBits(ShTy);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005099 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
5100 unsigned NVTBits = MVT::getSizeInBits(NVT);
5101
Chris Lattner7a3c8552007-10-14 20:35:12 +00005102 // Handle the case when Amt is an immediate.
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005103 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5104 unsigned Cst = CN->getValue();
5105 // Expand the incoming operand to be shifted, so that we have its parts
5106 SDOperand InL, InH;
5107 ExpandOp(Op, InL, InH);
5108 switch(Opc) {
5109 case ISD::SHL:
5110 if (Cst > VTBits) {
5111 Lo = DAG.getConstant(0, NVT);
5112 Hi = DAG.getConstant(0, NVT);
5113 } else if (Cst > NVTBits) {
5114 Lo = DAG.getConstant(0, NVT);
5115 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005116 } else if (Cst == NVTBits) {
5117 Lo = DAG.getConstant(0, NVT);
5118 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005119 } else {
5120 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5121 Hi = DAG.getNode(ISD::OR, NVT,
5122 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5123 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5124 }
5125 return true;
5126 case ISD::SRL:
5127 if (Cst > VTBits) {
5128 Lo = DAG.getConstant(0, NVT);
5129 Hi = DAG.getConstant(0, NVT);
5130 } else if (Cst > NVTBits) {
5131 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5132 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005133 } else if (Cst == NVTBits) {
5134 Lo = InH;
5135 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005136 } else {
5137 Lo = DAG.getNode(ISD::OR, NVT,
5138 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5139 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5140 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5141 }
5142 return true;
5143 case ISD::SRA:
5144 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005145 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005146 DAG.getConstant(NVTBits-1, ShTy));
5147 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005148 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005149 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005150 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005151 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005152 } else if (Cst == NVTBits) {
5153 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005154 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005155 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005156 } else {
5157 Lo = DAG.getNode(ISD::OR, NVT,
5158 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5159 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5160 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5161 }
5162 return true;
5163 }
5164 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005165
5166 // Okay, the shift amount isn't constant. However, if we can tell that it is
5167 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005168 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5169 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005170 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005171
Dan Gohman9e255b72008-02-22 01:12:31 +00005172 // If we know that if any of the high bits of the shift amount are one, then
5173 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005174 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005175 // Mask out the high bit, which we know is set.
5176 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005177 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005178
5179 // Expand the incoming operand to be shifted, so that we have its parts
5180 SDOperand InL, InH;
5181 ExpandOp(Op, InL, InH);
5182 switch(Opc) {
5183 case ISD::SHL:
5184 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5185 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5186 return true;
5187 case ISD::SRL:
5188 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5189 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5190 return true;
5191 case ISD::SRA:
5192 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5193 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5194 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5195 return true;
5196 }
5197 }
5198
Dan Gohman9e255b72008-02-22 01:12:31 +00005199 // If we know that the high bits of the shift amount are all zero, then we can
5200 // do this as a couple of simple shifts.
5201 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005202 // Compute 32-amt.
5203 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5204 DAG.getConstant(NVTBits, Amt.getValueType()),
5205 Amt);
5206
5207 // Expand the incoming operand to be shifted, so that we have its parts
5208 SDOperand InL, InH;
5209 ExpandOp(Op, InL, InH);
5210 switch(Opc) {
5211 case ISD::SHL:
5212 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5213 Hi = DAG.getNode(ISD::OR, NVT,
5214 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5215 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5216 return true;
5217 case ISD::SRL:
5218 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5219 Lo = DAG.getNode(ISD::OR, NVT,
5220 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5221 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5222 return true;
5223 case ISD::SRA:
5224 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5225 Lo = DAG.getNode(ISD::OR, NVT,
5226 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5227 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5228 return true;
5229 }
5230 }
5231
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005232 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005233}
Chris Lattner77e77a62005-01-21 06:05:23 +00005234
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005235
Chris Lattner77e77a62005-01-21 06:05:23 +00005236// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5237// does not fit into a register, return the lo part and set the hi part to the
5238// by-reg argument. If it does fit into a single register, return the result
5239// and leave the Hi part unset.
5240SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00005241 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005242 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5243 // The input chain to this libcall is the entry node of the function.
5244 // Legalizing the call will automatically add the previous call to the
5245 // dependence.
5246 SDOperand InChain = DAG.getEntryNode();
5247
Chris Lattner77e77a62005-01-21 06:05:23 +00005248 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005249 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005250 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5251 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5252 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00005253 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005254 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005255 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005256 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005257 }
5258 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005259
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005260 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00005261 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005262 std::pair<SDOperand,SDOperand> CallInfo =
Duncan Sands00fee652008-02-14 17:28:50 +00005263 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5264 false, Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005265
Chris Lattner6831a812006-02-13 09:18:02 +00005266 // Legalize the call sequence, starting with the chain. This will advance
5267 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5268 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5269 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00005270 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005271 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005272 default: assert(0 && "Unknown thing");
5273 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005274 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005275 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005276 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005277 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005278 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005279 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005280 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005281}
5282
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005283
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005284/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5285///
Chris Lattner77e77a62005-01-21 06:05:23 +00005286SDOperand SelectionDAGLegalize::
5287ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Dan Gohmand91446d2008-03-05 01:08:17 +00005288 MVT::ValueType SourceVT = Source.getValueType();
5289 assert(getTypeAction(SourceVT) == Expand &&
Chris Lattner77e77a62005-01-21 06:05:23 +00005290 "This is not an expansion!");
Chris Lattner77e77a62005-01-21 06:05:23 +00005291
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005292 if (!isSigned) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005293 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005294 // incoming integer is set. To handle this, we dynamically test to see if
5295 // it is set, and, if so, add a fudge factor.
5296 SDOperand Lo, Hi;
5297 ExpandOp(Source, Lo, Hi);
5298
Chris Lattner66de05b2005-05-13 04:45:13 +00005299 // If this is unsigned, and not supported, first perform the conversion to
5300 // signed, then adjust the result if the sign bit is set.
5301 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
Dan Gohmand91446d2008-03-05 01:08:17 +00005302 DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi));
Chris Lattner66de05b2005-05-13 04:45:13 +00005303
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005304 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
5305 DAG.getConstant(0, Hi.getValueType()),
5306 ISD::SETLT);
Chris Lattner0bd48932008-01-17 07:00:52 +00005307 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005308 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5309 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005310 uint64_t FF = 0x5f800000ULL;
5311 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00005312 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005313
Chris Lattner5839bf22005-08-26 17:15:30 +00005314 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00005315 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5316 SDOperand FudgeInReg;
5317 if (DestTy == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005318 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005319 PseudoSourceValue::getConstantPool(), 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005320 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005321 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005322 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005323 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005324 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman69de1932008-02-06 22:27:42 +00005325 MVT::f32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005326 else
5327 assert(0 && "Unexpected conversion");
5328
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005329 MVT::ValueType SCVT = SignedConv.getValueType();
5330 if (SCVT != DestTy) {
5331 // Destination type needs to be expanded as well. The FADD now we are
5332 // constructing will be expanded into a libcall.
5333 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
Dan Gohmand91446d2008-03-05 01:08:17 +00005334 assert(MVT::getSizeInBits(SCVT) * 2 == MVT::getSizeInBits(DestTy));
5335 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005336 SignedConv, SignedConv.getValue(1));
5337 }
5338 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5339 }
Chris Lattner473a9902005-09-29 06:44:39 +00005340 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005341 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005342
Chris Lattnera88a2602005-05-14 05:33:54 +00005343 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005344 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005345 default: assert(0 && "This action not implemented for this operation!");
5346 case TargetLowering::Legal:
5347 case TargetLowering::Expand:
5348 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005349 case TargetLowering::Custom: {
5350 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5351 Source), DAG);
5352 if (NV.Val)
5353 return LegalizeOp(NV);
5354 break; // The target decided this was legal after all
5355 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005356 }
5357
Chris Lattner13689e22005-05-12 07:00:44 +00005358 // Expand the source, then glue it back together for the call. We must expand
5359 // the source in case it is shared (this pass of legalize must traverse it).
5360 SDOperand SrcLo, SrcHi;
5361 ExpandOp(Source, SrcLo, SrcHi);
Dan Gohmand91446d2008-03-05 01:08:17 +00005362 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
Chris Lattner13689e22005-05-12 07:00:44 +00005363
Evan Cheng56966222007-01-12 02:11:51 +00005364 RTLIB::Libcall LC;
Dan Gohmand91446d2008-03-05 01:08:17 +00005365 if (SourceVT == MVT::i64) {
5366 if (DestTy == MVT::f32)
5367 LC = RTLIB::SINTTOFP_I64_F32;
5368 else {
5369 assert(DestTy == MVT::f64 && "Unknown fp value type!");
5370 LC = RTLIB::SINTTOFP_I64_F64;
5371 }
5372 } else if (SourceVT == MVT::i128) {
5373 if (DestTy == MVT::f32)
5374 LC = RTLIB::SINTTOFP_I128_F32;
5375 else if (DestTy == MVT::f64)
5376 LC = RTLIB::SINTTOFP_I128_F64;
5377 else if (DestTy == MVT::f80)
5378 LC = RTLIB::SINTTOFP_I128_F80;
5379 else {
5380 assert(DestTy == MVT::ppcf128 && "Unknown fp value type!");
5381 LC = RTLIB::SINTTOFP_I128_PPCF128;
5382 }
5383 } else {
5384 assert(0 && "Unknown int value type");
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005385 }
Chris Lattner6831a812006-02-13 09:18:02 +00005386
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005387 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00005388 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5389 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00005390 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5391 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00005392}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005393
Chris Lattner22cde6a2006-01-28 08:25:58 +00005394/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5395/// INT_TO_FP operation of the specified operand when the target requests that
5396/// we expand it. At this point, we know that the result and operand types are
5397/// legal for the target.
5398SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5399 SDOperand Op0,
5400 MVT::ValueType DestVT) {
5401 if (Op0.getValueType() == MVT::i32) {
5402 // simple 32-bit [signed|unsigned] integer to float/double expansion
5403
Chris Lattner23594d42008-01-16 07:03:22 +00005404 // Get the stack frame index of a 8 byte buffer.
5405 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5406
Chris Lattner22cde6a2006-01-28 08:25:58 +00005407 // word offset constant for Hi/Lo address computation
5408 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5409 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00005410 SDOperand Hi = StackSlot;
5411 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5412 if (TLI.isLittleEndian())
5413 std::swap(Hi, Lo);
5414
Chris Lattner22cde6a2006-01-28 08:25:58 +00005415 // if signed map to unsigned space
5416 SDOperand Op0Mapped;
5417 if (isSigned) {
5418 // constant used to invert sign bit (signed to unsigned mapping)
5419 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5420 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5421 } else {
5422 Op0Mapped = Op0;
5423 }
5424 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00005425 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005426 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005427 // initial hi portion of constructed double
5428 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5429 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00005430 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005431 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00005432 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005433 // FP constant to bias correct the final result
5434 SDOperand Bias = DAG.getConstantFP(isSigned ?
5435 BitsToDouble(0x4330000080000000ULL)
5436 : BitsToDouble(0x4330000000000000ULL),
5437 MVT::f64);
5438 // subtract the bias
5439 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5440 // final result
5441 SDOperand Result;
5442 // handle final rounding
5443 if (DestVT == MVT::f64) {
5444 // do nothing
5445 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005446 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00005447 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5448 DAG.getIntPtrConstant(0));
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005449 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5450 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005451 }
5452 return Result;
5453 }
5454 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5455 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5456
5457 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5458 DAG.getConstant(0, Op0.getValueType()),
5459 ISD::SETLT);
Chris Lattner0bd48932008-01-17 07:00:52 +00005460 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005461 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5462 SignSet, Four, Zero);
5463
5464 // If the sign bit of the integer is set, the large number will be treated
5465 // as a negative number. To counteract this, the dynamic code adds an
5466 // offset depending on the data type.
5467 uint64_t FF;
5468 switch (Op0.getValueType()) {
5469 default: assert(0 && "Unsupported integer type!");
5470 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5471 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5472 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5473 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5474 }
5475 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005476 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005477
5478 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5479 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5480 SDOperand FudgeInReg;
5481 if (DestVT == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005482 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005483 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005484 else {
Dan Gohman69de1932008-02-06 22:27:42 +00005485 FudgeInReg =
5486 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5487 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005488 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman69de1932008-02-06 22:27:42 +00005489 MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005490 }
5491
5492 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5493}
5494
5495/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5496/// *INT_TO_FP operation of the specified operand when the target requests that
5497/// we promote it. At this point, we know that the result and operand types are
5498/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5499/// operation that takes a larger input.
5500SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5501 MVT::ValueType DestVT,
5502 bool isSigned) {
5503 // First step, figure out the appropriate *INT_TO_FP operation to use.
5504 MVT::ValueType NewInTy = LegalOp.getValueType();
5505
5506 unsigned OpToUse = 0;
5507
5508 // Scan for the appropriate larger type to use.
5509 while (1) {
5510 NewInTy = (MVT::ValueType)(NewInTy+1);
5511 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5512
5513 // If the target supports SINT_TO_FP of this type, use it.
5514 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5515 default: break;
5516 case TargetLowering::Legal:
5517 if (!TLI.isTypeLegal(NewInTy))
5518 break; // Can't use this datatype.
5519 // FALL THROUGH.
5520 case TargetLowering::Custom:
5521 OpToUse = ISD::SINT_TO_FP;
5522 break;
5523 }
5524 if (OpToUse) break;
5525 if (isSigned) continue;
5526
5527 // If the target supports UINT_TO_FP of this type, use it.
5528 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5529 default: break;
5530 case TargetLowering::Legal:
5531 if (!TLI.isTypeLegal(NewInTy))
5532 break; // Can't use this datatype.
5533 // FALL THROUGH.
5534 case TargetLowering::Custom:
5535 OpToUse = ISD::UINT_TO_FP;
5536 break;
5537 }
5538 if (OpToUse) break;
5539
5540 // Otherwise, try a larger type.
5541 }
5542
5543 // Okay, we found the operation and type to use. Zero extend our input to the
5544 // desired type then run the operation on it.
5545 return DAG.getNode(OpToUse, DestVT,
5546 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5547 NewInTy, LegalOp));
5548}
5549
5550/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5551/// FP_TO_*INT operation of the specified operand when the target requests that
5552/// we promote it. At this point, we know that the result and operand types are
5553/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5554/// operation that returns a larger result.
5555SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5556 MVT::ValueType DestVT,
5557 bool isSigned) {
5558 // First step, figure out the appropriate FP_TO*INT operation to use.
5559 MVT::ValueType NewOutTy = DestVT;
5560
5561 unsigned OpToUse = 0;
5562
5563 // Scan for the appropriate larger type to use.
5564 while (1) {
5565 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5566 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5567
5568 // If the target supports FP_TO_SINT returning this type, use it.
5569 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5570 default: break;
5571 case TargetLowering::Legal:
5572 if (!TLI.isTypeLegal(NewOutTy))
5573 break; // Can't use this datatype.
5574 // FALL THROUGH.
5575 case TargetLowering::Custom:
5576 OpToUse = ISD::FP_TO_SINT;
5577 break;
5578 }
5579 if (OpToUse) break;
5580
5581 // If the target supports FP_TO_UINT of this type, use it.
5582 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5583 default: break;
5584 case TargetLowering::Legal:
5585 if (!TLI.isTypeLegal(NewOutTy))
5586 break; // Can't use this datatype.
5587 // FALL THROUGH.
5588 case TargetLowering::Custom:
5589 OpToUse = ISD::FP_TO_UINT;
5590 break;
5591 }
5592 if (OpToUse) break;
5593
5594 // Otherwise, try a larger type.
5595 }
5596
Chris Lattner27a6c732007-11-24 07:07:01 +00005597
5598 // Okay, we found the operation and type to use.
5599 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5600
5601 // If the operation produces an invalid type, it must be custom lowered. Use
5602 // the target lowering hooks to expand it. Just keep the low part of the
5603 // expanded operation, we know that we're truncating anyway.
5604 if (getTypeAction(NewOutTy) == Expand) {
5605 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5606 assert(Operation.Val && "Didn't return anything");
5607 }
5608
5609 // Truncate the result of the extended FP_TO_*INT operation to the desired
5610 // size.
5611 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005612}
5613
5614/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5615///
5616SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5617 MVT::ValueType VT = Op.getValueType();
5618 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5619 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5620 switch (VT) {
5621 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5622 case MVT::i16:
5623 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5624 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5625 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5626 case MVT::i32:
5627 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5628 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5629 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5630 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5631 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5632 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5633 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5634 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5635 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5636 case MVT::i64:
5637 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5638 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5639 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5640 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5641 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5642 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5643 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5644 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5645 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5646 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5647 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5648 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5649 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5650 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5651 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5652 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5653 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5654 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5655 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5656 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5657 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5658 }
5659}
5660
5661/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5662///
5663SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5664 switch (Opc) {
5665 default: assert(0 && "Cannot expand this yet!");
5666 case ISD::CTPOP: {
5667 static const uint64_t mask[6] = {
5668 0x5555555555555555ULL, 0x3333333333333333ULL,
5669 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5670 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5671 };
5672 MVT::ValueType VT = Op.getValueType();
5673 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005674 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005675 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5676 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5677 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5678 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5679 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5680 DAG.getNode(ISD::AND, VT,
5681 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5682 }
5683 return Op;
5684 }
5685 case ISD::CTLZ: {
5686 // for now, we do this:
5687 // x = x | (x >> 1);
5688 // x = x | (x >> 2);
5689 // ...
5690 // x = x | (x >>16);
5691 // x = x | (x >>32); // for 64-bit input
5692 // return popcount(~x);
5693 //
5694 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5695 MVT::ValueType VT = Op.getValueType();
5696 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005697 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005698 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5699 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5700 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5701 }
5702 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5703 return DAG.getNode(ISD::CTPOP, VT, Op);
5704 }
5705 case ISD::CTTZ: {
5706 // for now, we use: { return popcount(~x & (x - 1)); }
5707 // unless the target has ctlz but not ctpop, in which case we use:
5708 // { return 32 - nlz(~x & (x-1)); }
5709 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5710 MVT::ValueType VT = Op.getValueType();
5711 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5712 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5713 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5714 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5715 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5716 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5717 TLI.isOperationLegal(ISD::CTLZ, VT))
5718 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005719 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005720 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5721 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5722 }
5723 }
5724}
Chris Lattnere34b3962005-01-19 04:19:40 +00005725
Chris Lattner3e928bb2005-01-07 07:47:09 +00005726/// ExpandOp - Expand the specified SDOperand into its two component pieces
5727/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5728/// LegalizeNodes map is filled in for any results that are not expanded, the
5729/// ExpandedNodes map is filled in for any results that are expanded, and the
5730/// Lo/Hi values are returned.
5731void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5732 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005733 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005734 SDNode *Node = Op.Val;
5735 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005736 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005737 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005738 "Cannot expand to FP value or to larger int value!");
5739
Chris Lattner6fdcb252005-09-02 20:32:45 +00005740 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00005741 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005742 = ExpandedNodes.find(Op);
5743 if (I != ExpandedNodes.end()) {
5744 Lo = I->second.first;
5745 Hi = I->second.second;
5746 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005747 }
5748
Chris Lattner3e928bb2005-01-07 07:47:09 +00005749 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005750 case ISD::CopyFromReg:
5751 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005752 case ISD::FP_ROUND_INREG:
5753 if (VT == MVT::ppcf128 &&
5754 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5755 TargetLowering::Custom) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005756 SDOperand SrcLo, SrcHi, Src;
5757 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5758 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5759 SDOperand Result = TLI.LowerOperation(
5760 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005761 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5762 Lo = Result.Val->getOperand(0);
5763 Hi = Result.Val->getOperand(1);
5764 break;
5765 }
5766 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005767 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005768#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005769 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005770#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005771 assert(0 && "Do not know how to expand this operator!");
5772 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00005773 case ISD::EXTRACT_ELEMENT:
5774 ExpandOp(Node->getOperand(0), Lo, Hi);
5775 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5776 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00005777 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00005778 case ISD::EXTRACT_VECTOR_ELT:
5779 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5780 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5781 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5782 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005783 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005784 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005785 Lo = DAG.getNode(ISD::UNDEF, NVT);
5786 Hi = DAG.getNode(ISD::UNDEF, NVT);
5787 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005788 case ISD::Constant: {
Dan Gohman050f5502008-03-03 22:20:46 +00005789 unsigned NVTBits = MVT::getSizeInBits(NVT);
5790 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
5791 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
5792 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005793 break;
5794 }
Evan Cheng00495212006-12-12 21:32:44 +00005795 case ISD::ConstantFP: {
5796 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005797 if (CFP->getValueType(0) == MVT::ppcf128) {
5798 APInt api = CFP->getValueAPF().convertToAPInt();
5799 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5800 MVT::f64);
5801 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5802 MVT::f64);
5803 break;
5804 }
Evan Cheng279101e2006-12-12 22:19:28 +00005805 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005806 if (getTypeAction(Lo.getValueType()) == Expand)
5807 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005808 break;
5809 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005810 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005811 // Return the operands.
5812 Lo = Node->getOperand(0);
5813 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005814 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005815
5816 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00005817 if (Node->getNumValues() == 1) {
5818 ExpandOp(Op.getOperand(0), Lo, Hi);
5819 break;
5820 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005821 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5822 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5823 Op.getValue(1).getValueType() == MVT::Other &&
5824 "unhandled MERGE_VALUES");
5825 ExpandOp(Op.getOperand(0), Lo, Hi);
5826 // Remember that we legalized the chain.
5827 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5828 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005829
5830 case ISD::SIGN_EXTEND_INREG:
5831 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005832 // sext_inreg the low part if needed.
5833 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5834
5835 // The high part gets the sign extension from the lo-part. This handles
5836 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005837 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5838 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5839 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005840 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005841
Nate Begemand88fc032006-01-14 03:14:10 +00005842 case ISD::BSWAP: {
5843 ExpandOp(Node->getOperand(0), Lo, Hi);
5844 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5845 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5846 Lo = TempLo;
5847 break;
5848 }
5849
Chris Lattneredb1add2005-05-11 04:51:16 +00005850 case ISD::CTPOP:
5851 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005852 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5853 DAG.getNode(ISD::CTPOP, NVT, Lo),
5854 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005855 Hi = DAG.getConstant(0, NVT);
5856 break;
5857
Chris Lattner39a8f332005-05-12 19:05:01 +00005858 case ISD::CTLZ: {
5859 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005860 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005861 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5862 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005863 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5864 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005865 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5866 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5867
5868 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5869 Hi = DAG.getConstant(0, NVT);
5870 break;
5871 }
5872
5873 case ISD::CTTZ: {
5874 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005875 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005876 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5877 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005878 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5879 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005880 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5881 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5882
5883 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5884 Hi = DAG.getConstant(0, NVT);
5885 break;
5886 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005887
Nate Begemanacc398c2006-01-25 18:21:52 +00005888 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005889 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5890 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005891 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5892 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5893
5894 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005895 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005896 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00005897 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00005898 std::swap(Lo, Hi);
5899 break;
5900 }
5901
Chris Lattner3e928bb2005-01-07 07:47:09 +00005902 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005903 LoadSDNode *LD = cast<LoadSDNode>(Node);
5904 SDOperand Ch = LD->getChain(); // Legalize the chain.
5905 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5906 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005907 int SVOffset = LD->getSrcValueOffset();
5908 unsigned Alignment = LD->getAlignment();
5909 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005910
Evan Cheng466685d2006-10-09 20:57:25 +00005911 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005912 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5913 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005914 if (VT == MVT::f32 || VT == MVT::f64) {
5915 // f32->i32 or f64->i64 one to one expansion.
5916 // Remember that we legalized the chain.
5917 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005918 // Recursively expand the new load.
5919 if (getTypeAction(NVT) == Expand)
5920 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005921 break;
5922 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005923
Evan Cheng466685d2006-10-09 20:57:25 +00005924 // Increment the pointer to the other half.
5925 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5926 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00005927 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005928 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00005929 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005930 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5931 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005932
Evan Cheng466685d2006-10-09 20:57:25 +00005933 // Build a factor node to remember that this load is independent of the
5934 // other one.
5935 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5936 Hi.getValue(1));
5937
5938 // Remember that we legalized the chain.
5939 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00005940 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00005941 std::swap(Lo, Hi);
5942 } else {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005943 MVT::ValueType EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005944
Dale Johannesenb6210fc2007-10-19 20:29:00 +00005945 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5946 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00005947 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5948 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005949 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005950 // Remember that we legalized the chain.
5951 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5952 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5953 break;
5954 }
Evan Cheng466685d2006-10-09 20:57:25 +00005955
5956 if (EVT == NVT)
5957 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005958 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005959 else
5960 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005961 SVOffset, EVT, isVolatile,
5962 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005963
5964 // Remember that we legalized the chain.
5965 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5966
5967 if (ExtType == ISD::SEXTLOAD) {
5968 // The high part is obtained by SRA'ing all but one of the bits of the
5969 // lo part.
5970 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5971 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5972 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5973 } else if (ExtType == ISD::ZEXTLOAD) {
5974 // The high part is just a zero.
5975 Hi = DAG.getConstant(0, NVT);
5976 } else /* if (ExtType == ISD::EXTLOAD) */ {
5977 // The high part is undefined.
5978 Hi = DAG.getNode(ISD::UNDEF, NVT);
5979 }
5980 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005981 break;
5982 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005983 case ISD::AND:
5984 case ISD::OR:
5985 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5986 SDOperand LL, LH, RL, RH;
5987 ExpandOp(Node->getOperand(0), LL, LH);
5988 ExpandOp(Node->getOperand(1), RL, RH);
5989 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5990 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5991 break;
5992 }
5993 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005994 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005995 ExpandOp(Node->getOperand(1), LL, LH);
5996 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005997 if (getTypeAction(NVT) == Expand)
5998 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005999 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006000 if (VT != MVT::f32)
6001 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006002 break;
6003 }
Nate Begeman9373a812005-08-10 20:51:12 +00006004 case ISD::SELECT_CC: {
6005 SDOperand TL, TH, FL, FH;
6006 ExpandOp(Node->getOperand(2), TL, TH);
6007 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006008 if (getTypeAction(NVT) == Expand)
6009 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00006010 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6011 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006012 if (VT != MVT::f32)
6013 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6014 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006015 break;
6016 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006017 case ISD::ANY_EXTEND:
6018 // The low part is any extension of the input (which degenerates to a copy).
6019 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6020 // The high part is undefined.
6021 Hi = DAG.getNode(ISD::UNDEF, NVT);
6022 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006023 case ISD::SIGN_EXTEND: {
6024 // The low part is just a sign extension of the input (which degenerates to
6025 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006026 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006027
Chris Lattner3e928bb2005-01-07 07:47:09 +00006028 // The high part is obtained by SRA'ing all but one of the bits of the lo
6029 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00006030 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00006031 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6032 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006033 break;
6034 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006035 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006036 // The low part is just a zero extension of the input (which degenerates to
6037 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006038 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006039
Chris Lattner3e928bb2005-01-07 07:47:09 +00006040 // The high part is just a zero.
6041 Hi = DAG.getConstant(0, NVT);
6042 break;
Chris Lattner35481892005-12-23 00:16:34 +00006043
Chris Lattner4c948eb2007-02-13 23:55:16 +00006044 case ISD::TRUNCATE: {
6045 // The input value must be larger than this value. Expand *it*.
6046 SDOperand NewLo;
6047 ExpandOp(Node->getOperand(0), NewLo, Hi);
6048
6049 // The low part is now either the right size, or it is closer. If not the
6050 // right size, make an illegal truncate so we recursively expand it.
6051 if (NewLo.getValueType() != Node->getValueType(0))
6052 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6053 ExpandOp(NewLo, Lo, Hi);
6054 break;
6055 }
6056
Chris Lattner35481892005-12-23 00:16:34 +00006057 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006058 SDOperand Tmp;
6059 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6060 // If the target wants to, allow it to lower this itself.
6061 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6062 case Expand: assert(0 && "cannot expand FP!");
6063 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6064 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6065 }
6066 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6067 }
6068
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006069 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006070 if (VT == MVT::f32 || VT == MVT::f64) {
6071 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006072 if (getTypeAction(NVT) == Expand)
6073 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006074 break;
6075 }
6076
6077 // If source operand will be expanded to the same type as VT, i.e.
6078 // i64 <- f64, i32 <- f32, expand the source operand instead.
6079 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
6080 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6081 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006082 break;
6083 }
6084
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006085 // Turn this into a load/store pair by default.
6086 if (Tmp.Val == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006087 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006088
Chris Lattner35481892005-12-23 00:16:34 +00006089 ExpandOp(Tmp, Lo, Hi);
6090 break;
6091 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006092
Chris Lattner27a6c732007-11-24 07:07:01 +00006093 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006094 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6095 TargetLowering::Custom &&
6096 "Must custom expand ReadCycleCounter");
Chris Lattner27a6c732007-11-24 07:07:01 +00006097 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6098 assert(Tmp.Val && "Node must be custom expanded!");
6099 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner308575b2005-11-20 22:56:56 +00006100 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006101 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006102 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006103 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006104
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006105 case ISD::ATOMIC_LCS: {
6106 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6107 assert(Tmp.Val && "Node must be custom expanded!");
6108 ExpandOp(Tmp.getValue(0), Lo, Hi);
6109 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
6110 LegalizeOp(Tmp.getValue(1)));
6111 break;
6112 }
6113
6114
6115
Chris Lattner4e6c7462005-01-08 19:27:05 +00006116 // These operators cannot be expanded directly, emit them as calls to
6117 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006118 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006119 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00006120 SDOperand Op;
6121 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6122 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006123 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6124 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006125 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006126
Chris Lattnerf20d1832005-07-30 01:40:57 +00006127 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6128
Chris Lattner80a3e942005-07-29 00:33:32 +00006129 // Now that the custom expander is done, expand the result, which is still
6130 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00006131 if (Op.Val) {
6132 ExpandOp(Op, Lo, Hi);
6133 break;
6134 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006135 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006136
Dale Johannesen161e8972007-10-05 20:04:43 +00006137 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00006138 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006139 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00006140 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006141 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006142 else if (Node->getOperand(0).getValueType() == MVT::f80)
6143 LC = RTLIB::FPTOSINT_F80_I64;
6144 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6145 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00006146 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6147 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006148 break;
Evan Cheng56966222007-01-12 02:11:51 +00006149 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006150
Evan Cheng56966222007-01-12 02:11:51 +00006151 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006152 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006153 SDOperand Op;
6154 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6155 case Expand: assert(0 && "cannot expand FP!");
6156 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6157 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6158 }
6159
6160 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6161
6162 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00006163 if (Op.Val) {
6164 ExpandOp(Op, Lo, Hi);
6165 break;
6166 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006167 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006168
Evan Chengdaccea182007-10-05 01:09:32 +00006169 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00006170 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006171 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen72292f02007-09-28 18:44:17 +00006172 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006173 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006174 else if (Node->getOperand(0).getValueType() == MVT::f80)
6175 LC = RTLIB::FPTOUINT_F80_I64;
6176 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6177 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00006178 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6179 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006180 break;
Evan Cheng56966222007-01-12 02:11:51 +00006181 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006182
Evan Cheng05a2d562006-01-09 18:31:59 +00006183 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006184 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006185 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006186 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006187 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006188 Op = TLI.LowerOperation(Op, DAG);
6189 if (Op.Val) {
6190 // Now that the custom expander is done, expand the result, which is
6191 // still VT.
6192 ExpandOp(Op, Lo, Hi);
6193 break;
6194 }
6195 }
6196
Chris Lattner79980b02006-09-13 03:50:39 +00006197 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6198 // this X << 1 as X+X.
6199 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
6200 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
6201 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6202 SDOperand LoOps[2], HiOps[3];
6203 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6204 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6205 LoOps[1] = LoOps[0];
6206 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6207
6208 HiOps[1] = HiOps[0];
6209 HiOps[2] = Lo.getValue(1);
6210 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6211 break;
6212 }
6213 }
6214
Chris Lattnere34b3962005-01-19 04:19:40 +00006215 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006216 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006217 break;
Chris Lattner47599822005-04-02 03:38:53 +00006218
6219 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006220 TargetLowering::LegalizeAction Action =
6221 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6222 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6223 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006224 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006225 break;
6226 }
6227
Chris Lattnere34b3962005-01-19 04:19:40 +00006228 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006229 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
6230 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006231 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006232 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006233
Evan Cheng05a2d562006-01-09 18:31:59 +00006234 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006235 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006236 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006237 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006238 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006239 Op = TLI.LowerOperation(Op, DAG);
6240 if (Op.Val) {
6241 // Now that the custom expander is done, expand the result, which is
6242 // still VT.
6243 ExpandOp(Op, Lo, Hi);
6244 break;
6245 }
6246 }
6247
Chris Lattnere34b3962005-01-19 04:19:40 +00006248 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006249 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006250 break;
Chris Lattner47599822005-04-02 03:38:53 +00006251
6252 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006253 TargetLowering::LegalizeAction Action =
6254 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6255 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6256 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006257 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006258 break;
6259 }
6260
Chris Lattnere34b3962005-01-19 04:19:40 +00006261 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006262 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
6263 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006264 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006265 }
6266
6267 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006268 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006269 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006270 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006271 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006272 Op = TLI.LowerOperation(Op, DAG);
6273 if (Op.Val) {
6274 // Now that the custom expander is done, expand the result, which is
6275 // still VT.
6276 ExpandOp(Op, Lo, Hi);
6277 break;
6278 }
6279 }
6280
Chris Lattnere34b3962005-01-19 04:19:40 +00006281 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006282 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006283 break;
Chris Lattner47599822005-04-02 03:38:53 +00006284
6285 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006286 TargetLowering::LegalizeAction Action =
6287 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6288 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6289 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006290 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006291 break;
6292 }
6293
Chris Lattnere34b3962005-01-19 04:19:40 +00006294 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006295 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
6296 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006297 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006298 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006299
Misha Brukmanedf128a2005-04-21 22:36:52 +00006300 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006301 case ISD::SUB: {
6302 // If the target wants to custom expand this, let them.
6303 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6304 TargetLowering::Custom) {
6305 Op = TLI.LowerOperation(Op, DAG);
6306 if (Op.Val) {
6307 ExpandOp(Op, Lo, Hi);
6308 break;
6309 }
6310 }
6311
6312 // Expand the subcomponents.
6313 SDOperand LHSL, LHSH, RHSL, RHSH;
6314 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6315 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00006316 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6317 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006318 LoOps[0] = LHSL;
6319 LoOps[1] = RHSL;
6320 HiOps[0] = LHSH;
6321 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006322 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00006323 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006324 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006325 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006326 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00006327 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006328 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006329 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006330 }
Chris Lattner84f67882005-01-20 18:52:28 +00006331 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006332 }
Chris Lattnerb429f732007-05-17 18:15:41 +00006333
6334 case ISD::ADDC:
6335 case ISD::SUBC: {
6336 // Expand the subcomponents.
6337 SDOperand LHSL, LHSH, RHSL, RHSH;
6338 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6339 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6340 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6341 SDOperand LoOps[2] = { LHSL, RHSL };
6342 SDOperand HiOps[3] = { LHSH, RHSH };
6343
6344 if (Node->getOpcode() == ISD::ADDC) {
6345 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6346 HiOps[2] = Lo.getValue(1);
6347 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6348 } else {
6349 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6350 HiOps[2] = Lo.getValue(1);
6351 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6352 }
6353 // Remember that we legalized the flag.
6354 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6355 break;
6356 }
6357 case ISD::ADDE:
6358 case ISD::SUBE: {
6359 // Expand the subcomponents.
6360 SDOperand LHSL, LHSH, RHSL, RHSH;
6361 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6362 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6363 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6364 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6365 SDOperand HiOps[3] = { LHSH, RHSH };
6366
6367 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6368 HiOps[2] = Lo.getValue(1);
6369 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6370
6371 // Remember that we legalized the flag.
6372 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6373 break;
6374 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006375 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006376 // If the target wants to custom expand this, let them.
6377 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006378 SDOperand New = TLI.LowerOperation(Op, DAG);
6379 if (New.Val) {
6380 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006381 break;
6382 }
6383 }
6384
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006385 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6386 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006387 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6388 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6389 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanc7c16572005-04-11 03:01:51 +00006390 SDOperand LL, LH, RL, RH;
6391 ExpandOp(Node->getOperand(0), LL, LH);
6392 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006393 unsigned OuterBitSize = Op.getValueSizeInBits();
6394 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00006395 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6396 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006397 if (DAG.MaskedValueIsZero(Op.getOperand(0),
6398 APInt::getHighBitsSet(OuterBitSize, LHSSB)) &&
6399 DAG.MaskedValueIsZero(Op.getOperand(1),
6400 APInt::getHighBitsSet(OuterBitSize, RHSSB))) {
Dan Gohman525178c2007-10-08 18:33:35 +00006401 // The inputs are both zero-extended.
6402 if (HasUMUL_LOHI) {
6403 // We can emit a umul_lohi.
6404 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6405 Hi = SDOperand(Lo.Val, 1);
6406 break;
6407 }
6408 if (HasMULHU) {
6409 // We can emit a mulhu+mul.
6410 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6411 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6412 break;
6413 }
Dan Gohman525178c2007-10-08 18:33:35 +00006414 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006415 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00006416 // The input values are both sign-extended.
6417 if (HasSMUL_LOHI) {
6418 // We can emit a smul_lohi.
6419 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6420 Hi = SDOperand(Lo.Val, 1);
6421 break;
6422 }
6423 if (HasMULHS) {
6424 // We can emit a mulhs+mul.
6425 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6426 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6427 break;
6428 }
6429 }
6430 if (HasUMUL_LOHI) {
6431 // Lo,Hi = umul LHS, RHS.
6432 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6433 DAG.getVTList(NVT, NVT), LL, RL);
6434 Lo = UMulLOHI;
6435 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006436 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6437 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6438 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6439 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006440 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006441 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006442 if (HasMULHU) {
6443 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6444 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6445 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6446 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6447 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6448 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6449 break;
6450 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006451 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006452
Dan Gohman525178c2007-10-08 18:33:35 +00006453 // If nothing else, we can make a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006454 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6455 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006456 break;
6457 }
Evan Cheng56966222007-01-12 02:11:51 +00006458 case ISD::SDIV:
6459 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6460 break;
6461 case ISD::UDIV:
6462 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6463 break;
6464 case ISD::SREM:
6465 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6466 break;
6467 case ISD::UREM:
6468 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6469 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006470
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006471 case ISD::FADD:
Duncan Sands007f9842008-01-10 10:28:30 +00006472 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6473 RTLIB::ADD_F64,
6474 RTLIB::ADD_F80,
6475 RTLIB::ADD_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006476 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006477 break;
6478 case ISD::FSUB:
Duncan Sands007f9842008-01-10 10:28:30 +00006479 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6480 RTLIB::SUB_F64,
6481 RTLIB::SUB_F80,
6482 RTLIB::SUB_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006483 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006484 break;
6485 case ISD::FMUL:
Duncan Sands007f9842008-01-10 10:28:30 +00006486 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6487 RTLIB::MUL_F64,
6488 RTLIB::MUL_F80,
6489 RTLIB::MUL_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006490 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006491 break;
6492 case ISD::FDIV:
Duncan Sands007f9842008-01-10 10:28:30 +00006493 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6494 RTLIB::DIV_F64,
6495 RTLIB::DIV_F80,
6496 RTLIB::DIV_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006497 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006498 break;
6499 case ISD::FP_EXTEND:
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006500 if (VT == MVT::ppcf128) {
6501 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6502 Node->getOperand(0).getValueType()==MVT::f64);
6503 const uint64_t zero = 0;
6504 if (Node->getOperand(0).getValueType()==MVT::f32)
6505 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6506 else
6507 Hi = Node->getOperand(0);
6508 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6509 break;
6510 }
Evan Cheng56966222007-01-12 02:11:51 +00006511 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006512 break;
6513 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00006514 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006515 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006516 case ISD::FPOWI:
Duncan Sands007f9842008-01-10 10:28:30 +00006517 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6518 RTLIB::POWI_F64,
6519 RTLIB::POWI_F80,
6520 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006521 Node, false, Hi);
6522 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006523 case ISD::FSQRT:
6524 case ISD::FSIN:
6525 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00006526 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006527 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006528 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00006529 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6530 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006531 break;
6532 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00006533 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6534 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006535 break;
6536 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00006537 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6538 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006539 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006540 default: assert(0 && "Unreachable!");
6541 }
Evan Cheng56966222007-01-12 02:11:51 +00006542 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006543 break;
6544 }
Evan Cheng966bf242006-12-16 00:52:40 +00006545 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006546 if (VT == MVT::ppcf128) {
6547 SDOperand Tmp;
6548 ExpandOp(Node->getOperand(0), Lo, Tmp);
6549 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6550 // lo = hi==fabs(hi) ? lo : -lo;
6551 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6552 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6553 DAG.getCondCode(ISD::SETEQ));
6554 break;
6555 }
Evan Cheng966bf242006-12-16 00:52:40 +00006556 SDOperand Mask = (VT == MVT::f64)
6557 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6558 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6559 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6560 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6561 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6562 if (getTypeAction(NVT) == Expand)
6563 ExpandOp(Lo, Lo, Hi);
6564 break;
6565 }
6566 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006567 if (VT == MVT::ppcf128) {
6568 ExpandOp(Node->getOperand(0), Lo, Hi);
6569 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6570 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6571 break;
6572 }
Evan Cheng966bf242006-12-16 00:52:40 +00006573 SDOperand Mask = (VT == MVT::f64)
6574 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6575 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6576 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6577 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6578 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6579 if (getTypeAction(NVT) == Expand)
6580 ExpandOp(Lo, Lo, Hi);
6581 break;
6582 }
Evan Cheng912095b2007-01-04 21:56:39 +00006583 case ISD::FCOPYSIGN: {
6584 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6585 if (getTypeAction(NVT) == Expand)
6586 ExpandOp(Lo, Lo, Hi);
6587 break;
6588 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006589 case ISD::SINT_TO_FP:
6590 case ISD::UINT_TO_FP: {
6591 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6592 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6e63e092007-10-12 17:52:03 +00006593 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006594 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006595 if (isSigned) {
6596 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6597 Node->getOperand(0)));
6598 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6599 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006600 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006601 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6602 Node->getOperand(0)));
6603 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6604 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006605 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006606 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6607 DAG.getConstant(0, MVT::i32),
6608 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6609 DAG.getConstantFP(
6610 APFloat(APInt(128, 2, TwoE32)),
6611 MVT::ppcf128)),
6612 Hi,
6613 DAG.getCondCode(ISD::SETLT)),
6614 Lo, Hi);
6615 }
6616 break;
6617 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00006618 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6619 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006620 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00006621 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6622 Lo, Hi);
6623 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6624 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6625 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6626 DAG.getConstant(0, MVT::i64),
6627 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6628 DAG.getConstantFP(
6629 APFloat(APInt(128, 2, TwoE64)),
6630 MVT::ppcf128)),
6631 Hi,
6632 DAG.getCondCode(ISD::SETLT)),
6633 Lo, Hi);
6634 break;
6635 }
Evan Cheng64f638d2007-09-27 07:35:39 +00006636 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006637 if (Node->getOperand(0).getValueType() == MVT::i64) {
6638 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006639 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen73328d12007-09-19 23:55:34 +00006640 else if (VT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006641 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006642 else if (VT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00006643 assert(isSigned);
Dale Johannesen161e8972007-10-05 20:04:43 +00006644 LC = RTLIB::SINTTOFP_I64_F80;
6645 }
6646 else if (VT == MVT::ppcf128) {
6647 assert(isSigned);
6648 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen73328d12007-09-19 23:55:34 +00006649 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006650 } else {
6651 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006652 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006653 else
Evan Cheng56966222007-01-12 02:11:51 +00006654 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006655 }
6656
6657 // Promote the operand if needed.
6658 if (getTypeAction(SrcVT) == Promote) {
6659 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6660 Tmp = isSigned
6661 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6662 DAG.getValueType(SrcVT))
6663 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6664 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6665 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006666
6667 const char *LibCall = TLI.getLibcallName(LC);
6668 if (LibCall)
6669 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6670 else {
6671 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6672 Node->getOperand(0));
6673 if (getTypeAction(Lo.getValueType()) == Expand)
6674 ExpandOp(Lo, Lo, Hi);
6675 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006676 break;
6677 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006678 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006679
Chris Lattner83397362005-12-21 18:02:52 +00006680 // Make sure the resultant values have been legalized themselves, unless this
6681 // is a type that requires multi-step expansion.
6682 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6683 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006684 if (Hi.Val)
6685 // Don't legalize the high part if it is expanded to a single node.
6686 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006687 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006688
6689 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006690 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00006691 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006692}
6693
Dan Gohman7f321562007-06-25 16:23:39 +00006694/// SplitVectorOp - Given an operand of vector type, break it down into
6695/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00006696void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6697 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00006698 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006699 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006700 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00006701 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00006702
Dan Gohmane40c7b02007-09-24 15:54:53 +00006703 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006704
6705 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6706 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6707
6708 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6709 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6710
Chris Lattnerc7029802006-03-18 01:44:44 +00006711 // See if we already split it.
6712 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6713 = SplitNodes.find(Op);
6714 if (I != SplitNodes.end()) {
6715 Lo = I->second.first;
6716 Hi = I->second.second;
6717 return;
6718 }
6719
6720 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006721 default:
6722#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006723 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006724#endif
6725 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00006726 case ISD::UNDEF:
6727 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6728 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6729 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006730 case ISD::BUILD_PAIR:
6731 Lo = Node->getOperand(0);
6732 Hi = Node->getOperand(1);
6733 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006734 case ISD::INSERT_VECTOR_ELT: {
6735 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6736 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6737 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006738 if (Index < NewNumElts_Lo)
6739 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohman9fe46622007-09-28 23:53:40 +00006740 DAG.getConstant(Index, TLI.getPointerTy()));
6741 else
Nate Begeman5db1afb2007-11-15 21:15:26 +00006742 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6743 DAG.getConstant(Index - NewNumElts_Lo,
6744 TLI.getPointerTy()));
Dan Gohman9fe46622007-09-28 23:53:40 +00006745 break;
6746 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00006747 case ISD::VECTOR_SHUFFLE: {
6748 // Build the low part.
6749 SDOperand Mask = Node->getOperand(2);
6750 SmallVector<SDOperand, 8> Ops;
6751 MVT::ValueType PtrVT = TLI.getPointerTy();
6752
6753 // Insert all of the elements from the input that are needed. We use
6754 // buildvector of extractelement here because the input vectors will have
6755 // to be legalized, so this makes the code simpler.
6756 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6757 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6758 SDOperand InVec = Node->getOperand(0);
6759 if (Idx >= NumElements) {
6760 InVec = Node->getOperand(1);
6761 Idx -= NumElements;
6762 }
6763 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6764 DAG.getConstant(Idx, PtrVT)));
6765 }
6766 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6767 Ops.clear();
6768
6769 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6770 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6771 SDOperand InVec = Node->getOperand(0);
6772 if (Idx >= NumElements) {
6773 InVec = Node->getOperand(1);
6774 Idx -= NumElements;
6775 }
6776 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6777 DAG.getConstant(Idx, PtrVT)));
6778 }
6779 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6780 break;
6781 }
Dan Gohman7f321562007-06-25 16:23:39 +00006782 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006783 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00006784 Node->op_begin()+NewNumElts_Lo);
6785 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006786
Nate Begeman5db1afb2007-11-15 21:15:26 +00006787 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00006788 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006789 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006790 break;
6791 }
Dan Gohman7f321562007-06-25 16:23:39 +00006792 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00006793 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00006794 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6795 if (NewNumSubvectors == 1) {
6796 Lo = Node->getOperand(0);
6797 Hi = Node->getOperand(1);
6798 } else {
6799 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6800 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006801 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006802
Dan Gohman7f321562007-06-25 16:23:39 +00006803 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6804 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006805 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00006806 }
Dan Gohman65956352007-06-13 15:12:02 +00006807 break;
6808 }
Dan Gohmanc6230962007-10-17 14:48:28 +00006809 case ISD::SELECT: {
6810 SDOperand Cond = Node->getOperand(0);
6811
6812 SDOperand LL, LH, RL, RH;
6813 SplitVectorOp(Node->getOperand(1), LL, LH);
6814 SplitVectorOp(Node->getOperand(2), RL, RH);
6815
6816 if (MVT::isVector(Cond.getValueType())) {
6817 // Handle a vector merge.
6818 SDOperand CL, CH;
6819 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006820 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6821 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006822 } else {
6823 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00006824 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6825 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006826 }
6827 break;
6828 }
Dan Gohman7f321562007-06-25 16:23:39 +00006829 case ISD::ADD:
6830 case ISD::SUB:
6831 case ISD::MUL:
6832 case ISD::FADD:
6833 case ISD::FSUB:
6834 case ISD::FMUL:
6835 case ISD::SDIV:
6836 case ISD::UDIV:
6837 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006838 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006839 case ISD::AND:
6840 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00006841 case ISD::XOR:
6842 case ISD::UREM:
6843 case ISD::SREM:
6844 case ISD::FREM: {
Chris Lattnerc7029802006-03-18 01:44:44 +00006845 SDOperand LL, LH, RL, RH;
6846 SplitVectorOp(Node->getOperand(0), LL, LH);
6847 SplitVectorOp(Node->getOperand(1), RL, RH);
6848
Nate Begeman5db1afb2007-11-15 21:15:26 +00006849 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6850 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006851 break;
6852 }
Dan Gohman82669522007-10-11 23:57:53 +00006853 case ISD::FPOWI: {
6854 SDOperand L, H;
6855 SplitVectorOp(Node->getOperand(0), L, H);
6856
Nate Begeman5db1afb2007-11-15 21:15:26 +00006857 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6858 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00006859 break;
6860 }
6861 case ISD::CTTZ:
6862 case ISD::CTLZ:
6863 case ISD::CTPOP:
6864 case ISD::FNEG:
6865 case ISD::FABS:
6866 case ISD::FSQRT:
6867 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00006868 case ISD::FCOS:
6869 case ISD::FP_TO_SINT:
6870 case ISD::FP_TO_UINT:
6871 case ISD::SINT_TO_FP:
6872 case ISD::UINT_TO_FP: {
Dan Gohman82669522007-10-11 23:57:53 +00006873 SDOperand L, H;
6874 SplitVectorOp(Node->getOperand(0), L, H);
6875
Nate Begeman5db1afb2007-11-15 21:15:26 +00006876 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6877 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00006878 break;
6879 }
Dan Gohman7f321562007-06-25 16:23:39 +00006880 case ISD::LOAD: {
6881 LoadSDNode *LD = cast<LoadSDNode>(Node);
6882 SDOperand Ch = LD->getChain();
6883 SDOperand Ptr = LD->getBasePtr();
6884 const Value *SV = LD->getSrcValue();
6885 int SVOffset = LD->getSrcValueOffset();
6886 unsigned Alignment = LD->getAlignment();
6887 bool isVolatile = LD->isVolatile();
6888
Nate Begeman5db1afb2007-11-15 21:15:26 +00006889 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6890 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00006891 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006892 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006893 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006894 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006895 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00006896
6897 // Build a factor node to remember that this load is independent of the
6898 // other one.
6899 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6900 Hi.getValue(1));
6901
6902 // Remember that we legalized the chain.
6903 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00006904 break;
6905 }
Dan Gohman7f321562007-06-25 16:23:39 +00006906 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00006907 // We know the result is a vector. The input may be either a vector or a
6908 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00006909 SDOperand InOp = Node->getOperand(0);
6910 if (!MVT::isVector(InOp.getValueType()) ||
6911 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6912 // The input is a scalar or single-element vector.
6913 // Lower to a store/load so that it can be split.
6914 // FIXME: this could be improved probably.
Chris Lattner85dd3be2007-10-15 17:48:57 +00006915 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00006916 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val);
Chris Lattner7692eb42006-03-23 21:16:34 +00006917
Evan Cheng786225a2006-10-05 23:01:46 +00006918 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00006919 InOp, Ptr,
Dan Gohman3069b872008-02-07 18:41:25 +00006920 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00006921 FI->getIndex());
6922 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman3069b872008-02-07 18:41:25 +00006923 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00006924 FI->getIndex());
Chris Lattner7692eb42006-03-23 21:16:34 +00006925 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00006926 // Split the vector and convert each of the pieces now.
6927 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006928 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6929 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00006930 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00006931 }
Chris Lattnerc7029802006-03-18 01:44:44 +00006932 }
6933
6934 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006935 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00006936 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00006937 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006938}
6939
6940
Dan Gohman89b20c02007-06-27 14:06:22 +00006941/// ScalarizeVectorOp - Given an operand of single-element vector type
6942/// (e.g. v1f32), convert it into the equivalent operation that returns a
6943/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00006944SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6945 assert(MVT::isVector(Op.getValueType()) &&
6946 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006947 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00006948 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6949 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00006950
Dan Gohman7f321562007-06-25 16:23:39 +00006951 // See if we already scalarized it.
6952 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6953 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00006954
6955 SDOperand Result;
6956 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00006957 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006958#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006959 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006960#endif
Dan Gohman7f321562007-06-25 16:23:39 +00006961 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6962 case ISD::ADD:
6963 case ISD::FADD:
6964 case ISD::SUB:
6965 case ISD::FSUB:
6966 case ISD::MUL:
6967 case ISD::FMUL:
6968 case ISD::SDIV:
6969 case ISD::UDIV:
6970 case ISD::FDIV:
6971 case ISD::SREM:
6972 case ISD::UREM:
6973 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00006974 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006975 case ISD::AND:
6976 case ISD::OR:
6977 case ISD::XOR:
6978 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00006979 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00006980 ScalarizeVectorOp(Node->getOperand(0)),
6981 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00006982 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006983 case ISD::FNEG:
6984 case ISD::FABS:
6985 case ISD::FSQRT:
6986 case ISD::FSIN:
6987 case ISD::FCOS:
6988 Result = DAG.getNode(Node->getOpcode(),
6989 NewVT,
6990 ScalarizeVectorOp(Node->getOperand(0)));
6991 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00006992 case ISD::FPOWI:
6993 Result = DAG.getNode(Node->getOpcode(),
6994 NewVT,
6995 ScalarizeVectorOp(Node->getOperand(0)),
6996 Node->getOperand(1));
6997 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006998 case ISD::LOAD: {
6999 LoadSDNode *LD = cast<LoadSDNode>(Node);
7000 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7001 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00007002
Dan Gohman7f321562007-06-25 16:23:39 +00007003 const Value *SV = LD->getSrcValue();
7004 int SVOffset = LD->getSrcValueOffset();
7005 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
7006 LD->isVolatile(), LD->getAlignment());
7007
Chris Lattnerc7029802006-03-18 01:44:44 +00007008 // Remember that we legalized the chain.
7009 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7010 break;
7011 }
Dan Gohman7f321562007-06-25 16:23:39 +00007012 case ISD::BUILD_VECTOR:
7013 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007014 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007015 case ISD::INSERT_VECTOR_ELT:
7016 // Returning the inserted scalar element.
7017 Result = Node->getOperand(1);
7018 break;
7019 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007020 assert(Node->getOperand(0).getValueType() == NewVT &&
7021 "Concat of non-legal vectors not yet supported!");
7022 Result = Node->getOperand(0);
7023 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007024 case ISD::VECTOR_SHUFFLE: {
7025 // Figure out if the scalar is the LHS or RHS and return it.
7026 SDOperand EltNum = Node->getOperand(2).getOperand(0);
7027 if (cast<ConstantSDNode>(EltNum)->getValue())
7028 Result = ScalarizeVectorOp(Node->getOperand(1));
7029 else
7030 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007031 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007032 }
7033 case ISD::EXTRACT_SUBVECTOR:
7034 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00007035 assert(Result.getValueType() == NewVT);
7036 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007037 case ISD::BIT_CONVERT:
7038 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00007039 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007040 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007041 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007042 ScalarizeVectorOp(Op.getOperand(1)),
7043 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007044 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00007045 }
7046
7047 if (TLI.isTypeLegal(NewVT))
7048 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007049 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7050 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00007051 return Result;
7052}
7053
Chris Lattner3e928bb2005-01-07 07:47:09 +00007054
7055// SelectionDAG::Legalize - This is the entry point for the file.
7056//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00007057void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00007058 if (ViewLegalizeDAGs) viewGraph();
7059
Chris Lattner3e928bb2005-01-07 07:47:09 +00007060 /// run - This is the main entry point to this class.
7061 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00007062 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00007063}
7064