blob: 4686753a3ae34f0c23aa550d0f12222c17a3e450 [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
489 // double.
490 MVT::ValueType VT = CFP->getValueType(0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +0000491 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000492 CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000493 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000494 if (VT!=MVT::f64 && VT!=MVT::f32)
495 assert(0 && "Invalid type expansion");
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000496 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
Evan Chengef120572008-03-04 08:05:30 +0000497 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000498 }
499
Evan Chengef120572008-03-04 08:05:30 +0000500 MVT::ValueType OrigVT = VT;
501 MVT::ValueType SVT = VT;
502 while (SVT != MVT::f32) {
503 SVT = (unsigned)SVT - 1;
504 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
505 // Only do this if the target has a native EXTLOAD instruction from
506 // smaller type.
507 TLI.isLoadXLegal(ISD::EXTLOAD, SVT)) {
508 const Type *SType = MVT::getTypeForValueType(SVT);
509 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
510 VT = SVT;
511 Extend = true;
512 }
Evan Cheng00495212006-12-12 21:32:44 +0000513 }
514
515 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Chengef120572008-03-04 08:05:30 +0000516 if (Extend)
517 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000518 CPIdx, PseudoSourceValue::getConstantPool(),
Evan Chengef120572008-03-04 08:05:30 +0000519 0, VT);
520 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
521 PseudoSourceValue::getConstantPool(), 0);
Evan Cheng00495212006-12-12 21:32:44 +0000522}
523
Chris Lattner6831a812006-02-13 09:18:02 +0000524
Evan Cheng912095b2007-01-04 21:56:39 +0000525/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
526/// operations.
527static
528SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
529 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000530 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000531 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000532 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
533 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000534 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000535
Evan Cheng912095b2007-01-04 21:56:39 +0000536 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000537 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000538 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
539 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000540 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000541 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000542 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000543 // Shift right or sign-extend it if the two operands have different types.
544 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
545 if (SizeDiff > 0) {
546 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
547 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
548 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
549 } else if (SizeDiff < 0)
550 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000551
552 // Clear the sign bit of first operand.
553 SDOperand Mask2 = (VT == MVT::f64)
554 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
555 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
556 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000557 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000558 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
559
560 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000561 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
562 return Result;
563}
564
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000565/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
566static
567SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
568 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000569 SDOperand Chain = ST->getChain();
570 SDOperand Ptr = ST->getBasePtr();
571 SDOperand Val = ST->getValue();
572 MVT::ValueType VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000573 int Alignment = ST->getAlignment();
574 int SVOffset = ST->getSrcValueOffset();
Dale Johannesen8155d642008-02-27 22:36:00 +0000575 if (MVT::isFloatingPoint(ST->getMemoryVT()) ||
576 MVT::isVector(ST->getMemoryVT())) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000577 // Expand to a bitconvert of the value to the integer type of the
578 // same size, then a (misaligned) int store.
579 MVT::ValueType intVT;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000580 if (MVT::is128BitVector(VT) || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000581 intVT = MVT::i128;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000582 else if (MVT::is64BitVector(VT) || VT==MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000583 intVT = MVT::i64;
584 else if (VT==MVT::f32)
585 intVT = MVT::i32;
586 else
Dale Johannesencd9f1742008-02-28 18:36:51 +0000587 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000588
589 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
590 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
591 SVOffset, ST->isVolatile(), Alignment);
592 }
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000593 assert(MVT::isInteger(ST->getMemoryVT()) &&
Dale Johannesen8155d642008-02-27 22:36:00 +0000594 !MVT::isVector(ST->getMemoryVT()) &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000595 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000596 // Get the half-size VT
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000597 MVT::ValueType NewStoredVT = ST->getMemoryVT() - 1;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000598 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000599 int IncrementSize = NumBits / 8;
600
601 // Divide the stored value in two parts.
602 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
603 SDOperand Lo = Val;
604 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
605
606 // Store the two parts
607 SDOperand Store1, Store2;
608 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
609 ST->getSrcValue(), SVOffset, NewStoredVT,
610 ST->isVolatile(), Alignment);
611 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
612 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000613 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000614 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
615 ST->getSrcValue(), SVOffset + IncrementSize,
616 NewStoredVT, ST->isVolatile(), Alignment);
617
618 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
619}
620
621/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
622static
623SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
624 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000625 int SVOffset = LD->getSrcValueOffset();
626 SDOperand Chain = LD->getChain();
627 SDOperand Ptr = LD->getBasePtr();
628 MVT::ValueType VT = LD->getValueType(0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000629 MVT::ValueType LoadedVT = LD->getMemoryVT();
Dale Johannesen8155d642008-02-27 22:36:00 +0000630 if (MVT::isFloatingPoint(VT) || MVT::isVector(VT)) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000631 // Expand to a (misaligned) integer load of the same size,
Dale Johannesen8155d642008-02-27 22:36:00 +0000632 // then bitconvert to floating point or vector.
Dale Johannesen907f28c2007-09-08 19:29:23 +0000633 MVT::ValueType intVT;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000634 if (MVT::is128BitVector(LoadedVT) ||
635 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000636 intVT = MVT::i128;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000637 else if (MVT::is64BitVector(LoadedVT) || LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000638 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000639 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000640 intVT = MVT::i32;
641 else
Dale Johannesen8155d642008-02-27 22:36:00 +0000642 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000643
644 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
645 SVOffset, LD->isVolatile(),
646 LD->getAlignment());
647 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Dale Johannesen8155d642008-02-27 22:36:00 +0000648 if (MVT::isFloatingPoint(VT) && LoadedVT != VT)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000649 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
650
651 SDOperand Ops[] = { Result, Chain };
652 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
653 Ops, 2);
654 }
Dale Johannesen8155d642008-02-27 22:36:00 +0000655 assert(MVT::isInteger(LoadedVT) && !MVT::isVector(LoadedVT) &&
Chris Lattnere400af82007-11-19 21:38:03 +0000656 "Unaligned load of unsupported type.");
657
Dale Johannesen8155d642008-02-27 22:36:00 +0000658 // Compute the new VT that is half the size of the old one. This is an
659 // integer MVT.
Chris Lattnere400af82007-11-19 21:38:03 +0000660 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
661 MVT::ValueType NewLoadedVT;
Dale Johannesen8155d642008-02-27 22:36:00 +0000662 NewLoadedVT = MVT::getIntegerType(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000663 NumBits >>= 1;
664
665 unsigned Alignment = LD->getAlignment();
666 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000667 ISD::LoadExtType HiExtType = LD->getExtensionType();
668
669 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
670 if (HiExtType == ISD::NON_EXTLOAD)
671 HiExtType = ISD::ZEXTLOAD;
672
673 // Load the value in two parts
674 SDOperand Lo, Hi;
675 if (TLI.isLittleEndian()) {
676 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
677 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
678 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
679 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
680 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
681 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000682 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000683 } else {
684 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
685 NewLoadedVT,LD->isVolatile(), Alignment);
686 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
687 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
688 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
689 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000690 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000691 }
692
693 // aggregate the two parts
694 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
695 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
696 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
697
698 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
699 Hi.getValue(1));
700
701 SDOperand Ops[] = { Result, TF };
702 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
703}
Evan Cheng912095b2007-01-04 21:56:39 +0000704
Dan Gohman82669522007-10-11 23:57:53 +0000705/// UnrollVectorOp - We know that the given vector has a legal type, however
706/// the operation it performs is not legal and is an operation that we have
707/// no way of lowering. "Unroll" the vector, splitting out the scalars and
708/// operating on each element individually.
709SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
710 MVT::ValueType VT = Op.getValueType();
711 assert(isTypeLegal(VT) &&
712 "Caller should expand or promote operands that are not legal!");
713 assert(Op.Val->getNumValues() == 1 &&
714 "Can't unroll a vector with multiple results!");
715 unsigned NE = MVT::getVectorNumElements(VT);
716 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
717
718 SmallVector<SDOperand, 8> Scalars;
719 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
720 for (unsigned i = 0; i != NE; ++i) {
721 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
722 SDOperand Operand = Op.getOperand(j);
723 MVT::ValueType OperandVT = Operand.getValueType();
724 if (MVT::isVector(OperandVT)) {
725 // A vector operand; extract a single element.
726 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
727 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
728 OperandEltVT,
729 Operand,
730 DAG.getConstant(i, MVT::i32));
731 } else {
732 // A scalar operand; just use it as is.
733 Operands[j] = Operand;
734 }
735 }
736 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
737 &Operands[0], Operands.size()));
738 }
739
740 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
741}
742
Duncan Sands007f9842008-01-10 10:28:30 +0000743/// GetFPLibCall - Return the right libcall for the given floating point type.
744static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
745 RTLIB::Libcall Call_F32,
746 RTLIB::Libcall Call_F64,
747 RTLIB::Libcall Call_F80,
748 RTLIB::Libcall Call_PPCF128) {
749 return
750 VT == MVT::f32 ? Call_F32 :
751 VT == MVT::f64 ? Call_F64 :
752 VT == MVT::f80 ? Call_F80 :
753 VT == MVT::ppcf128 ? Call_PPCF128 :
754 RTLIB::UNKNOWN_LIBCALL;
755}
756
Dan Gohmana3466152007-07-13 20:14:11 +0000757/// LegalizeOp - We know that the specified value has a legal type, and
758/// that its operands are legal. Now ensure that the operation itself
759/// is legal, recursively ensuring that the operands' operations remain
760/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000761SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000762 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
763 return Op;
764
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000765 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000766 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000767 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000768
Chris Lattner3e928bb2005-01-07 07:47:09 +0000769 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000770 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000771 if (Node->getNumValues() > 1) {
772 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000773 if (getTypeAction(Node->getValueType(i)) != Legal) {
774 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000775 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000776 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000777 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000778 }
779 }
780
Chris Lattner45982da2005-05-12 16:53:42 +0000781 // Note that LegalizeOp may be reentered even from single-use nodes, which
782 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000783 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000784 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000785
Nate Begeman9373a812005-08-10 20:51:12 +0000786 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000787 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000788 bool isCustom = false;
789
Chris Lattner3e928bb2005-01-07 07:47:09 +0000790 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000791 case ISD::FrameIndex:
792 case ISD::EntryToken:
793 case ISD::Register:
794 case ISD::BasicBlock:
795 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000796 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000797 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000798 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000799 case ISD::TargetConstantPool:
800 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000801 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000802 case ISD::TargetExternalSymbol:
803 case ISD::VALUETYPE:
804 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +0000805 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +0000806 case ISD::STRING:
807 case ISD::CONDCODE:
808 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000809 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000810 "This must be legal!");
811 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000812 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000813 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
814 // If this is a target node, legalize it by legalizing the operands then
815 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000816 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000817 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000818 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000819
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000820 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000821
822 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
823 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
824 return Result.getValue(Op.ResNo);
825 }
826 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000827#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000828 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000829#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000830 assert(0 && "Do not know how to legalize this operator!");
831 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000832 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000833 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000834 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000835 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000836 case ISD::ConstantPool:
837 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000838 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
839 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000840 case TargetLowering::Custom:
841 Tmp1 = TLI.LowerOperation(Op, DAG);
842 if (Tmp1.Val) Result = Tmp1;
843 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000844 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000845 break;
846 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000847 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000848 case ISD::FRAMEADDR:
849 case ISD::RETURNADDR:
850 // The only option for these nodes is to custom lower them. If the target
851 // does not custom lower them, then return zero.
852 Tmp1 = TLI.LowerOperation(Op, DAG);
853 if (Tmp1.Val)
854 Result = Tmp1;
855 else
856 Result = DAG.getConstant(0, TLI.getPointerTy());
857 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000858 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000859 MVT::ValueType VT = Node->getValueType(0);
860 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
861 default: assert(0 && "This action is not supported yet!");
862 case TargetLowering::Custom:
863 Result = TLI.LowerOperation(Op, DAG);
864 if (Result.Val) break;
865 // Fall Thru
866 case TargetLowering::Legal:
867 Result = DAG.getConstant(0, VT);
868 break;
869 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000870 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000871 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000872 case ISD::EXCEPTIONADDR: {
873 Tmp1 = LegalizeOp(Node->getOperand(0));
874 MVT::ValueType VT = Node->getValueType(0);
875 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
876 default: assert(0 && "This action is not supported yet!");
877 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000878 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000879 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000880 }
881 break;
882 case TargetLowering::Custom:
883 Result = TLI.LowerOperation(Op, DAG);
884 if (Result.Val) break;
885 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000886 case TargetLowering::Legal: {
887 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
888 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000889 Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000890 break;
891 }
892 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000893 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000894 if (Result.Val->getNumValues() == 1) break;
895
896 assert(Result.Val->getNumValues() == 2 &&
897 "Cannot return more than two values!");
898
899 // Since we produced two values, make sure to remember that we
900 // legalized both of them.
901 Tmp1 = LegalizeOp(Result);
902 Tmp2 = LegalizeOp(Result.getValue(1));
903 AddLegalizedOperand(Op.getValue(0), Tmp1);
904 AddLegalizedOperand(Op.getValue(1), Tmp2);
905 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000906 case ISD::EHSELECTION: {
907 Tmp1 = LegalizeOp(Node->getOperand(0));
908 Tmp2 = LegalizeOp(Node->getOperand(1));
909 MVT::ValueType VT = Node->getValueType(0);
910 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
911 default: assert(0 && "This action is not supported yet!");
912 case TargetLowering::Expand: {
913 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000914 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000915 }
916 break;
917 case TargetLowering::Custom:
918 Result = TLI.LowerOperation(Op, DAG);
919 if (Result.Val) break;
920 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000921 case TargetLowering::Legal: {
922 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
923 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000924 Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +0000925 break;
926 }
927 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000928 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000929 if (Result.Val->getNumValues() == 1) break;
930
931 assert(Result.Val->getNumValues() == 2 &&
932 "Cannot return more than two values!");
933
934 // Since we produced two values, make sure to remember that we
935 // legalized both of them.
936 Tmp1 = LegalizeOp(Result);
937 Tmp2 = LegalizeOp(Result.getValue(1));
938 AddLegalizedOperand(Op.getValue(0), Tmp1);
939 AddLegalizedOperand(Op.getValue(1), Tmp2);
940 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000941 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000942 MVT::ValueType VT = Node->getValueType(0);
943 // The only "good" option for this node is to custom lower it.
944 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
945 default: assert(0 && "This action is not supported at all!");
946 case TargetLowering::Custom:
947 Result = TLI.LowerOperation(Op, DAG);
948 if (Result.Val) break;
949 // Fall Thru
950 case TargetLowering::Legal:
951 // Target does not know, how to lower this, lower to noop
952 Result = LegalizeOp(Node->getOperand(0));
953 break;
954 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000955 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000956 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000957 case ISD::AssertSext:
958 case ISD::AssertZext:
959 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000960 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000961 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000962 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000963 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000964 Result = Node->getOperand(Op.ResNo);
965 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000966 case ISD::CopyFromReg:
967 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000968 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000969 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000970 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000971 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000972 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000973 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000974 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000975 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
976 } else {
977 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
978 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000979 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
980 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000981 // Since CopyFromReg produces two values, make sure to remember that we
982 // legalized both of them.
983 AddLegalizedOperand(Op.getValue(0), Result);
984 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
985 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000986 case ISD::UNDEF: {
987 MVT::ValueType VT = Op.getValueType();
988 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000989 default: assert(0 && "This action is not supported yet!");
990 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000991 if (MVT::isInteger(VT))
992 Result = DAG.getConstant(0, VT);
993 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000994 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
995 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000996 else
997 assert(0 && "Unknown value type!");
998 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000999 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001000 break;
1001 }
1002 break;
1003 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001004
Chris Lattner48b61a72006-03-28 00:40:33 +00001005 case ISD::INTRINSIC_W_CHAIN:
1006 case ISD::INTRINSIC_WO_CHAIN:
1007 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001008 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001009 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1010 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001011 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001012
1013 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001014 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001015 TargetLowering::Custom) {
1016 Tmp3 = TLI.LowerOperation(Result, DAG);
1017 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001018 }
1019
1020 if (Result.Val->getNumValues() == 1) break;
1021
1022 // Must have return value and chain result.
1023 assert(Result.Val->getNumValues() == 2 &&
1024 "Cannot return more than two values!");
1025
1026 // Since loads produce two values, make sure to remember that we
1027 // legalized both of them.
1028 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1029 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1030 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001031 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001032
1033 case ISD::LOCATION:
1034 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1035 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1036
1037 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1038 case TargetLowering::Promote:
1039 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001040 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001041 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001042 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +00001043 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001044
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001045 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001046 const std::string &FName =
1047 cast<StringSDNode>(Node->getOperand(3))->getValue();
1048 const std::string &DirName =
1049 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001050 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001051
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001052 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +00001053 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +00001054 SDOperand LineOp = Node->getOperand(1);
1055 SDOperand ColOp = Node->getOperand(2);
1056
1057 if (useDEBUG_LOC) {
1058 Ops.push_back(LineOp); // line #
1059 Ops.push_back(ColOp); // col #
1060 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001061 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001062 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +00001063 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1064 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Evan Chenga647c922008-02-01 02:05:57 +00001065 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001066 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Evan Chengbb81d972008-01-31 09:59:15 +00001067 Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label
1068 Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001069 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001070 } else {
1071 Result = Tmp1; // chain
1072 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001073 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001074 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001075 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +00001076 if (Tmp1 != Node->getOperand(0) ||
1077 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001078 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001079 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +00001080 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1081 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1082 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1083 } else {
1084 // Otherwise promote them.
1085 Ops.push_back(PromoteOp(Node->getOperand(1)));
1086 Ops.push_back(PromoteOp(Node->getOperand(2)));
1087 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001088 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1089 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001090 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001091 }
1092 break;
1093 }
1094 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001095
1096 case ISD::DECLARE:
1097 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1098 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1099 default: assert(0 && "This action is not supported yet!");
1100 case TargetLowering::Legal:
1101 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1102 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1103 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1104 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1105 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001106 case TargetLowering::Expand:
1107 Result = LegalizeOp(Node->getOperand(0));
1108 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001109 }
1110 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001111
1112 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001113 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001114 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001115 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001116 case TargetLowering::Legal:
1117 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1118 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1119 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1120 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001121 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001122 break;
1123 }
1124 break;
1125
Jim Laskey1ee29252007-01-26 14:34:52 +00001126 case ISD::LABEL:
Evan Chengbb81d972008-01-31 09:59:15 +00001127 assert(Node->getNumOperands() == 3 && "Invalid LABEL node!");
Jim Laskey1ee29252007-01-26 14:34:52 +00001128 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001129 default: assert(0 && "This action is not supported yet!");
1130 case TargetLowering::Legal:
1131 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1132 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Evan Chengbb81d972008-01-31 09:59:15 +00001133 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand.
1134 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001135 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001136 case TargetLowering::Expand:
1137 Result = LegalizeOp(Node->getOperand(0));
1138 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001139 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001140 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001141
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001142 case ISD::MEMBARRIER: {
1143 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001144 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1145 default: assert(0 && "This action is not supported yet!");
1146 case TargetLowering::Legal: {
1147 SDOperand Ops[6];
1148 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001149 for (int x = 1; x < 6; ++x) {
1150 Ops[x] = Node->getOperand(x);
1151 if (!isTypeLegal(Ops[x].getValueType()))
1152 Ops[x] = PromoteOp(Ops[x]);
1153 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001154 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1155 break;
1156 }
1157 case TargetLowering::Expand:
1158 //There is no libgcc call for this op
1159 Result = Node->getOperand(0); // Noop
1160 break;
1161 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001162 break;
1163 }
1164
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001165 case ISD::ATOMIC_LCS:
1166 case ISD::ATOMIC_LAS:
1167 case ISD::ATOMIC_SWAP: {
1168 assert(((Node->getNumOperands() == 4 && Node->getOpcode() == ISD::ATOMIC_LCS) ||
1169 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_LAS) ||
1170 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_SWAP)) &&
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001171 "Invalid Atomic node!");
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001172 int num = Node->getOpcode() == ISD::ATOMIC_LCS ? 4 : 3;
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001173 SDOperand Ops[4];
1174 for (int x = 0; x < num; ++x)
1175 Ops[x] = LegalizeOp(Node->getOperand(x));
1176 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num);
1177
1178 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001179 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001180 case TargetLowering::Custom:
1181 Result = TLI.LowerOperation(Result, DAG);
1182 break;
1183 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001184 break;
1185 }
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001186 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1187 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1188 return Result.getValue(Op.ResNo);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001189 }
1190
Scott Michelc1513d22007-08-08 23:23:31 +00001191 case ISD::Constant: {
1192 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1193 unsigned opAction =
1194 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1195
Chris Lattner3e928bb2005-01-07 07:47:09 +00001196 // We know we don't need to expand constants here, constants only have one
1197 // value and we check that it is fine above.
1198
Scott Michelc1513d22007-08-08 23:23:31 +00001199 if (opAction == TargetLowering::Custom) {
1200 Tmp1 = TLI.LowerOperation(Result, DAG);
1201 if (Tmp1.Val)
1202 Result = Tmp1;
1203 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001204 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001205 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001206 case ISD::ConstantFP: {
1207 // Spill FP immediates to the constant pool if the target cannot directly
1208 // codegen them. Targets often have some immediate values that can be
1209 // efficiently generated into an FP register without a load. We explicitly
1210 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001211 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1212
Chris Lattner3181a772006-01-29 06:26:56 +00001213 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1214 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001215 case TargetLowering::Legal:
1216 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001217 case TargetLowering::Custom:
1218 Tmp3 = TLI.LowerOperation(Result, DAG);
1219 if (Tmp3.Val) {
1220 Result = Tmp3;
1221 break;
1222 }
1223 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001224 case TargetLowering::Expand: {
1225 // Check to see if this FP immediate is already legal.
1226 bool isLegal = false;
1227 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1228 E = TLI.legal_fpimm_end(); I != E; ++I) {
1229 if (CFP->isExactlyValue(*I)) {
1230 isLegal = true;
1231 break;
1232 }
1233 }
1234 // If this is a legal constant, turn it into a TargetConstantFP node.
1235 if (isLegal)
1236 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001237 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001238 }
Nate Begemane1795842008-02-14 08:57:00 +00001239 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001240 break;
1241 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001242 case ISD::TokenFactor:
1243 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001244 Tmp1 = LegalizeOp(Node->getOperand(0));
1245 Tmp2 = LegalizeOp(Node->getOperand(1));
1246 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1247 } else if (Node->getNumOperands() == 3) {
1248 Tmp1 = LegalizeOp(Node->getOperand(0));
1249 Tmp2 = LegalizeOp(Node->getOperand(1));
1250 Tmp3 = LegalizeOp(Node->getOperand(2));
1251 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001252 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001253 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001254 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001255 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1256 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001257 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001258 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001259 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001260
1261 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001262 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001263 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001264 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1265 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001266
1267 // The number of incoming and outgoing values should match; unless the final
1268 // outgoing value is a flag.
1269 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1270 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1271 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1272 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001273 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001274
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001275 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001276 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001277 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001278 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1279 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001280 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001281 if (Op.ResNo == i)
1282 Tmp2 = Tmp1;
1283 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1284 }
1285 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001286 case ISD::EXTRACT_SUBREG: {
1287 Tmp1 = LegalizeOp(Node->getOperand(0));
1288 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1289 assert(idx && "Operand must be a constant");
1290 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1291 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1292 }
1293 break;
1294 case ISD::INSERT_SUBREG: {
1295 Tmp1 = LegalizeOp(Node->getOperand(0));
1296 Tmp2 = LegalizeOp(Node->getOperand(1));
1297 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1298 assert(idx && "Operand must be a constant");
1299 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1300 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1301 }
1302 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001303 case ISD::BUILD_VECTOR:
1304 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001305 default: assert(0 && "This action is not supported yet!");
1306 case TargetLowering::Custom:
1307 Tmp3 = TLI.LowerOperation(Result, DAG);
1308 if (Tmp3.Val) {
1309 Result = Tmp3;
1310 break;
1311 }
1312 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001313 case TargetLowering::Expand:
1314 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001315 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001316 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001317 break;
1318 case ISD::INSERT_VECTOR_ELT:
1319 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001320 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001321
1322 // The type of the value to insert may not be legal, even though the vector
1323 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1324 // here.
1325 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1326 default: assert(0 && "Cannot expand insert element operand");
1327 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1328 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1329 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001330 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1331
1332 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1333 Node->getValueType(0))) {
1334 default: assert(0 && "This action is not supported yet!");
1335 case TargetLowering::Legal:
1336 break;
1337 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001338 Tmp4 = TLI.LowerOperation(Result, DAG);
1339 if (Tmp4.Val) {
1340 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001341 break;
1342 }
1343 // FALLTHROUGH
1344 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001345 // If the insert index is a constant, codegen this as a scalar_to_vector,
1346 // then a shuffle that inserts it into the right position in the vector.
1347 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001348 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1349 // match the element type of the vector being created.
1350 if (Tmp2.getValueType() ==
1351 MVT::getVectorElementType(Op.getValueType())) {
1352 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1353 Tmp1.getValueType(), Tmp2);
1354
1355 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1356 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1357 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1358
1359 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1360 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1361 // elt 0 of the RHS.
1362 SmallVector<SDOperand, 8> ShufOps;
1363 for (unsigned i = 0; i != NumElts; ++i) {
1364 if (i != InsertPos->getValue())
1365 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1366 else
1367 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1368 }
1369 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1370 &ShufOps[0], ShufOps.size());
1371
1372 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1373 Tmp1, ScVec, ShufMask);
1374 Result = LegalizeOp(Result);
1375 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001376 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001377 }
1378
Chris Lattner2332b9f2006-03-19 01:17:20 +00001379 // If the target doesn't support this, we have to spill the input vector
1380 // to a temporary stack slot, update the element, then reload it. This is
1381 // badness. We could also load the value into a vector register (either
1382 // with a "move to register" or "extload into register" instruction, then
1383 // permute it into place, if the idx is a constant and if the idx is
1384 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001385 MVT::ValueType VT = Tmp1.getValueType();
Nate Begeman0325d902008-02-13 06:43:04 +00001386 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Chengf6bf87f2006-04-08 01:46:37 +00001387 MVT::ValueType IdxVT = Tmp3.getValueType();
1388 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner85dd3be2007-10-15 17:48:57 +00001389 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohman69de1932008-02-06 22:27:42 +00001390
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00001391 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val);
Dan Gohman69de1932008-02-06 22:27:42 +00001392 int SPFI = StackPtrFI->getIndex();
1393
Evan Chengeb0b4612006-03-31 01:27:51 +00001394 // Store the vector.
Dan Gohman69de1932008-02-06 22:27:42 +00001395 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00001396 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00001397 SPFI);
Evan Chengeb0b4612006-03-31 01:27:51 +00001398
1399 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001400 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1401 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001402 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001403 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1404 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1405 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001406 // Store the scalar value.
Nate Begeman0325d902008-02-13 06:43:04 +00001407 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
1408 PseudoSourceValue::getFixedStack(), SPFI, EltVT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001409 // Load the updated vector.
Dan Gohman69de1932008-02-06 22:27:42 +00001410 Result = DAG.getLoad(VT, Ch, StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00001411 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001412 break;
1413 }
1414 }
1415 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001416 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001417 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1418 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1419 break;
1420 }
1421
Chris Lattnerce872152006-03-19 06:31:19 +00001422 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1423 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1424 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1425 Node->getValueType(0))) {
1426 default: assert(0 && "This action is not supported yet!");
1427 case TargetLowering::Legal:
1428 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001429 case TargetLowering::Custom:
1430 Tmp3 = TLI.LowerOperation(Result, DAG);
1431 if (Tmp3.Val) {
1432 Result = Tmp3;
1433 break;
1434 }
1435 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001436 case TargetLowering::Expand:
1437 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001438 break;
1439 }
Chris Lattnerce872152006-03-19 06:31:19 +00001440 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001441 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001442 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1443 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1444 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1445
1446 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001447 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1448 default: assert(0 && "Unknown operation action!");
1449 case TargetLowering::Legal:
1450 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1451 "vector shuffle should not be created if not legal!");
1452 break;
1453 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001454 Tmp3 = TLI.LowerOperation(Result, DAG);
1455 if (Tmp3.Val) {
1456 Result = Tmp3;
1457 break;
1458 }
1459 // FALLTHROUGH
1460 case TargetLowering::Expand: {
1461 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001462 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001463 MVT::ValueType PtrVT = TLI.getPointerTy();
1464 SDOperand Mask = Node->getOperand(2);
1465 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001466 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001467 for (unsigned i = 0; i != NumElems; ++i) {
1468 SDOperand Arg = Mask.getOperand(i);
1469 if (Arg.getOpcode() == ISD::UNDEF) {
1470 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1471 } else {
1472 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1473 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1474 if (Idx < NumElems)
1475 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1476 DAG.getConstant(Idx, PtrVT)));
1477 else
1478 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1479 DAG.getConstant(Idx - NumElems, PtrVT)));
1480 }
1481 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001482 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001483 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001484 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001485 case TargetLowering::Promote: {
1486 // Change base type to a different vector type.
1487 MVT::ValueType OVT = Node->getValueType(0);
1488 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1489
1490 // Cast the two input vectors.
1491 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1492 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1493
1494 // Convert the shuffle mask to the right # elements.
1495 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1496 assert(Tmp3.Val && "Shuffle not legal?");
1497 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1498 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1499 break;
1500 }
Chris Lattner87100e02006-03-20 01:52:29 +00001501 }
1502 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001503
1504 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001505 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001506 Tmp2 = LegalizeOp(Node->getOperand(1));
1507 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001508 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001509 break;
1510
Dan Gohman7f321562007-06-25 16:23:39 +00001511 case ISD::EXTRACT_SUBVECTOR:
1512 Tmp1 = Node->getOperand(0);
1513 Tmp2 = LegalizeOp(Node->getOperand(1));
1514 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1515 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001516 break;
1517
Chris Lattner6831a812006-02-13 09:18:02 +00001518 case ISD::CALLSEQ_START: {
1519 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1520
1521 // Recursively Legalize all of the inputs of the call end that do not lead
1522 // to this call start. This ensures that any libcalls that need be inserted
1523 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001524 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001525 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001526 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1527 NodesLeadingTo);
1528 }
Chris Lattner6831a812006-02-13 09:18:02 +00001529
1530 // Now that we legalized all of the inputs (which may have inserted
1531 // libcalls) create the new CALLSEQ_START node.
1532 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1533
1534 // Merge in the last call, to ensure that this call start after the last
1535 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001536 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001537 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1538 Tmp1 = LegalizeOp(Tmp1);
1539 }
Chris Lattner6831a812006-02-13 09:18:02 +00001540
1541 // Do not try to legalize the target-specific arguments (#1+).
1542 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001543 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001544 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001545 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001546 }
1547
1548 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001549 AddLegalizedOperand(Op.getValue(0), Result);
1550 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1551 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1552
Chris Lattner6831a812006-02-13 09:18:02 +00001553 // Now that the callseq_start and all of the non-call nodes above this call
1554 // sequence have been legalized, legalize the call itself. During this
1555 // process, no libcalls can/will be inserted, guaranteeing that no calls
1556 // can overlap.
1557 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1558 SDOperand InCallSEQ = LastCALLSEQ_END;
1559 // Note that we are selecting this call!
1560 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1561 IsLegalizingCall = true;
1562
1563 // Legalize the call, starting from the CALLSEQ_END.
1564 LegalizeOp(LastCALLSEQ_END);
1565 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1566 return Result;
1567 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001568 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001569 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1570 // will cause this node to be legalized as well as handling libcalls right.
1571 if (LastCALLSEQ_END.Val != Node) {
1572 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001573 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001574 assert(I != LegalizedNodes.end() &&
1575 "Legalizing the call start should have legalized this node!");
1576 return I->second;
1577 }
1578
1579 // Otherwise, the call start has been legalized and everything is going
1580 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001581 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001582 // Do not try to legalize the target-specific arguments (#1+), except for
1583 // an optional flag input.
1584 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1585 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001586 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001587 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001588 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001589 }
1590 } else {
1591 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1592 if (Tmp1 != Node->getOperand(0) ||
1593 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001594 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001595 Ops[0] = Tmp1;
1596 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001597 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001598 }
Chris Lattner6a542892006-01-24 05:48:21 +00001599 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001600 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001601 // This finishes up call legalization.
1602 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001603
1604 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1605 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1606 if (Node->getNumValues() == 2)
1607 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1608 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001609 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001610 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001611 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1612 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1613 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001614 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001615
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001616 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001617 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001618 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001619 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001620 case TargetLowering::Expand: {
1621 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1622 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1623 " not tell us which reg is the stack pointer!");
1624 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001625
1626 // Chain the dynamic stack allocation so that it doesn't modify the stack
1627 // pointer when other instructions are using the stack.
1628 Chain = DAG.getCALLSEQ_START(Chain,
1629 DAG.getConstant(0, TLI.getPointerTy()));
1630
Chris Lattner903d2782006-01-15 08:54:32 +00001631 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001632 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1633 Chain = SP.getValue(1);
1634 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1635 unsigned StackAlign =
1636 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1637 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001638 SP = DAG.getNode(ISD::AND, VT, SP,
1639 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001640 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001641 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1642
1643 Tmp2 =
1644 DAG.getCALLSEQ_END(Chain,
1645 DAG.getConstant(0, TLI.getPointerTy()),
1646 DAG.getConstant(0, TLI.getPointerTy()),
1647 SDOperand());
1648
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001649 Tmp1 = LegalizeOp(Tmp1);
1650 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001651 break;
1652 }
1653 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001654 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1655 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001656 Tmp1 = LegalizeOp(Tmp3);
1657 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001658 }
Chris Lattner903d2782006-01-15 08:54:32 +00001659 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001660 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001661 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001662 }
Chris Lattner903d2782006-01-15 08:54:32 +00001663 // Since this op produce two values, make sure to remember that we
1664 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001665 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1666 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001667 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001668 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001669 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001670 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001671 bool Changed = false;
1672 // Legalize all of the operands of the inline asm, in case they are nodes
1673 // that need to be expanded or something. Note we skip the asm string and
1674 // all of the TargetConstant flags.
1675 SDOperand Op = LegalizeOp(Ops[0]);
1676 Changed = Op != Ops[0];
1677 Ops[0] = Op;
1678
1679 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1680 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1681 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1682 for (++i; NumVals; ++i, --NumVals) {
1683 SDOperand Op = LegalizeOp(Ops[i]);
1684 if (Op != Ops[i]) {
1685 Changed = true;
1686 Ops[i] = Op;
1687 }
1688 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001689 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001690
1691 if (HasInFlag) {
1692 Op = LegalizeOp(Ops.back());
1693 Changed |= Op != Ops.back();
1694 Ops.back() = Op;
1695 }
1696
1697 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001698 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001699
1700 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001701 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001702 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1703 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001704 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001705 case ISD::BR:
1706 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001707 // Ensure that libcalls are emitted before a branch.
1708 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1709 Tmp1 = LegalizeOp(Tmp1);
1710 LastCALLSEQ_END = DAG.getEntryNode();
1711
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001712 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001713 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001714 case ISD::BRIND:
1715 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1716 // Ensure that libcalls are emitted before a branch.
1717 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1718 Tmp1 = LegalizeOp(Tmp1);
1719 LastCALLSEQ_END = DAG.getEntryNode();
1720
1721 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1722 default: assert(0 && "Indirect target must be legal type (pointer)!");
1723 case Legal:
1724 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1725 break;
1726 }
1727 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1728 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001729 case ISD::BR_JT:
1730 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1731 // Ensure that libcalls are emitted before a branch.
1732 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1733 Tmp1 = LegalizeOp(Tmp1);
1734 LastCALLSEQ_END = DAG.getEntryNode();
1735
1736 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1737 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1738
1739 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1740 default: assert(0 && "This action is not supported yet!");
1741 case TargetLowering::Legal: break;
1742 case TargetLowering::Custom:
1743 Tmp1 = TLI.LowerOperation(Result, DAG);
1744 if (Tmp1.Val) Result = Tmp1;
1745 break;
1746 case TargetLowering::Expand: {
1747 SDOperand Chain = Result.getOperand(0);
1748 SDOperand Table = Result.getOperand(1);
1749 SDOperand Index = Result.getOperand(2);
1750
1751 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001752 MachineFunction &MF = DAG.getMachineFunction();
1753 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001754 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1755 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001756
1757 SDOperand LD;
1758 switch (EntrySize) {
1759 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001760 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001761 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001762 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001763 PseudoSourceValue::getJumpTable(), 0); break;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001764 }
1765
Evan Chengcc415862007-11-09 01:32:10 +00001766 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001767 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001768 // For PIC, the sequence is:
1769 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001770 // RelocBase can be JumpTable, GOT or some sort of global base.
1771 if (PTy != MVT::i32)
1772 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1773 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1774 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001775 }
Evan Chengcc415862007-11-09 01:32:10 +00001776 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001777 }
1778 }
1779 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001780 case ISD::BRCOND:
1781 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001782 // Ensure that libcalls are emitted before a return.
1783 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1784 Tmp1 = LegalizeOp(Tmp1);
1785 LastCALLSEQ_END = DAG.getEntryNode();
1786
Chris Lattner47e92232005-01-18 19:27:06 +00001787 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1788 case Expand: assert(0 && "It's impossible to expand bools");
1789 case Legal:
1790 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1791 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001792 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00001793 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001794
1795 // The top bits of the promoted condition are not necessarily zero, ensure
1796 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001797 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001798 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001799 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00001800 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001801 break;
1802 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001803 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001804
1805 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001806 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001807
1808 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1809 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001810 case TargetLowering::Legal: break;
1811 case TargetLowering::Custom:
1812 Tmp1 = TLI.LowerOperation(Result, DAG);
1813 if (Tmp1.Val) Result = Tmp1;
1814 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001815 case TargetLowering::Expand:
1816 // Expand brcond's setcc into its constituent parts and create a BR_CC
1817 // Node.
1818 if (Tmp2.getOpcode() == ISD::SETCC) {
1819 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1820 Tmp2.getOperand(0), Tmp2.getOperand(1),
1821 Node->getOperand(2));
1822 } else {
1823 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1824 DAG.getCondCode(ISD::SETNE), Tmp2,
1825 DAG.getConstant(0, Tmp2.getValueType()),
1826 Node->getOperand(2));
1827 }
1828 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001829 }
1830 break;
1831 case ISD::BR_CC:
1832 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001833 // Ensure that libcalls are emitted before a branch.
1834 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1835 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001836 Tmp2 = Node->getOperand(2); // LHS
1837 Tmp3 = Node->getOperand(3); // RHS
1838 Tmp4 = Node->getOperand(1); // CC
1839
1840 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001841 LastCALLSEQ_END = DAG.getEntryNode();
1842
Nate Begeman750ac1b2006-02-01 07:19:44 +00001843 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1844 // the LHS is a legal SETCC itself. In this case, we need to compare
1845 // the result against zero to select between true and false values.
1846 if (Tmp3.Val == 0) {
1847 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1848 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001849 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001850
1851 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1852 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001853
Chris Lattner181b7a32005-12-17 23:46:46 +00001854 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1855 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001856 case TargetLowering::Legal: break;
1857 case TargetLowering::Custom:
1858 Tmp4 = TLI.LowerOperation(Result, DAG);
1859 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001860 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001861 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001862 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001863 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001864 LoadSDNode *LD = cast<LoadSDNode>(Node);
1865 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1866 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001867
Evan Cheng466685d2006-10-09 20:57:25 +00001868 ISD::LoadExtType ExtType = LD->getExtensionType();
1869 if (ExtType == ISD::NON_EXTLOAD) {
1870 MVT::ValueType VT = Node->getValueType(0);
1871 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1872 Tmp3 = Result.getValue(0);
1873 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001874
Evan Cheng466685d2006-10-09 20:57:25 +00001875 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1876 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001877 case TargetLowering::Legal:
1878 // If this is an unaligned load and the target doesn't support it,
1879 // expand it.
1880 if (!TLI.allowsUnalignedMemoryAccesses()) {
1881 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001882 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001883 if (LD->getAlignment() < ABIAlignment){
1884 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1885 TLI);
1886 Tmp3 = Result.getOperand(0);
1887 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001888 Tmp3 = LegalizeOp(Tmp3);
1889 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001890 }
1891 }
1892 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001893 case TargetLowering::Custom:
1894 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1895 if (Tmp1.Val) {
1896 Tmp3 = LegalizeOp(Tmp1);
1897 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001898 }
Evan Cheng466685d2006-10-09 20:57:25 +00001899 break;
1900 case TargetLowering::Promote: {
1901 // Only promote a load of vector type to another.
1902 assert(MVT::isVector(VT) && "Cannot promote this load!");
1903 // Change base type to a different vector type.
1904 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1905
1906 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001907 LD->getSrcValueOffset(),
1908 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001909 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1910 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001911 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001912 }
Evan Cheng466685d2006-10-09 20:57:25 +00001913 }
1914 // Since loads produce two values, make sure to remember that we
1915 // legalized both of them.
1916 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1917 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1918 return Op.ResNo ? Tmp4 : Tmp3;
1919 } else {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001920 MVT::ValueType SrcVT = LD->getMemoryVT();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001921 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1922 int SVOffset = LD->getSrcValueOffset();
1923 unsigned Alignment = LD->getAlignment();
1924 bool isVolatile = LD->isVolatile();
1925
1926 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1927 // Some targets pretend to have an i1 loading operation, and actually
1928 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1929 // bits are guaranteed to be zero; it helps the optimizers understand
1930 // that these bits are zero. It is also useful for EXTLOAD, since it
1931 // tells the optimizers that those bits are undefined. It would be
1932 // nice to have an effective generic way of getting these benefits...
1933 // Until such a way is found, don't insist on promoting i1 here.
1934 (SrcVT != MVT::i1 ||
1935 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1936 // Promote to a byte-sized load if not loading an integral number of
1937 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1938 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1939 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1940 SDOperand Ch;
1941
1942 // The extra bits are guaranteed to be zero, since we stored them that
1943 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1944
1945 ISD::LoadExtType NewExtType =
1946 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1947
1948 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1949 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1950 NVT, isVolatile, Alignment);
1951
1952 Ch = Result.getValue(1); // The chain.
1953
1954 if (ExtType == ISD::SEXTLOAD)
1955 // Having the top bits zero doesn't help when sign extending.
1956 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1957 Result, DAG.getValueType(SrcVT));
1958 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1959 // All the top bits are guaranteed to be zero - inform the optimizers.
1960 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1961 DAG.getValueType(SrcVT));
1962
1963 Tmp1 = LegalizeOp(Result);
1964 Tmp2 = LegalizeOp(Ch);
1965 } else if (SrcWidth & (SrcWidth - 1)) {
1966 // If not loading a power-of-2 number of bits, expand as two loads.
1967 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1968 "Unsupported extload!");
1969 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1970 assert(RoundWidth < SrcWidth);
1971 unsigned ExtraWidth = SrcWidth - RoundWidth;
1972 assert(ExtraWidth < RoundWidth);
1973 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
1974 "Load size not an integral number of bytes!");
1975 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
1976 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
1977 SDOperand Lo, Hi, Ch;
1978 unsigned IncrementSize;
1979
1980 if (TLI.isLittleEndian()) {
1981 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
1982 // Load the bottom RoundWidth bits.
1983 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
1984 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
1985 Alignment);
1986
1987 // Load the remaining ExtraWidth bits.
1988 IncrementSize = RoundWidth / 8;
1989 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1990 DAG.getIntPtrConstant(IncrementSize));
1991 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1992 LD->getSrcValue(), SVOffset + IncrementSize,
1993 ExtraVT, isVolatile,
1994 MinAlign(Alignment, IncrementSize));
1995
1996 // Build a factor node to remember that this load is independent of the
1997 // other one.
1998 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1999 Hi.getValue(1));
2000
2001 // Move the top bits to the right place.
2002 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2003 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2004
2005 // Join the hi and lo parts.
2006 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002007 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002008 // Big endian - avoid unaligned loads.
2009 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2010 // Load the top RoundWidth bits.
2011 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2012 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2013 Alignment);
2014
2015 // Load the remaining ExtraWidth bits.
2016 IncrementSize = RoundWidth / 8;
2017 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2018 DAG.getIntPtrConstant(IncrementSize));
2019 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2020 LD->getSrcValue(), SVOffset + IncrementSize,
2021 ExtraVT, isVolatile,
2022 MinAlign(Alignment, IncrementSize));
2023
2024 // Build a factor node to remember that this load is independent of the
2025 // other one.
2026 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2027 Hi.getValue(1));
2028
2029 // Move the top bits to the right place.
2030 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2031 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2032
2033 // Join the hi and lo parts.
2034 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2035 }
2036
2037 Tmp1 = LegalizeOp(Result);
2038 Tmp2 = LegalizeOp(Ch);
2039 } else {
2040 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2041 default: assert(0 && "This action is not supported yet!");
2042 case TargetLowering::Custom:
2043 isCustom = true;
2044 // FALLTHROUGH
2045 case TargetLowering::Legal:
2046 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2047 Tmp1 = Result.getValue(0);
2048 Tmp2 = Result.getValue(1);
2049
2050 if (isCustom) {
2051 Tmp3 = TLI.LowerOperation(Result, DAG);
2052 if (Tmp3.Val) {
2053 Tmp1 = LegalizeOp(Tmp3);
2054 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2055 }
2056 } else {
2057 // If this is an unaligned load and the target doesn't support it,
2058 // expand it.
2059 if (!TLI.allowsUnalignedMemoryAccesses()) {
2060 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002061 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002062 if (LD->getAlignment() < ABIAlignment){
2063 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2064 TLI);
2065 Tmp1 = Result.getOperand(0);
2066 Tmp2 = Result.getOperand(1);
2067 Tmp1 = LegalizeOp(Tmp1);
2068 Tmp2 = LegalizeOp(Tmp2);
2069 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002070 }
2071 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002072 break;
2073 case TargetLowering::Expand:
2074 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2075 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2076 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2077 LD->getSrcValueOffset(),
2078 LD->isVolatile(), LD->getAlignment());
2079 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2080 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2081 Tmp2 = LegalizeOp(Load.getValue(1));
2082 break;
2083 }
2084 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2085 // Turn the unsupported load into an EXTLOAD followed by an explicit
2086 // zero/sign extend inreg.
2087 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2088 Tmp1, Tmp2, LD->getSrcValue(),
2089 LD->getSrcValueOffset(), SrcVT,
2090 LD->isVolatile(), LD->getAlignment());
2091 SDOperand ValRes;
2092 if (ExtType == ISD::SEXTLOAD)
2093 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2094 Result, DAG.getValueType(SrcVT));
2095 else
2096 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2097 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2098 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002099 break;
2100 }
Evan Cheng466685d2006-10-09 20:57:25 +00002101 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002102
Evan Cheng466685d2006-10-09 20:57:25 +00002103 // Since loads produce two values, make sure to remember that we legalized
2104 // both of them.
2105 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2106 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2107 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002108 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002109 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002110 case ISD::EXTRACT_ELEMENT: {
2111 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2112 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002113 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002114 case Legal:
2115 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2116 // 1 -> Hi
2117 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2118 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2119 TLI.getShiftAmountTy()));
2120 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2121 } else {
2122 // 0 -> Lo
2123 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2124 Node->getOperand(0));
2125 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002126 break;
2127 case Expand:
2128 // Get both the low and high parts.
2129 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2130 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2131 Result = Tmp2; // 1 -> Hi
2132 else
2133 Result = Tmp1; // 0 -> Lo
2134 break;
2135 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002136 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002137 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002138
2139 case ISD::CopyToReg:
2140 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002141
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002142 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002143 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002144 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002145 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002146 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002147 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002148 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002149 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002150 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002151 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002152 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2153 Tmp3);
2154 } else {
2155 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002156 }
2157
2158 // Since this produces two values, make sure to remember that we legalized
2159 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002160 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00002161 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002162 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002163 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002164 break;
2165
2166 case ISD::RET:
2167 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002168
2169 // Ensure that libcalls are emitted before a return.
2170 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2171 Tmp1 = LegalizeOp(Tmp1);
2172 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002173
Chris Lattner3e928bb2005-01-07 07:47:09 +00002174 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002175 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002176 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002177 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002178 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002179 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002180 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002181 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002182 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00002183 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00002184 SDOperand Lo, Hi;
2185 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002186
2187 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002188 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002189 std::swap(Lo, Hi);
2190
Evan Cheng13acce32006-12-11 19:27:14 +00002191 if (Hi.Val)
2192 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2193 else
2194 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002195 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002196 } else {
2197 SDNode *InVal = Tmp2.Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002198 int InIx = Tmp2.ResNo;
2199 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2200 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattnerf87324e2006-04-11 01:31:51 +00002201
Dan Gohman7f321562007-06-25 16:23:39 +00002202 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002203 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00002204 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002205 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002206 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002207 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002208 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002209 } else if (NumElems == 1) {
2210 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002211 Tmp2 = ScalarizeVectorOp(Tmp2);
2212 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002213 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002214
2215 // FIXME: Returns of gcc generic vectors smaller than a legal type
2216 // should be returned in integer registers!
2217
Chris Lattnerf87324e2006-04-11 01:31:51 +00002218 // The scalarized value type may not be legal, e.g. it might require
2219 // promotion or expansion. Relegalize the return.
2220 Result = LegalizeOp(Result);
2221 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002222 // FIXME: Returns of gcc generic vectors larger than a legal vector
2223 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00002224 SDOperand Lo, Hi;
2225 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002226 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002227 Result = LegalizeOp(Result);
2228 }
2229 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002230 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002231 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002232 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002233 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002234 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002235 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002236 }
2237 break;
2238 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002239 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002240 break;
2241 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002242 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002243 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002244 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002245 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2246 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002247 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002248 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002249 break;
2250 case Expand: {
2251 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00002252 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002253 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002254 ExpandOp(Node->getOperand(i), Lo, Hi);
2255 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002256 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00002257 if (Hi.Val) {
2258 NewValues.push_back(Hi);
2259 NewValues.push_back(Node->getOperand(i+1));
2260 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002261 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002262 }
2263 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002264 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002265 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002266
2267 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002268 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002269 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002270 Result = DAG.getNode(ISD::RET, MVT::Other,
2271 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002272 break;
2273 }
2274 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002275
Chris Lattner6862dbc2006-01-29 21:02:23 +00002276 if (Result.getOpcode() == ISD::RET) {
2277 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2278 default: assert(0 && "This action is not supported yet!");
2279 case TargetLowering::Legal: break;
2280 case TargetLowering::Custom:
2281 Tmp1 = TLI.LowerOperation(Result, DAG);
2282 if (Tmp1.Val) Result = Tmp1;
2283 break;
2284 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002285 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002286 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002287 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002288 StoreSDNode *ST = cast<StoreSDNode>(Node);
2289 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2290 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002291 int SVOffset = ST->getSrcValueOffset();
2292 unsigned Alignment = ST->getAlignment();
2293 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002294
Evan Cheng8b2794a2006-10-13 21:14:26 +00002295 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002296 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2297 // FIXME: We shouldn't do this for TargetConstantFP's.
2298 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2299 // to phase ordering between legalized code and the dag combiner. This
2300 // probably means that we need to integrate dag combiner and legalizer
2301 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002302 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002303 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002304 if (CFP->getValueType(0) == MVT::f32 &&
2305 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00002306 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2307 convertToAPInt().getZExtValue(),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002308 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002309 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2310 SVOffset, isVolatile, Alignment);
2311 break;
2312 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002313 // If this target supports 64-bit registers, do a single 64-bit store.
2314 if (getTypeAction(MVT::i64) == Legal) {
2315 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2316 getZExtValue(), MVT::i64);
2317 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2318 SVOffset, isVolatile, Alignment);
2319 break;
2320 } else if (getTypeAction(MVT::i32) == Legal) {
2321 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2322 // stores. If the target supports neither 32- nor 64-bits, this
2323 // xform is certainly not worth it.
2324 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2325 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2326 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002327 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002328
2329 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2330 SVOffset, isVolatile, Alignment);
2331 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002332 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002333 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002334 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002335
2336 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2337 break;
2338 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002339 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002340 }
2341
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002342 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002343 case Legal: {
2344 Tmp3 = LegalizeOp(ST->getValue());
2345 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2346 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002347
Evan Cheng8b2794a2006-10-13 21:14:26 +00002348 MVT::ValueType VT = Tmp3.getValueType();
2349 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2350 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002351 case TargetLowering::Legal:
2352 // If this is an unaligned store and the target doesn't support it,
2353 // expand it.
2354 if (!TLI.allowsUnalignedMemoryAccesses()) {
2355 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002356 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002357 if (ST->getAlignment() < ABIAlignment)
2358 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2359 TLI);
2360 }
2361 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002362 case TargetLowering::Custom:
2363 Tmp1 = TLI.LowerOperation(Result, DAG);
2364 if (Tmp1.Val) Result = Tmp1;
2365 break;
2366 case TargetLowering::Promote:
2367 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2368 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2369 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2370 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002371 ST->getSrcValue(), SVOffset, isVolatile,
2372 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002373 break;
2374 }
2375 break;
2376 }
2377 case Promote:
2378 // Truncate the value and store the result.
2379 Tmp3 = PromoteOp(ST->getValue());
2380 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002381 SVOffset, ST->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002382 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002383 break;
2384
2385 case Expand:
2386 unsigned IncrementSize = 0;
2387 SDOperand Lo, Hi;
2388
2389 // If this is a vector type, then we have to calculate the increment as
2390 // the product of the element size in bytes, and the number of elements
2391 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002392 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002393 SDNode *InVal = ST->getValue().Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002394 int InIx = ST->getValue().ResNo;
Chris Lattner0bd48932008-01-17 07:00:52 +00002395 MVT::ValueType InVT = InVal->getValueType(InIx);
2396 unsigned NumElems = MVT::getVectorNumElements(InVT);
2397 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002398
Dan Gohman7f321562007-06-25 16:23:39 +00002399 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002400 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002401 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002402 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002403 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002404 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002405 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002406 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002407 Result = LegalizeOp(Result);
2408 break;
2409 } else if (NumElems == 1) {
2410 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002411 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002412 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002413 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002414 // The scalarized value type may not be legal, e.g. it might require
2415 // promotion or expansion. Relegalize the scalar store.
2416 Result = LegalizeOp(Result);
2417 break;
2418 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002419 SplitVectorOp(ST->getValue(), Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00002420 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2421 MVT::getSizeInBits(EVT)/8;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002422 }
2423 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002424 ExpandOp(ST->getValue(), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002425 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002426
Duncan Sands0753fc12008-02-11 10:37:04 +00002427 if (TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002428 std::swap(Lo, Hi);
2429 }
2430
2431 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002432 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002433
2434 if (Hi.Val == NULL) {
2435 // Must be int <-> float one-to-one expansion.
2436 Result = Lo;
2437 break;
2438 }
2439
Evan Cheng8b2794a2006-10-13 21:14:26 +00002440 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002441 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002442 assert(isTypeLegal(Tmp2.getValueType()) &&
2443 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002444 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002445 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002446 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002447 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002448 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2449 break;
2450 }
2451 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002452 switch (getTypeAction(ST->getValue().getValueType())) {
2453 case Legal:
2454 Tmp3 = LegalizeOp(ST->getValue());
2455 break;
2456 case Promote:
2457 // We can promote the value, the truncstore will still take care of it.
2458 Tmp3 = PromoteOp(ST->getValue());
2459 break;
2460 case Expand:
2461 // Just store the low part. This may become a non-trunc store, so make
2462 // sure to use getTruncStore, not UpdateNodeOperands below.
2463 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2464 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2465 SVOffset, MVT::i8, isVolatile, Alignment);
2466 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002467
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002468 MVT::ValueType StVT = ST->getMemoryVT();
Duncan Sands7e857202008-01-22 07:17:34 +00002469 unsigned StWidth = MVT::getSizeInBits(StVT);
2470
2471 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2472 // Promote to a byte-sized store with upper bits zero if not
2473 // storing an integral number of bytes. For example, promote
2474 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2475 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2476 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2477 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2478 SVOffset, NVT, isVolatile, Alignment);
2479 } else if (StWidth & (StWidth - 1)) {
2480 // If not storing a power-of-2 number of bits, expand as two stores.
2481 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2482 "Unsupported truncstore!");
2483 unsigned RoundWidth = 1 << Log2_32(StWidth);
2484 assert(RoundWidth < StWidth);
2485 unsigned ExtraWidth = StWidth - RoundWidth;
2486 assert(ExtraWidth < RoundWidth);
2487 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2488 "Store size not an integral number of bytes!");
2489 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2490 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2491 SDOperand Lo, Hi;
2492 unsigned IncrementSize;
2493
2494 if (TLI.isLittleEndian()) {
2495 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2496 // Store the bottom RoundWidth bits.
2497 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2498 SVOffset, RoundVT,
2499 isVolatile, Alignment);
2500
2501 // Store the remaining ExtraWidth bits.
2502 IncrementSize = RoundWidth / 8;
2503 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2504 DAG.getIntPtrConstant(IncrementSize));
2505 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2506 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2507 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2508 SVOffset + IncrementSize, ExtraVT, isVolatile,
2509 MinAlign(Alignment, IncrementSize));
2510 } else {
2511 // Big endian - avoid unaligned stores.
2512 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2513 // Store the top RoundWidth bits.
2514 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2515 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2516 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2517 RoundVT, isVolatile, Alignment);
2518
2519 // Store the remaining ExtraWidth bits.
2520 IncrementSize = RoundWidth / 8;
2521 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2522 DAG.getIntPtrConstant(IncrementSize));
2523 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2524 SVOffset + IncrementSize, ExtraVT, isVolatile,
2525 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002526 }
Duncan Sands7e857202008-01-22 07:17:34 +00002527
2528 // The order of the stores doesn't matter.
2529 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2530 } else {
2531 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2532 Tmp2 != ST->getBasePtr())
2533 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2534 ST->getOffset());
2535
2536 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2537 default: assert(0 && "This action is not supported yet!");
2538 case TargetLowering::Legal:
2539 // If this is an unaligned store and the target doesn't support it,
2540 // expand it.
2541 if (!TLI.allowsUnalignedMemoryAccesses()) {
2542 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002543 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Duncan Sands7e857202008-01-22 07:17:34 +00002544 if (ST->getAlignment() < ABIAlignment)
2545 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2546 TLI);
2547 }
2548 break;
2549 case TargetLowering::Custom:
2550 Result = TLI.LowerOperation(Result, DAG);
2551 break;
2552 case Expand:
2553 // TRUNCSTORE:i16 i32 -> STORE i16
2554 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2555 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2556 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2557 isVolatile, Alignment);
2558 break;
2559 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002560 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002561 }
2562 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002563 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002564 case ISD::PCMARKER:
2565 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002566 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002567 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002568 case ISD::STACKSAVE:
2569 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002570 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2571 Tmp1 = Result.getValue(0);
2572 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002573
Chris Lattner140d53c2006-01-13 02:50:02 +00002574 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2575 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002576 case TargetLowering::Legal: break;
2577 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002578 Tmp3 = TLI.LowerOperation(Result, DAG);
2579 if (Tmp3.Val) {
2580 Tmp1 = LegalizeOp(Tmp3);
2581 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002582 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002583 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002584 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002585 // Expand to CopyFromReg if the target set
2586 // StackPointerRegisterToSaveRestore.
2587 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002588 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002589 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002590 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002591 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002592 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2593 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002594 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002595 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002596 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002597
2598 // Since stacksave produce two values, make sure to remember that we
2599 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002600 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2601 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2602 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002603
Chris Lattner140d53c2006-01-13 02:50:02 +00002604 case ISD::STACKRESTORE:
2605 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2606 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002607 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002608
2609 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2610 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002611 case TargetLowering::Legal: break;
2612 case TargetLowering::Custom:
2613 Tmp1 = TLI.LowerOperation(Result, DAG);
2614 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002615 break;
2616 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002617 // Expand to CopyToReg if the target set
2618 // StackPointerRegisterToSaveRestore.
2619 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2620 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2621 } else {
2622 Result = Tmp1;
2623 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002624 break;
2625 }
2626 break;
2627
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002628 case ISD::READCYCLECOUNTER:
2629 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002630 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002631 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2632 Node->getValueType(0))) {
2633 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002634 case TargetLowering::Legal:
2635 Tmp1 = Result.getValue(0);
2636 Tmp2 = Result.getValue(1);
2637 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002638 case TargetLowering::Custom:
2639 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002640 Tmp1 = LegalizeOp(Result.getValue(0));
2641 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002642 break;
2643 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002644
2645 // Since rdcc produce two values, make sure to remember that we legalized
2646 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002647 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2648 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002649 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002650
Chris Lattner2ee743f2005-01-14 22:08:15 +00002651 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002652 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2653 case Expand: assert(0 && "It's impossible to expand bools");
2654 case Legal:
2655 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2656 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002657 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00002658 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002659 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002660 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002661 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002662 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002663 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002664 break;
2665 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002666 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002667 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002668 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002669
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002670 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002671
Nate Begemanb942a3d2005-08-23 04:29:48 +00002672 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002673 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002674 case TargetLowering::Legal: break;
2675 case TargetLowering::Custom: {
2676 Tmp1 = TLI.LowerOperation(Result, DAG);
2677 if (Tmp1.Val) Result = Tmp1;
2678 break;
2679 }
Nate Begeman9373a812005-08-10 20:51:12 +00002680 case TargetLowering::Expand:
2681 if (Tmp1.getOpcode() == ISD::SETCC) {
2682 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2683 Tmp2, Tmp3,
2684 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2685 } else {
2686 Result = DAG.getSelectCC(Tmp1,
2687 DAG.getConstant(0, Tmp1.getValueType()),
2688 Tmp2, Tmp3, ISD::SETNE);
2689 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002690 break;
2691 case TargetLowering::Promote: {
2692 MVT::ValueType NVT =
2693 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2694 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002695 if (MVT::isVector(Tmp2.getValueType())) {
2696 ExtOp = ISD::BIT_CONVERT;
2697 TruncOp = ISD::BIT_CONVERT;
2698 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002699 ExtOp = ISD::ANY_EXTEND;
2700 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002701 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002702 ExtOp = ISD::FP_EXTEND;
2703 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002704 }
2705 // Promote each of the values to the new type.
2706 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2707 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2708 // Perform the larger operation, then round down.
2709 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002710 if (TruncOp != ISD::FP_ROUND)
2711 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2712 else
2713 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2714 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002715 break;
2716 }
2717 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002718 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002719 case ISD::SELECT_CC: {
2720 Tmp1 = Node->getOperand(0); // LHS
2721 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002722 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2723 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002724 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002725
Nate Begeman750ac1b2006-02-01 07:19:44 +00002726 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2727
2728 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2729 // the LHS is a legal SETCC itself. In this case, we need to compare
2730 // the result against zero to select between true and false values.
2731 if (Tmp2.Val == 0) {
2732 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2733 CC = DAG.getCondCode(ISD::SETNE);
2734 }
2735 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2736
2737 // Everything is legal, see if we should expand this op or something.
2738 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2739 default: assert(0 && "This action is not supported yet!");
2740 case TargetLowering::Legal: break;
2741 case TargetLowering::Custom:
2742 Tmp1 = TLI.LowerOperation(Result, DAG);
2743 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002744 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002745 }
2746 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002747 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002748 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002749 Tmp1 = Node->getOperand(0);
2750 Tmp2 = Node->getOperand(1);
2751 Tmp3 = Node->getOperand(2);
2752 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2753
2754 // If we had to Expand the SetCC operands into a SELECT node, then it may
2755 // not always be possible to return a true LHS & RHS. In this case, just
2756 // return the value we legalized, returned in the LHS
2757 if (Tmp2.Val == 0) {
2758 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002759 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002760 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002761
Chris Lattner73e142f2006-01-30 22:43:50 +00002762 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002763 default: assert(0 && "Cannot handle this action for SETCC yet!");
2764 case TargetLowering::Custom:
2765 isCustom = true;
2766 // FALLTHROUGH.
2767 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002768 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002769 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002770 Tmp4 = TLI.LowerOperation(Result, DAG);
2771 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002772 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002773 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002774 case TargetLowering::Promote: {
2775 // First step, figure out the appropriate operation to use.
2776 // Allow SETCC to not be supported for all legal data types
2777 // Mostly this targets FP
2778 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002779 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002780
2781 // Scan for the appropriate larger type to use.
2782 while (1) {
2783 NewInTy = (MVT::ValueType)(NewInTy+1);
2784
2785 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2786 "Fell off of the edge of the integer world");
2787 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2788 "Fell off of the edge of the floating point world");
2789
2790 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002791 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002792 break;
2793 }
2794 if (MVT::isInteger(NewInTy))
2795 assert(0 && "Cannot promote Legal Integer SETCC yet");
2796 else {
2797 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2798 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2799 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002800 Tmp1 = LegalizeOp(Tmp1);
2801 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002802 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002803 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002804 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002805 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002806 case TargetLowering::Expand:
2807 // Expand a setcc node into a select_cc of the same condition, lhs, and
2808 // rhs that selects between const 1 (true) and const 0 (false).
2809 MVT::ValueType VT = Node->getValueType(0);
2810 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2811 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002812 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002813 break;
2814 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002815 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002816 case ISD::MEMSET:
2817 case ISD::MEMCPY:
2818 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002819 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002820 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2821
2822 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2823 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2824 case Expand: assert(0 && "Cannot expand a byte!");
2825 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002826 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002827 break;
2828 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002829 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002830 break;
2831 }
2832 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002833 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002834 }
Chris Lattner272455b2005-02-02 03:44:41 +00002835
2836 SDOperand Tmp4;
2837 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002838 case Expand: {
2839 // Length is too big, just take the lo-part of the length.
2840 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002841 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002842 break;
2843 }
Chris Lattnere5605212005-01-28 22:29:18 +00002844 case Legal:
2845 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002846 break;
2847 case Promote:
2848 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002849 break;
2850 }
2851
2852 SDOperand Tmp5;
2853 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2854 case Expand: assert(0 && "Cannot expand this yet!");
2855 case Legal:
2856 Tmp5 = LegalizeOp(Node->getOperand(4));
2857 break;
2858 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002859 Tmp5 = PromoteOp(Node->getOperand(4));
2860 break;
2861 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002862
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002863 SDOperand Tmp6;
2864 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2865 case Expand: assert(0 && "Cannot expand this yet!");
2866 case Legal:
2867 Tmp6 = LegalizeOp(Node->getOperand(5));
2868 break;
2869 case Promote:
2870 Tmp6 = PromoteOp(Node->getOperand(5));
2871 break;
2872 }
2873
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002874 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2875 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002876 case TargetLowering::Custom:
2877 isCustom = true;
2878 // FALLTHROUGH
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002879 case TargetLowering::Legal: {
2880 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2881 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner456a93a2006-01-28 07:39:30 +00002882 if (isCustom) {
2883 Tmp1 = TLI.LowerOperation(Result, DAG);
2884 if (Tmp1.Val) Result = Tmp1;
2885 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002886 break;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002887 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002888 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002889 // Otherwise, the target does not support this operation. Lower the
2890 // operation to an explicit libcall as appropriate.
2891 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002892 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002893 TargetLowering::ArgListTy Args;
2894 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002895
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002896 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002897 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002898 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002899 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002900 // Extend the (previously legalized) ubyte argument to be an int value
2901 // for the call.
2902 if (Tmp3.getValueType() > MVT::i32)
2903 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2904 else
2905 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002906 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002907 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002908 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002909 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002910
2911 FnName = "memset";
2912 } else if (Node->getOpcode() == ISD::MEMCPY ||
2913 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002914 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002915 Entry.Node = Tmp2; Args.push_back(Entry);
2916 Entry.Node = Tmp3; Args.push_back(Entry);
2917 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002918 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2919 } else {
2920 assert(0 && "Unknown op!");
2921 }
Chris Lattner45982da2005-05-12 16:53:42 +00002922
Chris Lattnere1bd8222005-01-11 05:57:22 +00002923 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00002924 TLI.LowerCallTo(Tmp1, Type::VoidTy,
2925 false, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002926 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002927 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002928 break;
2929 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002930 }
2931 break;
2932 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002933
Chris Lattner5b359c62005-04-02 04:00:59 +00002934 case ISD::SHL_PARTS:
2935 case ISD::SRA_PARTS:
2936 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002937 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002938 bool Changed = false;
2939 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2940 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2941 Changed |= Ops.back() != Node->getOperand(i);
2942 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002943 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002944 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002945
Evan Cheng05a2d562006-01-09 18:31:59 +00002946 switch (TLI.getOperationAction(Node->getOpcode(),
2947 Node->getValueType(0))) {
2948 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002949 case TargetLowering::Legal: break;
2950 case TargetLowering::Custom:
2951 Tmp1 = TLI.LowerOperation(Result, DAG);
2952 if (Tmp1.Val) {
2953 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002954 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002955 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002956 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2957 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002958 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002959 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002960 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002961 return RetVal;
2962 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002963 break;
2964 }
2965
Chris Lattner2c8086f2005-04-02 05:00:07 +00002966 // Since these produce multiple values, make sure to remember that we
2967 // legalized all of them.
2968 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2969 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2970 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002971 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002972
2973 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002974 case ISD::ADD:
2975 case ISD::SUB:
2976 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002977 case ISD::MULHS:
2978 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002979 case ISD::UDIV:
2980 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002981 case ISD::AND:
2982 case ISD::OR:
2983 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002984 case ISD::SHL:
2985 case ISD::SRL:
2986 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002987 case ISD::FADD:
2988 case ISD::FSUB:
2989 case ISD::FMUL:
2990 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002991 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002992 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002993 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2994 case Expand: assert(0 && "Not possible");
2995 case Legal:
2996 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2997 break;
2998 case Promote:
2999 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3000 break;
3001 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003002
3003 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003004
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003005 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003006 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003007 case TargetLowering::Legal: break;
3008 case TargetLowering::Custom:
3009 Tmp1 = TLI.LowerOperation(Result, DAG);
3010 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00003011 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003012 case TargetLowering::Expand: {
Dan Gohman525178c2007-10-08 18:33:35 +00003013 MVT::ValueType VT = Op.getValueType();
3014
3015 // See if multiply or divide can be lowered using two-result operations.
3016 SDVTList VTs = DAG.getVTList(VT, VT);
3017 if (Node->getOpcode() == ISD::MUL) {
3018 // We just need the low half of the multiply; try both the signed
3019 // and unsigned forms. If the target supports both SMUL_LOHI and
3020 // UMUL_LOHI, form a preference by checking which forms of plain
3021 // MULH it supports.
3022 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3023 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3024 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3025 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3026 unsigned OpToUse = 0;
3027 if (HasSMUL_LOHI && !HasMULHS) {
3028 OpToUse = ISD::SMUL_LOHI;
3029 } else if (HasUMUL_LOHI && !HasMULHU) {
3030 OpToUse = ISD::UMUL_LOHI;
3031 } else if (HasSMUL_LOHI) {
3032 OpToUse = ISD::SMUL_LOHI;
3033 } else if (HasUMUL_LOHI) {
3034 OpToUse = ISD::UMUL_LOHI;
3035 }
3036 if (OpToUse) {
3037 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
3038 break;
3039 }
3040 }
3041 if (Node->getOpcode() == ISD::MULHS &&
3042 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
3043 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3044 break;
3045 }
3046 if (Node->getOpcode() == ISD::MULHU &&
3047 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
3048 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3049 break;
3050 }
3051 if (Node->getOpcode() == ISD::SDIV &&
3052 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3053 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3054 break;
3055 }
3056 if (Node->getOpcode() == ISD::UDIV &&
3057 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3058 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3059 break;
3060 }
3061
Dan Gohman82669522007-10-11 23:57:53 +00003062 // Check to see if we have a libcall for this operator.
3063 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3064 bool isSigned = false;
3065 switch (Node->getOpcode()) {
3066 case ISD::UDIV:
3067 case ISD::SDIV:
3068 if (VT == MVT::i32) {
3069 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00003070 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003071 isSigned = Node->getOpcode() == ISD::SDIV;
3072 }
3073 break;
3074 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003075 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3076 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003077 break;
3078 default: break;
3079 }
3080 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3081 SDOperand Dummy;
3082 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003083 break;
3084 }
3085
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003086 assert(MVT::isVector(Node->getValueType(0)) &&
3087 "Cannot expand this binary operator!");
3088 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003089 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003090 break;
3091 }
Evan Chengcc987612006-04-12 21:20:24 +00003092 case TargetLowering::Promote: {
3093 switch (Node->getOpcode()) {
3094 default: assert(0 && "Do not know how to promote this BinOp!");
3095 case ISD::AND:
3096 case ISD::OR:
3097 case ISD::XOR: {
3098 MVT::ValueType OVT = Node->getValueType(0);
3099 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3100 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3101 // Bit convert each of the values to the new type.
3102 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3103 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3104 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3105 // Bit convert the result back the original type.
3106 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3107 break;
3108 }
3109 }
3110 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003111 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003112 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003113
Dan Gohmane14ea862007-10-05 14:17:22 +00003114 case ISD::SMUL_LOHI:
3115 case ISD::UMUL_LOHI:
3116 case ISD::SDIVREM:
3117 case ISD::UDIVREM:
3118 // These nodes will only be produced by target-specific lowering, so
3119 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003120 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003121 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003122
3123 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3124 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3125 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003126 break;
3127
Chris Lattnera09f8482006-03-05 05:09:38 +00003128 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3129 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3130 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3131 case Expand: assert(0 && "Not possible");
3132 case Legal:
3133 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3134 break;
3135 case Promote:
3136 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3137 break;
3138 }
3139
3140 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3141
3142 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3143 default: assert(0 && "Operation not supported");
3144 case TargetLowering::Custom:
3145 Tmp1 = TLI.LowerOperation(Result, DAG);
3146 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003147 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003148 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003149 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003150 // If this target supports fabs/fneg natively and select is cheap,
3151 // do this efficiently.
3152 if (!TLI.isSelectExpensive() &&
3153 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3154 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003155 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003156 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003157 // Get the sign bit of the RHS.
3158 MVT::ValueType IVT =
3159 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3160 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
3161 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
3162 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3163 // Get the absolute value of the result.
3164 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3165 // Select between the nabs and abs value based on the sign bit of
3166 // the input.
3167 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3168 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3169 AbsVal),
3170 AbsVal);
3171 Result = LegalizeOp(Result);
3172 break;
3173 }
3174
3175 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00003176 MVT::ValueType NVT =
3177 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3178 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3179 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3180 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003181 break;
3182 }
Evan Cheng912095b2007-01-04 21:56:39 +00003183 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003184 break;
3185
Nate Begeman551bf3f2006-02-17 05:43:56 +00003186 case ISD::ADDC:
3187 case ISD::SUBC:
3188 Tmp1 = LegalizeOp(Node->getOperand(0));
3189 Tmp2 = LegalizeOp(Node->getOperand(1));
3190 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3191 // Since this produces two values, make sure to remember that we legalized
3192 // both of them.
3193 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3194 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3195 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003196
Nate Begeman551bf3f2006-02-17 05:43:56 +00003197 case ISD::ADDE:
3198 case ISD::SUBE:
3199 Tmp1 = LegalizeOp(Node->getOperand(0));
3200 Tmp2 = LegalizeOp(Node->getOperand(1));
3201 Tmp3 = LegalizeOp(Node->getOperand(2));
3202 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3203 // Since this produces two values, make sure to remember that we legalized
3204 // both of them.
3205 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3206 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3207 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003208
Nate Begeman419f8b62005-10-18 00:27:41 +00003209 case ISD::BUILD_PAIR: {
3210 MVT::ValueType PairTy = Node->getValueType(0);
3211 // TODO: handle the case where the Lo and Hi operands are not of legal type
3212 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3213 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3214 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003215 case TargetLowering::Promote:
3216 case TargetLowering::Custom:
3217 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003218 case TargetLowering::Legal:
3219 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3220 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3221 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003222 case TargetLowering::Expand:
3223 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3224 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3225 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3226 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3227 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003228 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003229 break;
3230 }
3231 break;
3232 }
3233
Nate Begemanc105e192005-04-06 00:23:54 +00003234 case ISD::UREM:
3235 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003236 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003237 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3238 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003239
Nate Begemanc105e192005-04-06 00:23:54 +00003240 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003241 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3242 case TargetLowering::Custom:
3243 isCustom = true;
3244 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003245 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003246 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003247 if (isCustom) {
3248 Tmp1 = TLI.LowerOperation(Result, DAG);
3249 if (Tmp1.Val) Result = Tmp1;
3250 }
Nate Begemanc105e192005-04-06 00:23:54 +00003251 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003252 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003253 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003254 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman525178c2007-10-08 18:33:35 +00003255 MVT::ValueType VT = Node->getValueType(0);
3256
3257 // See if remainder can be lowered using two-result operations.
3258 SDVTList VTs = DAG.getVTList(VT, VT);
3259 if (Node->getOpcode() == ISD::SREM &&
3260 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3261 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3262 break;
3263 }
3264 if (Node->getOpcode() == ISD::UREM &&
3265 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3266 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3267 break;
3268 }
3269
3270 if (MVT::isInteger(VT)) {
3271 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003272 TargetLowering::Legal) {
3273 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003274 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003275 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3276 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman80176312007-11-05 23:35:22 +00003277 } else if (MVT::isVector(VT)) {
3278 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003279 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003280 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003281 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003282 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3283 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003284 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003285 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003286 }
Dan Gohman0d974262007-11-06 22:11:54 +00003287 } else {
3288 assert(MVT::isFloatingPoint(VT) &&
3289 "remainder op must have integer or floating-point type");
Dan Gohman80176312007-11-05 23:35:22 +00003290 if (MVT::isVector(VT)) {
3291 Result = LegalizeOp(UnrollVectorOp(Op));
3292 } else {
3293 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003294 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3295 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman80176312007-11-05 23:35:22 +00003296 SDOperand Dummy;
3297 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3298 false/*sign irrelevant*/, Dummy);
3299 }
Nate Begemanc105e192005-04-06 00:23:54 +00003300 }
3301 break;
3302 }
Dan Gohman525178c2007-10-08 18:33:35 +00003303 }
Nate Begemanc105e192005-04-06 00:23:54 +00003304 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003305 case ISD::VAARG: {
3306 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3307 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3308
Chris Lattner5c62f332006-01-28 07:42:08 +00003309 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003310 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3311 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003312 case TargetLowering::Custom:
3313 isCustom = true;
3314 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003315 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003316 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3317 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003318 Tmp1 = Result.getValue(1);
3319
3320 if (isCustom) {
3321 Tmp2 = TLI.LowerOperation(Result, DAG);
3322 if (Tmp2.Val) {
3323 Result = LegalizeOp(Tmp2);
3324 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3325 }
3326 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003327 break;
3328 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003329 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3330 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003331 // Increment the pointer, VAList, to the next vaarg
3332 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3333 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3334 TLI.getPointerTy()));
3335 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003336 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003337 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003338 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003339 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003340 Result = LegalizeOp(Result);
3341 break;
3342 }
3343 }
3344 // Since VAARG produces two values, make sure to remember that we
3345 // legalized both of them.
3346 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00003347 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3348 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003349 }
3350
3351 case ISD::VACOPY:
3352 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3353 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3354 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3355
3356 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3357 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003358 case TargetLowering::Custom:
3359 isCustom = true;
3360 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003361 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003362 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3363 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003364 if (isCustom) {
3365 Tmp1 = TLI.LowerOperation(Result, DAG);
3366 if (Tmp1.Val) Result = Tmp1;
3367 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003368 break;
3369 case TargetLowering::Expand:
3370 // This defaults to loading a pointer from the input and storing it to the
3371 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003372 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3373 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3374 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VD, 0);
3375 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VS, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003376 break;
3377 }
3378 break;
3379
3380 case ISD::VAEND:
3381 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3382 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3383
3384 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3385 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003386 case TargetLowering::Custom:
3387 isCustom = true;
3388 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003389 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003390 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003391 if (isCustom) {
3392 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3393 if (Tmp1.Val) Result = Tmp1;
3394 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003395 break;
3396 case TargetLowering::Expand:
3397 Result = Tmp1; // Default to a no-op, return the chain
3398 break;
3399 }
3400 break;
3401
3402 case ISD::VASTART:
3403 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3404 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3405
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003406 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3407
Nate Begemanacc398c2006-01-25 18:21:52 +00003408 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3409 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003410 case TargetLowering::Legal: break;
3411 case TargetLowering::Custom:
3412 Tmp1 = TLI.LowerOperation(Result, DAG);
3413 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003414 break;
3415 }
3416 break;
3417
Nate Begeman35ef9132006-01-11 21:21:00 +00003418 case ISD::ROTL:
3419 case ISD::ROTR:
3420 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3421 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003422 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003423 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3424 default:
3425 assert(0 && "ROTL/ROTR legalize operation not supported");
3426 break;
3427 case TargetLowering::Legal:
3428 break;
3429 case TargetLowering::Custom:
3430 Tmp1 = TLI.LowerOperation(Result, DAG);
3431 if (Tmp1.Val) Result = Tmp1;
3432 break;
3433 case TargetLowering::Promote:
3434 assert(0 && "Do not know how to promote ROTL/ROTR");
3435 break;
3436 case TargetLowering::Expand:
3437 assert(0 && "Do not know how to expand ROTL/ROTR");
3438 break;
3439 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003440 break;
3441
Nate Begemand88fc032006-01-14 03:14:10 +00003442 case ISD::BSWAP:
3443 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3444 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003445 case TargetLowering::Custom:
3446 assert(0 && "Cannot custom legalize this yet!");
3447 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003448 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003449 break;
3450 case TargetLowering::Promote: {
3451 MVT::ValueType OVT = Tmp1.getValueType();
3452 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00003453 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00003454
Chris Lattner456a93a2006-01-28 07:39:30 +00003455 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3456 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3457 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3458 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3459 break;
3460 }
3461 case TargetLowering::Expand:
3462 Result = ExpandBSWAP(Tmp1);
3463 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003464 }
3465 break;
3466
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003467 case ISD::CTPOP:
3468 case ISD::CTTZ:
3469 case ISD::CTLZ:
3470 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3471 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003472 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003473 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003474 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003475 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003476 TargetLowering::Custom) {
3477 Tmp1 = TLI.LowerOperation(Result, DAG);
3478 if (Tmp1.Val) {
3479 Result = Tmp1;
3480 }
Scott Michel910b66d2007-07-30 21:00:31 +00003481 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003482 break;
3483 case TargetLowering::Promote: {
3484 MVT::ValueType OVT = Tmp1.getValueType();
3485 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003486
3487 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003488 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3489 // Perform the larger operation, then subtract if needed.
3490 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003491 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003492 case ISD::CTPOP:
3493 Result = Tmp1;
3494 break;
3495 case ISD::CTTZ:
3496 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003497 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003498 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003499 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003500 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00003501 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003502 break;
3503 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003504 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003505 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003506 DAG.getConstant(MVT::getSizeInBits(NVT) -
3507 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003508 break;
3509 }
3510 break;
3511 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003512 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003513 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003514 break;
3515 }
3516 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003517
Chris Lattner2c8086f2005-04-02 05:00:07 +00003518 // Unary operators
3519 case ISD::FABS:
3520 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003521 case ISD::FSQRT:
3522 case ISD::FSIN:
3523 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003524 Tmp1 = LegalizeOp(Node->getOperand(0));
3525 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003526 case TargetLowering::Promote:
3527 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003528 isCustom = true;
3529 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003530 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003531 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003532 if (isCustom) {
3533 Tmp1 = TLI.LowerOperation(Result, DAG);
3534 if (Tmp1.Val) Result = Tmp1;
3535 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003536 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003537 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003538 switch (Node->getOpcode()) {
3539 default: assert(0 && "Unreachable!");
3540 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003541 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3542 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003543 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003544 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003545 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003546 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3547 MVT::ValueType VT = Node->getValueType(0);
3548 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003549 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003550 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3551 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003552 break;
3553 }
3554 case ISD::FSQRT:
3555 case ISD::FSIN:
3556 case ISD::FCOS: {
3557 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003558
3559 // Expand unsupported unary vector operators by unrolling them.
3560 if (MVT::isVector(VT)) {
3561 Result = LegalizeOp(UnrollVectorOp(Op));
3562 break;
3563 }
3564
Evan Cheng56966222007-01-12 02:11:51 +00003565 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003566 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003567 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003568 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3569 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003570 break;
3571 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003572 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3573 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003574 break;
3575 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003576 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3577 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003578 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003579 default: assert(0 && "Unreachable!");
3580 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003581 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003582 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3583 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003584 break;
3585 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003586 }
3587 break;
3588 }
3589 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003590 case ISD::FPOWI: {
Dan Gohman82669522007-10-11 23:57:53 +00003591 MVT::ValueType VT = Node->getValueType(0);
3592
3593 // Expand unsupported unary vector operators by unrolling them.
3594 if (MVT::isVector(VT)) {
3595 Result = LegalizeOp(UnrollVectorOp(Op));
3596 break;
3597 }
3598
3599 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003600 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3601 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003602 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003603 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3604 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003605 break;
3606 }
Chris Lattner35481892005-12-23 00:16:34 +00003607 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003608 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003609 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3610 Node->getValueType(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003611 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3612 // The input has to be a vector type, we have to either scalarize it, pack
3613 // it, or convert it based on whether the input vector type is legal.
3614 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesene5269622007-10-20 00:07:52 +00003615 int InIx = Node->getOperand(0).ResNo;
3616 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3617 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohman7f321562007-06-25 16:23:39 +00003618
3619 // Figure out if there is a simple type corresponding to this Vector
3620 // type. If so, convert to the vector type.
3621 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3622 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003623 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003624 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3625 LegalizeOp(Node->getOperand(0)));
3626 break;
3627 } else if (NumElems == 1) {
3628 // Turn this into a bit convert of the scalar input.
3629 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3630 ScalarizeVectorOp(Node->getOperand(0)));
3631 break;
3632 } else {
3633 // FIXME: UNIMP! Store then reload
3634 assert(0 && "Cast from unsupported vector type not implemented yet!");
3635 }
Chris Lattner67993f72006-01-23 07:30:46 +00003636 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003637 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3638 Node->getOperand(0).getValueType())) {
3639 default: assert(0 && "Unknown operation action!");
3640 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003641 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3642 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003643 break;
3644 case TargetLowering::Legal:
3645 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003646 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003647 break;
3648 }
3649 }
3650 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003651
Chris Lattner2c8086f2005-04-02 05:00:07 +00003652 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003653 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003654 case ISD::UINT_TO_FP: {
3655 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003656 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3657 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003658 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003659 Node->getOperand(0).getValueType())) {
3660 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003661 case TargetLowering::Custom:
3662 isCustom = true;
3663 // FALLTHROUGH
3664 case TargetLowering::Legal:
3665 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003666 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003667 if (isCustom) {
3668 Tmp1 = TLI.LowerOperation(Result, DAG);
3669 if (Tmp1.Val) Result = Tmp1;
3670 }
3671 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003672 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003673 Result = ExpandLegalINT_TO_FP(isSigned,
3674 LegalizeOp(Node->getOperand(0)),
3675 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003676 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003677 case TargetLowering::Promote:
3678 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3679 Node->getValueType(0),
3680 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003681 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003682 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003683 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003684 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003685 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3686 Node->getValueType(0), Node->getOperand(0));
3687 break;
3688 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003689 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003690 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003691 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3692 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003693 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003694 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3695 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003696 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003697 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3698 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003699 break;
3700 }
3701 break;
3702 }
3703 case ISD::TRUNCATE:
3704 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3705 case Legal:
3706 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003707 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003708 break;
3709 case Expand:
3710 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3711
3712 // Since the result is legal, we should just be able to truncate the low
3713 // part of the source.
3714 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3715 break;
3716 case Promote:
3717 Result = PromoteOp(Node->getOperand(0));
3718 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3719 break;
3720 }
3721 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003722
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003723 case ISD::FP_TO_SINT:
3724 case ISD::FP_TO_UINT:
3725 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3726 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003727 Tmp1 = LegalizeOp(Node->getOperand(0));
3728
Chris Lattner1618beb2005-07-29 00:11:56 +00003729 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3730 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003731 case TargetLowering::Custom:
3732 isCustom = true;
3733 // FALLTHROUGH
3734 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003735 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003736 if (isCustom) {
3737 Tmp1 = TLI.LowerOperation(Result, DAG);
3738 if (Tmp1.Val) Result = Tmp1;
3739 }
3740 break;
3741 case TargetLowering::Promote:
3742 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3743 Node->getOpcode() == ISD::FP_TO_SINT);
3744 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003745 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003746 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3747 SDOperand True, False;
3748 MVT::ValueType VT = Node->getOperand(0).getValueType();
3749 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00003750 const uint64_t zero[] = {0, 0};
3751 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003752 APInt x = APInt::getSignBit(MVT::getSizeInBits(NVT));
3753 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003754 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begemand2558e32005-08-14 01:20:53 +00003755 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3756 Node->getOperand(0), Tmp2, ISD::SETLT);
3757 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3758 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003759 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003760 Tmp2));
3761 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003762 DAG.getConstant(x, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003763 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3764 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003765 } else {
3766 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3767 }
3768 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003769 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003770 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003771 case Expand: {
Evan Cheng6af00d52006-12-13 01:57:55 +00003772 MVT::ValueType VT = Op.getValueType();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003773 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003774 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003775 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003776 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3777 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3778 Node->getOperand(0), DAG.getValueType(MVT::f64));
3779 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3780 DAG.getIntPtrConstant(1));
3781 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3782 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003783 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3784 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3785 Tmp2 = DAG.getConstantFP(apf, OVT);
3786 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3787 // FIXME: generated code sucks.
3788 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3789 DAG.getNode(ISD::ADD, MVT::i32,
3790 DAG.getNode(ISD::FP_TO_SINT, VT,
3791 DAG.getNode(ISD::FSUB, OVT,
3792 Node->getOperand(0), Tmp2)),
3793 DAG.getConstant(0x80000000, MVT::i32)),
3794 DAG.getNode(ISD::FP_TO_SINT, VT,
3795 Node->getOperand(0)),
3796 DAG.getCondCode(ISD::SETGE));
3797 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003798 break;
3799 }
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003800 // Convert f32 / f64 to i32 / i64.
Evan Cheng56966222007-01-12 02:11:51 +00003801 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003802 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003803 case ISD::FP_TO_SINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003804 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003805 LC = (VT == MVT::i32)
3806 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003807 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003808 LC = (VT == MVT::i32)
3809 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003810 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003811 assert(VT == MVT::i64);
Dale Johannesen161e8972007-10-05 20:04:43 +00003812 LC = RTLIB::FPTOSINT_F80_I64;
3813 }
3814 else if (OVT == MVT::ppcf128) {
3815 assert(VT == MVT::i64);
3816 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003817 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003818 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003819 }
3820 case ISD::FP_TO_UINT: {
Dale Johannesen73328d12007-09-19 23:55:34 +00003821 if (OVT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003822 LC = (VT == MVT::i32)
3823 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003824 else if (OVT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00003825 LC = (VT == MVT::i32)
3826 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00003827 else if (OVT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003828 LC = (VT == MVT::i32)
Dale Johannesen161e8972007-10-05 20:04:43 +00003829 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3830 }
3831 else if (OVT == MVT::ppcf128) {
3832 assert(VT == MVT::i64);
3833 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00003834 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003835 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003836 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003837 default: assert(0 && "Unreachable!");
3838 }
3839 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003840 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3841 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003842 break;
3843 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003844 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003845 Tmp1 = PromoteOp(Node->getOperand(0));
3846 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3847 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003848 break;
3849 }
3850 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003851
Chris Lattnerf2670a82008-01-16 06:57:07 +00003852 case ISD::FP_EXTEND: {
Chris Lattner0bd48932008-01-17 07:00:52 +00003853 MVT::ValueType DstVT = Op.getValueType();
3854 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3855 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3856 // The only other way we can lower this is to turn it into a STORE,
3857 // LOAD pair, targetting a temporary location (a stack slot).
3858 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3859 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00003860 }
3861 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3862 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3863 case Legal:
3864 Tmp1 = LegalizeOp(Node->getOperand(0));
3865 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3866 break;
3867 case Promote:
3868 Tmp1 = PromoteOp(Node->getOperand(0));
3869 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3870 break;
3871 }
3872 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003873 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003874 case ISD::FP_ROUND: {
Chris Lattner0bd48932008-01-17 07:00:52 +00003875 MVT::ValueType DstVT = Op.getValueType();
3876 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3877 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3878 if (SrcVT == MVT::ppcf128) {
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003879 SDOperand Lo;
3880 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00003881 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003882 if (DstVT!=MVT::f64)
3883 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00003884 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00003885 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003886 // The only other way we can lower this is to turn it into a STORE,
3887 // LOAD pair, targetting a temporary location (a stack slot).
3888 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3889 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00003890 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00003891 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3892 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3893 case Legal:
3894 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003895 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003896 break;
3897 case Promote:
3898 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003899 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3900 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003901 break;
3902 }
3903 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003904 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003905 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003906 case ISD::ZERO_EXTEND:
3907 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003908 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003909 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003910 case Legal:
3911 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel0123b7d2008-02-15 23:05:48 +00003912 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3913 TargetLowering::Custom) {
3914 Tmp2 = TLI.LowerOperation(Result, DAG);
3915 if (Tmp2.Val) {
3916 Tmp1 = Tmp2;
3917 }
3918 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003919 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003920 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003921 case Promote:
3922 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003923 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003924 Tmp1 = PromoteOp(Node->getOperand(0));
3925 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003926 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003927 case ISD::ZERO_EXTEND:
3928 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003929 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003930 Result = DAG.getZeroExtendInReg(Result,
3931 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003932 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003933 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003934 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003935 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003936 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003937 Result,
3938 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003939 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003940 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003941 }
3942 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003943 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003944 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003945 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003946 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003947
3948 // If this operation is not supported, convert it to a shl/shr or load/store
3949 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003950 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3951 default: assert(0 && "This action not supported for this op yet!");
3952 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003953 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003954 break;
3955 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003956 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003957 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003958 // NOTE: we could fall back on load/store here too for targets without
3959 // SAR. However, it is doubtful that any exist.
3960 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3961 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003962 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003963 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3964 Node->getOperand(0), ShiftCst);
3965 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3966 Result, ShiftCst);
3967 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003968 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003969 // EXTLOAD pair, targetting a temporary location (a stack slot).
3970
3971 // NOTE: there is a choice here between constantly creating new stack
3972 // slots and always reusing the same one. We currently always create
3973 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00003974 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3975 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00003976 } else {
3977 assert(0 && "Unknown op");
3978 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003979 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003980 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003981 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003982 }
Duncan Sands36397f52007-07-27 12:58:54 +00003983 case ISD::TRAMPOLINE: {
3984 SDOperand Ops[6];
3985 for (unsigned i = 0; i != 6; ++i)
3986 Ops[i] = LegalizeOp(Node->getOperand(i));
3987 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3988 // The only option for this node is to custom lower it.
3989 Result = TLI.LowerOperation(Result, DAG);
3990 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003991
3992 // Since trampoline produces two values, make sure to remember that we
3993 // legalized both of them.
3994 Tmp1 = LegalizeOp(Result.getValue(1));
3995 Result = LegalizeOp(Result);
3996 AddLegalizedOperand(SDOperand(Node, 0), Result);
3997 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3998 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003999 }
Dan Gohman1a024862008-01-31 00:41:03 +00004000 case ISD::FLT_ROUNDS_: {
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004001 MVT::ValueType VT = Node->getValueType(0);
4002 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4003 default: assert(0 && "This action not supported for this op yet!");
4004 case TargetLowering::Custom:
4005 Result = TLI.LowerOperation(Op, DAG);
4006 if (Result.Val) break;
4007 // Fall Thru
4008 case TargetLowering::Legal:
4009 // If this operation is not supported, lower it to constant 1
4010 Result = DAG.getConstant(1, VT);
4011 break;
4012 }
4013 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004014 case ISD::TRAP: {
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004015 MVT::ValueType VT = Node->getValueType(0);
4016 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4017 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004018 case TargetLowering::Legal:
4019 Tmp1 = LegalizeOp(Node->getOperand(0));
4020 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4021 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004022 case TargetLowering::Custom:
4023 Result = TLI.LowerOperation(Op, DAG);
4024 if (Result.Val) break;
4025 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004026 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004027 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004028 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004029 TargetLowering::ArgListTy Args;
4030 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004031 TLI.LowerCallTo(Tmp1, Type::VoidTy,
4032 false, false, false, CallingConv::C, false,
Chris Lattner034f12e2008-01-15 22:09:33 +00004033 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
4034 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004035 Result = CallResult.second;
4036 break;
4037 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004038 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004039 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00004040 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004041
Chris Lattner4ddd2832006-04-08 04:13:17 +00004042 assert(Result.getValueType() == Op.getValueType() &&
4043 "Bad legalization!");
4044
Chris Lattner456a93a2006-01-28 07:39:30 +00004045 // Make sure that the generated code is itself legal.
4046 if (Result != Op)
4047 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004048
Chris Lattner45982da2005-05-12 16:53:42 +00004049 // Note that LegalizeOp may be reentered even from single-use nodes, which
4050 // means that we always must cache transformed nodes.
4051 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004052 return Result;
4053}
4054
Chris Lattner8b6fa222005-01-15 22:16:26 +00004055/// PromoteOp - Given an operation that produces a value in an invalid type,
4056/// promote it to compute the value into a larger type. The produced value will
4057/// have the correct bits for the low portion of the register, but no guarantee
4058/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00004059SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
4060 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004061 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004062 assert(getTypeAction(VT) == Promote &&
4063 "Caller should expand or legalize operands that are not promotable!");
4064 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
4065 "Cannot promote to smaller type!");
4066
Chris Lattner03c85462005-01-15 05:21:40 +00004067 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00004068 SDOperand Result;
4069 SDNode *Node = Op.Val;
4070
Chris Lattner40030bf2007-02-04 01:17:38 +00004071 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004072 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004073
Chris Lattner03c85462005-01-15 05:21:40 +00004074 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004075 case ISD::CopyFromReg:
4076 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004077 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004078#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004079 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004080#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004081 assert(0 && "Do not know how to promote this operator!");
4082 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004083 case ISD::UNDEF:
4084 Result = DAG.getNode(ISD::UNDEF, NVT);
4085 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004086 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004087 if (VT != MVT::i1)
4088 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4089 else
4090 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004091 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4092 break;
4093 case ISD::ConstantFP:
4094 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4095 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4096 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004097
Chris Lattner82fbfb62005-01-18 02:59:52 +00004098 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004099 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004100 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
4101 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004102 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00004103
Chris Lattner03c85462005-01-15 05:21:40 +00004104 case ISD::TRUNCATE:
4105 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4106 case Legal:
4107 Result = LegalizeOp(Node->getOperand(0));
4108 assert(Result.getValueType() >= NVT &&
4109 "This truncation doesn't make sense!");
4110 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4111 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4112 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004113 case Promote:
4114 // The truncation is not required, because we don't guarantee anything
4115 // about high bits anyway.
4116 Result = PromoteOp(Node->getOperand(0));
4117 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004118 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004119 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4120 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004121 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004122 }
4123 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004124 case ISD::SIGN_EXTEND:
4125 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004126 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004127 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4128 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4129 case Legal:
4130 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004131 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004132 break;
4133 case Promote:
4134 // Promote the reg if it's smaller.
4135 Result = PromoteOp(Node->getOperand(0));
4136 // The high bits are not guaranteed to be anything. Insert an extend.
4137 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004138 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004139 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004140 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004141 Result = DAG.getZeroExtendInReg(Result,
4142 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004143 break;
4144 }
4145 break;
Chris Lattner35481892005-12-23 00:16:34 +00004146 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004147 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4148 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004149 Result = PromoteOp(Result);
4150 break;
4151
Chris Lattner8b6fa222005-01-15 22:16:26 +00004152 case ISD::FP_EXTEND:
4153 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4154 case ISD::FP_ROUND:
4155 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4156 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4157 case Promote: assert(0 && "Unreachable with 2 FP types!");
4158 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004159 if (Node->getConstantOperandVal(1) == 0) {
4160 // Input is legal? Do an FP_ROUND_INREG.
4161 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4162 DAG.getValueType(VT));
4163 } else {
4164 // Just remove the truncate, it isn't affecting the value.
4165 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4166 Node->getOperand(1));
4167 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004168 break;
4169 }
4170 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004171 case ISD::SINT_TO_FP:
4172 case ISD::UINT_TO_FP:
4173 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4174 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004175 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004176 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004177 break;
4178
4179 case Promote:
4180 Result = PromoteOp(Node->getOperand(0));
4181 if (Node->getOpcode() == ISD::SINT_TO_FP)
4182 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004183 Result,
4184 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004185 else
Chris Lattner23993562005-04-13 02:38:47 +00004186 Result = DAG.getZeroExtendInReg(Result,
4187 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004188 // No extra round required here.
4189 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004190 break;
4191 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004192 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4193 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004194 // Round if we cannot tolerate excess precision.
4195 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004196 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4197 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004198 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004199 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004200 break;
4201
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004202 case ISD::SIGN_EXTEND_INREG:
4203 Result = PromoteOp(Node->getOperand(0));
4204 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4205 Node->getOperand(1));
4206 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004207 case ISD::FP_TO_SINT:
4208 case ISD::FP_TO_UINT:
4209 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4210 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004211 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004212 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004213 break;
4214 case Promote:
4215 // The input result is prerounded, so we don't have to do anything
4216 // special.
4217 Tmp1 = PromoteOp(Node->getOperand(0));
4218 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004219 }
Nate Begemand2558e32005-08-14 01:20:53 +00004220 // If we're promoting a UINT to a larger size, check to see if the new node
4221 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4222 // we can use that instead. This allows us to generate better code for
4223 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4224 // legal, such as PowerPC.
4225 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004226 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004227 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4228 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004229 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4230 } else {
4231 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4232 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004233 break;
4234
Chris Lattner2c8086f2005-04-02 05:00:07 +00004235 case ISD::FABS:
4236 case ISD::FNEG:
4237 Tmp1 = PromoteOp(Node->getOperand(0));
4238 assert(Tmp1.getValueType() == NVT);
4239 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4240 // NOTE: we do not have to do any extra rounding here for
4241 // NoExcessFPPrecision, because we know the input will have the appropriate
4242 // precision, and these operations don't modify precision at all.
4243 break;
4244
Chris Lattnerda6ba872005-04-28 21:44:33 +00004245 case ISD::FSQRT:
4246 case ISD::FSIN:
4247 case ISD::FCOS:
4248 Tmp1 = PromoteOp(Node->getOperand(0));
4249 assert(Tmp1.getValueType() == NVT);
4250 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004251 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004252 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4253 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004254 break;
4255
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004256 case ISD::FPOWI: {
4257 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4258 // directly as well, which may be better.
4259 Tmp1 = PromoteOp(Node->getOperand(0));
4260 assert(Tmp1.getValueType() == NVT);
4261 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4262 if (NoExcessFPPrecision)
4263 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4264 DAG.getValueType(VT));
4265 break;
4266 }
4267
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004268 case ISD::ATOMIC_LCS: {
4269 Tmp2 = PromoteOp(Node->getOperand(2));
4270 Tmp3 = PromoteOp(Node->getOperand(3));
4271 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4272 Node->getOperand(1), Tmp2, Tmp3,
4273 cast<AtomicSDNode>(Node)->getVT());
4274 // Remember that we legalized the chain.
4275 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4276 break;
4277 }
4278 case ISD::ATOMIC_LAS:
4279 case ISD::ATOMIC_SWAP: {
4280 Tmp2 = PromoteOp(Node->getOperand(2));
4281 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4282 Node->getOperand(1), Tmp2,
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
Chris Lattner03c85462005-01-15 05:21:40 +00004289 case ISD::AND:
4290 case ISD::OR:
4291 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004292 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004293 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004294 case ISD::MUL:
4295 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004296 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004297 // that too is okay if they are integer operations.
4298 Tmp1 = PromoteOp(Node->getOperand(0));
4299 Tmp2 = PromoteOp(Node->getOperand(1));
4300 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4301 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004302 break;
4303 case ISD::FADD:
4304 case ISD::FSUB:
4305 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004306 Tmp1 = PromoteOp(Node->getOperand(0));
4307 Tmp2 = PromoteOp(Node->getOperand(1));
4308 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4309 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4310
4311 // Floating point operations will give excess precision that we may not be
4312 // able to tolerate. If we DO allow excess precision, just leave it,
4313 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004314 // FIXME: Why would we need to round FP ops more than integer ones?
4315 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004316 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004317 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4318 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004319 break;
4320
Chris Lattner8b6fa222005-01-15 22:16:26 +00004321 case ISD::SDIV:
4322 case ISD::SREM:
4323 // These operators require that their input be sign extended.
4324 Tmp1 = PromoteOp(Node->getOperand(0));
4325 Tmp2 = PromoteOp(Node->getOperand(1));
4326 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004327 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4328 DAG.getValueType(VT));
4329 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4330 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004331 }
4332 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4333
4334 // Perform FP_ROUND: this is probably overly pessimistic.
4335 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004336 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4337 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004338 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004339 case ISD::FDIV:
4340 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004341 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004342 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004343 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004344 case Expand: assert(0 && "not implemented");
4345 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4346 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004347 }
4348 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004349 case Expand: assert(0 && "not implemented");
4350 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4351 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004352 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004353 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4354
4355 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004356 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004357 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4358 DAG.getValueType(VT));
4359 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004360
4361 case ISD::UDIV:
4362 case ISD::UREM:
4363 // These operators require that their input be zero extended.
4364 Tmp1 = PromoteOp(Node->getOperand(0));
4365 Tmp2 = PromoteOp(Node->getOperand(1));
4366 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004367 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4368 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004369 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4370 break;
4371
4372 case ISD::SHL:
4373 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004374 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004375 break;
4376 case ISD::SRA:
4377 // The input value must be properly sign extended.
4378 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004379 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4380 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004381 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004382 break;
4383 case ISD::SRL:
4384 // The input value must be properly zero extended.
4385 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004386 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004387 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004388 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004389
4390 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004391 Tmp1 = Node->getOperand(0); // Get the chain.
4392 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004393 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4394 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4395 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4396 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004397 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4398 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004399 // Increment the pointer, VAList, to the next vaarg
4400 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4401 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4402 TLI.getPointerTy()));
4403 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004404 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004405 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004406 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004407 }
4408 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004409 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004410 break;
4411
Evan Cheng466685d2006-10-09 20:57:25 +00004412 case ISD::LOAD: {
4413 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004414 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4415 ? ISD::EXTLOAD : LD->getExtensionType();
4416 Result = DAG.getExtLoad(ExtType, NVT,
4417 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004418 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004419 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004420 LD->isVolatile(),
4421 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004422 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004423 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004424 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004425 }
Chris Lattner03c85462005-01-15 05:21:40 +00004426 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00004427 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4428 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00004429 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004430 break;
Nate Begeman9373a812005-08-10 20:51:12 +00004431 case ISD::SELECT_CC:
4432 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4433 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4434 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004435 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004436 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004437 case ISD::BSWAP:
4438 Tmp1 = Node->getOperand(0);
4439 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4440 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4441 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004442 DAG.getConstant(MVT::getSizeInBits(NVT) -
4443 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00004444 TLI.getShiftAmountTy()));
4445 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004446 case ISD::CTPOP:
4447 case ISD::CTTZ:
4448 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004449 // Zero extend the argument
4450 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004451 // Perform the larger operation, then subtract if needed.
4452 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004453 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004454 case ISD::CTPOP:
4455 Result = Tmp1;
4456 break;
4457 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004458 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00004459 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004460 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4461 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004462 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004463 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004464 break;
4465 case ISD::CTLZ:
4466 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004467 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004468 DAG.getConstant(MVT::getSizeInBits(NVT) -
4469 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004470 break;
4471 }
4472 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004473 case ISD::EXTRACT_SUBVECTOR:
4474 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004475 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004476 case ISD::EXTRACT_VECTOR_ELT:
4477 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4478 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004479 }
4480
4481 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004482
4483 // Make sure the result is itself legal.
4484 Result = LegalizeOp(Result);
4485
4486 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004487 AddPromotedOperand(Op, Result);
4488 return Result;
4489}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004490
Dan Gohman7f321562007-06-25 16:23:39 +00004491/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4492/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4493/// based on the vector type. The return type of this matches the element type
4494/// of the vector, which may not be legal for the target.
4495SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004496 // We know that operand #0 is the Vec vector. If the index is a constant
4497 // or if the invec is a supported hardware type, we can use it. Otherwise,
4498 // lower to a store then an indexed load.
4499 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004500 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004501
Dan Gohmane40c7b02007-09-24 15:54:53 +00004502 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00004503 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00004504
Dan Gohman7f321562007-06-25 16:23:39 +00004505 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4506 default: assert(0 && "This action is not supported yet!");
4507 case TargetLowering::Custom: {
4508 Vec = LegalizeOp(Vec);
4509 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4510 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4511 if (Tmp3.Val)
4512 return Tmp3;
4513 break;
4514 }
4515 case TargetLowering::Legal:
4516 if (isTypeLegal(TVT)) {
4517 Vec = LegalizeOp(Vec);
4518 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004519 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004520 }
4521 break;
4522 case TargetLowering::Expand:
4523 break;
4524 }
4525
4526 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004527 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004528 Op = ScalarizeVectorOp(Vec);
4529 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00004530 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00004531 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00004532 SDOperand Lo, Hi;
4533 SplitVectorOp(Vec, Lo, Hi);
Nate Begeman55030dc2008-01-29 02:24:00 +00004534 if (CIdx->getValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00004535 Vec = Lo;
4536 } else {
4537 Vec = Hi;
Nate Begeman55030dc2008-01-29 02:24:00 +00004538 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00004539 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004540 }
Dan Gohman7f321562007-06-25 16:23:39 +00004541
Chris Lattner15972212006-03-31 17:55:51 +00004542 // It's now an extract from the appropriate high or low part. Recurse.
4543 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004544 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004545 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004546 // Store the value to a temporary stack slot, then LOAD the scalar
4547 // element back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004548 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00004549 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4550
4551 // Add the offset to the index.
4552 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4553 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4554 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004555
4556 if (MVT::getSizeInBits(Idx.getValueType()) >
4557 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004558 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004559 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004560 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004561
Dan Gohman7f321562007-06-25 16:23:39 +00004562 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4563
4564 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004565 }
Dan Gohman7f321562007-06-25 16:23:39 +00004566 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004567}
4568
Dan Gohman7f321562007-06-25 16:23:39 +00004569/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004570/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00004571SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004572 // We know that operand #0 is the Vec vector. For now we assume the index
4573 // is a constant and that the extracted result is a supported hardware type.
4574 SDOperand Vec = Op.getOperand(0);
4575 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4576
Dan Gohman7f321562007-06-25 16:23:39 +00004577 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004578
4579 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4580 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004581 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004582 }
4583
4584 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4585 SDOperand Lo, Hi;
4586 SplitVectorOp(Vec, Lo, Hi);
4587 if (CIdx->getValue() < NumElems/2) {
4588 Vec = Lo;
4589 } else {
4590 Vec = Hi;
4591 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4592 }
4593
4594 // It's now an extract from the appropriate high or low part. Recurse.
4595 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004596 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004597}
4598
Nate Begeman750ac1b2006-02-01 07:19:44 +00004599/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4600/// with condition CC on the current target. This usually involves legalizing
4601/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4602/// there may be no choice but to create a new SetCC node to represent the
4603/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4604/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4605void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4606 SDOperand &RHS,
4607 SDOperand &CC) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00004608 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004609
4610 switch (getTypeAction(LHS.getValueType())) {
4611 case Legal:
4612 Tmp1 = LegalizeOp(LHS); // LHS
4613 Tmp2 = LegalizeOp(RHS); // RHS
4614 break;
4615 case Promote:
4616 Tmp1 = PromoteOp(LHS); // LHS
4617 Tmp2 = PromoteOp(RHS); // RHS
4618
4619 // If this is an FP compare, the operands have already been extended.
4620 if (MVT::isInteger(LHS.getValueType())) {
4621 MVT::ValueType VT = LHS.getValueType();
4622 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4623
4624 // Otherwise, we have to insert explicit sign or zero extends. Note
4625 // that we could insert sign extends for ALL conditions, but zero extend
4626 // is cheaper on many machines (an AND instead of two shifts), so prefer
4627 // it.
4628 switch (cast<CondCodeSDNode>(CC)->get()) {
4629 default: assert(0 && "Unknown integer comparison!");
4630 case ISD::SETEQ:
4631 case ISD::SETNE:
4632 case ISD::SETUGE:
4633 case ISD::SETUGT:
4634 case ISD::SETULE:
4635 case ISD::SETULT:
4636 // ALL of these operations will work if we either sign or zero extend
4637 // the operands (including the unsigned comparisons!). Zero extend is
4638 // usually a simpler/cheaper operation, so prefer it.
4639 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4640 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4641 break;
4642 case ISD::SETGE:
4643 case ISD::SETGT:
4644 case ISD::SETLT:
4645 case ISD::SETLE:
4646 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4647 DAG.getValueType(VT));
4648 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4649 DAG.getValueType(VT));
4650 break;
4651 }
4652 }
4653 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004654 case Expand: {
4655 MVT::ValueType VT = LHS.getValueType();
4656 if (VT == MVT::f32 || VT == MVT::f64) {
4657 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004658 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004659 switch (cast<CondCodeSDNode>(CC)->get()) {
4660 case ISD::SETEQ:
4661 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004662 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004663 break;
4664 case ISD::SETNE:
4665 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004666 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004667 break;
4668 case ISD::SETGE:
4669 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004670 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004671 break;
4672 case ISD::SETLT:
4673 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004674 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004675 break;
4676 case ISD::SETLE:
4677 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004678 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004679 break;
4680 case ISD::SETGT:
4681 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004682 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004683 break;
4684 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004685 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4686 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004687 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004688 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004689 break;
4690 default:
Evan Cheng56966222007-01-12 02:11:51 +00004691 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004692 switch (cast<CondCodeSDNode>(CC)->get()) {
4693 case ISD::SETONE:
4694 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004695 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004696 // Fallthrough
4697 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004698 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004699 break;
4700 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004701 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004702 break;
4703 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004704 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004705 break;
4706 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004707 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004708 break;
Evan Cheng56966222007-01-12 02:11:51 +00004709 case ISD::SETUEQ:
4710 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004711 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004712 default: assert(0 && "Unsupported FP setcc!");
4713 }
4714 }
4715
4716 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004717 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004718 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004719 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004720 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004721 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004722 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004723 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004724 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004725 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004726 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004727 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004728 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004729 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4730 Tmp2 = SDOperand();
4731 }
4732 LHS = Tmp1;
4733 RHS = Tmp2;
4734 return;
4735 }
4736
Nate Begeman750ac1b2006-02-01 07:19:44 +00004737 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4738 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004739 ExpandOp(RHS, RHSLo, RHSHi);
4740 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4741
4742 if (VT==MVT::ppcf128) {
4743 // FIXME: This generated code sucks. We want to generate
4744 // FCMP crN, hi1, hi2
4745 // BNE crN, L:
4746 // FCMP crN, lo1, lo2
4747 // The following can be improved, but not that much.
4748 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4749 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4750 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4751 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4752 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4753 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4754 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4755 Tmp2 = SDOperand();
4756 break;
4757 }
4758
4759 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004760 case ISD::SETEQ:
4761 case ISD::SETNE:
4762 if (RHSLo == RHSHi)
4763 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4764 if (RHSCST->isAllOnesValue()) {
4765 // Comparison to -1.
4766 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4767 Tmp2 = RHSLo;
4768 break;
4769 }
4770
4771 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4772 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4773 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4774 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4775 break;
4776 default:
4777 // If this is a comparison of the sign bit, just look at the top part.
4778 // X > -1, x < 0
4779 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4780 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4781 CST->getValue() == 0) || // X < 0
4782 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4783 CST->isAllOnesValue())) { // X > -1
4784 Tmp1 = LHSHi;
4785 Tmp2 = RHSHi;
4786 break;
4787 }
4788
4789 // FIXME: This generated code sucks.
4790 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004791 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004792 default: assert(0 && "Unknown integer setcc!");
4793 case ISD::SETLT:
4794 case ISD::SETULT: LowCC = ISD::SETULT; break;
4795 case ISD::SETGT:
4796 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4797 case ISD::SETLE:
4798 case ISD::SETULE: LowCC = ISD::SETULE; break;
4799 case ISD::SETGE:
4800 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4801 }
4802
4803 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4804 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4805 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4806
4807 // NOTE: on targets without efficient SELECT of bools, we can always use
4808 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004809 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4810 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4811 false, DagCombineInfo);
4812 if (!Tmp1.Val)
4813 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4814 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4815 CCCode, false, DagCombineInfo);
4816 if (!Tmp2.Val)
Chris Lattner85dd3be2007-10-15 17:48:57 +00004817 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004818
4819 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4820 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4821 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4822 (Tmp2C && Tmp2C->getValue() == 0 &&
4823 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4824 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4825 (Tmp2C && Tmp2C->getValue() == 1 &&
4826 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4827 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4828 // low part is known false, returns high part.
4829 // For LE / GE, if high part is known false, ignore the low part.
4830 // For LT / GT, if high part is known true, ignore the low part.
4831 Tmp1 = Tmp2;
4832 Tmp2 = SDOperand();
4833 } else {
4834 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4835 ISD::SETEQ, false, DagCombineInfo);
4836 if (!Result.Val)
4837 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4838 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4839 Result, Tmp1, Tmp2));
4840 Tmp1 = Result;
4841 Tmp2 = SDOperand();
4842 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004843 }
4844 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004845 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004846 LHS = Tmp1;
4847 RHS = Tmp2;
4848}
4849
Chris Lattner1401d152008-01-16 07:45:30 +00004850/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4851/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4852/// a load from the stack slot to DestVT, extending it if needed.
4853/// The resultant code need not be legal.
4854SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4855 MVT::ValueType SlotVT,
4856 MVT::ValueType DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00004857 // Create the stack frame object.
Chris Lattner1401d152008-01-16 07:45:30 +00004858 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4859
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00004860 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00004861 int SPFI = StackPtrFI->getIndex();
4862
Chris Lattner1401d152008-01-16 07:45:30 +00004863 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4864 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4865 unsigned DestSize = MVT::getSizeInBits(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004866
Chris Lattner1401d152008-01-16 07:45:30 +00004867 // Emit a store to the stack slot. Use a truncstore if the input value is
4868 // later than DestVT.
4869 SDOperand Store;
4870 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00004871 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004872 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00004873 SPFI, SlotVT);
Chris Lattner1401d152008-01-16 07:45:30 +00004874 else {
4875 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00004876 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004877 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00004878 SPFI, SlotVT);
Chris Lattner1401d152008-01-16 07:45:30 +00004879 }
4880
Chris Lattner35481892005-12-23 00:16:34 +00004881 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00004882 if (SlotSize == DestSize)
4883 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4884
4885 assert(SlotSize < DestSize && "Unknown extension!");
4886 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Chris Lattner35481892005-12-23 00:16:34 +00004887}
4888
Chris Lattner4352cc92006-04-04 17:23:26 +00004889SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4890 // Create a vector sized/aligned stack slot, store the value to element #0,
4891 // then load the whole vector back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004892 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00004893
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00004894 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00004895 int SPFI = StackPtrFI->getIndex();
4896
Evan Cheng786225a2006-10-05 23:01:46 +00004897 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004898 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohman69de1932008-02-06 22:27:42 +00004899 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004900 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner4352cc92006-04-04 17:23:26 +00004901}
4902
4903
Chris Lattnerce872152006-03-19 06:31:19 +00004904/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004905/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004906SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4907
4908 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004909 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004910 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004911 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004912 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004913 std::map<SDOperand, std::vector<unsigned> > Values;
4914 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004915 bool isConstant = true;
4916 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4917 SplatValue.getOpcode() != ISD::UNDEF)
4918 isConstant = false;
4919
Evan Cheng033e6812006-03-24 01:17:21 +00004920 for (unsigned i = 1; i < NumElems; ++i) {
4921 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004922 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004923 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004924 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004925 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004926 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004927
4928 // If this isn't a constant element or an undef, we can't use a constant
4929 // pool load.
4930 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4931 V.getOpcode() != ISD::UNDEF)
4932 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004933 }
4934
4935 if (isOnlyLowElement) {
4936 // If the low element is an undef too, then this whole things is an undef.
4937 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4938 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4939 // Otherwise, turn this into a scalar_to_vector node.
4940 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4941 Node->getOperand(0));
4942 }
4943
Chris Lattner2eb86532006-03-24 07:29:17 +00004944 // If all elements are constants, create a load from the constant pool.
4945 if (isConstant) {
4946 MVT::ValueType VT = Node->getValueType(0);
4947 const Type *OpNTy =
4948 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4949 std::vector<Constant*> CV;
4950 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4951 if (ConstantFPSDNode *V =
4952 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004953 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004954 } else if (ConstantSDNode *V =
4955 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004956 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004957 } else {
4958 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4959 CV.push_back(UndefValue::get(OpNTy));
4960 }
4961 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004962 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004963 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00004964 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004965 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004966 }
4967
Chris Lattner87100e02006-03-20 01:52:29 +00004968 if (SplatValue.Val) { // Splat of one value?
4969 // Build the shuffle constant vector: <0, 0, 0, 0>
4970 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004971 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004972 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004973 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004974 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4975 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004976
4977 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004978 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004979 // Get the splatted value into the low element of a vector register.
4980 SDOperand LowValVec =
4981 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4982
4983 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4984 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4985 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4986 SplatMask);
4987 }
4988 }
4989
Evan Cheng033e6812006-03-24 01:17:21 +00004990 // If there are only two unique elements, we may be able to turn this into a
4991 // vector shuffle.
4992 if (Values.size() == 2) {
4993 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4994 MVT::ValueType MaskVT =
4995 MVT::getIntVectorWithNumElements(NumElems);
4996 std::vector<SDOperand> MaskVec(NumElems);
4997 unsigned i = 0;
4998 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4999 E = Values.end(); I != E; ++I) {
5000 for (std::vector<unsigned>::iterator II = I->second.begin(),
5001 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00005002 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00005003 i += NumElems;
5004 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005005 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
5006 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005007
5008 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005009 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5010 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005011 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00005012 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
5013 E = Values.end(); I != E; ++I) {
5014 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5015 I->first);
5016 Ops.push_back(Op);
5017 }
5018 Ops.push_back(ShuffleMask);
5019
5020 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005021 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
5022 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005023 }
5024 }
Chris Lattnerce872152006-03-19 06:31:19 +00005025
5026 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5027 // aligned object on the stack, store each element into it, then load
5028 // the result as a vector.
5029 MVT::ValueType VT = Node->getValueType(0);
5030 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00005031 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005032
5033 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005034 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00005035 unsigned TypeByteSize =
5036 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005037 // Store (in the right endianness) the elements to memory.
5038 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5039 // Ignore undef elements.
5040 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5041
Chris Lattner841c8822006-03-22 01:46:54 +00005042 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005043
5044 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5045 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5046
Evan Cheng786225a2006-10-05 23:01:46 +00005047 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00005048 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00005049 }
5050
5051 SDOperand StoreChain;
5052 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005053 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5054 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005055 else
5056 StoreChain = DAG.getEntryNode();
5057
5058 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00005059 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005060}
5061
Chris Lattner5b359c62005-04-02 04:00:59 +00005062void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5063 SDOperand Op, SDOperand Amt,
5064 SDOperand &Lo, SDOperand &Hi) {
5065 // Expand the subcomponents.
5066 SDOperand LHSL, LHSH;
5067 ExpandOp(Op, LHSL, LHSH);
5068
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005069 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005070 MVT::ValueType VT = LHSL.getValueType();
5071 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005072 Hi = Lo.getValue(1);
5073}
5074
5075
Chris Lattnere34b3962005-01-19 04:19:40 +00005076/// ExpandShift - Try to find a clever way to expand this shift operation out to
5077/// smaller elements. If we can't find a way that is more efficient than a
5078/// libcall on this target, return false. Otherwise, return true with the
5079/// low-parts expanded into Lo and Hi.
5080bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
5081 SDOperand &Lo, SDOperand &Hi) {
5082 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5083 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005084
Chris Lattnere34b3962005-01-19 04:19:40 +00005085 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005086 SDOperand ShAmt = LegalizeOp(Amt);
5087 MVT::ValueType ShTy = ShAmt.getValueType();
Dan Gohman91dc17b2008-02-20 16:57:27 +00005088 unsigned ShBits = MVT::getSizeInBits(ShTy);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005089 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
5090 unsigned NVTBits = MVT::getSizeInBits(NVT);
5091
Chris Lattner7a3c8552007-10-14 20:35:12 +00005092 // Handle the case when Amt is an immediate.
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005093 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5094 unsigned Cst = CN->getValue();
5095 // Expand the incoming operand to be shifted, so that we have its parts
5096 SDOperand InL, InH;
5097 ExpandOp(Op, InL, InH);
5098 switch(Opc) {
5099 case ISD::SHL:
5100 if (Cst > VTBits) {
5101 Lo = DAG.getConstant(0, NVT);
5102 Hi = DAG.getConstant(0, NVT);
5103 } else if (Cst > NVTBits) {
5104 Lo = DAG.getConstant(0, NVT);
5105 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005106 } else if (Cst == NVTBits) {
5107 Lo = DAG.getConstant(0, NVT);
5108 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005109 } else {
5110 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5111 Hi = DAG.getNode(ISD::OR, NVT,
5112 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5113 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5114 }
5115 return true;
5116 case ISD::SRL:
5117 if (Cst > VTBits) {
5118 Lo = DAG.getConstant(0, NVT);
5119 Hi = DAG.getConstant(0, NVT);
5120 } else if (Cst > NVTBits) {
5121 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5122 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005123 } else if (Cst == NVTBits) {
5124 Lo = InH;
5125 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005126 } else {
5127 Lo = DAG.getNode(ISD::OR, NVT,
5128 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5129 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5130 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5131 }
5132 return true;
5133 case ISD::SRA:
5134 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005135 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005136 DAG.getConstant(NVTBits-1, ShTy));
5137 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005138 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005139 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005140 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005141 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005142 } else if (Cst == NVTBits) {
5143 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005144 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005145 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005146 } else {
5147 Lo = DAG.getNode(ISD::OR, NVT,
5148 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5149 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5150 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5151 }
5152 return true;
5153 }
5154 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005155
5156 // Okay, the shift amount isn't constant. However, if we can tell that it is
5157 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005158 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5159 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005160 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005161
Dan Gohman9e255b72008-02-22 01:12:31 +00005162 // If we know that if any of the high bits of the shift amount are one, then
5163 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005164 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005165 // Mask out the high bit, which we know is set.
5166 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005167 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005168
5169 // Expand the incoming operand to be shifted, so that we have its parts
5170 SDOperand InL, InH;
5171 ExpandOp(Op, InL, InH);
5172 switch(Opc) {
5173 case ISD::SHL:
5174 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5175 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5176 return true;
5177 case ISD::SRL:
5178 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5179 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5180 return true;
5181 case ISD::SRA:
5182 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5183 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5184 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5185 return true;
5186 }
5187 }
5188
Dan Gohman9e255b72008-02-22 01:12:31 +00005189 // If we know that the high bits of the shift amount are all zero, then we can
5190 // do this as a couple of simple shifts.
5191 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005192 // Compute 32-amt.
5193 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5194 DAG.getConstant(NVTBits, Amt.getValueType()),
5195 Amt);
5196
5197 // Expand the incoming operand to be shifted, so that we have its parts
5198 SDOperand InL, InH;
5199 ExpandOp(Op, InL, InH);
5200 switch(Opc) {
5201 case ISD::SHL:
5202 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5203 Hi = DAG.getNode(ISD::OR, NVT,
5204 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5205 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5206 return true;
5207 case ISD::SRL:
5208 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5209 Lo = DAG.getNode(ISD::OR, NVT,
5210 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5211 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5212 return true;
5213 case ISD::SRA:
5214 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5215 Lo = DAG.getNode(ISD::OR, NVT,
5216 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5217 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5218 return true;
5219 }
5220 }
5221
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005222 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005223}
Chris Lattner77e77a62005-01-21 06:05:23 +00005224
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005225
Chris Lattner77e77a62005-01-21 06:05:23 +00005226// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5227// does not fit into a register, return the lo part and set the hi part to the
5228// by-reg argument. If it does fit into a single register, return the result
5229// and leave the Hi part unset.
5230SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00005231 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005232 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5233 // The input chain to this libcall is the entry node of the function.
5234 // Legalizing the call will automatically add the previous call to the
5235 // dependence.
5236 SDOperand InChain = DAG.getEntryNode();
5237
Chris Lattner77e77a62005-01-21 06:05:23 +00005238 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005239 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005240 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5241 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5242 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00005243 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005244 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005245 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005246 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005247 }
5248 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005249
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005250 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00005251 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005252 std::pair<SDOperand,SDOperand> CallInfo =
Duncan Sands00fee652008-02-14 17:28:50 +00005253 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5254 false, Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005255
Chris Lattner6831a812006-02-13 09:18:02 +00005256 // Legalize the call sequence, starting with the chain. This will advance
5257 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5258 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5259 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00005260 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005261 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005262 default: assert(0 && "Unknown thing");
5263 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005264 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005265 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005266 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005267 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005268 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005269 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005270 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005271}
5272
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005273
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005274/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5275///
Chris Lattner77e77a62005-01-21 06:05:23 +00005276SDOperand SelectionDAGLegalize::
5277ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005278 assert(getTypeAction(Source.getValueType()) == Expand &&
5279 "This is not an expansion!");
5280 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
5281
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005282 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00005283 assert(Source.getValueType() == MVT::i64 &&
5284 "This only works for 64-bit -> FP");
5285 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
5286 // incoming integer is set. To handle this, we dynamically test to see if
5287 // it is set, and, if so, add a fudge factor.
5288 SDOperand Lo, Hi;
5289 ExpandOp(Source, Lo, Hi);
5290
Chris Lattner66de05b2005-05-13 04:45:13 +00005291 // If this is unsigned, and not supported, first perform the conversion to
5292 // signed, then adjust the result if the sign bit is set.
5293 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
5294 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
5295
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005296 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
5297 DAG.getConstant(0, Hi.getValueType()),
5298 ISD::SETLT);
Chris Lattner0bd48932008-01-17 07:00:52 +00005299 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005300 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5301 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005302 uint64_t FF = 0x5f800000ULL;
5303 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005304 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005305
Chris Lattner5839bf22005-08-26 17:15:30 +00005306 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00005307 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5308 SDOperand FudgeInReg;
5309 if (DestTy == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005310 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005311 PseudoSourceValue::getConstantPool(), 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005312 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005313 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005314 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005315 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005316 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman69de1932008-02-06 22:27:42 +00005317 MVT::f32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005318 else
5319 assert(0 && "Unexpected conversion");
5320
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005321 MVT::ValueType SCVT = SignedConv.getValueType();
5322 if (SCVT != DestTy) {
5323 // Destination type needs to be expanded as well. The FADD now we are
5324 // constructing will be expanded into a libcall.
5325 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
5326 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
5327 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
5328 SignedConv, SignedConv.getValue(1));
5329 }
5330 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5331 }
Chris Lattner473a9902005-09-29 06:44:39 +00005332 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005333 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005334
Chris Lattnera88a2602005-05-14 05:33:54 +00005335 // Check to see if the target has a custom way to lower this. If so, use it.
5336 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
5337 default: assert(0 && "This action not implemented for this operation!");
5338 case TargetLowering::Legal:
5339 case TargetLowering::Expand:
5340 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005341 case TargetLowering::Custom: {
5342 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5343 Source), DAG);
5344 if (NV.Val)
5345 return LegalizeOp(NV);
5346 break; // The target decided this was legal after all
5347 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005348 }
5349
Chris Lattner13689e22005-05-12 07:00:44 +00005350 // Expand the source, then glue it back together for the call. We must expand
5351 // the source in case it is shared (this pass of legalize must traverse it).
5352 SDOperand SrcLo, SrcHi;
5353 ExpandOp(Source, SrcLo, SrcHi);
5354 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
5355
Evan Cheng56966222007-01-12 02:11:51 +00005356 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005357 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005358 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005359 else {
5360 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00005361 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005362 }
Chris Lattner6831a812006-02-13 09:18:02 +00005363
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005364 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00005365 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5366 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00005367 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5368 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00005369}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005370
Chris Lattner22cde6a2006-01-28 08:25:58 +00005371/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5372/// INT_TO_FP operation of the specified operand when the target requests that
5373/// we expand it. At this point, we know that the result and operand types are
5374/// legal for the target.
5375SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5376 SDOperand Op0,
5377 MVT::ValueType DestVT) {
5378 if (Op0.getValueType() == MVT::i32) {
5379 // simple 32-bit [signed|unsigned] integer to float/double expansion
5380
Chris Lattner23594d42008-01-16 07:03:22 +00005381 // Get the stack frame index of a 8 byte buffer.
5382 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5383
Chris Lattner22cde6a2006-01-28 08:25:58 +00005384 // word offset constant for Hi/Lo address computation
5385 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5386 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00005387 SDOperand Hi = StackSlot;
5388 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5389 if (TLI.isLittleEndian())
5390 std::swap(Hi, Lo);
5391
Chris Lattner22cde6a2006-01-28 08:25:58 +00005392 // if signed map to unsigned space
5393 SDOperand Op0Mapped;
5394 if (isSigned) {
5395 // constant used to invert sign bit (signed to unsigned mapping)
5396 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5397 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5398 } else {
5399 Op0Mapped = Op0;
5400 }
5401 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00005402 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005403 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005404 // initial hi portion of constructed double
5405 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5406 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00005407 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005408 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00005409 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005410 // FP constant to bias correct the final result
5411 SDOperand Bias = DAG.getConstantFP(isSigned ?
5412 BitsToDouble(0x4330000080000000ULL)
5413 : BitsToDouble(0x4330000000000000ULL),
5414 MVT::f64);
5415 // subtract the bias
5416 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5417 // final result
5418 SDOperand Result;
5419 // handle final rounding
5420 if (DestVT == MVT::f64) {
5421 // do nothing
5422 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005423 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00005424 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5425 DAG.getIntPtrConstant(0));
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005426 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5427 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005428 }
5429 return Result;
5430 }
5431 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5432 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5433
5434 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5435 DAG.getConstant(0, Op0.getValueType()),
5436 ISD::SETLT);
Chris Lattner0bd48932008-01-17 07:00:52 +00005437 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005438 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5439 SignSet, Four, Zero);
5440
5441 // If the sign bit of the integer is set, the large number will be treated
5442 // as a negative number. To counteract this, the dynamic code adds an
5443 // offset depending on the data type.
5444 uint64_t FF;
5445 switch (Op0.getValueType()) {
5446 default: assert(0 && "Unsupported integer type!");
5447 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5448 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5449 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5450 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5451 }
5452 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005453 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005454
5455 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5456 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5457 SDOperand FudgeInReg;
5458 if (DestVT == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005459 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005460 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005461 else {
Dan Gohman69de1932008-02-06 22:27:42 +00005462 FudgeInReg =
5463 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5464 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005465 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman69de1932008-02-06 22:27:42 +00005466 MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005467 }
5468
5469 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5470}
5471
5472/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5473/// *INT_TO_FP operation of the specified operand when the target requests that
5474/// we promote it. At this point, we know that the result and operand types are
5475/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5476/// operation that takes a larger input.
5477SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5478 MVT::ValueType DestVT,
5479 bool isSigned) {
5480 // First step, figure out the appropriate *INT_TO_FP operation to use.
5481 MVT::ValueType NewInTy = LegalOp.getValueType();
5482
5483 unsigned OpToUse = 0;
5484
5485 // Scan for the appropriate larger type to use.
5486 while (1) {
5487 NewInTy = (MVT::ValueType)(NewInTy+1);
5488 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5489
5490 // If the target supports SINT_TO_FP of this type, use it.
5491 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5492 default: break;
5493 case TargetLowering::Legal:
5494 if (!TLI.isTypeLegal(NewInTy))
5495 break; // Can't use this datatype.
5496 // FALL THROUGH.
5497 case TargetLowering::Custom:
5498 OpToUse = ISD::SINT_TO_FP;
5499 break;
5500 }
5501 if (OpToUse) break;
5502 if (isSigned) continue;
5503
5504 // If the target supports UINT_TO_FP of this type, use it.
5505 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5506 default: break;
5507 case TargetLowering::Legal:
5508 if (!TLI.isTypeLegal(NewInTy))
5509 break; // Can't use this datatype.
5510 // FALL THROUGH.
5511 case TargetLowering::Custom:
5512 OpToUse = ISD::UINT_TO_FP;
5513 break;
5514 }
5515 if (OpToUse) break;
5516
5517 // Otherwise, try a larger type.
5518 }
5519
5520 // Okay, we found the operation and type to use. Zero extend our input to the
5521 // desired type then run the operation on it.
5522 return DAG.getNode(OpToUse, DestVT,
5523 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5524 NewInTy, LegalOp));
5525}
5526
5527/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5528/// FP_TO_*INT operation of the specified operand when the target requests that
5529/// we promote it. At this point, we know that the result and operand types are
5530/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5531/// operation that returns a larger result.
5532SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5533 MVT::ValueType DestVT,
5534 bool isSigned) {
5535 // First step, figure out the appropriate FP_TO*INT operation to use.
5536 MVT::ValueType NewOutTy = DestVT;
5537
5538 unsigned OpToUse = 0;
5539
5540 // Scan for the appropriate larger type to use.
5541 while (1) {
5542 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5543 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5544
5545 // If the target supports FP_TO_SINT returning this type, use it.
5546 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5547 default: break;
5548 case TargetLowering::Legal:
5549 if (!TLI.isTypeLegal(NewOutTy))
5550 break; // Can't use this datatype.
5551 // FALL THROUGH.
5552 case TargetLowering::Custom:
5553 OpToUse = ISD::FP_TO_SINT;
5554 break;
5555 }
5556 if (OpToUse) break;
5557
5558 // If the target supports FP_TO_UINT of this type, use it.
5559 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5560 default: break;
5561 case TargetLowering::Legal:
5562 if (!TLI.isTypeLegal(NewOutTy))
5563 break; // Can't use this datatype.
5564 // FALL THROUGH.
5565 case TargetLowering::Custom:
5566 OpToUse = ISD::FP_TO_UINT;
5567 break;
5568 }
5569 if (OpToUse) break;
5570
5571 // Otherwise, try a larger type.
5572 }
5573
Chris Lattner27a6c732007-11-24 07:07:01 +00005574
5575 // Okay, we found the operation and type to use.
5576 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5577
5578 // If the operation produces an invalid type, it must be custom lowered. Use
5579 // the target lowering hooks to expand it. Just keep the low part of the
5580 // expanded operation, we know that we're truncating anyway.
5581 if (getTypeAction(NewOutTy) == Expand) {
5582 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5583 assert(Operation.Val && "Didn't return anything");
5584 }
5585
5586 // Truncate the result of the extended FP_TO_*INT operation to the desired
5587 // size.
5588 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005589}
5590
5591/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5592///
5593SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5594 MVT::ValueType VT = Op.getValueType();
5595 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5596 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5597 switch (VT) {
5598 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5599 case MVT::i16:
5600 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5601 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5602 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5603 case MVT::i32:
5604 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5605 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5606 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5607 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5608 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5609 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5610 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5611 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5612 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5613 case MVT::i64:
5614 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5615 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5616 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5617 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5618 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5619 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5620 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5621 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5622 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5623 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5624 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5625 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5626 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5627 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5628 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5629 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5630 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5631 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5632 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5633 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5634 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5635 }
5636}
5637
5638/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5639///
5640SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5641 switch (Opc) {
5642 default: assert(0 && "Cannot expand this yet!");
5643 case ISD::CTPOP: {
5644 static const uint64_t mask[6] = {
5645 0x5555555555555555ULL, 0x3333333333333333ULL,
5646 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5647 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5648 };
5649 MVT::ValueType VT = Op.getValueType();
5650 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005651 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005652 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5653 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5654 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5655 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5656 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5657 DAG.getNode(ISD::AND, VT,
5658 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5659 }
5660 return Op;
5661 }
5662 case ISD::CTLZ: {
5663 // for now, we do this:
5664 // x = x | (x >> 1);
5665 // x = x | (x >> 2);
5666 // ...
5667 // x = x | (x >>16);
5668 // x = x | (x >>32); // for 64-bit input
5669 // return popcount(~x);
5670 //
5671 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
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 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5677 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5678 }
5679 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5680 return DAG.getNode(ISD::CTPOP, VT, Op);
5681 }
5682 case ISD::CTTZ: {
5683 // for now, we use: { return popcount(~x & (x - 1)); }
5684 // unless the target has ctlz but not ctpop, in which case we use:
5685 // { return 32 - nlz(~x & (x-1)); }
5686 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5687 MVT::ValueType VT = Op.getValueType();
5688 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5689 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5690 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5691 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5692 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5693 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5694 TLI.isOperationLegal(ISD::CTLZ, VT))
5695 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005696 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005697 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5698 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5699 }
5700 }
5701}
Chris Lattnere34b3962005-01-19 04:19:40 +00005702
Chris Lattner3e928bb2005-01-07 07:47:09 +00005703/// ExpandOp - Expand the specified SDOperand into its two component pieces
5704/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5705/// LegalizeNodes map is filled in for any results that are not expanded, the
5706/// ExpandedNodes map is filled in for any results that are expanded, and the
5707/// Lo/Hi values are returned.
5708void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5709 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005710 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005711 SDNode *Node = Op.Val;
5712 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005713 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005714 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005715 "Cannot expand to FP value or to larger int value!");
5716
Chris Lattner6fdcb252005-09-02 20:32:45 +00005717 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00005718 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005719 = ExpandedNodes.find(Op);
5720 if (I != ExpandedNodes.end()) {
5721 Lo = I->second.first;
5722 Hi = I->second.second;
5723 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005724 }
5725
Chris Lattner3e928bb2005-01-07 07:47:09 +00005726 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005727 case ISD::CopyFromReg:
5728 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005729 case ISD::FP_ROUND_INREG:
5730 if (VT == MVT::ppcf128 &&
5731 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5732 TargetLowering::Custom) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005733 SDOperand SrcLo, SrcHi, Src;
5734 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5735 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5736 SDOperand Result = TLI.LowerOperation(
5737 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005738 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5739 Lo = Result.Val->getOperand(0);
5740 Hi = Result.Val->getOperand(1);
5741 break;
5742 }
5743 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005744 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005745#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005746 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005747#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005748 assert(0 && "Do not know how to expand this operator!");
5749 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00005750 case ISD::EXTRACT_ELEMENT:
5751 ExpandOp(Node->getOperand(0), Lo, Hi);
5752 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5753 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00005754 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00005755 case ISD::EXTRACT_VECTOR_ELT:
5756 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5757 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5758 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5759 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005760 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005761 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005762 Lo = DAG.getNode(ISD::UNDEF, NVT);
5763 Hi = DAG.getNode(ISD::UNDEF, NVT);
5764 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005765 case ISD::Constant: {
Dan Gohman050f5502008-03-03 22:20:46 +00005766 unsigned NVTBits = MVT::getSizeInBits(NVT);
5767 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
5768 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
5769 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005770 break;
5771 }
Evan Cheng00495212006-12-12 21:32:44 +00005772 case ISD::ConstantFP: {
5773 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005774 if (CFP->getValueType(0) == MVT::ppcf128) {
5775 APInt api = CFP->getValueAPF().convertToAPInt();
5776 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5777 MVT::f64);
5778 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5779 MVT::f64);
5780 break;
5781 }
Evan Cheng279101e2006-12-12 22:19:28 +00005782 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005783 if (getTypeAction(Lo.getValueType()) == Expand)
5784 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005785 break;
5786 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005787 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005788 // Return the operands.
5789 Lo = Node->getOperand(0);
5790 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005791 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005792
5793 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00005794 if (Node->getNumValues() == 1) {
5795 ExpandOp(Op.getOperand(0), Lo, Hi);
5796 break;
5797 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005798 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5799 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5800 Op.getValue(1).getValueType() == MVT::Other &&
5801 "unhandled MERGE_VALUES");
5802 ExpandOp(Op.getOperand(0), Lo, Hi);
5803 // Remember that we legalized the chain.
5804 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5805 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005806
5807 case ISD::SIGN_EXTEND_INREG:
5808 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005809 // sext_inreg the low part if needed.
5810 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5811
5812 // The high part gets the sign extension from the lo-part. This handles
5813 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005814 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5815 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5816 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005817 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005818
Nate Begemand88fc032006-01-14 03:14:10 +00005819 case ISD::BSWAP: {
5820 ExpandOp(Node->getOperand(0), Lo, Hi);
5821 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5822 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5823 Lo = TempLo;
5824 break;
5825 }
5826
Chris Lattneredb1add2005-05-11 04:51:16 +00005827 case ISD::CTPOP:
5828 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005829 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5830 DAG.getNode(ISD::CTPOP, NVT, Lo),
5831 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005832 Hi = DAG.getConstant(0, NVT);
5833 break;
5834
Chris Lattner39a8f332005-05-12 19:05:01 +00005835 case ISD::CTLZ: {
5836 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005837 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005838 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5839 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005840 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5841 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005842 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5843 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5844
5845 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5846 Hi = DAG.getConstant(0, NVT);
5847 break;
5848 }
5849
5850 case ISD::CTTZ: {
5851 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005852 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005853 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5854 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005855 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5856 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005857 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5858 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5859
5860 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5861 Hi = DAG.getConstant(0, NVT);
5862 break;
5863 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005864
Nate Begemanacc398c2006-01-25 18:21:52 +00005865 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005866 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5867 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005868 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5869 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5870
5871 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005872 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005873 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00005874 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00005875 std::swap(Lo, Hi);
5876 break;
5877 }
5878
Chris Lattner3e928bb2005-01-07 07:47:09 +00005879 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005880 LoadSDNode *LD = cast<LoadSDNode>(Node);
5881 SDOperand Ch = LD->getChain(); // Legalize the chain.
5882 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5883 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005884 int SVOffset = LD->getSrcValueOffset();
5885 unsigned Alignment = LD->getAlignment();
5886 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005887
Evan Cheng466685d2006-10-09 20:57:25 +00005888 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005889 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5890 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005891 if (VT == MVT::f32 || VT == MVT::f64) {
5892 // f32->i32 or f64->i64 one to one expansion.
5893 // Remember that we legalized the chain.
5894 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005895 // Recursively expand the new load.
5896 if (getTypeAction(NVT) == Expand)
5897 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005898 break;
5899 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005900
Evan Cheng466685d2006-10-09 20:57:25 +00005901 // Increment the pointer to the other half.
5902 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5903 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00005904 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005905 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00005906 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005907 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5908 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005909
Evan Cheng466685d2006-10-09 20:57:25 +00005910 // Build a factor node to remember that this load is independent of the
5911 // other one.
5912 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5913 Hi.getValue(1));
5914
5915 // Remember that we legalized the chain.
5916 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00005917 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00005918 std::swap(Lo, Hi);
5919 } else {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005920 MVT::ValueType EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005921
Dale Johannesenb6210fc2007-10-19 20:29:00 +00005922 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5923 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00005924 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5925 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005926 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005927 // Remember that we legalized the chain.
5928 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5929 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5930 break;
5931 }
Evan Cheng466685d2006-10-09 20:57:25 +00005932
5933 if (EVT == NVT)
5934 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005935 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005936 else
5937 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005938 SVOffset, EVT, isVolatile,
5939 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005940
5941 // Remember that we legalized the chain.
5942 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5943
5944 if (ExtType == ISD::SEXTLOAD) {
5945 // The high part is obtained by SRA'ing all but one of the bits of the
5946 // lo part.
5947 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5948 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5949 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5950 } else if (ExtType == ISD::ZEXTLOAD) {
5951 // The high part is just a zero.
5952 Hi = DAG.getConstant(0, NVT);
5953 } else /* if (ExtType == ISD::EXTLOAD) */ {
5954 // The high part is undefined.
5955 Hi = DAG.getNode(ISD::UNDEF, NVT);
5956 }
5957 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005958 break;
5959 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005960 case ISD::AND:
5961 case ISD::OR:
5962 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5963 SDOperand LL, LH, RL, RH;
5964 ExpandOp(Node->getOperand(0), LL, LH);
5965 ExpandOp(Node->getOperand(1), RL, RH);
5966 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5967 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5968 break;
5969 }
5970 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005971 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005972 ExpandOp(Node->getOperand(1), LL, LH);
5973 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005974 if (getTypeAction(NVT) == Expand)
5975 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005976 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005977 if (VT != MVT::f32)
5978 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005979 break;
5980 }
Nate Begeman9373a812005-08-10 20:51:12 +00005981 case ISD::SELECT_CC: {
5982 SDOperand TL, TH, FL, FH;
5983 ExpandOp(Node->getOperand(2), TL, TH);
5984 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005985 if (getTypeAction(NVT) == Expand)
5986 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005987 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5988 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005989 if (VT != MVT::f32)
5990 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5991 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005992 break;
5993 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005994 case ISD::ANY_EXTEND:
5995 // The low part is any extension of the input (which degenerates to a copy).
5996 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5997 // The high part is undefined.
5998 Hi = DAG.getNode(ISD::UNDEF, NVT);
5999 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006000 case ISD::SIGN_EXTEND: {
6001 // The low part is just a sign extension of the input (which degenerates to
6002 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006003 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006004
Chris Lattner3e928bb2005-01-07 07:47:09 +00006005 // The high part is obtained by SRA'ing all but one of the bits of the lo
6006 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00006007 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00006008 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6009 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006010 break;
6011 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006012 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006013 // The low part is just a zero extension of the input (which degenerates to
6014 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006015 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006016
Chris Lattner3e928bb2005-01-07 07:47:09 +00006017 // The high part is just a zero.
6018 Hi = DAG.getConstant(0, NVT);
6019 break;
Chris Lattner35481892005-12-23 00:16:34 +00006020
Chris Lattner4c948eb2007-02-13 23:55:16 +00006021 case ISD::TRUNCATE: {
6022 // The input value must be larger than this value. Expand *it*.
6023 SDOperand NewLo;
6024 ExpandOp(Node->getOperand(0), NewLo, Hi);
6025
6026 // The low part is now either the right size, or it is closer. If not the
6027 // right size, make an illegal truncate so we recursively expand it.
6028 if (NewLo.getValueType() != Node->getValueType(0))
6029 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6030 ExpandOp(NewLo, Lo, Hi);
6031 break;
6032 }
6033
Chris Lattner35481892005-12-23 00:16:34 +00006034 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006035 SDOperand Tmp;
6036 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6037 // If the target wants to, allow it to lower this itself.
6038 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6039 case Expand: assert(0 && "cannot expand FP!");
6040 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6041 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6042 }
6043 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6044 }
6045
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006046 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006047 if (VT == MVT::f32 || VT == MVT::f64) {
6048 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006049 if (getTypeAction(NVT) == Expand)
6050 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006051 break;
6052 }
6053
6054 // If source operand will be expanded to the same type as VT, i.e.
6055 // i64 <- f64, i32 <- f32, expand the source operand instead.
6056 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
6057 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6058 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006059 break;
6060 }
6061
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006062 // Turn this into a load/store pair by default.
6063 if (Tmp.Val == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006064 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006065
Chris Lattner35481892005-12-23 00:16:34 +00006066 ExpandOp(Tmp, Lo, Hi);
6067 break;
6068 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006069
Chris Lattner27a6c732007-11-24 07:07:01 +00006070 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006071 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6072 TargetLowering::Custom &&
6073 "Must custom expand ReadCycleCounter");
Chris Lattner27a6c732007-11-24 07:07:01 +00006074 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6075 assert(Tmp.Val && "Node must be custom expanded!");
6076 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner308575b2005-11-20 22:56:56 +00006077 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006078 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006079 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006080 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006081
Chris Lattner4e6c7462005-01-08 19:27:05 +00006082 // These operators cannot be expanded directly, emit them as calls to
6083 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006084 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006085 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00006086 SDOperand Op;
6087 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6088 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006089 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6090 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006091 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006092
Chris Lattnerf20d1832005-07-30 01:40:57 +00006093 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6094
Chris Lattner80a3e942005-07-29 00:33:32 +00006095 // Now that the custom expander is done, expand the result, which is still
6096 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00006097 if (Op.Val) {
6098 ExpandOp(Op, Lo, Hi);
6099 break;
6100 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006101 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006102
Dale Johannesen161e8972007-10-05 20:04:43 +00006103 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00006104 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006105 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen73328d12007-09-19 23:55:34 +00006106 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006107 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006108 else if (Node->getOperand(0).getValueType() == MVT::f80)
6109 LC = RTLIB::FPTOSINT_F80_I64;
6110 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6111 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00006112 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6113 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006114 break;
Evan Cheng56966222007-01-12 02:11:51 +00006115 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006116
Evan Cheng56966222007-01-12 02:11:51 +00006117 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006118 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006119 SDOperand Op;
6120 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6121 case Expand: assert(0 && "cannot expand FP!");
6122 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6123 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6124 }
6125
6126 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6127
6128 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00006129 if (Op.Val) {
6130 ExpandOp(Op, Lo, Hi);
6131 break;
6132 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006133 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006134
Evan Chengdaccea182007-10-05 01:09:32 +00006135 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner4e6c7462005-01-08 19:27:05 +00006136 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006137 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen72292f02007-09-28 18:44:17 +00006138 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006139 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006140 else if (Node->getOperand(0).getValueType() == MVT::f80)
6141 LC = RTLIB::FPTOUINT_F80_I64;
6142 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6143 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng56966222007-01-12 02:11:51 +00006144 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6145 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006146 break;
Evan Cheng56966222007-01-12 02:11:51 +00006147 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006148
Evan Cheng05a2d562006-01-09 18:31:59 +00006149 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006150 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006151 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006152 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006153 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006154 Op = TLI.LowerOperation(Op, DAG);
6155 if (Op.Val) {
6156 // Now that the custom expander is done, expand the result, which is
6157 // still VT.
6158 ExpandOp(Op, Lo, Hi);
6159 break;
6160 }
6161 }
6162
Chris Lattner79980b02006-09-13 03:50:39 +00006163 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6164 // this X << 1 as X+X.
6165 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
6166 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
6167 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6168 SDOperand LoOps[2], HiOps[3];
6169 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6170 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6171 LoOps[1] = LoOps[0];
6172 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6173
6174 HiOps[1] = HiOps[0];
6175 HiOps[2] = Lo.getValue(1);
6176 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6177 break;
6178 }
6179 }
6180
Chris Lattnere34b3962005-01-19 04:19:40 +00006181 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006182 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006183 break;
Chris Lattner47599822005-04-02 03:38:53 +00006184
6185 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006186 TargetLowering::LegalizeAction Action =
6187 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6188 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6189 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006190 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006191 break;
6192 }
6193
Chris Lattnere34b3962005-01-19 04:19:40 +00006194 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006195 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
6196 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006197 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006198 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006199
Evan Cheng05a2d562006-01-09 18:31:59 +00006200 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006201 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006202 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006203 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006204 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006205 Op = TLI.LowerOperation(Op, DAG);
6206 if (Op.Val) {
6207 // Now that the custom expander is done, expand the result, which is
6208 // still VT.
6209 ExpandOp(Op, Lo, Hi);
6210 break;
6211 }
6212 }
6213
Chris Lattnere34b3962005-01-19 04:19:40 +00006214 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006215 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006216 break;
Chris Lattner47599822005-04-02 03:38:53 +00006217
6218 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006219 TargetLowering::LegalizeAction Action =
6220 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6221 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6222 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006223 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006224 break;
6225 }
6226
Chris Lattnere34b3962005-01-19 04:19:40 +00006227 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006228 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
6229 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006230 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006231 }
6232
6233 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006234 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006235 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006236 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006237 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006238 Op = TLI.LowerOperation(Op, DAG);
6239 if (Op.Val) {
6240 // Now that the custom expander is done, expand the result, which is
6241 // still VT.
6242 ExpandOp(Op, Lo, Hi);
6243 break;
6244 }
6245 }
6246
Chris Lattnere34b3962005-01-19 04:19:40 +00006247 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006248 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006249 break;
Chris Lattner47599822005-04-02 03:38:53 +00006250
6251 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006252 TargetLowering::LegalizeAction Action =
6253 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6254 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6255 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006256 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006257 break;
6258 }
6259
Chris Lattnere34b3962005-01-19 04:19:40 +00006260 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006261 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
6262 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006263 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006264 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006265
Misha Brukmanedf128a2005-04-21 22:36:52 +00006266 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006267 case ISD::SUB: {
6268 // If the target wants to custom expand this, let them.
6269 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6270 TargetLowering::Custom) {
6271 Op = TLI.LowerOperation(Op, DAG);
6272 if (Op.Val) {
6273 ExpandOp(Op, Lo, Hi);
6274 break;
6275 }
6276 }
6277
6278 // Expand the subcomponents.
6279 SDOperand LHSL, LHSH, RHSL, RHSH;
6280 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6281 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00006282 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6283 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006284 LoOps[0] = LHSL;
6285 LoOps[1] = RHSL;
6286 HiOps[0] = LHSH;
6287 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006288 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00006289 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006290 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006291 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006292 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00006293 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006294 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006295 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006296 }
Chris Lattner84f67882005-01-20 18:52:28 +00006297 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006298 }
Chris Lattnerb429f732007-05-17 18:15:41 +00006299
6300 case ISD::ADDC:
6301 case ISD::SUBC: {
6302 // Expand the subcomponents.
6303 SDOperand LHSL, LHSH, RHSL, RHSH;
6304 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6305 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6306 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6307 SDOperand LoOps[2] = { LHSL, RHSL };
6308 SDOperand HiOps[3] = { LHSH, RHSH };
6309
6310 if (Node->getOpcode() == ISD::ADDC) {
6311 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6312 HiOps[2] = Lo.getValue(1);
6313 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6314 } else {
6315 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6316 HiOps[2] = Lo.getValue(1);
6317 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6318 }
6319 // Remember that we legalized the flag.
6320 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6321 break;
6322 }
6323 case ISD::ADDE:
6324 case ISD::SUBE: {
6325 // Expand the subcomponents.
6326 SDOperand LHSL, LHSH, RHSL, RHSH;
6327 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6328 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6329 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6330 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6331 SDOperand HiOps[3] = { LHSH, RHSH };
6332
6333 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6334 HiOps[2] = Lo.getValue(1);
6335 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6336
6337 // Remember that we legalized the flag.
6338 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6339 break;
6340 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006341 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006342 // If the target wants to custom expand this, let them.
6343 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006344 SDOperand New = TLI.LowerOperation(Op, DAG);
6345 if (New.Val) {
6346 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006347 break;
6348 }
6349 }
6350
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006351 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6352 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006353 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6354 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6355 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanc7c16572005-04-11 03:01:51 +00006356 SDOperand LL, LH, RL, RH;
6357 ExpandOp(Node->getOperand(0), LL, LH);
6358 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006359 unsigned OuterBitSize = Op.getValueSizeInBits();
6360 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00006361 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6362 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006363 if (DAG.MaskedValueIsZero(Op.getOperand(0),
6364 APInt::getHighBitsSet(OuterBitSize, LHSSB)) &&
6365 DAG.MaskedValueIsZero(Op.getOperand(1),
6366 APInt::getHighBitsSet(OuterBitSize, RHSSB))) {
Dan Gohman525178c2007-10-08 18:33:35 +00006367 // The inputs are both zero-extended.
6368 if (HasUMUL_LOHI) {
6369 // We can emit a umul_lohi.
6370 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6371 Hi = SDOperand(Lo.Val, 1);
6372 break;
6373 }
6374 if (HasMULHU) {
6375 // We can emit a mulhu+mul.
6376 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6377 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6378 break;
6379 }
Dan Gohman525178c2007-10-08 18:33:35 +00006380 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006381 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00006382 // The input values are both sign-extended.
6383 if (HasSMUL_LOHI) {
6384 // We can emit a smul_lohi.
6385 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6386 Hi = SDOperand(Lo.Val, 1);
6387 break;
6388 }
6389 if (HasMULHS) {
6390 // We can emit a mulhs+mul.
6391 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6392 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6393 break;
6394 }
6395 }
6396 if (HasUMUL_LOHI) {
6397 // Lo,Hi = umul LHS, RHS.
6398 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6399 DAG.getVTList(NVT, NVT), LL, RL);
6400 Lo = UMulLOHI;
6401 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006402 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6403 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6404 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6405 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006406 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006407 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006408 if (HasMULHU) {
6409 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6410 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6411 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6412 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6413 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6414 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6415 break;
6416 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006417 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006418
Dan Gohman525178c2007-10-08 18:33:35 +00006419 // If nothing else, we can make a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006420 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6421 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006422 break;
6423 }
Evan Cheng56966222007-01-12 02:11:51 +00006424 case ISD::SDIV:
6425 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6426 break;
6427 case ISD::UDIV:
6428 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6429 break;
6430 case ISD::SREM:
6431 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6432 break;
6433 case ISD::UREM:
6434 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6435 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006436
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006437 case ISD::FADD:
Duncan Sands007f9842008-01-10 10:28:30 +00006438 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6439 RTLIB::ADD_F64,
6440 RTLIB::ADD_F80,
6441 RTLIB::ADD_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006442 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006443 break;
6444 case ISD::FSUB:
Duncan Sands007f9842008-01-10 10:28:30 +00006445 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6446 RTLIB::SUB_F64,
6447 RTLIB::SUB_F80,
6448 RTLIB::SUB_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006449 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006450 break;
6451 case ISD::FMUL:
Duncan Sands007f9842008-01-10 10:28:30 +00006452 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6453 RTLIB::MUL_F64,
6454 RTLIB::MUL_F80,
6455 RTLIB::MUL_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006456 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006457 break;
6458 case ISD::FDIV:
Duncan Sands007f9842008-01-10 10:28:30 +00006459 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6460 RTLIB::DIV_F64,
6461 RTLIB::DIV_F80,
6462 RTLIB::DIV_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006463 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006464 break;
6465 case ISD::FP_EXTEND:
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006466 if (VT == MVT::ppcf128) {
6467 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6468 Node->getOperand(0).getValueType()==MVT::f64);
6469 const uint64_t zero = 0;
6470 if (Node->getOperand(0).getValueType()==MVT::f32)
6471 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6472 else
6473 Hi = Node->getOperand(0);
6474 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6475 break;
6476 }
Evan Cheng56966222007-01-12 02:11:51 +00006477 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006478 break;
6479 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00006480 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006481 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006482 case ISD::FPOWI:
Duncan Sands007f9842008-01-10 10:28:30 +00006483 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6484 RTLIB::POWI_F64,
6485 RTLIB::POWI_F80,
6486 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006487 Node, false, Hi);
6488 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006489 case ISD::FSQRT:
6490 case ISD::FSIN:
6491 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00006492 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006493 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006494 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00006495 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6496 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006497 break;
6498 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00006499 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6500 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006501 break;
6502 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00006503 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6504 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006505 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006506 default: assert(0 && "Unreachable!");
6507 }
Evan Cheng56966222007-01-12 02:11:51 +00006508 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006509 break;
6510 }
Evan Cheng966bf242006-12-16 00:52:40 +00006511 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006512 if (VT == MVT::ppcf128) {
6513 SDOperand Tmp;
6514 ExpandOp(Node->getOperand(0), Lo, Tmp);
6515 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6516 // lo = hi==fabs(hi) ? lo : -lo;
6517 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6518 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6519 DAG.getCondCode(ISD::SETEQ));
6520 break;
6521 }
Evan Cheng966bf242006-12-16 00:52:40 +00006522 SDOperand Mask = (VT == MVT::f64)
6523 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6524 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6525 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6526 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6527 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6528 if (getTypeAction(NVT) == Expand)
6529 ExpandOp(Lo, Lo, Hi);
6530 break;
6531 }
6532 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006533 if (VT == MVT::ppcf128) {
6534 ExpandOp(Node->getOperand(0), Lo, Hi);
6535 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6536 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6537 break;
6538 }
Evan Cheng966bf242006-12-16 00:52:40 +00006539 SDOperand Mask = (VT == MVT::f64)
6540 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6541 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6542 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6543 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6544 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6545 if (getTypeAction(NVT) == Expand)
6546 ExpandOp(Lo, Lo, Hi);
6547 break;
6548 }
Evan Cheng912095b2007-01-04 21:56:39 +00006549 case ISD::FCOPYSIGN: {
6550 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6551 if (getTypeAction(NVT) == Expand)
6552 ExpandOp(Lo, Lo, Hi);
6553 break;
6554 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006555 case ISD::SINT_TO_FP:
6556 case ISD::UINT_TO_FP: {
6557 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6558 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesen6e63e092007-10-12 17:52:03 +00006559 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006560 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006561 if (isSigned) {
6562 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6563 Node->getOperand(0)));
6564 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6565 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006566 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006567 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6568 Node->getOperand(0)));
6569 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6570 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006571 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006572 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6573 DAG.getConstant(0, MVT::i32),
6574 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6575 DAG.getConstantFP(
6576 APFloat(APInt(128, 2, TwoE32)),
6577 MVT::ppcf128)),
6578 Hi,
6579 DAG.getCondCode(ISD::SETLT)),
6580 Lo, Hi);
6581 }
6582 break;
6583 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00006584 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6585 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006586 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00006587 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6588 Lo, Hi);
6589 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6590 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6591 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6592 DAG.getConstant(0, MVT::i64),
6593 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6594 DAG.getConstantFP(
6595 APFloat(APInt(128, 2, TwoE64)),
6596 MVT::ppcf128)),
6597 Hi,
6598 DAG.getCondCode(ISD::SETLT)),
6599 Lo, Hi);
6600 break;
6601 }
Evan Cheng64f638d2007-09-27 07:35:39 +00006602 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006603 if (Node->getOperand(0).getValueType() == MVT::i64) {
6604 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006605 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen73328d12007-09-19 23:55:34 +00006606 else if (VT == MVT::f64)
Evan Cheng56966222007-01-12 02:11:51 +00006607 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesen161e8972007-10-05 20:04:43 +00006608 else if (VT == MVT::f80) {
Dale Johannesen73328d12007-09-19 23:55:34 +00006609 assert(isSigned);
Dale Johannesen161e8972007-10-05 20:04:43 +00006610 LC = RTLIB::SINTTOFP_I64_F80;
6611 }
6612 else if (VT == MVT::ppcf128) {
6613 assert(isSigned);
6614 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen73328d12007-09-19 23:55:34 +00006615 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006616 } else {
6617 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00006618 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006619 else
Evan Cheng56966222007-01-12 02:11:51 +00006620 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00006621 }
6622
6623 // Promote the operand if needed.
6624 if (getTypeAction(SrcVT) == Promote) {
6625 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6626 Tmp = isSigned
6627 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6628 DAG.getValueType(SrcVT))
6629 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6630 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6631 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00006632
6633 const char *LibCall = TLI.getLibcallName(LC);
6634 if (LibCall)
6635 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6636 else {
6637 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6638 Node->getOperand(0));
6639 if (getTypeAction(Lo.getValueType()) == Expand)
6640 ExpandOp(Lo, Lo, Hi);
6641 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006642 break;
6643 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006644 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006645
Chris Lattner83397362005-12-21 18:02:52 +00006646 // Make sure the resultant values have been legalized themselves, unless this
6647 // is a type that requires multi-step expansion.
6648 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6649 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006650 if (Hi.Val)
6651 // Don't legalize the high part if it is expanded to a single node.
6652 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006653 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006654
6655 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006656 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00006657 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006658}
6659
Dan Gohman7f321562007-06-25 16:23:39 +00006660/// SplitVectorOp - Given an operand of vector type, break it down into
6661/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00006662void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6663 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00006664 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006665 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006666 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00006667 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00006668
Dan Gohmane40c7b02007-09-24 15:54:53 +00006669 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006670
6671 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6672 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6673
6674 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6675 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6676
Chris Lattnerc7029802006-03-18 01:44:44 +00006677 // See if we already split it.
6678 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6679 = SplitNodes.find(Op);
6680 if (I != SplitNodes.end()) {
6681 Lo = I->second.first;
6682 Hi = I->second.second;
6683 return;
6684 }
6685
6686 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006687 default:
6688#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006689 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006690#endif
6691 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00006692 case ISD::UNDEF:
6693 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6694 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6695 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006696 case ISD::BUILD_PAIR:
6697 Lo = Node->getOperand(0);
6698 Hi = Node->getOperand(1);
6699 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006700 case ISD::INSERT_VECTOR_ELT: {
6701 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6702 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6703 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006704 if (Index < NewNumElts_Lo)
6705 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohman9fe46622007-09-28 23:53:40 +00006706 DAG.getConstant(Index, TLI.getPointerTy()));
6707 else
Nate Begeman5db1afb2007-11-15 21:15:26 +00006708 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6709 DAG.getConstant(Index - NewNumElts_Lo,
6710 TLI.getPointerTy()));
Dan Gohman9fe46622007-09-28 23:53:40 +00006711 break;
6712 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00006713 case ISD::VECTOR_SHUFFLE: {
6714 // Build the low part.
6715 SDOperand Mask = Node->getOperand(2);
6716 SmallVector<SDOperand, 8> Ops;
6717 MVT::ValueType PtrVT = TLI.getPointerTy();
6718
6719 // Insert all of the elements from the input that are needed. We use
6720 // buildvector of extractelement here because the input vectors will have
6721 // to be legalized, so this makes the code simpler.
6722 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6723 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6724 SDOperand InVec = Node->getOperand(0);
6725 if (Idx >= NumElements) {
6726 InVec = Node->getOperand(1);
6727 Idx -= NumElements;
6728 }
6729 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6730 DAG.getConstant(Idx, PtrVT)));
6731 }
6732 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6733 Ops.clear();
6734
6735 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6736 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6737 SDOperand InVec = Node->getOperand(0);
6738 if (Idx >= NumElements) {
6739 InVec = Node->getOperand(1);
6740 Idx -= NumElements;
6741 }
6742 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6743 DAG.getConstant(Idx, PtrVT)));
6744 }
6745 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6746 break;
6747 }
Dan Gohman7f321562007-06-25 16:23:39 +00006748 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006749 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00006750 Node->op_begin()+NewNumElts_Lo);
6751 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006752
Nate Begeman5db1afb2007-11-15 21:15:26 +00006753 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00006754 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006755 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006756 break;
6757 }
Dan Gohman7f321562007-06-25 16:23:39 +00006758 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00006759 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00006760 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6761 if (NewNumSubvectors == 1) {
6762 Lo = Node->getOperand(0);
6763 Hi = Node->getOperand(1);
6764 } else {
6765 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6766 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006767 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006768
Dan Gohman7f321562007-06-25 16:23:39 +00006769 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6770 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006771 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00006772 }
Dan Gohman65956352007-06-13 15:12:02 +00006773 break;
6774 }
Dan Gohmanc6230962007-10-17 14:48:28 +00006775 case ISD::SELECT: {
6776 SDOperand Cond = Node->getOperand(0);
6777
6778 SDOperand LL, LH, RL, RH;
6779 SplitVectorOp(Node->getOperand(1), LL, LH);
6780 SplitVectorOp(Node->getOperand(2), RL, RH);
6781
6782 if (MVT::isVector(Cond.getValueType())) {
6783 // Handle a vector merge.
6784 SDOperand CL, CH;
6785 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006786 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6787 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006788 } else {
6789 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00006790 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6791 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006792 }
6793 break;
6794 }
Dan Gohman7f321562007-06-25 16:23:39 +00006795 case ISD::ADD:
6796 case ISD::SUB:
6797 case ISD::MUL:
6798 case ISD::FADD:
6799 case ISD::FSUB:
6800 case ISD::FMUL:
6801 case ISD::SDIV:
6802 case ISD::UDIV:
6803 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006804 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006805 case ISD::AND:
6806 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00006807 case ISD::XOR:
6808 case ISD::UREM:
6809 case ISD::SREM:
6810 case ISD::FREM: {
Chris Lattnerc7029802006-03-18 01:44:44 +00006811 SDOperand LL, LH, RL, RH;
6812 SplitVectorOp(Node->getOperand(0), LL, LH);
6813 SplitVectorOp(Node->getOperand(1), RL, RH);
6814
Nate Begeman5db1afb2007-11-15 21:15:26 +00006815 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6816 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006817 break;
6818 }
Dan Gohman82669522007-10-11 23:57:53 +00006819 case ISD::FPOWI: {
6820 SDOperand L, H;
6821 SplitVectorOp(Node->getOperand(0), L, H);
6822
Nate Begeman5db1afb2007-11-15 21:15:26 +00006823 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6824 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00006825 break;
6826 }
6827 case ISD::CTTZ:
6828 case ISD::CTLZ:
6829 case ISD::CTPOP:
6830 case ISD::FNEG:
6831 case ISD::FABS:
6832 case ISD::FSQRT:
6833 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00006834 case ISD::FCOS:
6835 case ISD::FP_TO_SINT:
6836 case ISD::FP_TO_UINT:
6837 case ISD::SINT_TO_FP:
6838 case ISD::UINT_TO_FP: {
Dan Gohman82669522007-10-11 23:57:53 +00006839 SDOperand L, H;
6840 SplitVectorOp(Node->getOperand(0), L, H);
6841
Nate Begeman5db1afb2007-11-15 21:15:26 +00006842 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6843 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00006844 break;
6845 }
Dan Gohman7f321562007-06-25 16:23:39 +00006846 case ISD::LOAD: {
6847 LoadSDNode *LD = cast<LoadSDNode>(Node);
6848 SDOperand Ch = LD->getChain();
6849 SDOperand Ptr = LD->getBasePtr();
6850 const Value *SV = LD->getSrcValue();
6851 int SVOffset = LD->getSrcValueOffset();
6852 unsigned Alignment = LD->getAlignment();
6853 bool isVolatile = LD->isVolatile();
6854
Nate Begeman5db1afb2007-11-15 21:15:26 +00006855 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6856 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00006857 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006858 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006859 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006860 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006861 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00006862
6863 // Build a factor node to remember that this load is independent of the
6864 // other one.
6865 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6866 Hi.getValue(1));
6867
6868 // Remember that we legalized the chain.
6869 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00006870 break;
6871 }
Dan Gohman7f321562007-06-25 16:23:39 +00006872 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00006873 // We know the result is a vector. The input may be either a vector or a
6874 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00006875 SDOperand InOp = Node->getOperand(0);
6876 if (!MVT::isVector(InOp.getValueType()) ||
6877 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6878 // The input is a scalar or single-element vector.
6879 // Lower to a store/load so that it can be split.
6880 // FIXME: this could be improved probably.
Chris Lattner85dd3be2007-10-15 17:48:57 +00006881 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00006882 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val);
Chris Lattner7692eb42006-03-23 21:16:34 +00006883
Evan Cheng786225a2006-10-05 23:01:46 +00006884 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00006885 InOp, Ptr,
Dan Gohman3069b872008-02-07 18:41:25 +00006886 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00006887 FI->getIndex());
6888 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman3069b872008-02-07 18:41:25 +00006889 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00006890 FI->getIndex());
Chris Lattner7692eb42006-03-23 21:16:34 +00006891 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00006892 // Split the vector and convert each of the pieces now.
6893 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006894 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6895 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00006896 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00006897 }
Chris Lattnerc7029802006-03-18 01:44:44 +00006898 }
6899
6900 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006901 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00006902 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00006903 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006904}
6905
6906
Dan Gohman89b20c02007-06-27 14:06:22 +00006907/// ScalarizeVectorOp - Given an operand of single-element vector type
6908/// (e.g. v1f32), convert it into the equivalent operation that returns a
6909/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00006910SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6911 assert(MVT::isVector(Op.getValueType()) &&
6912 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006913 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00006914 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6915 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00006916
Dan Gohman7f321562007-06-25 16:23:39 +00006917 // See if we already scalarized it.
6918 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6919 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00006920
6921 SDOperand Result;
6922 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00006923 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006924#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006925 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006926#endif
Dan Gohman7f321562007-06-25 16:23:39 +00006927 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6928 case ISD::ADD:
6929 case ISD::FADD:
6930 case ISD::SUB:
6931 case ISD::FSUB:
6932 case ISD::MUL:
6933 case ISD::FMUL:
6934 case ISD::SDIV:
6935 case ISD::UDIV:
6936 case ISD::FDIV:
6937 case ISD::SREM:
6938 case ISD::UREM:
6939 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00006940 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006941 case ISD::AND:
6942 case ISD::OR:
6943 case ISD::XOR:
6944 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00006945 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00006946 ScalarizeVectorOp(Node->getOperand(0)),
6947 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00006948 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006949 case ISD::FNEG:
6950 case ISD::FABS:
6951 case ISD::FSQRT:
6952 case ISD::FSIN:
6953 case ISD::FCOS:
6954 Result = DAG.getNode(Node->getOpcode(),
6955 NewVT,
6956 ScalarizeVectorOp(Node->getOperand(0)));
6957 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00006958 case ISD::FPOWI:
6959 Result = DAG.getNode(Node->getOpcode(),
6960 NewVT,
6961 ScalarizeVectorOp(Node->getOperand(0)),
6962 Node->getOperand(1));
6963 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006964 case ISD::LOAD: {
6965 LoadSDNode *LD = cast<LoadSDNode>(Node);
6966 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6967 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00006968
Dan Gohman7f321562007-06-25 16:23:39 +00006969 const Value *SV = LD->getSrcValue();
6970 int SVOffset = LD->getSrcValueOffset();
6971 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6972 LD->isVolatile(), LD->getAlignment());
6973
Chris Lattnerc7029802006-03-18 01:44:44 +00006974 // Remember that we legalized the chain.
6975 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6976 break;
6977 }
Dan Gohman7f321562007-06-25 16:23:39 +00006978 case ISD::BUILD_VECTOR:
6979 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00006980 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006981 case ISD::INSERT_VECTOR_ELT:
6982 // Returning the inserted scalar element.
6983 Result = Node->getOperand(1);
6984 break;
6985 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00006986 assert(Node->getOperand(0).getValueType() == NewVT &&
6987 "Concat of non-legal vectors not yet supported!");
6988 Result = Node->getOperand(0);
6989 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006990 case ISD::VECTOR_SHUFFLE: {
6991 // Figure out if the scalar is the LHS or RHS and return it.
6992 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6993 if (cast<ConstantSDNode>(EltNum)->getValue())
6994 Result = ScalarizeVectorOp(Node->getOperand(1));
6995 else
6996 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00006997 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006998 }
6999 case ISD::EXTRACT_SUBVECTOR:
7000 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00007001 assert(Result.getValueType() == NewVT);
7002 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007003 case ISD::BIT_CONVERT:
7004 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00007005 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007006 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007007 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007008 ScalarizeVectorOp(Op.getOperand(1)),
7009 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007010 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00007011 }
7012
7013 if (TLI.isTypeLegal(NewVT))
7014 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007015 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7016 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00007017 return Result;
7018}
7019
Chris Lattner3e928bb2005-01-07 07:47:09 +00007020
7021// SelectionDAG::Legalize - This is the entry point for the file.
7022//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00007023void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00007024 if (ViewLegalizeDAGs) viewGraph();
7025
Chris Lattner3e928bb2005-01-07 07:47:09 +00007026 /// run - This is the main entry point to this class.
7027 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00007028 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00007029}
7030