blob: 12b7b4aff520ba2cfa61a0dbf388f67cdade3439 [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"
Dan Gohman707e0182008-04-12 04:36:06 +000025#include "llvm/Target/TargetSubtarget.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000026#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000027#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000028#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000029#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000030#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000031#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000033#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000034#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000035#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000036using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
40/// hacks on it until the target machine can handle it. This involves
41/// eliminating value sizes the machine cannot handle (promoting small sizes to
42/// large sizes or splitting up large values into small values) as well as
43/// eliminating operations the machine cannot handle.
44///
45/// This code also does a small amount of optimization and recognition of idioms
46/// as part of its processing. For example, if a target does not support a
47/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
48/// will attempt merge setcc and brc instructions into brcc's.
49///
50namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000051class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000052 TargetLowering &TLI;
53 SelectionDAG &DAG;
54
Chris Lattner6831a812006-02-13 09:18:02 +000055 // Libcall insertion helpers.
56
57 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
58 /// legalized. We use this to ensure that calls are properly serialized
59 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000060 SDValue LastCALLSEQ_END;
Chris Lattner6831a812006-02-13 09:18:02 +000061
62 /// IsLegalizingCall - This member is used *only* for purposes of providing
63 /// helpful assertions that a libcall isn't created while another call is
64 /// being legalized (which could lead to non-serialized call sequences).
65 bool IsLegalizingCall;
66
Chris Lattner3e928bb2005-01-07 07:47:09 +000067 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000068 Legal, // The target natively supports this operation.
69 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000070 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 };
Chris Lattner6831a812006-02-13 09:18:02 +000072
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 /// ValueTypeActions - This is a bitvector that contains two bits for each
74 /// value type, where the two bits correspond to the LegalizeAction enum.
75 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000076 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000077
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 /// LegalizedNodes - For nodes that are of legal width, and that have more
79 /// than one use, this map indicates what regularized operand to use. This
80 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000081 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000082
Chris Lattner03c85462005-01-15 05:21:40 +000083 /// PromotedNodes - For nodes that are below legal width, and that have more
84 /// than one use, this map indicates what promoted value to use. This allows
85 /// us to avoid promoting the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000086 DenseMap<SDValue, SDValue> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000087
Chris Lattnerc7029802006-03-18 01:44:44 +000088 /// ExpandedNodes - For nodes that need to be expanded this map indicates
89 /// which which operands are the expanded version of the input. This allows
90 /// us to avoid expanding the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000091 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000092
Chris Lattnerc7029802006-03-18 01:44:44 +000093 /// SplitNodes - For vector nodes that need to be split, this map indicates
94 /// which which operands are the split version of the input. This allows us
95 /// to avoid splitting the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000096 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +000097
Dan Gohman7f321562007-06-25 16:23:39 +000098 /// ScalarizedNodes - For nodes that need to be converted from vector types to
99 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000100 /// processed to the result.
Dan Gohman475871a2008-07-27 21:46:04 +0000101 std::map<SDValue, SDValue> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000102
Dan Gohman475871a2008-07-27 21:46:04 +0000103 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000104 LegalizedNodes.insert(std::make_pair(From, To));
105 // If someone requests legalization of the new node, return itself.
106 if (From != To)
107 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000108 }
Dan Gohman475871a2008-07-27 21:46:04 +0000109 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman6b345ee2008-07-07 17:46:23 +0000110 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Chris Lattner03c85462005-01-15 05:21:40 +0000111 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000112 // If someone requests legalization of the new node, return itself.
113 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000114 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000115
Chris Lattner3e928bb2005-01-07 07:47:09 +0000116public:
Dan Gohman1002c022008-07-07 18:00:37 +0000117 explicit SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000118
Chris Lattner3e928bb2005-01-07 07:47:09 +0000119 /// getTypeAction - Return how we should legalize values of this type, either
120 /// it is already legal or we need to expand it into multiple registers of
121 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000122 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000123 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000124 }
125
126 /// isTypeLegal - Return true if this type is legal on this target.
127 ///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000128 bool isTypeLegal(MVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000129 return getTypeAction(VT) == Legal;
130 }
131
Chris Lattner3e928bb2005-01-07 07:47:09 +0000132 void LegalizeDAG();
133
Chris Lattner456a93a2006-01-28 07:39:30 +0000134private:
Dan Gohman7f321562007-06-25 16:23:39 +0000135 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000136 /// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000137 void HandleOp(SDValue Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000138
139 /// LegalizeOp - We know that the specified value has a legal type.
140 /// Recursively ensure that the operands have legal types, then return the
141 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000142 SDValue LegalizeOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000143
Dan Gohman82669522007-10-11 23:57:53 +0000144 /// UnrollVectorOp - We know that the given vector has a legal type, however
145 /// the operation it performs is not legal and is an operation that we have
146 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
147 /// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000148 SDValue UnrollVectorOp(SDValue O);
Nate Begeman68679912008-04-25 18:07:40 +0000149
150 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
151 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
152 /// is necessary to spill the vector being inserted into to memory, perform
153 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000154 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
155 SDValue Idx);
Dan Gohman82669522007-10-11 23:57:53 +0000156
Chris Lattnerc7029802006-03-18 01:44:44 +0000157 /// PromoteOp - Given an operation that produces a value in an invalid type,
158 /// promote it to compute the value into a larger type. The produced value
159 /// will have the correct bits for the low portion of the register, but no
160 /// guarantee is made about the top bits: it may be zero, sign-extended, or
161 /// garbage.
Dan Gohman475871a2008-07-27 21:46:04 +0000162 SDValue PromoteOp(SDValue O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000163
Dan Gohman475871a2008-07-27 21:46:04 +0000164 /// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattnerc7029802006-03-18 01:44:44 +0000165 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
166 /// the LegalizeNodes map is filled in for any results that are not expanded,
167 /// the ExpandedNodes map is filled in for any results that are expanded, and
168 /// the Lo/Hi values are returned. This applies to integer types and Vector
169 /// types.
Dan Gohman475871a2008-07-27 21:46:04 +0000170 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000171
Dan Gohman7f321562007-06-25 16:23:39 +0000172 /// SplitVectorOp - Given an operand of vector type, break it down into
173 /// two smaller values.
Dan Gohman475871a2008-07-27 21:46:04 +0000174 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000175
Dan Gohman89b20c02007-06-27 14:06:22 +0000176 /// ScalarizeVectorOp - Given an operand of single-element vector type
177 /// (e.g. v1f32), convert it into the equivalent operation that returns a
178 /// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +0000179 SDValue ScalarizeVectorOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000180
Duncan Sandsd038e042008-07-21 10:20:31 +0000181 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Chris Lattner4352cc92006-04-04 17:23:26 +0000182 /// specified mask and type. Targets can specify exactly which masks they
183 /// support and the code generator is tasked with not creating illegal masks.
184 ///
185 /// Note that this will also return true for shuffles that are promoted to a
186 /// different type.
187 ///
188 /// If this is a legal shuffle, this method returns the (possibly promoted)
189 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman475871a2008-07-27 21:46:04 +0000190 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Chris Lattner4352cc92006-04-04 17:23:26 +0000191
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000192 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000193 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000194
Dan Gohman475871a2008-07-27 21:46:04 +0000195 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000196
Dan Gohman475871a2008-07-27 21:46:04 +0000197 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
198 SDValue &Hi);
199 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000200
Dan Gohman475871a2008-07-27 21:46:04 +0000201 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
202 SDValue ExpandBUILD_VECTOR(SDNode *Node);
203 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
204 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
205 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
206 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000207
Dan Gohman475871a2008-07-27 21:46:04 +0000208 SDValue ExpandBSWAP(SDValue Op);
209 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
210 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
211 SDValue &Lo, SDValue &Hi);
212 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
213 SDValue &Lo, SDValue &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000214
Dan Gohman475871a2008-07-27 21:46:04 +0000215 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
216 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000217};
218}
219
Chris Lattner4352cc92006-04-04 17:23:26 +0000220/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
221/// specified mask and type. Targets can specify exactly which masks they
222/// support and the code generator is tasked with not creating illegal masks.
223///
224/// Note that this will also return true for shuffles that are promoted to a
225/// different type.
Dan Gohman475871a2008-07-27 21:46:04 +0000226SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Chris Lattner4352cc92006-04-04 17:23:26 +0000227 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
228 default: return 0;
229 case TargetLowering::Legal:
230 case TargetLowering::Custom:
231 break;
232 case TargetLowering::Promote: {
233 // If this is promoted to a different type, convert the shuffle mask and
234 // ask if it is legal in the promoted type!
Duncan Sands83ec4b62008-06-06 12:08:01 +0000235 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd038e042008-07-21 10:20:31 +0000236 MVT EltVT = NVT.getVectorElementType();
Chris Lattner4352cc92006-04-04 17:23:26 +0000237
238 // If we changed # elements, change the shuffle mask.
239 unsigned NumEltsGrowth =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000240 NVT.getVectorNumElements() / VT.getVectorNumElements();
Chris Lattner4352cc92006-04-04 17:23:26 +0000241 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
242 if (NumEltsGrowth > 1) {
243 // Renumber the elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000244 SmallVector<SDValue, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000245 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000246 SDValue InOp = Mask.getOperand(i);
Chris Lattner4352cc92006-04-04 17:23:26 +0000247 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
248 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd038e042008-07-21 10:20:31 +0000249 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000250 else {
251 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
Duncan Sandsd038e042008-07-21 10:20:31 +0000252 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000253 }
254 }
255 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000256 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000257 }
258 VT = NVT;
259 break;
260 }
261 }
262 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
263}
264
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000265SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
266 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
267 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000268 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000269 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000270}
271
Chris Lattnere10e6f72007-06-18 21:28:10 +0000272/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
273/// contains all of a nodes operands before it contains the node.
274static void ComputeTopDownOrdering(SelectionDAG &DAG,
275 SmallVector<SDNode*, 64> &Order) {
276
277 DenseMap<SDNode*, unsigned> Visited;
278 std::vector<SDNode*> Worklist;
279 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000280
Chris Lattnere10e6f72007-06-18 21:28:10 +0000281 // Compute ordering from all of the leaves in the graphs, those (like the
282 // entry node) that have no operands.
283 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
284 E = DAG.allnodes_end(); I != E; ++I) {
285 if (I->getNumOperands() == 0) {
286 Visited[I] = 0 - 1U;
287 Worklist.push_back(I);
288 }
Chris Lattner32fca002005-10-06 01:20:27 +0000289 }
290
Chris Lattnere10e6f72007-06-18 21:28:10 +0000291 while (!Worklist.empty()) {
292 SDNode *N = Worklist.back();
293 Worklist.pop_back();
294
295 if (++Visited[N] != N->getNumOperands())
296 continue; // Haven't visited all operands yet
297
298 Order.push_back(N);
299
300 // Now that we have N in, add anything that uses it if all of their operands
301 // are now done.
Dan Gohman89684502008-07-27 20:43:25 +0000302 Worklist.insert(Worklist.end(), N->use_begin(), N->use_end());
Chris Lattnere10e6f72007-06-18 21:28:10 +0000303 }
304
305 assert(Order.size() == Visited.size() &&
Dan Gohman3461cc92008-06-20 17:15:19 +0000306 Order.size() == DAG.allnodes_size() &&
Chris Lattnere10e6f72007-06-18 21:28:10 +0000307 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000308}
309
Chris Lattner1618beb2005-07-29 00:11:56 +0000310
Chris Lattner3e928bb2005-01-07 07:47:09 +0000311void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000312 LastCALLSEQ_END = DAG.getEntryNode();
313 IsLegalizingCall = false;
314
Chris Lattnerab510a72005-10-02 17:49:46 +0000315 // The legalize process is inherently a bottom-up recursive process (users
316 // legalize their uses before themselves). Given infinite stack space, we
317 // could just start legalizing on the root and traverse the whole graph. In
318 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000319 // blocks. To avoid this problem, compute an ordering of the nodes where each
320 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000321 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000322 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000323
Chris Lattnerc7029802006-03-18 01:44:44 +0000324 for (unsigned i = 0, e = Order.size(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +0000325 HandleOp(SDValue(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000326
327 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000328 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000329 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
330 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000331
332 ExpandedNodes.clear();
333 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000334 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000335 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000336 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000337
338 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000339 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000340}
341
Chris Lattner6831a812006-02-13 09:18:02 +0000342
343/// FindCallEndFromCallStart - Given a chained node that is part of a call
344/// sequence, find the CALLSEQ_END node that terminates the call sequence.
345static SDNode *FindCallEndFromCallStart(SDNode *Node) {
346 if (Node->getOpcode() == ISD::CALLSEQ_END)
347 return Node;
348 if (Node->use_empty())
349 return 0; // No CallSeqEnd
350
351 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000352 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000353 if (TheChain.getValueType() != MVT::Other) {
354 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000355 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000356 if (TheChain.getValueType() != MVT::Other) {
357 // Otherwise, hunt for it.
358 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
359 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000360 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000361 break;
362 }
363
364 // Otherwise, we walked into a node without a chain.
365 if (TheChain.getValueType() != MVT::Other)
366 return 0;
367 }
368 }
369
370 for (SDNode::use_iterator UI = Node->use_begin(),
371 E = Node->use_end(); UI != E; ++UI) {
372
373 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000374 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000375 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
376 if (User->getOperand(i) == TheChain)
377 if (SDNode *Result = FindCallEndFromCallStart(User))
378 return Result;
379 }
380 return 0;
381}
382
383/// FindCallStartFromCallEnd - Given a chained node that is part of a call
384/// sequence, find the CALLSEQ_START node that initiates the call sequence.
385static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
386 assert(Node && "Didn't find callseq_start for a call??");
387 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
388
389 assert(Node->getOperand(0).getValueType() == MVT::Other &&
390 "Node doesn't have a token chain argument!");
391 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
392}
393
394/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
395/// see if any uses can reach Dest. If no dest operands can get to dest,
396/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000397///
398/// Keep track of the nodes we fine that actually do lead to Dest in
399/// NodesLeadingTo. This avoids retraversing them exponential number of times.
400///
401bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000402 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000403 if (N == Dest) return true; // N certainly leads to Dest :)
404
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000405 // If we've already processed this node and it does lead to Dest, there is no
406 // need to reprocess it.
407 if (NodesLeadingTo.count(N)) return true;
408
Chris Lattner6831a812006-02-13 09:18:02 +0000409 // If the first result of this node has been already legalized, then it cannot
410 // reach N.
411 switch (getTypeAction(N->getValueType(0))) {
412 case Legal:
Dan Gohman475871a2008-07-27 21:46:04 +0000413 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000414 break;
415 case Promote:
Dan Gohman475871a2008-07-27 21:46:04 +0000416 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000417 break;
418 case Expand:
Dan Gohman475871a2008-07-27 21:46:04 +0000419 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000420 break;
421 }
422
423 // Okay, this node has not already been legalized. Check and legalize all
424 // operands. If none lead to Dest, then we can legalize this node.
425 bool OperandsLeadToDest = false;
426 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
427 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000428 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000429
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000430 if (OperandsLeadToDest) {
431 NodesLeadingTo.insert(N);
432 return true;
433 }
Chris Lattner6831a812006-02-13 09:18:02 +0000434
435 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000436 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000437 return false;
438}
439
Dan Gohman7f321562007-06-25 16:23:39 +0000440/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000441/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000442void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000443 MVT VT = Op.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000444 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000445 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000446 case Legal: (void)LegalizeOp(Op); break;
447 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000448 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000449 if (!VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000450 // If this is an illegal scalar, expand it into its two component
451 // pieces.
Dan Gohman475871a2008-07-27 21:46:04 +0000452 SDValue X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000453 if (Op.getOpcode() == ISD::TargetConstant)
454 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000455 ExpandOp(Op, X, Y);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000456 } else if (VT.getVectorNumElements() == 1) {
Dan Gohman7f321562007-06-25 16:23:39 +0000457 // If this is an illegal single element vector, convert it to a
458 // scalar operation.
459 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000460 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000461 // Otherwise, this is an illegal multiple element vector.
462 // Split it in half and legalize both parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000463 SDValue X, Y;
Dan Gohman7f321562007-06-25 16:23:39 +0000464 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000465 }
466 break;
467 }
468}
Chris Lattner6831a812006-02-13 09:18:02 +0000469
Evan Cheng9f877882006-12-13 20:57:08 +0000470/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
471/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000472static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000473 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000474 bool Extend = false;
475
476 // If a FP immediate is precise when represented as a float and if the
477 // target can do an extending load from float to double, we put it into
478 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000479 // double. This shrinks FP constants and canonicalizes them for targets where
480 // an FP extending load is the same cost as a normal load (such as on the x87
481 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000482 MVT VT = CFP->getValueType(0);
Chris Lattner02a260a2008-04-20 00:41:09 +0000483 ConstantFP *LLVMC = ConstantFP::get(CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000484 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000485 if (VT!=MVT::f64 && VT!=MVT::f32)
486 assert(0 && "Invalid type expansion");
Dan Gohman6cf9b8a2008-03-11 00:11:06 +0000487 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000488 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000489 }
490
Duncan Sands83ec4b62008-06-06 12:08:01 +0000491 MVT OrigVT = VT;
492 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000493 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000494 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000495 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
496 // Only do this if the target has a native EXTLOAD instruction from
497 // smaller type.
Evan Cheng6fd599f2008-03-05 01:30:59 +0000498 TLI.isLoadXLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000499 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000500 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000501 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
502 VT = SVT;
503 Extend = true;
504 }
Evan Cheng00495212006-12-12 21:32:44 +0000505 }
506
Dan Gohman475871a2008-07-27 21:46:04 +0000507 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Chengef120572008-03-04 08:05:30 +0000508 if (Extend)
509 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000510 CPIdx, PseudoSourceValue::getConstantPool(),
Evan Chengef120572008-03-04 08:05:30 +0000511 0, VT);
512 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
513 PseudoSourceValue::getConstantPool(), 0);
Evan Cheng00495212006-12-12 21:32:44 +0000514}
515
Chris Lattner6831a812006-02-13 09:18:02 +0000516
Evan Cheng912095b2007-01-04 21:56:39 +0000517/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
518/// operations.
519static
Dan Gohman475871a2008-07-27 21:46:04 +0000520SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
521 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000522 MVT VT = Node->getValueType(0);
523 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000524 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
525 "fcopysign expansion only supported for f32 and f64");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000526 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000527
Evan Cheng912095b2007-01-04 21:56:39 +0000528 // First get the sign bit of second operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000529 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000530 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
531 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000532 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman475871a2008-07-27 21:46:04 +0000533 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000534 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000535 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000536 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng912095b2007-01-04 21:56:39 +0000537 if (SizeDiff > 0) {
538 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
539 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
540 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000541 } else if (SizeDiff < 0) {
542 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
543 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
544 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
545 }
Evan Cheng068c5f42007-01-05 21:31:51 +0000546
547 // Clear the sign bit of first operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000548 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng068c5f42007-01-05 21:31:51 +0000549 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
550 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
551 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman475871a2008-07-27 21:46:04 +0000552 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000553 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
554
555 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000556 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
557 return Result;
558}
559
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000560/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
561static
Dan Gohman475871a2008-07-27 21:46:04 +0000562SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
563 TargetLowering &TLI) {
564 SDValue Chain = ST->getChain();
565 SDValue Ptr = ST->getBasePtr();
566 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000567 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000568 int Alignment = ST->getAlignment();
569 int SVOffset = ST->getSrcValueOffset();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000570 if (ST->getMemoryVT().isFloatingPoint() ||
571 ST->getMemoryVT().isVector()) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000572 // Expand to a bitconvert of the value to the integer type of the
573 // same size, then a (misaligned) int store.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000574 MVT intVT;
575 if (VT.is128BitVector() || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000576 intVT = MVT::i128;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000577 else if (VT.is64BitVector() || VT==MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000578 intVT = MVT::i64;
579 else if (VT==MVT::f32)
580 intVT = MVT::i32;
581 else
Dale Johannesencd9f1742008-02-28 18:36:51 +0000582 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000583
Dan Gohman475871a2008-07-27 21:46:04 +0000584 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000585 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
586 SVOffset, ST->isVolatile(), Alignment);
587 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000588 assert(ST->getMemoryVT().isInteger() &&
589 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000590 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000591 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000592 MVT NewStoredVT =
593 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
594 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000595 int IncrementSize = NumBits / 8;
596
597 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000598 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
599 SDValue Lo = Val;
600 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000601
602 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000603 SDValue Store1, Store2;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000604 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
605 ST->getSrcValue(), SVOffset, NewStoredVT,
606 ST->isVolatile(), Alignment);
607 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
608 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000609 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000610 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
611 ST->getSrcValue(), SVOffset + IncrementSize,
612 NewStoredVT, ST->isVolatile(), Alignment);
613
614 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
615}
616
617/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
618static
Dan Gohman475871a2008-07-27 21:46:04 +0000619SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
620 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000621 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000622 SDValue Chain = LD->getChain();
623 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000624 MVT VT = LD->getValueType(0);
625 MVT LoadedVT = LD->getMemoryVT();
626 if (VT.isFloatingPoint() || VT.isVector()) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000627 // Expand to a (misaligned) integer load of the same size,
Dale Johannesen8155d642008-02-27 22:36:00 +0000628 // then bitconvert to floating point or vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000629 MVT intVT;
630 if (LoadedVT.is128BitVector() ||
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000631 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000632 intVT = MVT::i128;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000633 else if (LoadedVT.is64BitVector() || LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000634 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000635 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000636 intVT = MVT::i32;
637 else
Dale Johannesen8155d642008-02-27 22:36:00 +0000638 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000639
Dan Gohman475871a2008-07-27 21:46:04 +0000640 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000641 SVOffset, LD->isVolatile(),
642 LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +0000643 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000644 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000645 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
646
Dan Gohman475871a2008-07-27 21:46:04 +0000647 SDValue Ops[] = { Result, Chain };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000648 return DAG.getMergeValues(Ops, 2);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000649 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000650 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000651 "Unaligned load of unsupported type.");
652
Dale Johannesen8155d642008-02-27 22:36:00 +0000653 // Compute the new VT that is half the size of the old one. This is an
654 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000655 unsigned NumBits = LoadedVT.getSizeInBits();
656 MVT NewLoadedVT;
657 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000658 NumBits >>= 1;
659
660 unsigned Alignment = LD->getAlignment();
661 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000662 ISD::LoadExtType HiExtType = LD->getExtensionType();
663
664 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
665 if (HiExtType == ISD::NON_EXTLOAD)
666 HiExtType = ISD::ZEXTLOAD;
667
668 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000669 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000670 if (TLI.isLittleEndian()) {
671 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
672 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
673 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
674 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
675 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
676 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000677 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000678 } else {
679 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
680 NewLoadedVT,LD->isVolatile(), Alignment);
681 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
682 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
683 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
684 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000685 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000686 }
687
688 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000689 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
690 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000691 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
692
Dan Gohman475871a2008-07-27 21:46:04 +0000693 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000694 Hi.getValue(1));
695
Dan Gohman475871a2008-07-27 21:46:04 +0000696 SDValue Ops[] = { Result, TF };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000697 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000698}
Evan Cheng912095b2007-01-04 21:56:39 +0000699
Dan Gohman82669522007-10-11 23:57:53 +0000700/// UnrollVectorOp - We know that the given vector has a legal type, however
701/// the operation it performs is not legal and is an operation that we have
702/// no way of lowering. "Unroll" the vector, splitting out the scalars and
703/// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000704SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000705 MVT VT = Op.getValueType();
Dan Gohman82669522007-10-11 23:57:53 +0000706 assert(isTypeLegal(VT) &&
707 "Caller should expand or promote operands that are not legal!");
708 assert(Op.Val->getNumValues() == 1 &&
709 "Can't unroll a vector with multiple results!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000710 unsigned NE = VT.getVectorNumElements();
711 MVT EltVT = VT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000712
Dan Gohman475871a2008-07-27 21:46:04 +0000713 SmallVector<SDValue, 8> Scalars;
714 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman82669522007-10-11 23:57:53 +0000715 for (unsigned i = 0; i != NE; ++i) {
716 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +0000717 SDValue Operand = Op.getOperand(j);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000718 MVT OperandVT = Operand.getValueType();
719 if (OperandVT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +0000720 // A vector operand; extract a single element.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000721 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000722 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
723 OperandEltVT,
724 Operand,
725 DAG.getConstant(i, MVT::i32));
726 } else {
727 // A scalar operand; just use it as is.
728 Operands[j] = Operand;
729 }
730 }
731 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
732 &Operands[0], Operands.size()));
733 }
734
735 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
736}
737
Duncan Sands007f9842008-01-10 10:28:30 +0000738/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000739static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000740 RTLIB::Libcall Call_F32,
741 RTLIB::Libcall Call_F64,
742 RTLIB::Libcall Call_F80,
743 RTLIB::Libcall Call_PPCF128) {
744 return
745 VT == MVT::f32 ? Call_F32 :
746 VT == MVT::f64 ? Call_F64 :
747 VT == MVT::f80 ? Call_F80 :
748 VT == MVT::ppcf128 ? Call_PPCF128 :
749 RTLIB::UNKNOWN_LIBCALL;
750}
751
Nate Begeman68679912008-04-25 18:07:40 +0000752/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
753/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
754/// is necessary to spill the vector being inserted into to memory, perform
755/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000756SDValue SelectionDAGLegalize::
757PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
758 SDValue Tmp1 = Vec;
759 SDValue Tmp2 = Val;
760 SDValue Tmp3 = Idx;
Nate Begeman68679912008-04-25 18:07:40 +0000761
762 // If the target doesn't support this, we have to spill the input vector
763 // to a temporary stack slot, update the element, then reload it. This is
764 // badness. We could also load the value into a vector register (either
765 // with a "move to register" or "extload into register" instruction, then
766 // permute it into place, if the idx is a constant and if the idx is
767 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000768 MVT VT = Tmp1.getValueType();
769 MVT EltVT = VT.getVectorElementType();
770 MVT IdxVT = Tmp3.getValueType();
771 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000772 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000773
Dan Gohmana54cf172008-07-11 22:44:52 +0000774 int SPFI = cast<FrameIndexSDNode>(StackPtr.Val)->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000775
776 // Store the vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000777 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +0000778 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000779
780 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000781 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman68679912008-04-25 18:07:40 +0000782 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
783 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000784 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman68679912008-04-25 18:07:40 +0000785 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman475871a2008-07-27 21:46:04 +0000786 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000787 // Store the scalar value.
788 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000789 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000790 // Load the updated vector.
Dan Gohmana54cf172008-07-11 22:44:52 +0000791 return DAG.getLoad(VT, Ch, StackPtr,
792 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000793}
794
Dan Gohmana3466152007-07-13 20:14:11 +0000795/// LegalizeOp - We know that the specified value has a legal type, and
796/// that its operands are legal. Now ensure that the operation itself
797/// is legal, recursively ensuring that the operands' operations remain
798/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000799SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000800 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
801 return Op;
802
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000803 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000804 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000805 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000806
Chris Lattner3e928bb2005-01-07 07:47:09 +0000807 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000808 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000809 if (Node->getNumValues() > 1) {
810 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000811 if (getTypeAction(Node->getValueType(i)) != Legal) {
812 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000813 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000814 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000815 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000816 }
817 }
818
Chris Lattner45982da2005-05-12 16:53:42 +0000819 // Note that LegalizeOp may be reentered even from single-use nodes, which
820 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000821 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000822 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000823
Dan Gohman475871a2008-07-27 21:46:04 +0000824 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
825 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000826 bool isCustom = false;
827
Chris Lattner3e928bb2005-01-07 07:47:09 +0000828 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000829 case ISD::FrameIndex:
830 case ISD::EntryToken:
831 case ISD::Register:
832 case ISD::BasicBlock:
833 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000834 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000835 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000836 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000837 case ISD::TargetConstantPool:
838 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000839 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000840 case ISD::TargetExternalSymbol:
841 case ISD::VALUETYPE:
842 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +0000843 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +0000844 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +0000845 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +0000846 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000847 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000848 "This must be legal!");
849 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000850 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000851 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
852 // If this is a target node, legalize it by legalizing the operands then
853 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +0000854 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000855 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000856 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000857
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000858 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000859
860 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
861 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
862 return Result.getValue(Op.ResNo);
863 }
864 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000865#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000866 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000867#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000868 assert(0 && "Do not know how to legalize this operator!");
869 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000870 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000871 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000872 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000873 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000874 case ISD::ConstantPool:
875 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000876 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
877 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000878 case TargetLowering::Custom:
879 Tmp1 = TLI.LowerOperation(Op, DAG);
880 if (Tmp1.Val) Result = Tmp1;
881 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000882 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000883 break;
884 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000885 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000886 case ISD::FRAMEADDR:
887 case ISD::RETURNADDR:
888 // The only option for these nodes is to custom lower them. If the target
889 // does not custom lower them, then return zero.
890 Tmp1 = TLI.LowerOperation(Op, DAG);
891 if (Tmp1.Val)
892 Result = Tmp1;
893 else
894 Result = DAG.getConstant(0, TLI.getPointerTy());
895 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000896 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000897 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000898 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
899 default: assert(0 && "This action is not supported yet!");
900 case TargetLowering::Custom:
901 Result = TLI.LowerOperation(Op, DAG);
902 if (Result.Val) break;
903 // Fall Thru
904 case TargetLowering::Legal:
905 Result = DAG.getConstant(0, VT);
906 break;
907 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000908 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000909 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000910 case ISD::EXCEPTIONADDR: {
911 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000912 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000913 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
914 default: assert(0 && "This action is not supported yet!");
915 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000916 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000917 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000918 }
919 break;
920 case TargetLowering::Custom:
921 Result = TLI.LowerOperation(Op, DAG);
922 if (Result.Val) break;
923 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000924 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000925 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000926 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000927 break;
928 }
929 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000930 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000931 if (Result.Val->getNumValues() == 1) break;
932
933 assert(Result.Val->getNumValues() == 2 &&
934 "Cannot return more than two values!");
935
936 // Since we produced two values, make sure to remember that we
937 // legalized both of them.
938 Tmp1 = LegalizeOp(Result);
939 Tmp2 = LegalizeOp(Result.getValue(1));
940 AddLegalizedOperand(Op.getValue(0), Tmp1);
941 AddLegalizedOperand(Op.getValue(1), Tmp2);
942 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000943 case ISD::EHSELECTION: {
944 Tmp1 = LegalizeOp(Node->getOperand(0));
945 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000946 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +0000947 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
948 default: assert(0 && "This action is not supported yet!");
949 case TargetLowering::Expand: {
950 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000951 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000952 }
953 break;
954 case TargetLowering::Custom:
955 Result = TLI.LowerOperation(Op, DAG);
956 if (Result.Val) break;
957 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000958 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000959 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000960 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +0000961 break;
962 }
963 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000964 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000965 if (Result.Val->getNumValues() == 1) break;
966
967 assert(Result.Val->getNumValues() == 2 &&
968 "Cannot return more than two values!");
969
970 // Since we produced two values, make sure to remember that we
971 // legalized both of them.
972 Tmp1 = LegalizeOp(Result);
973 Tmp2 = LegalizeOp(Result.getValue(1));
974 AddLegalizedOperand(Op.getValue(0), Tmp1);
975 AddLegalizedOperand(Op.getValue(1), Tmp2);
976 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000977 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000978 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000979 // The only "good" option for this node is to custom lower it.
980 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
981 default: assert(0 && "This action is not supported at all!");
982 case TargetLowering::Custom:
983 Result = TLI.LowerOperation(Op, DAG);
984 if (Result.Val) break;
985 // Fall Thru
986 case TargetLowering::Legal:
987 // Target does not know, how to lower this, lower to noop
988 Result = LegalizeOp(Node->getOperand(0));
989 break;
990 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000991 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000992 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000993 case ISD::AssertSext:
994 case ISD::AssertZext:
995 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000996 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000997 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000998 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000999 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +00001000 Result = Node->getOperand(Op.ResNo);
1001 break;
Chris Lattner69a52152005-01-14 22:38:01 +00001002 case ISD::CopyFromReg:
1003 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001004 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001005 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001006 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +00001007 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001008 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001009 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001010 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001011 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1012 } else {
1013 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1014 }
Chris Lattner7310fb12005-12-18 15:27:43 +00001015 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1016 }
Chris Lattner13c184d2005-01-28 06:27:38 +00001017 // Since CopyFromReg produces two values, make sure to remember that we
1018 // legalized both of them.
1019 AddLegalizedOperand(Op.getValue(0), Result);
1020 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1021 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001022 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001023 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001024 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +00001025 default: assert(0 && "This action is not supported yet!");
1026 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001027 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001028 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001029 else if (VT.isFloatingPoint())
1030 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +00001031 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001032 else
1033 assert(0 && "Unknown value type!");
1034 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001035 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001036 break;
1037 }
1038 break;
1039 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001040
Chris Lattner48b61a72006-03-28 00:40:33 +00001041 case ISD::INTRINSIC_W_CHAIN:
1042 case ISD::INTRINSIC_WO_CHAIN:
1043 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +00001044 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001045 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1046 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001047 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001048
1049 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001050 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001051 TargetLowering::Custom) {
1052 Tmp3 = TLI.LowerOperation(Result, DAG);
1053 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001054 }
1055
1056 if (Result.Val->getNumValues() == 1) break;
1057
1058 // Must have return value and chain result.
1059 assert(Result.Val->getNumValues() == 2 &&
1060 "Cannot return more than two values!");
1061
1062 // Since loads produce two values, make sure to remember that we
1063 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001064 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1065 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattner13fc2f12006-03-27 20:28:29 +00001066 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001067 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001068
Dan Gohman7f460202008-06-30 20:59:49 +00001069 case ISD::DBG_STOPPOINT:
1070 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +00001071 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1072
Dan Gohman7f460202008-06-30 20:59:49 +00001073 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +00001074 case TargetLowering::Promote:
1075 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001076 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001077 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001078 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohman44066042008-07-01 00:05:16 +00001079 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001080
Dan Gohman7f460202008-06-30 20:59:49 +00001081 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001082 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman7f460202008-06-30 20:59:49 +00001083 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1084 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001085
Dan Gohman7f460202008-06-30 20:59:49 +00001086 unsigned Line = DSP->getLine();
1087 unsigned Col = DSP->getColumn();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001088
1089 if (useDEBUG_LOC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001090 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng71e86852008-07-08 20:06:39 +00001091 DAG.getConstant(Col, MVT::i32),
1092 DAG.getConstant(SrcFile, MVT::i32) };
1093 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001094 } else {
Evan Chenga647c922008-02-01 02:05:57 +00001095 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohman44066042008-07-01 00:05:16 +00001096 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001097 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001098 } else {
1099 Result = Tmp1; // chain
1100 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001101 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001102 }
Evan Cheng71e86852008-07-08 20:06:39 +00001103 case TargetLowering::Legal: {
1104 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1105 if (Action == Legal && Tmp1 == Node->getOperand(0))
1106 break;
1107
Dan Gohman475871a2008-07-27 21:46:04 +00001108 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001109 Ops.push_back(Tmp1);
1110 if (Action == Legal) {
1111 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1112 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1113 } else {
1114 // Otherwise promote them.
1115 Ops.push_back(PromoteOp(Node->getOperand(1)));
1116 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner36ce6912005-11-29 06:21:05 +00001117 }
Evan Cheng71e86852008-07-08 20:06:39 +00001118 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1119 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1120 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001121 break;
1122 }
Evan Cheng71e86852008-07-08 20:06:39 +00001123 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001124 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001125
1126 case ISD::DECLARE:
1127 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1128 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1129 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 address.
1133 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1134 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1135 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001136 case TargetLowering::Expand:
1137 Result = LegalizeOp(Node->getOperand(0));
1138 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001139 }
1140 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001141
1142 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001143 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001144 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001145 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001146 case TargetLowering::Legal: {
1147 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001148 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng71e86852008-07-08 20:06:39 +00001149 if (Action == Legal && Tmp1 == Node->getOperand(0))
1150 break;
1151 if (Action == Legal) {
1152 Tmp2 = Node->getOperand(1);
1153 Tmp3 = Node->getOperand(2);
1154 Tmp4 = Node->getOperand(3);
1155 } else {
1156 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1157 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1158 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1159 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001160 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001161 break;
1162 }
Evan Cheng71e86852008-07-08 20:06:39 +00001163 }
Jim Laskeyabf6d172006-01-05 01:25:28 +00001164 break;
1165
Dan Gohman44066042008-07-01 00:05:16 +00001166 case ISD::DBG_LABEL:
1167 case ISD::EH_LABEL:
1168 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1169 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001170 default: assert(0 && "This action is not supported yet!");
1171 case TargetLowering::Legal:
1172 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001173 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001174 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001175 case TargetLowering::Expand:
1176 Result = LegalizeOp(Node->getOperand(0));
1177 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001178 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001179 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001180
Evan Cheng27b7db52008-03-08 00:58:38 +00001181 case ISD::PREFETCH:
1182 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1183 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1184 default: assert(0 && "This action is not supported yet!");
1185 case TargetLowering::Legal:
1186 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1187 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1188 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1189 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1190 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1191 break;
1192 case TargetLowering::Expand:
1193 // It's a noop.
1194 Result = LegalizeOp(Node->getOperand(0));
1195 break;
1196 }
1197 break;
1198
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001199 case ISD::MEMBARRIER: {
1200 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001201 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1202 default: assert(0 && "This action is not supported yet!");
1203 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001204 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001205 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001206 for (int x = 1; x < 6; ++x) {
1207 Ops[x] = Node->getOperand(x);
1208 if (!isTypeLegal(Ops[x].getValueType()))
1209 Ops[x] = PromoteOp(Ops[x]);
1210 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001211 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1212 break;
1213 }
1214 case TargetLowering::Expand:
1215 //There is no libgcc call for this op
1216 Result = Node->getOperand(0); // Noop
1217 break;
1218 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001219 break;
1220 }
1221
Mon P Wang28873102008-06-25 08:15:39 +00001222 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001223 unsigned int num_operands = 4;
1224 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001225 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001226 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001227 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001228 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1229
1230 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1231 default: assert(0 && "This action is not supported yet!");
1232 case TargetLowering::Custom:
1233 Result = TLI.LowerOperation(Result, DAG);
1234 break;
1235 case TargetLowering::Legal:
1236 break;
1237 }
Dan Gohman475871a2008-07-27 21:46:04 +00001238 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1239 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Mon P Wang63307c32008-05-05 19:05:59 +00001240 return Result.getValue(Op.ResNo);
Duncan Sands126d9072008-07-04 11:47:58 +00001241 }
Mon P Wang28873102008-06-25 08:15:39 +00001242 case ISD::ATOMIC_LOAD_ADD:
1243 case ISD::ATOMIC_LOAD_SUB:
Mon P Wang63307c32008-05-05 19:05:59 +00001244 case ISD::ATOMIC_LOAD_AND:
1245 case ISD::ATOMIC_LOAD_OR:
1246 case ISD::ATOMIC_LOAD_XOR:
Andrew Lenharth507a58a2008-06-14 05:48:15 +00001247 case ISD::ATOMIC_LOAD_NAND:
Mon P Wang63307c32008-05-05 19:05:59 +00001248 case ISD::ATOMIC_LOAD_MIN:
1249 case ISD::ATOMIC_LOAD_MAX:
1250 case ISD::ATOMIC_LOAD_UMIN:
1251 case ISD::ATOMIC_LOAD_UMAX:
1252 case ISD::ATOMIC_SWAP: {
1253 unsigned int num_operands = 3;
1254 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001255 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001256 for (unsigned int x = 0; x < num_operands; ++x)
1257 Ops[x] = LegalizeOp(Node->getOperand(x));
1258 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001259
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001260 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001261 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001262 case TargetLowering::Custom:
1263 Result = TLI.LowerOperation(Result, DAG);
1264 break;
Mon P Wang63307c32008-05-05 19:05:59 +00001265 case TargetLowering::Expand:
Dan Gohman475871a2008-07-27 21:46:04 +00001266 Result = SDValue(TLI.ReplaceNodeResults(Op.Val, DAG),0);
Mon P Wang63307c32008-05-05 19:05:59 +00001267 break;
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001268 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001269 break;
1270 }
Dan Gohman475871a2008-07-27 21:46:04 +00001271 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1272 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001273 return Result.getValue(Op.ResNo);
Duncan Sands126d9072008-07-04 11:47:58 +00001274 }
Scott Michelc1513d22007-08-08 23:23:31 +00001275 case ISD::Constant: {
1276 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1277 unsigned opAction =
1278 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1279
Chris Lattner3e928bb2005-01-07 07:47:09 +00001280 // We know we don't need to expand constants here, constants only have one
1281 // value and we check that it is fine above.
1282
Scott Michelc1513d22007-08-08 23:23:31 +00001283 if (opAction == TargetLowering::Custom) {
1284 Tmp1 = TLI.LowerOperation(Result, DAG);
1285 if (Tmp1.Val)
1286 Result = Tmp1;
1287 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001288 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001289 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001290 case ISD::ConstantFP: {
1291 // Spill FP immediates to the constant pool if the target cannot directly
1292 // codegen them. Targets often have some immediate values that can be
1293 // efficiently generated into an FP register without a load. We explicitly
1294 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001295 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1296
Chris Lattner3181a772006-01-29 06:26:56 +00001297 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1298 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001299 case TargetLowering::Legal:
1300 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001301 case TargetLowering::Custom:
1302 Tmp3 = TLI.LowerOperation(Result, DAG);
1303 if (Tmp3.Val) {
1304 Result = Tmp3;
1305 break;
1306 }
1307 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001308 case TargetLowering::Expand: {
1309 // Check to see if this FP immediate is already legal.
1310 bool isLegal = false;
1311 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1312 E = TLI.legal_fpimm_end(); I != E; ++I) {
1313 if (CFP->isExactlyValue(*I)) {
1314 isLegal = true;
1315 break;
1316 }
1317 }
1318 // If this is a legal constant, turn it into a TargetConstantFP node.
1319 if (isLegal)
1320 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001321 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001322 }
Nate Begemane1795842008-02-14 08:57:00 +00001323 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001324 break;
1325 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001326 case ISD::TokenFactor:
1327 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001328 Tmp1 = LegalizeOp(Node->getOperand(0));
1329 Tmp2 = LegalizeOp(Node->getOperand(1));
1330 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1331 } else if (Node->getNumOperands() == 3) {
1332 Tmp1 = LegalizeOp(Node->getOperand(0));
1333 Tmp2 = LegalizeOp(Node->getOperand(1));
1334 Tmp3 = LegalizeOp(Node->getOperand(2));
1335 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001336 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001337 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001338 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001339 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1340 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001341 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001342 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001343 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001344
1345 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001346 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001347 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001348 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1349 assert(Tmp3.Val && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001350 // A call within a calling sequence must be legalized to something
1351 // other than the normal CALLSEQ_END. Violating this gets Legalize
1352 // into an infinite loop.
1353 assert ((!IsLegalizingCall ||
1354 Node->getOpcode() != ISD::CALL ||
1355 Tmp3.Val->getOpcode() != ISD::CALLSEQ_END) &&
1356 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001357
1358 // The number of incoming and outgoing values should match; unless the final
1359 // outgoing value is a flag.
1360 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1361 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1362 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1363 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001364 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001365
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001366 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001367 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001368 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001369 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1370 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001371 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001372 if (Op.ResNo == i)
1373 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001374 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001375 }
1376 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001377 case ISD::EXTRACT_SUBREG: {
1378 Tmp1 = LegalizeOp(Node->getOperand(0));
1379 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1380 assert(idx && "Operand must be a constant");
1381 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1382 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1383 }
1384 break;
1385 case ISD::INSERT_SUBREG: {
1386 Tmp1 = LegalizeOp(Node->getOperand(0));
1387 Tmp2 = LegalizeOp(Node->getOperand(1));
1388 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1389 assert(idx && "Operand must be a constant");
1390 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1391 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1392 }
1393 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001394 case ISD::BUILD_VECTOR:
1395 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001396 default: assert(0 && "This action is not supported yet!");
1397 case TargetLowering::Custom:
1398 Tmp3 = TLI.LowerOperation(Result, DAG);
1399 if (Tmp3.Val) {
1400 Result = Tmp3;
1401 break;
1402 }
1403 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001404 case TargetLowering::Expand:
1405 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001406 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001407 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001408 break;
1409 case ISD::INSERT_VECTOR_ELT:
1410 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001411 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001412
1413 // The type of the value to insert may not be legal, even though the vector
1414 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1415 // here.
1416 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1417 default: assert(0 && "Cannot expand insert element operand");
1418 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1419 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1420 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001421 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1422
1423 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1424 Node->getValueType(0))) {
1425 default: assert(0 && "This action is not supported yet!");
1426 case TargetLowering::Legal:
1427 break;
1428 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001429 Tmp4 = TLI.LowerOperation(Result, DAG);
1430 if (Tmp4.Val) {
1431 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001432 break;
1433 }
1434 // FALLTHROUGH
1435 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001436 // If the insert index is a constant, codegen this as a scalar_to_vector,
1437 // then a shuffle that inserts it into the right position in the vector.
1438 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001439 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1440 // match the element type of the vector being created.
1441 if (Tmp2.getValueType() ==
Duncan Sands83ec4b62008-06-06 12:08:01 +00001442 Op.getValueType().getVectorElementType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001443 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman0325d902008-02-13 06:43:04 +00001444 Tmp1.getValueType(), Tmp2);
1445
Duncan Sands83ec4b62008-06-06 12:08:01 +00001446 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1447 MVT ShufMaskVT =
1448 MVT::getIntVectorWithNumElements(NumElts);
1449 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman0325d902008-02-13 06:43:04 +00001450
1451 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1452 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1453 // elt 0 of the RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00001454 SmallVector<SDValue, 8> ShufOps;
Nate Begeman0325d902008-02-13 06:43:04 +00001455 for (unsigned i = 0; i != NumElts; ++i) {
1456 if (i != InsertPos->getValue())
1457 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1458 else
1459 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1460 }
Dan Gohman475871a2008-07-27 21:46:04 +00001461 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman0325d902008-02-13 06:43:04 +00001462 &ShufOps[0], ShufOps.size());
1463
1464 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1465 Tmp1, ScVec, ShufMask);
1466 Result = LegalizeOp(Result);
1467 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001468 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001469 }
Nate Begeman68679912008-04-25 18:07:40 +00001470 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001471 break;
1472 }
1473 }
1474 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001475 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001476 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1477 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1478 break;
1479 }
1480
Chris Lattnerce872152006-03-19 06:31:19 +00001481 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1482 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1483 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1484 Node->getValueType(0))) {
1485 default: assert(0 && "This action is not supported yet!");
1486 case TargetLowering::Legal:
1487 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001488 case TargetLowering::Custom:
1489 Tmp3 = TLI.LowerOperation(Result, DAG);
1490 if (Tmp3.Val) {
1491 Result = Tmp3;
1492 break;
1493 }
1494 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001495 case TargetLowering::Expand:
1496 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001497 break;
1498 }
Chris Lattnerce872152006-03-19 06:31:19 +00001499 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001500 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001501 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1502 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1503 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1504
1505 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001506 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1507 default: assert(0 && "Unknown operation action!");
1508 case TargetLowering::Legal:
1509 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1510 "vector shuffle should not be created if not legal!");
1511 break;
1512 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001513 Tmp3 = TLI.LowerOperation(Result, DAG);
1514 if (Tmp3.Val) {
1515 Result = Tmp3;
1516 break;
1517 }
1518 // FALLTHROUGH
1519 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001520 MVT VT = Node->getValueType(0);
1521 MVT EltVT = VT.getVectorElementType();
1522 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001523 SDValue Mask = Node->getOperand(2);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001524 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001525 SmallVector<SDValue,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001526 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001527 SDValue Arg = Mask.getOperand(i);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001528 if (Arg.getOpcode() == ISD::UNDEF) {
1529 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1530 } else {
1531 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1532 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1533 if (Idx < NumElems)
1534 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1535 DAG.getConstant(Idx, PtrVT)));
1536 else
1537 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1538 DAG.getConstant(Idx - NumElems, PtrVT)));
1539 }
1540 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001541 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001542 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001543 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001544 case TargetLowering::Promote: {
1545 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001546 MVT OVT = Node->getValueType(0);
1547 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001548
1549 // Cast the two input vectors.
1550 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1551 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1552
1553 // Convert the shuffle mask to the right # elements.
Dan Gohman475871a2008-07-27 21:46:04 +00001554 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00001555 assert(Tmp3.Val && "Shuffle not legal?");
1556 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1557 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1558 break;
1559 }
Chris Lattner87100e02006-03-20 01:52:29 +00001560 }
1561 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001562
1563 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001564 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001565 Tmp2 = LegalizeOp(Node->getOperand(1));
1566 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001567 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001568 break;
1569
Dan Gohman7f321562007-06-25 16:23:39 +00001570 case ISD::EXTRACT_SUBVECTOR:
1571 Tmp1 = Node->getOperand(0);
1572 Tmp2 = LegalizeOp(Node->getOperand(1));
1573 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1574 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001575 break;
1576
Chris Lattner6831a812006-02-13 09:18:02 +00001577 case ISD::CALLSEQ_START: {
1578 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1579
1580 // Recursively Legalize all of the inputs of the call end that do not lead
1581 // to this call start. This ensures that any libcalls that need be inserted
1582 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001583 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001584 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001585 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1586 NodesLeadingTo);
1587 }
Chris Lattner6831a812006-02-13 09:18:02 +00001588
1589 // Now that we legalized all of the inputs (which may have inserted
1590 // libcalls) create the new CALLSEQ_START node.
1591 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1592
1593 // Merge in the last call, to ensure that this call start after the last
1594 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001595 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001596 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1597 Tmp1 = LegalizeOp(Tmp1);
1598 }
Chris Lattner6831a812006-02-13 09:18:02 +00001599
1600 // Do not try to legalize the target-specific arguments (#1+).
1601 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001602 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001603 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001604 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001605 }
1606
1607 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001608 AddLegalizedOperand(Op.getValue(0), Result);
1609 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1610 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1611
Chris Lattner6831a812006-02-13 09:18:02 +00001612 // Now that the callseq_start and all of the non-call nodes above this call
1613 // sequence have been legalized, legalize the call itself. During this
1614 // process, no libcalls can/will be inserted, guaranteeing that no calls
1615 // can overlap.
1616 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001617 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001618 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001619 IsLegalizingCall = true;
1620
1621 // Legalize the call, starting from the CALLSEQ_END.
1622 LegalizeOp(LastCALLSEQ_END);
1623 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1624 return Result;
1625 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001626 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001627 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1628 // will cause this node to be legalized as well as handling libcalls right.
1629 if (LastCALLSEQ_END.Val != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001630 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1631 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001632 assert(I != LegalizedNodes.end() &&
1633 "Legalizing the call start should have legalized this node!");
1634 return I->second;
1635 }
1636
1637 // Otherwise, the call start has been legalized and everything is going
1638 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001639 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001640 // Do not try to legalize the target-specific arguments (#1+), except for
1641 // an optional flag input.
1642 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1643 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001644 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001645 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001646 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001647 }
1648 } else {
1649 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1650 if (Tmp1 != Node->getOperand(0) ||
1651 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001652 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001653 Ops[0] = Tmp1;
1654 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001655 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001656 }
Chris Lattner6a542892006-01-24 05:48:21 +00001657 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001658 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001659 // This finishes up call legalization.
1660 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001661
1662 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001663 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001664 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001665 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattner4b653a02006-02-14 00:55:02 +00001666 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001667 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001668 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001669 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1670 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1671 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001672 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001673
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001674 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001675 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001676 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001677 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001678 case TargetLowering::Expand: {
1679 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1680 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1681 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001682 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001683
1684 // Chain the dynamic stack allocation so that it doesn't modify the stack
1685 // pointer when other instructions are using the stack.
1686 Chain = DAG.getCALLSEQ_START(Chain,
1687 DAG.getConstant(0, TLI.getPointerTy()));
1688
Dan Gohman475871a2008-07-27 21:46:04 +00001689 SDValue Size = Tmp2.getOperand(1);
1690 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001691 Chain = SP.getValue(1);
1692 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1693 unsigned StackAlign =
1694 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1695 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001696 SP = DAG.getNode(ISD::AND, VT, SP,
1697 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001698 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001699 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1700
1701 Tmp2 =
1702 DAG.getCALLSEQ_END(Chain,
1703 DAG.getConstant(0, TLI.getPointerTy()),
1704 DAG.getConstant(0, TLI.getPointerTy()),
Dan Gohman475871a2008-07-27 21:46:04 +00001705 SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001706
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001707 Tmp1 = LegalizeOp(Tmp1);
1708 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001709 break;
1710 }
1711 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001712 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1713 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001714 Tmp1 = LegalizeOp(Tmp3);
1715 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001716 }
Chris Lattner903d2782006-01-15 08:54:32 +00001717 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001718 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001719 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001720 }
Chris Lattner903d2782006-01-15 08:54:32 +00001721 // Since this op produce two values, make sure to remember that we
1722 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001723 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1724 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001725 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001726 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001727 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001728 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001729 bool Changed = false;
1730 // Legalize all of the operands of the inline asm, in case they are nodes
1731 // that need to be expanded or something. Note we skip the asm string and
1732 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001733 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001734 Changed = Op != Ops[0];
1735 Ops[0] = Op;
1736
1737 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1738 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1739 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1740 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001741 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001742 if (Op != Ops[i]) {
1743 Changed = true;
1744 Ops[i] = Op;
1745 }
1746 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001747 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001748
1749 if (HasInFlag) {
1750 Op = LegalizeOp(Ops.back());
1751 Changed |= Op != Ops.back();
1752 Ops.back() = Op;
1753 }
1754
1755 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001756 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001757
1758 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001759 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1760 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001761 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001762 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001763 case ISD::BR:
1764 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001765 // Ensure that libcalls are emitted before a branch.
1766 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1767 Tmp1 = LegalizeOp(Tmp1);
1768 LastCALLSEQ_END = DAG.getEntryNode();
1769
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001770 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001771 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001772 case ISD::BRIND:
1773 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1774 // Ensure that libcalls are emitted before a branch.
1775 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1776 Tmp1 = LegalizeOp(Tmp1);
1777 LastCALLSEQ_END = DAG.getEntryNode();
1778
1779 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1780 default: assert(0 && "Indirect target must be legal type (pointer)!");
1781 case Legal:
1782 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1783 break;
1784 }
1785 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1786 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001787 case ISD::BR_JT:
1788 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1789 // Ensure that libcalls are emitted before a branch.
1790 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1791 Tmp1 = LegalizeOp(Tmp1);
1792 LastCALLSEQ_END = DAG.getEntryNode();
1793
1794 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1795 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1796
1797 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1798 default: assert(0 && "This action is not supported yet!");
1799 case TargetLowering::Legal: break;
1800 case TargetLowering::Custom:
1801 Tmp1 = TLI.LowerOperation(Result, DAG);
1802 if (Tmp1.Val) Result = Tmp1;
1803 break;
1804 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00001805 SDValue Chain = Result.getOperand(0);
1806 SDValue Table = Result.getOperand(1);
1807 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001808
Duncan Sands83ec4b62008-06-06 12:08:01 +00001809 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001810 MachineFunction &MF = DAG.getMachineFunction();
1811 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001812 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman475871a2008-07-27 21:46:04 +00001813 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001814
Dan Gohman475871a2008-07-27 21:46:04 +00001815 SDValue LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001816 switch (EntrySize) {
1817 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001818 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001819 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001820 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001821 PseudoSourceValue::getJumpTable(), 0); break;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001822 }
1823
Evan Chengcc415862007-11-09 01:32:10 +00001824 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001825 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001826 // For PIC, the sequence is:
1827 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001828 // RelocBase can be JumpTable, GOT or some sort of global base.
1829 if (PTy != MVT::i32)
1830 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1831 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1832 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001833 }
Evan Chengcc415862007-11-09 01:32:10 +00001834 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001835 }
1836 }
1837 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001838 case ISD::BRCOND:
1839 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001840 // Ensure that libcalls are emitted before a return.
1841 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1842 Tmp1 = LegalizeOp(Tmp1);
1843 LastCALLSEQ_END = DAG.getEntryNode();
1844
Chris Lattner47e92232005-01-18 19:27:06 +00001845 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1846 case Expand: assert(0 && "It's impossible to expand bools");
1847 case Legal:
1848 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1849 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001850 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00001851 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001852
1853 // The top bits of the promoted condition are not necessarily zero, ensure
1854 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001855 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001856 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001857 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00001858 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001859 break;
1860 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001861 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001862
1863 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001864 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001865
1866 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1867 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001868 case TargetLowering::Legal: break;
1869 case TargetLowering::Custom:
1870 Tmp1 = TLI.LowerOperation(Result, DAG);
1871 if (Tmp1.Val) Result = Tmp1;
1872 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001873 case TargetLowering::Expand:
1874 // Expand brcond's setcc into its constituent parts and create a BR_CC
1875 // Node.
1876 if (Tmp2.getOpcode() == ISD::SETCC) {
1877 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1878 Tmp2.getOperand(0), Tmp2.getOperand(1),
1879 Node->getOperand(2));
1880 } else {
1881 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1882 DAG.getCondCode(ISD::SETNE), Tmp2,
1883 DAG.getConstant(0, Tmp2.getValueType()),
1884 Node->getOperand(2));
1885 }
1886 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001887 }
1888 break;
1889 case ISD::BR_CC:
1890 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001891 // Ensure that libcalls are emitted before a branch.
1892 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1893 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001894 Tmp2 = Node->getOperand(2); // LHS
1895 Tmp3 = Node->getOperand(3); // RHS
1896 Tmp4 = Node->getOperand(1); // CC
1897
1898 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001899 LastCALLSEQ_END = DAG.getEntryNode();
1900
Nate Begeman750ac1b2006-02-01 07:19:44 +00001901 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1902 // the LHS is a legal SETCC itself. In this case, we need to compare
1903 // the result against zero to select between true and false values.
1904 if (Tmp3.Val == 0) {
1905 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1906 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001907 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001908
1909 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1910 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001911
Chris Lattner181b7a32005-12-17 23:46:46 +00001912 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1913 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001914 case TargetLowering::Legal: break;
1915 case TargetLowering::Custom:
1916 Tmp4 = TLI.LowerOperation(Result, DAG);
1917 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001918 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001919 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001920 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001921 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001922 LoadSDNode *LD = cast<LoadSDNode>(Node);
1923 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1924 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001925
Evan Cheng466685d2006-10-09 20:57:25 +00001926 ISD::LoadExtType ExtType = LD->getExtensionType();
1927 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001928 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00001929 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1930 Tmp3 = Result.getValue(0);
1931 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001932
Evan Cheng466685d2006-10-09 20:57:25 +00001933 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1934 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001935 case TargetLowering::Legal:
1936 // If this is an unaligned load and the target doesn't support it,
1937 // expand it.
1938 if (!TLI.allowsUnalignedMemoryAccesses()) {
1939 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00001940 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001941 if (LD->getAlignment() < ABIAlignment){
1942 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1943 TLI);
1944 Tmp3 = Result.getOperand(0);
1945 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001946 Tmp3 = LegalizeOp(Tmp3);
1947 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001948 }
1949 }
1950 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001951 case TargetLowering::Custom:
1952 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1953 if (Tmp1.Val) {
1954 Tmp3 = LegalizeOp(Tmp1);
1955 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001956 }
Evan Cheng466685d2006-10-09 20:57:25 +00001957 break;
1958 case TargetLowering::Promote: {
1959 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001960 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00001961 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001962 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00001963
1964 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001965 LD->getSrcValueOffset(),
1966 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001967 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1968 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001969 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001970 }
Evan Cheng466685d2006-10-09 20:57:25 +00001971 }
1972 // Since loads produce two values, make sure to remember that we
1973 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001974 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
1975 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Evan Cheng466685d2006-10-09 20:57:25 +00001976 return Op.ResNo ? Tmp4 : Tmp3;
1977 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001978 MVT SrcVT = LD->getMemoryVT();
1979 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001980 int SVOffset = LD->getSrcValueOffset();
1981 unsigned Alignment = LD->getAlignment();
1982 bool isVolatile = LD->isVolatile();
1983
Duncan Sands83ec4b62008-06-06 12:08:01 +00001984 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001985 // Some targets pretend to have an i1 loading operation, and actually
1986 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1987 // bits are guaranteed to be zero; it helps the optimizers understand
1988 // that these bits are zero. It is also useful for EXTLOAD, since it
1989 // tells the optimizers that those bits are undefined. It would be
1990 // nice to have an effective generic way of getting these benefits...
1991 // Until such a way is found, don't insist on promoting i1 here.
1992 (SrcVT != MVT::i1 ||
1993 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1994 // Promote to a byte-sized load if not loading an integral number of
1995 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001996 unsigned NewWidth = SrcVT.getStoreSizeInBits();
1997 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00001998 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001999
2000 // The extra bits are guaranteed to be zero, since we stored them that
2001 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2002
2003 ISD::LoadExtType NewExtType =
2004 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2005
2006 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2007 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2008 NVT, isVolatile, Alignment);
2009
2010 Ch = Result.getValue(1); // The chain.
2011
2012 if (ExtType == ISD::SEXTLOAD)
2013 // Having the top bits zero doesn't help when sign extending.
2014 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2015 Result, DAG.getValueType(SrcVT));
2016 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2017 // All the top bits are guaranteed to be zero - inform the optimizers.
2018 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2019 DAG.getValueType(SrcVT));
2020
2021 Tmp1 = LegalizeOp(Result);
2022 Tmp2 = LegalizeOp(Ch);
2023 } else if (SrcWidth & (SrcWidth - 1)) {
2024 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002025 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002026 "Unsupported extload!");
2027 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2028 assert(RoundWidth < SrcWidth);
2029 unsigned ExtraWidth = SrcWidth - RoundWidth;
2030 assert(ExtraWidth < RoundWidth);
2031 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2032 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002033 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2034 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002035 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002036 unsigned IncrementSize;
2037
2038 if (TLI.isLittleEndian()) {
2039 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2040 // Load the bottom RoundWidth bits.
2041 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2042 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2043 Alignment);
2044
2045 // Load the remaining ExtraWidth bits.
2046 IncrementSize = RoundWidth / 8;
2047 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2048 DAG.getIntPtrConstant(IncrementSize));
2049 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2050 LD->getSrcValue(), SVOffset + IncrementSize,
2051 ExtraVT, isVolatile,
2052 MinAlign(Alignment, IncrementSize));
2053
2054 // Build a factor node to remember that this load is independent of the
2055 // other one.
2056 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2057 Hi.getValue(1));
2058
2059 // Move the top bits to the right place.
2060 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2061 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2062
2063 // Join the hi and lo parts.
2064 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002065 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002066 // Big endian - avoid unaligned loads.
2067 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2068 // Load the top RoundWidth bits.
2069 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2070 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2071 Alignment);
2072
2073 // Load the remaining ExtraWidth bits.
2074 IncrementSize = RoundWidth / 8;
2075 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2076 DAG.getIntPtrConstant(IncrementSize));
2077 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2078 LD->getSrcValue(), SVOffset + IncrementSize,
2079 ExtraVT, isVolatile,
2080 MinAlign(Alignment, IncrementSize));
2081
2082 // Build a factor node to remember that this load is independent of the
2083 // other one.
2084 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2085 Hi.getValue(1));
2086
2087 // Move the top bits to the right place.
2088 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2089 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2090
2091 // Join the hi and lo parts.
2092 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2093 }
2094
2095 Tmp1 = LegalizeOp(Result);
2096 Tmp2 = LegalizeOp(Ch);
2097 } else {
2098 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2099 default: assert(0 && "This action is not supported yet!");
2100 case TargetLowering::Custom:
2101 isCustom = true;
2102 // FALLTHROUGH
2103 case TargetLowering::Legal:
2104 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2105 Tmp1 = Result.getValue(0);
2106 Tmp2 = Result.getValue(1);
2107
2108 if (isCustom) {
2109 Tmp3 = TLI.LowerOperation(Result, DAG);
2110 if (Tmp3.Val) {
2111 Tmp1 = LegalizeOp(Tmp3);
2112 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2113 }
2114 } else {
2115 // If this is an unaligned load and the target doesn't support it,
2116 // expand it.
2117 if (!TLI.allowsUnalignedMemoryAccesses()) {
2118 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002119 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002120 if (LD->getAlignment() < ABIAlignment){
2121 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2122 TLI);
2123 Tmp1 = Result.getOperand(0);
2124 Tmp2 = Result.getOperand(1);
2125 Tmp1 = LegalizeOp(Tmp1);
2126 Tmp2 = LegalizeOp(Tmp2);
2127 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002128 }
2129 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002130 break;
2131 case TargetLowering::Expand:
2132 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2133 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman475871a2008-07-27 21:46:04 +00002134 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002135 LD->getSrcValueOffset(),
2136 LD->isVolatile(), LD->getAlignment());
2137 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2138 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2139 Tmp2 = LegalizeOp(Load.getValue(1));
2140 break;
2141 }
2142 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2143 // Turn the unsupported load into an EXTLOAD followed by an explicit
2144 // zero/sign extend inreg.
2145 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2146 Tmp1, Tmp2, LD->getSrcValue(),
2147 LD->getSrcValueOffset(), SrcVT,
2148 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002149 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002150 if (ExtType == ISD::SEXTLOAD)
2151 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2152 Result, DAG.getValueType(SrcVT));
2153 else
2154 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2155 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2156 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002157 break;
2158 }
Evan Cheng466685d2006-10-09 20:57:25 +00002159 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002160
Evan Cheng466685d2006-10-09 20:57:25 +00002161 // Since loads produce two values, make sure to remember that we legalized
2162 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002163 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2164 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Evan Cheng466685d2006-10-09 20:57:25 +00002165 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002166 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002167 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002168 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002169 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5dc897b2005-10-19 00:06:56 +00002170 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002171 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002172 case Legal:
2173 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2174 // 1 -> Hi
2175 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002176 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5dc897b2005-10-19 00:06:56 +00002177 TLI.getShiftAmountTy()));
2178 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2179 } else {
2180 // 0 -> Lo
2181 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2182 Node->getOperand(0));
2183 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002184 break;
2185 case Expand:
2186 // Get both the low and high parts.
2187 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2188 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2189 Result = Tmp2; // 1 -> Hi
2190 else
2191 Result = Tmp1; // 0 -> Lo
2192 break;
2193 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002194 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002195 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002196
2197 case ISD::CopyToReg:
2198 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002199
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002200 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002201 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002202 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002203 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002204 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002205 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002206 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002207 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002208 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002209 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002210 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2211 Tmp3);
2212 } else {
2213 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002214 }
2215
2216 // Since this produces two values, make sure to remember that we legalized
2217 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002218 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2219 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002220 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002221 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002222 break;
2223
2224 case ISD::RET:
2225 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002226
2227 // Ensure that libcalls are emitted before a return.
2228 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2229 Tmp1 = LegalizeOp(Tmp1);
2230 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002231
Chris Lattner3e928bb2005-01-07 07:47:09 +00002232 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002233 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002234 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002235 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002236 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002237 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002238 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002239 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002240 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002241 if (!Tmp2.getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002242 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002243 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002244
2245 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002246 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002247 std::swap(Lo, Hi);
2248
Evan Cheng13acce32006-12-11 19:27:14 +00002249 if (Hi.Val)
2250 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2251 else
2252 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002253 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002254 } else {
2255 SDNode *InVal = Tmp2.Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002256 int InIx = Tmp2.ResNo;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002257 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2258 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002259
Dan Gohman7f321562007-06-25 16:23:39 +00002260 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002261 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002262 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002263 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002264 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002265 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002266 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002267 } else if (NumElems == 1) {
2268 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002269 Tmp2 = ScalarizeVectorOp(Tmp2);
2270 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002271 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002272
2273 // FIXME: Returns of gcc generic vectors smaller than a legal type
2274 // should be returned in integer registers!
2275
Chris Lattnerf87324e2006-04-11 01:31:51 +00002276 // The scalarized value type may not be legal, e.g. it might require
2277 // promotion or expansion. Relegalize the return.
2278 Result = LegalizeOp(Result);
2279 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002280 // FIXME: Returns of gcc generic vectors larger than a legal vector
2281 // type should be returned by reference!
Dan Gohman475871a2008-07-27 21:46:04 +00002282 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002283 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002284 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002285 Result = LegalizeOp(Result);
2286 }
2287 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002288 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002289 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002290 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002291 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002292 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002293 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002294 }
2295 break;
2296 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002297 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002298 break;
2299 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002300 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002301 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002302 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002303 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2304 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002305 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002306 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002307 break;
2308 case Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002309 SDValue Lo, Hi;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002310 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002311 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002312 ExpandOp(Node->getOperand(i), Lo, Hi);
2313 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002314 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00002315 if (Hi.Val) {
2316 NewValues.push_back(Hi);
2317 NewValues.push_back(Node->getOperand(i+1));
2318 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002319 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002320 }
2321 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002322 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002323 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002324
2325 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002326 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002327 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002328 Result = DAG.getNode(ISD::RET, MVT::Other,
2329 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002330 break;
2331 }
2332 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002333
Chris Lattner6862dbc2006-01-29 21:02:23 +00002334 if (Result.getOpcode() == ISD::RET) {
2335 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2336 default: assert(0 && "This action is not supported yet!");
2337 case TargetLowering::Legal: break;
2338 case TargetLowering::Custom:
2339 Tmp1 = TLI.LowerOperation(Result, DAG);
2340 if (Tmp1.Val) Result = Tmp1;
2341 break;
2342 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002343 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002344 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002345 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002346 StoreSDNode *ST = cast<StoreSDNode>(Node);
2347 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2348 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002349 int SVOffset = ST->getSrcValueOffset();
2350 unsigned Alignment = ST->getAlignment();
2351 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002352
Evan Cheng8b2794a2006-10-13 21:14:26 +00002353 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002354 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2355 // FIXME: We shouldn't do this for TargetConstantFP's.
2356 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2357 // to phase ordering between legalized code and the dag combiner. This
2358 // probably means that we need to integrate dag combiner and legalizer
2359 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002360 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002361 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002362 if (CFP->getValueType(0) == MVT::f32 &&
2363 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002364 Tmp3 = DAG.getConstant(CFP->getValueAPF().
2365 convertToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002366 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002367 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2368 SVOffset, isVolatile, Alignment);
2369 break;
2370 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002371 // If this target supports 64-bit registers, do a single 64-bit store.
2372 if (getTypeAction(MVT::i64) == Legal) {
2373 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002374 zextOrTrunc(64), MVT::i64);
Chris Lattner3cb93512007-10-15 05:46:06 +00002375 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2376 SVOffset, isVolatile, Alignment);
2377 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002378 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002379 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2380 // stores. If the target supports neither 32- nor 64-bits, this
2381 // xform is certainly not worth it.
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002382 const APInt &IntVal =CFP->getValueAPF().convertToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002383 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2384 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002385 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002386
2387 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2388 SVOffset, isVolatile, Alignment);
2389 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002390 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002391 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002392 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002393
2394 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2395 break;
2396 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002397 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002398 }
2399
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002400 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002401 case Legal: {
2402 Tmp3 = LegalizeOp(ST->getValue());
2403 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2404 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002405
Duncan Sands83ec4b62008-06-06 12:08:01 +00002406 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002407 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2408 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002409 case TargetLowering::Legal:
2410 // If this is an unaligned store and the target doesn't support it,
2411 // expand it.
2412 if (!TLI.allowsUnalignedMemoryAccesses()) {
2413 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002414 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002415 if (ST->getAlignment() < ABIAlignment)
2416 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2417 TLI);
2418 }
2419 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002420 case TargetLowering::Custom:
2421 Tmp1 = TLI.LowerOperation(Result, DAG);
2422 if (Tmp1.Val) Result = Tmp1;
2423 break;
2424 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002425 assert(VT.isVector() && "Unknown legal promote case!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002426 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2427 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2428 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002429 ST->getSrcValue(), SVOffset, isVolatile,
2430 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002431 break;
2432 }
2433 break;
2434 }
2435 case Promote:
2436 // Truncate the value and store the result.
2437 Tmp3 = PromoteOp(ST->getValue());
2438 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002439 SVOffset, ST->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002440 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002441 break;
2442
2443 case Expand:
2444 unsigned IncrementSize = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002445 SDValue Lo, Hi;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002446
2447 // If this is a vector type, then we have to calculate the increment as
2448 // the product of the element size in bytes, and the number of elements
2449 // in the high half of the vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002450 if (ST->getValue().getValueType().isVector()) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002451 SDNode *InVal = ST->getValue().Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002452 int InIx = ST->getValue().ResNo;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002453 MVT InVT = InVal->getValueType(InIx);
2454 unsigned NumElems = InVT.getVectorNumElements();
2455 MVT EVT = InVT.getVectorElementType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002456
Dan Gohman7f321562007-06-25 16:23:39 +00002457 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002458 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002459 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002460 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002461 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002462 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002463 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002464 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002465 Result = LegalizeOp(Result);
2466 break;
2467 } else if (NumElems == 1) {
2468 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002469 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002470 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002471 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002472 // The scalarized value type may not be legal, e.g. it might require
2473 // promotion or expansion. Relegalize the scalar store.
2474 Result = LegalizeOp(Result);
2475 break;
2476 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002477 SplitVectorOp(ST->getValue(), Lo, Hi);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002478 IncrementSize = Lo.Val->getValueType(0).getVectorNumElements() *
2479 EVT.getSizeInBits()/8;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002480 }
2481 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002482 ExpandOp(ST->getValue(), Lo, Hi);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002483 IncrementSize = Hi.Val ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002484
Duncan Sands0753fc12008-02-11 10:37:04 +00002485 if (TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002486 std::swap(Lo, Hi);
2487 }
2488
2489 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002490 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002491
2492 if (Hi.Val == NULL) {
2493 // Must be int <-> float one-to-one expansion.
2494 Result = Lo;
2495 break;
2496 }
2497
Evan Cheng8b2794a2006-10-13 21:14:26 +00002498 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002499 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002500 assert(isTypeLegal(Tmp2.getValueType()) &&
2501 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002502 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002503 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002504 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002505 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002506 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2507 break;
2508 }
2509 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002510 switch (getTypeAction(ST->getValue().getValueType())) {
2511 case Legal:
2512 Tmp3 = LegalizeOp(ST->getValue());
2513 break;
2514 case Promote:
2515 // We can promote the value, the truncstore will still take care of it.
2516 Tmp3 = PromoteOp(ST->getValue());
2517 break;
2518 case Expand:
2519 // Just store the low part. This may become a non-trunc store, so make
2520 // sure to use getTruncStore, not UpdateNodeOperands below.
2521 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2522 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2523 SVOffset, MVT::i8, isVolatile, Alignment);
2524 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002525
Duncan Sands83ec4b62008-06-06 12:08:01 +00002526 MVT StVT = ST->getMemoryVT();
2527 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002528
Duncan Sands83ec4b62008-06-06 12:08:01 +00002529 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002530 // Promote to a byte-sized store with upper bits zero if not
2531 // storing an integral number of bytes. For example, promote
2532 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002533 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands7e857202008-01-22 07:17:34 +00002534 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2535 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2536 SVOffset, NVT, isVolatile, Alignment);
2537 } else if (StWidth & (StWidth - 1)) {
2538 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002539 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002540 "Unsupported truncstore!");
2541 unsigned RoundWidth = 1 << Log2_32(StWidth);
2542 assert(RoundWidth < StWidth);
2543 unsigned ExtraWidth = StWidth - RoundWidth;
2544 assert(ExtraWidth < RoundWidth);
2545 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2546 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002547 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2548 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002549 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002550 unsigned IncrementSize;
2551
2552 if (TLI.isLittleEndian()) {
2553 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2554 // Store the bottom RoundWidth bits.
2555 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2556 SVOffset, RoundVT,
2557 isVolatile, Alignment);
2558
2559 // Store the remaining ExtraWidth bits.
2560 IncrementSize = RoundWidth / 8;
2561 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2562 DAG.getIntPtrConstant(IncrementSize));
2563 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2564 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2565 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2566 SVOffset + IncrementSize, ExtraVT, isVolatile,
2567 MinAlign(Alignment, IncrementSize));
2568 } else {
2569 // Big endian - avoid unaligned stores.
2570 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2571 // Store the top RoundWidth bits.
2572 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2573 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2574 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2575 RoundVT, isVolatile, Alignment);
2576
2577 // Store the remaining ExtraWidth bits.
2578 IncrementSize = RoundWidth / 8;
2579 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2580 DAG.getIntPtrConstant(IncrementSize));
2581 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2582 SVOffset + IncrementSize, ExtraVT, isVolatile,
2583 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002584 }
Duncan Sands7e857202008-01-22 07:17:34 +00002585
2586 // The order of the stores doesn't matter.
2587 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2588 } else {
2589 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2590 Tmp2 != ST->getBasePtr())
2591 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2592 ST->getOffset());
2593
2594 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2595 default: assert(0 && "This action is not supported yet!");
2596 case TargetLowering::Legal:
2597 // If this is an unaligned store and the target doesn't support it,
2598 // expand it.
2599 if (!TLI.allowsUnalignedMemoryAccesses()) {
2600 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002601 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002602 if (ST->getAlignment() < ABIAlignment)
2603 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2604 TLI);
2605 }
2606 break;
2607 case TargetLowering::Custom:
2608 Result = TLI.LowerOperation(Result, DAG);
2609 break;
2610 case Expand:
2611 // TRUNCSTORE:i16 i32 -> STORE i16
2612 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2613 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2614 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2615 isVolatile, Alignment);
2616 break;
2617 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002618 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002619 }
2620 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002621 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002622 case ISD::PCMARKER:
2623 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002624 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002625 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002626 case ISD::STACKSAVE:
2627 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002628 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2629 Tmp1 = Result.getValue(0);
2630 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002631
Chris Lattner140d53c2006-01-13 02:50:02 +00002632 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2633 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002634 case TargetLowering::Legal: break;
2635 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002636 Tmp3 = TLI.LowerOperation(Result, DAG);
2637 if (Tmp3.Val) {
2638 Tmp1 = LegalizeOp(Tmp3);
2639 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002640 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002641 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002642 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002643 // Expand to CopyFromReg if the target set
2644 // StackPointerRegisterToSaveRestore.
2645 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002646 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002647 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002648 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002649 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002650 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2651 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002652 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002653 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002654 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002655
2656 // Since stacksave produce two values, make sure to remember that we
2657 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002658 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2659 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002660 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002661
Chris Lattner140d53c2006-01-13 02:50:02 +00002662 case ISD::STACKRESTORE:
2663 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2664 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002665 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002666
2667 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2668 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002669 case TargetLowering::Legal: break;
2670 case TargetLowering::Custom:
2671 Tmp1 = TLI.LowerOperation(Result, DAG);
2672 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002673 break;
2674 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002675 // Expand to CopyToReg if the target set
2676 // StackPointerRegisterToSaveRestore.
2677 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2678 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2679 } else {
2680 Result = Tmp1;
2681 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002682 break;
2683 }
2684 break;
2685
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002686 case ISD::READCYCLECOUNTER:
2687 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002688 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002689 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2690 Node->getValueType(0))) {
2691 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002692 case TargetLowering::Legal:
2693 Tmp1 = Result.getValue(0);
2694 Tmp2 = Result.getValue(1);
2695 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002696 case TargetLowering::Custom:
2697 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002698 Tmp1 = LegalizeOp(Result.getValue(0));
2699 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002700 break;
2701 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002702
2703 // Since rdcc produce two values, make sure to remember that we legalized
2704 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002705 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2706 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002707 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002708
Chris Lattner2ee743f2005-01-14 22:08:15 +00002709 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002710 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2711 case Expand: assert(0 && "It's impossible to expand bools");
2712 case Legal:
2713 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2714 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002715 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00002716 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002717 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002718 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002719 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002720 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002721 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002722 break;
2723 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002724 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002725 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002726 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002727
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002728 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002729
Nate Begemanb942a3d2005-08-23 04:29:48 +00002730 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002731 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002732 case TargetLowering::Legal: break;
2733 case TargetLowering::Custom: {
2734 Tmp1 = TLI.LowerOperation(Result, DAG);
2735 if (Tmp1.Val) Result = Tmp1;
2736 break;
2737 }
Nate Begeman9373a812005-08-10 20:51:12 +00002738 case TargetLowering::Expand:
2739 if (Tmp1.getOpcode() == ISD::SETCC) {
2740 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2741 Tmp2, Tmp3,
2742 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2743 } else {
2744 Result = DAG.getSelectCC(Tmp1,
2745 DAG.getConstant(0, Tmp1.getValueType()),
2746 Tmp2, Tmp3, ISD::SETNE);
2747 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002748 break;
2749 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002750 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002751 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2752 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002753 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002754 ExtOp = ISD::BIT_CONVERT;
2755 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002756 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002757 ExtOp = ISD::ANY_EXTEND;
2758 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002759 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002760 ExtOp = ISD::FP_EXTEND;
2761 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002762 }
2763 // Promote each of the values to the new type.
2764 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2765 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2766 // Perform the larger operation, then round down.
2767 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002768 if (TruncOp != ISD::FP_ROUND)
2769 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2770 else
2771 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2772 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002773 break;
2774 }
2775 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002776 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002777 case ISD::SELECT_CC: {
2778 Tmp1 = Node->getOperand(0); // LHS
2779 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002780 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2781 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00002782 SDValue CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002783
Nate Begeman750ac1b2006-02-01 07:19:44 +00002784 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2785
2786 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2787 // the LHS is a legal SETCC itself. In this case, we need to compare
2788 // the result against zero to select between true and false values.
2789 if (Tmp2.Val == 0) {
2790 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2791 CC = DAG.getCondCode(ISD::SETNE);
2792 }
2793 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2794
2795 // Everything is legal, see if we should expand this op or something.
2796 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2797 default: assert(0 && "This action is not supported yet!");
2798 case TargetLowering::Legal: break;
2799 case TargetLowering::Custom:
2800 Tmp1 = TLI.LowerOperation(Result, DAG);
2801 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002802 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002803 }
2804 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002805 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002806 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002807 Tmp1 = Node->getOperand(0);
2808 Tmp2 = Node->getOperand(1);
2809 Tmp3 = Node->getOperand(2);
2810 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2811
2812 // If we had to Expand the SetCC operands into a SELECT node, then it may
2813 // not always be possible to return a true LHS & RHS. In this case, just
2814 // return the value we legalized, returned in the LHS
2815 if (Tmp2.Val == 0) {
2816 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002817 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002818 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002819
Chris Lattner73e142f2006-01-30 22:43:50 +00002820 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002821 default: assert(0 && "Cannot handle this action for SETCC yet!");
2822 case TargetLowering::Custom:
2823 isCustom = true;
2824 // FALLTHROUGH.
2825 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002826 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002827 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002828 Tmp4 = TLI.LowerOperation(Result, DAG);
2829 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002830 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002831 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002832 case TargetLowering::Promote: {
2833 // First step, figure out the appropriate operation to use.
2834 // Allow SETCC to not be supported for all legal data types
2835 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00002836 MVT NewInTy = Node->getOperand(0).getValueType();
2837 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002838
2839 // Scan for the appropriate larger type to use.
2840 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002841 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00002842
Duncan Sands83ec4b62008-06-06 12:08:01 +00002843 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002844 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002845 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002846 "Fell off of the edge of the floating point world");
2847
2848 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002849 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002850 break;
2851 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002852 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00002853 assert(0 && "Cannot promote Legal Integer SETCC yet");
2854 else {
2855 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2856 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2857 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002858 Tmp1 = LegalizeOp(Tmp1);
2859 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002860 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002861 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002862 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002863 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002864 case TargetLowering::Expand:
2865 // Expand a setcc node into a select_cc of the same condition, lhs, and
2866 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00002867 MVT VT = Node->getValueType(0);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002868 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2869 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002870 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002871 break;
2872 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002873 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00002874 case ISD::VSETCC: {
2875 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2876 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00002877 SDValue CC = Node->getOperand(2);
Nate Begemanb43e9c12008-05-12 19:40:03 +00002878
2879 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
2880
2881 // Everything is legal, see if we should expand this op or something.
2882 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
2883 default: assert(0 && "This action is not supported yet!");
2884 case TargetLowering::Legal: break;
2885 case TargetLowering::Custom:
2886 Tmp1 = TLI.LowerOperation(Result, DAG);
2887 if (Tmp1.Val) Result = Tmp1;
2888 break;
2889 }
2890 break;
2891 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002892
Chris Lattner5b359c62005-04-02 04:00:59 +00002893 case ISD::SHL_PARTS:
2894 case ISD::SRA_PARTS:
2895 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00002896 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002897 bool Changed = false;
2898 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2899 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2900 Changed |= Ops.back() != Node->getOperand(i);
2901 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002902 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002903 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002904
Evan Cheng05a2d562006-01-09 18:31:59 +00002905 switch (TLI.getOperationAction(Node->getOpcode(),
2906 Node->getValueType(0))) {
2907 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002908 case TargetLowering::Legal: break;
2909 case TargetLowering::Custom:
2910 Tmp1 = TLI.LowerOperation(Result, DAG);
2911 if (Tmp1.Val) {
Dan Gohman475871a2008-07-27 21:46:04 +00002912 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002913 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002914 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00002915 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Evan Cheng05a2d562006-01-09 18:31:59 +00002916 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002917 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002918 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002919 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002920 return RetVal;
2921 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002922 break;
2923 }
2924
Chris Lattner2c8086f2005-04-02 05:00:07 +00002925 // Since these produce multiple values, make sure to remember that we
2926 // legalized all of them.
2927 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00002928 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Chris Lattner2c8086f2005-04-02 05:00:07 +00002929 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002930 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002931
2932 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002933 case ISD::ADD:
2934 case ISD::SUB:
2935 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002936 case ISD::MULHS:
2937 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002938 case ISD::UDIV:
2939 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002940 case ISD::AND:
2941 case ISD::OR:
2942 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002943 case ISD::SHL:
2944 case ISD::SRL:
2945 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002946 case ISD::FADD:
2947 case ISD::FSUB:
2948 case ISD::FMUL:
2949 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002950 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002951 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002952 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2953 case Expand: assert(0 && "Not possible");
2954 case Legal:
2955 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2956 break;
2957 case Promote:
2958 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2959 break;
2960 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002961
2962 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002963
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002964 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002965 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002966 case TargetLowering::Legal: break;
2967 case TargetLowering::Custom:
2968 Tmp1 = TLI.LowerOperation(Result, DAG);
2969 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002970 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002971 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002972 MVT VT = Op.getValueType();
Dan Gohman525178c2007-10-08 18:33:35 +00002973
2974 // See if multiply or divide can be lowered using two-result operations.
2975 SDVTList VTs = DAG.getVTList(VT, VT);
2976 if (Node->getOpcode() == ISD::MUL) {
2977 // We just need the low half of the multiply; try both the signed
2978 // and unsigned forms. If the target supports both SMUL_LOHI and
2979 // UMUL_LOHI, form a preference by checking which forms of plain
2980 // MULH it supports.
2981 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2982 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2983 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2984 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2985 unsigned OpToUse = 0;
2986 if (HasSMUL_LOHI && !HasMULHS) {
2987 OpToUse = ISD::SMUL_LOHI;
2988 } else if (HasUMUL_LOHI && !HasMULHU) {
2989 OpToUse = ISD::UMUL_LOHI;
2990 } else if (HasSMUL_LOHI) {
2991 OpToUse = ISD::SMUL_LOHI;
2992 } else if (HasUMUL_LOHI) {
2993 OpToUse = ISD::UMUL_LOHI;
2994 }
2995 if (OpToUse) {
Dan Gohman475871a2008-07-27 21:46:04 +00002996 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
Dan Gohman525178c2007-10-08 18:33:35 +00002997 break;
2998 }
2999 }
3000 if (Node->getOpcode() == ISD::MULHS &&
3001 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003002 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003003 break;
3004 }
3005 if (Node->getOpcode() == ISD::MULHU &&
3006 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003007 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003008 break;
3009 }
3010 if (Node->getOpcode() == ISD::SDIV &&
3011 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003012 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003013 break;
3014 }
3015 if (Node->getOpcode() == ISD::UDIV &&
3016 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003017 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003018 break;
3019 }
3020
Dan Gohman82669522007-10-11 23:57:53 +00003021 // Check to see if we have a libcall for this operator.
3022 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3023 bool isSigned = false;
3024 switch (Node->getOpcode()) {
3025 case ISD::UDIV:
3026 case ISD::SDIV:
3027 if (VT == MVT::i32) {
3028 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00003029 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003030 isSigned = Node->getOpcode() == ISD::SDIV;
3031 }
3032 break;
3033 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003034 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3035 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003036 break;
3037 default: break;
3038 }
3039 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003040 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003041 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003042 break;
3043 }
3044
Duncan Sands83ec4b62008-06-06 12:08:01 +00003045 assert(Node->getValueType(0).isVector() &&
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003046 "Cannot expand this binary operator!");
3047 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003048 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003049 break;
3050 }
Evan Chengcc987612006-04-12 21:20:24 +00003051 case TargetLowering::Promote: {
3052 switch (Node->getOpcode()) {
3053 default: assert(0 && "Do not know how to promote this BinOp!");
3054 case ISD::AND:
3055 case ISD::OR:
3056 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003057 MVT OVT = Node->getValueType(0);
3058 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3059 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00003060 // Bit convert each of the values to the new type.
3061 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3062 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3063 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3064 // Bit convert the result back the original type.
3065 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3066 break;
3067 }
3068 }
3069 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003070 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003071 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003072
Dan Gohmane14ea862007-10-05 14:17:22 +00003073 case ISD::SMUL_LOHI:
3074 case ISD::UMUL_LOHI:
3075 case ISD::SDIVREM:
3076 case ISD::UDIVREM:
3077 // These nodes will only be produced by target-specific lowering, so
3078 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003079 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003080 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003081
3082 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3083 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3084 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003085 break;
3086
Chris Lattnera09f8482006-03-05 05:09:38 +00003087 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3088 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3089 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3090 case Expand: assert(0 && "Not possible");
3091 case Legal:
3092 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3093 break;
3094 case Promote:
3095 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3096 break;
3097 }
3098
3099 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3100
3101 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3102 default: assert(0 && "Operation not supported");
3103 case TargetLowering::Custom:
3104 Tmp1 = TLI.LowerOperation(Result, DAG);
3105 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003106 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003107 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003108 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003109 // If this target supports fabs/fneg natively and select is cheap,
3110 // do this efficiently.
3111 if (!TLI.isSelectExpensive() &&
3112 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3113 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003114 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003115 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003116 // Get the sign bit of the RHS.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003117 MVT IVT =
Chris Lattner8f4191d2006-03-13 06:08:38 +00003118 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman475871a2008-07-27 21:46:04 +00003119 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003120 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003121 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3122 // Get the absolute value of the result.
Dan Gohman475871a2008-07-27 21:46:04 +00003123 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Chris Lattner8f4191d2006-03-13 06:08:38 +00003124 // Select between the nabs and abs value based on the sign bit of
3125 // the input.
3126 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3127 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3128 AbsVal),
3129 AbsVal);
3130 Result = LegalizeOp(Result);
3131 break;
3132 }
3133
3134 // Otherwise, do bitwise ops!
Duncan Sands83ec4b62008-06-06 12:08:01 +00003135 MVT NVT =
Evan Cheng912095b2007-01-04 21:56:39 +00003136 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3137 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3138 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3139 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003140 break;
3141 }
Evan Cheng912095b2007-01-04 21:56:39 +00003142 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003143 break;
3144
Nate Begeman551bf3f2006-02-17 05:43:56 +00003145 case ISD::ADDC:
3146 case ISD::SUBC:
3147 Tmp1 = LegalizeOp(Node->getOperand(0));
3148 Tmp2 = LegalizeOp(Node->getOperand(1));
3149 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3150 // Since this produces two values, make sure to remember that we legalized
3151 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003152 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3153 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Nate Begeman551bf3f2006-02-17 05:43:56 +00003154 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003155
Nate Begeman551bf3f2006-02-17 05:43:56 +00003156 case ISD::ADDE:
3157 case ISD::SUBE:
3158 Tmp1 = LegalizeOp(Node->getOperand(0));
3159 Tmp2 = LegalizeOp(Node->getOperand(1));
3160 Tmp3 = LegalizeOp(Node->getOperand(2));
3161 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3162 // Since this produces two values, make sure to remember that we legalized
3163 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003164 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3165 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Nate Begeman551bf3f2006-02-17 05:43:56 +00003166 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003167
Nate Begeman419f8b62005-10-18 00:27:41 +00003168 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003169 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003170 // TODO: handle the case where the Lo and Hi operands are not of legal type
3171 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3172 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3173 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003174 case TargetLowering::Promote:
3175 case TargetLowering::Custom:
3176 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003177 case TargetLowering::Legal:
3178 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3179 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3180 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003181 case TargetLowering::Expand:
3182 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3183 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3184 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003185 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003186 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003187 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003188 break;
3189 }
3190 break;
3191 }
3192
Nate Begemanc105e192005-04-06 00:23:54 +00003193 case ISD::UREM:
3194 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003195 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003196 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3197 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003198
Nate Begemanc105e192005-04-06 00:23:54 +00003199 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003200 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3201 case TargetLowering::Custom:
3202 isCustom = true;
3203 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003204 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003205 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003206 if (isCustom) {
3207 Tmp1 = TLI.LowerOperation(Result, DAG);
3208 if (Tmp1.Val) Result = Tmp1;
3209 }
Nate Begemanc105e192005-04-06 00:23:54 +00003210 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003211 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003212 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003213 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003214 MVT VT = Node->getValueType(0);
Dan Gohman525178c2007-10-08 18:33:35 +00003215
3216 // See if remainder can be lowered using two-result operations.
3217 SDVTList VTs = DAG.getVTList(VT, VT);
3218 if (Node->getOpcode() == ISD::SREM &&
3219 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003220 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003221 break;
3222 }
3223 if (Node->getOpcode() == ISD::UREM &&
3224 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003225 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003226 break;
3227 }
3228
Duncan Sands83ec4b62008-06-06 12:08:01 +00003229 if (VT.isInteger()) {
Dan Gohman525178c2007-10-08 18:33:35 +00003230 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003231 TargetLowering::Legal) {
3232 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003233 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003234 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3235 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003236 } else if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003237 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003238 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003239 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003240 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003241 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3242 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman475871a2008-07-27 21:46:04 +00003243 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003244 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003245 }
Dan Gohman0d974262007-11-06 22:11:54 +00003246 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003247 assert(VT.isFloatingPoint() &&
Dan Gohman0d974262007-11-06 22:11:54 +00003248 "remainder op must have integer or floating-point type");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003249 if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003250 Result = LegalizeOp(UnrollVectorOp(Op));
3251 } else {
3252 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003253 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3254 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003255 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003256 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman80176312007-11-05 23:35:22 +00003257 }
Nate Begemanc105e192005-04-06 00:23:54 +00003258 }
3259 break;
3260 }
Dan Gohman525178c2007-10-08 18:33:35 +00003261 }
Nate Begemanc105e192005-04-06 00:23:54 +00003262 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003263 case ISD::VAARG: {
3264 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3265 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3266
Duncan Sands83ec4b62008-06-06 12:08:01 +00003267 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003268 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3269 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003270 case TargetLowering::Custom:
3271 isCustom = true;
3272 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003273 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003274 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3275 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003276 Tmp1 = Result.getValue(1);
3277
3278 if (isCustom) {
3279 Tmp2 = TLI.LowerOperation(Result, DAG);
3280 if (Tmp2.Val) {
3281 Result = LegalizeOp(Tmp2);
3282 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3283 }
3284 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003285 break;
3286 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003287 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003288 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003289 // Increment the pointer, VAList, to the next vaarg
3290 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003291 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begemanacc398c2006-01-25 18:21:52 +00003292 TLI.getPointerTy()));
3293 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003294 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003295 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003296 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003297 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003298 Result = LegalizeOp(Result);
3299 break;
3300 }
3301 }
3302 // Since VAARG produces two values, make sure to remember that we
3303 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003304 AddLegalizedOperand(SDValue(Node, 0), Result);
3305 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003306 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003307 }
3308
3309 case ISD::VACOPY:
3310 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3311 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3312 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3313
3314 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3315 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003316 case TargetLowering::Custom:
3317 isCustom = true;
3318 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003319 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3321 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003322 if (isCustom) {
3323 Tmp1 = TLI.LowerOperation(Result, DAG);
3324 if (Tmp1.Val) Result = Tmp1;
3325 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003326 break;
3327 case TargetLowering::Expand:
3328 // This defaults to loading a pointer from the input and storing it to the
3329 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003330 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3331 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman499c1bd2008-04-17 02:09:26 +00003332 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3333 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003334 break;
3335 }
3336 break;
3337
3338 case ISD::VAEND:
3339 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3340 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3341
3342 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3343 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003344 case TargetLowering::Custom:
3345 isCustom = true;
3346 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003347 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003348 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003349 if (isCustom) {
3350 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3351 if (Tmp1.Val) Result = Tmp1;
3352 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003353 break;
3354 case TargetLowering::Expand:
3355 Result = Tmp1; // Default to a no-op, return the chain
3356 break;
3357 }
3358 break;
3359
3360 case ISD::VASTART:
3361 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3362 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3363
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003364 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3365
Nate Begemanacc398c2006-01-25 18:21:52 +00003366 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3367 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003368 case TargetLowering::Legal: break;
3369 case TargetLowering::Custom:
3370 Tmp1 = TLI.LowerOperation(Result, DAG);
3371 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003372 break;
3373 }
3374 break;
3375
Nate Begeman35ef9132006-01-11 21:21:00 +00003376 case ISD::ROTL:
3377 case ISD::ROTR:
3378 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3379 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003380 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003381 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3382 default:
3383 assert(0 && "ROTL/ROTR legalize operation not supported");
3384 break;
3385 case TargetLowering::Legal:
3386 break;
3387 case TargetLowering::Custom:
3388 Tmp1 = TLI.LowerOperation(Result, DAG);
3389 if (Tmp1.Val) Result = Tmp1;
3390 break;
3391 case TargetLowering::Promote:
3392 assert(0 && "Do not know how to promote ROTL/ROTR");
3393 break;
3394 case TargetLowering::Expand:
3395 assert(0 && "Do not know how to expand ROTL/ROTR");
3396 break;
3397 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003398 break;
3399
Nate Begemand88fc032006-01-14 03:14:10 +00003400 case ISD::BSWAP:
3401 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3402 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003403 case TargetLowering::Custom:
3404 assert(0 && "Cannot custom legalize this yet!");
3405 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003406 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003407 break;
3408 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003409 MVT OVT = Tmp1.getValueType();
3410 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3411 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003412
Chris Lattner456a93a2006-01-28 07:39:30 +00003413 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3414 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3415 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3416 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3417 break;
3418 }
3419 case TargetLowering::Expand:
3420 Result = ExpandBSWAP(Tmp1);
3421 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003422 }
3423 break;
3424
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003425 case ISD::CTPOP:
3426 case ISD::CTTZ:
3427 case ISD::CTLZ:
3428 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3429 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003430 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003431 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003432 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003433 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003434 TargetLowering::Custom) {
3435 Tmp1 = TLI.LowerOperation(Result, DAG);
3436 if (Tmp1.Val) {
3437 Result = Tmp1;
3438 }
Scott Michel910b66d2007-07-30 21:00:31 +00003439 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003440 break;
3441 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003442 MVT OVT = Tmp1.getValueType();
3443 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003444
3445 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003446 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3447 // Perform the larger operation, then subtract if needed.
3448 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003449 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003450 case ISD::CTPOP:
3451 Result = Tmp1;
3452 break;
3453 case ISD::CTTZ:
3454 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00003455 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003456 DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003457 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003458 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003459 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003460 break;
3461 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003462 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003463 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003464 DAG.getConstant(NVT.getSizeInBits() -
3465 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003466 break;
3467 }
3468 break;
3469 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003470 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003471 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003472 break;
3473 }
3474 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003475
Chris Lattner2c8086f2005-04-02 05:00:07 +00003476 // Unary operators
3477 case ISD::FABS:
3478 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003479 case ISD::FSQRT:
3480 case ISD::FSIN:
3481 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003482 Tmp1 = LegalizeOp(Node->getOperand(0));
3483 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003484 case TargetLowering::Promote:
3485 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003486 isCustom = true;
3487 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003488 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003489 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003490 if (isCustom) {
3491 Tmp1 = TLI.LowerOperation(Result, DAG);
3492 if (Tmp1.Val) Result = Tmp1;
3493 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003494 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003495 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003496 switch (Node->getOpcode()) {
3497 default: assert(0 && "Unreachable!");
3498 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003499 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3500 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003501 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003502 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003503 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003504 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003505 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003506 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003507 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00003508 ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003509 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3510 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003511 break;
3512 }
3513 case ISD::FSQRT:
3514 case ISD::FSIN:
3515 case ISD::FCOS: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003516 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003517
3518 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003519 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003520 Result = LegalizeOp(UnrollVectorOp(Op));
3521 break;
3522 }
3523
Evan Cheng56966222007-01-12 02:11:51 +00003524 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003525 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003526 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003527 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3528 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003529 break;
3530 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003531 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3532 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003533 break;
3534 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003535 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3536 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003537 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003538 default: assert(0 && "Unreachable!");
3539 }
Dan Gohman475871a2008-07-27 21:46:04 +00003540 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003541 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003542 break;
3543 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003544 }
3545 break;
3546 }
3547 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003548 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003549 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003550
3551 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003552 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003553 Result = LegalizeOp(UnrollVectorOp(Op));
3554 break;
3555 }
3556
3557 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003558 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3559 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003560 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003561 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003562 break;
3563 }
Chris Lattner35481892005-12-23 00:16:34 +00003564 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003565 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003566 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3567 Node->getValueType(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003568 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003569 // The input has to be a vector type, we have to either scalarize it, pack
3570 // it, or convert it based on whether the input vector type is legal.
3571 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesene5269622007-10-20 00:07:52 +00003572 int InIx = Node->getOperand(0).ResNo;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003573 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3574 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohman7f321562007-06-25 16:23:39 +00003575
3576 // Figure out if there is a simple type corresponding to this Vector
3577 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003578 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00003579 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003580 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003581 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3582 LegalizeOp(Node->getOperand(0)));
3583 break;
3584 } else if (NumElems == 1) {
3585 // Turn this into a bit convert of the scalar input.
3586 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3587 ScalarizeVectorOp(Node->getOperand(0)));
3588 break;
3589 } else {
3590 // FIXME: UNIMP! Store then reload
3591 assert(0 && "Cast from unsupported vector type not implemented yet!");
3592 }
Chris Lattner67993f72006-01-23 07:30:46 +00003593 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003594 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3595 Node->getOperand(0).getValueType())) {
3596 default: assert(0 && "Unknown operation action!");
3597 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003598 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3599 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003600 break;
3601 case TargetLowering::Legal:
3602 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003603 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003604 break;
3605 }
3606 }
3607 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003608
Chris Lattner2c8086f2005-04-02 05:00:07 +00003609 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003610 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003611 case ISD::UINT_TO_FP: {
3612 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003613 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3614 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003615 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003616 Node->getOperand(0).getValueType())) {
3617 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003618 case TargetLowering::Custom:
3619 isCustom = true;
3620 // FALLTHROUGH
3621 case TargetLowering::Legal:
3622 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003623 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003624 if (isCustom) {
3625 Tmp1 = TLI.LowerOperation(Result, DAG);
3626 if (Tmp1.Val) Result = Tmp1;
3627 }
3628 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003629 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003630 Result = ExpandLegalINT_TO_FP(isSigned,
3631 LegalizeOp(Node->getOperand(0)),
3632 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003633 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003634 case TargetLowering::Promote:
3635 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3636 Node->getValueType(0),
3637 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003638 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003639 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003640 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003641 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003642 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3643 Node->getValueType(0), Node->getOperand(0));
3644 break;
3645 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003646 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003647 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003648 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3649 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003650 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003651 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3652 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003653 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003654 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3655 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003656 break;
3657 }
3658 break;
3659 }
3660 case ISD::TRUNCATE:
3661 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3662 case Legal:
3663 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003664 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003665 break;
3666 case Expand:
3667 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3668
3669 // Since the result is legal, we should just be able to truncate the low
3670 // part of the source.
3671 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3672 break;
3673 case Promote:
3674 Result = PromoteOp(Node->getOperand(0));
3675 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3676 break;
3677 }
3678 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003679
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003680 case ISD::FP_TO_SINT:
3681 case ISD::FP_TO_UINT:
3682 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3683 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003684 Tmp1 = LegalizeOp(Node->getOperand(0));
3685
Chris Lattner1618beb2005-07-29 00:11:56 +00003686 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3687 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003688 case TargetLowering::Custom:
3689 isCustom = true;
3690 // FALLTHROUGH
3691 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003692 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003693 if (isCustom) {
3694 Tmp1 = TLI.LowerOperation(Result, DAG);
3695 if (Tmp1.Val) Result = Tmp1;
3696 }
3697 break;
3698 case TargetLowering::Promote:
3699 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3700 Node->getOpcode() == ISD::FP_TO_SINT);
3701 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003702 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003703 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman475871a2008-07-27 21:46:04 +00003704 SDValue True, False;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003705 MVT VT = Node->getOperand(0).getValueType();
3706 MVT NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00003707 const uint64_t zero[] = {0, 0};
Duncan Sands83ec4b62008-06-06 12:08:01 +00003708 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3709 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003710 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003711 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003712 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Nate Begemand2558e32005-08-14 01:20:53 +00003713 Node->getOperand(0), Tmp2, ISD::SETLT);
3714 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3715 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003716 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003717 Tmp2));
3718 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003719 DAG.getConstant(x, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003720 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3721 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003722 } else {
3723 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3724 }
3725 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003726 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003727 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003728 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003729 MVT VT = Op.getValueType();
3730 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003731 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003732 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003733 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3734 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3735 Node->getOperand(0), DAG.getValueType(MVT::f64));
3736 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3737 DAG.getIntPtrConstant(1));
3738 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3739 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003740 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3741 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3742 Tmp2 = DAG.getConstantFP(apf, OVT);
3743 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3744 // FIXME: generated code sucks.
3745 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3746 DAG.getNode(ISD::ADD, MVT::i32,
3747 DAG.getNode(ISD::FP_TO_SINT, VT,
3748 DAG.getNode(ISD::FSUB, OVT,
3749 Node->getOperand(0), Tmp2)),
3750 DAG.getConstant(0x80000000, MVT::i32)),
3751 DAG.getNode(ISD::FP_TO_SINT, VT,
3752 Node->getOperand(0)),
3753 DAG.getCondCode(ISD::SETGE));
3754 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003755 break;
3756 }
Dan Gohmana2e94852008-03-10 23:03:31 +00003757 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsb2ff8852008-07-17 02:36:29 +00003758 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
3759 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
3760 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman475871a2008-07-27 21:46:04 +00003761 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003762 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003763 break;
3764 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003765 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003766 Tmp1 = PromoteOp(Node->getOperand(0));
3767 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3768 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003769 break;
3770 }
3771 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003772
Chris Lattnerf2670a82008-01-16 06:57:07 +00003773 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003774 MVT DstVT = Op.getValueType();
3775 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00003776 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3777 // The only other way we can lower this is to turn it into a STORE,
3778 // LOAD pair, targetting a temporary location (a stack slot).
3779 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3780 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00003781 }
3782 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3783 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3784 case Legal:
3785 Tmp1 = LegalizeOp(Node->getOperand(0));
3786 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3787 break;
3788 case Promote:
3789 Tmp1 = PromoteOp(Node->getOperand(0));
3790 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3791 break;
3792 }
3793 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003794 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003795 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003796 MVT DstVT = Op.getValueType();
3797 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00003798 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3799 if (SrcVT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00003800 SDValue Lo;
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003801 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00003802 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003803 if (DstVT!=MVT::f64)
3804 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00003805 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00003806 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003807 // The only other way we can lower this is to turn it into a STORE,
3808 // LOAD pair, targetting a temporary location (a stack slot).
3809 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3810 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00003811 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00003812 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3813 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3814 case Legal:
3815 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003816 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003817 break;
3818 case Promote:
3819 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003820 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3821 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003822 break;
3823 }
3824 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003825 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003826 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003827 case ISD::ZERO_EXTEND:
3828 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003829 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003830 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003831 case Legal:
3832 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel82747a52008-04-30 00:26:38 +00003833 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel0123b7d2008-02-15 23:05:48 +00003834 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3835 TargetLowering::Custom) {
Scott Michel82747a52008-04-30 00:26:38 +00003836 Tmp1 = TLI.LowerOperation(Result, DAG);
3837 if (Tmp1.Val) Result = Tmp1;
Scott Michel0123b7d2008-02-15 23:05:48 +00003838 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003839 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003840 case Promote:
3841 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003842 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003843 Tmp1 = PromoteOp(Node->getOperand(0));
3844 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003845 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003846 case ISD::ZERO_EXTEND:
3847 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003848 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003849 Result = DAG.getZeroExtendInReg(Result,
3850 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003851 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003852 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003853 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003854 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003855 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003856 Result,
3857 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003858 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003859 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003860 }
3861 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003862 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003863 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003864 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003865 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003866
3867 // If this operation is not supported, convert it to a shl/shr or load/store
3868 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003869 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3870 default: assert(0 && "This action not supported for this op yet!");
3871 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003872 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003873 break;
3874 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003875 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003876 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003877 // NOTE: we could fall back on load/store here too for targets without
3878 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003879 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
3880 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003881 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003882 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3883 Node->getOperand(0), ShiftCst);
3884 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3885 Result, ShiftCst);
3886 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003887 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003888 // EXTLOAD pair, targetting a temporary location (a stack slot).
3889
3890 // NOTE: there is a choice here between constantly creating new stack
3891 // slots and always reusing the same one. We currently always create
3892 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00003893 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3894 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00003895 } else {
3896 assert(0 && "Unknown op");
3897 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003898 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003899 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003900 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003901 }
Duncan Sands36397f52007-07-27 12:58:54 +00003902 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00003903 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00003904 for (unsigned i = 0; i != 6; ++i)
3905 Ops[i] = LegalizeOp(Node->getOperand(i));
3906 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3907 // The only option for this node is to custom lower it.
3908 Result = TLI.LowerOperation(Result, DAG);
3909 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003910
3911 // Since trampoline produces two values, make sure to remember that we
3912 // legalized both of them.
3913 Tmp1 = LegalizeOp(Result.getValue(1));
3914 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00003915 AddLegalizedOperand(SDValue(Node, 0), Result);
3916 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Duncan Sandsf7331b32007-09-11 14:10:23 +00003917 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003918 }
Dan Gohman9c78a392008-05-14 00:43:10 +00003919 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003920 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003921 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3922 default: assert(0 && "This action not supported for this op yet!");
3923 case TargetLowering::Custom:
3924 Result = TLI.LowerOperation(Op, DAG);
3925 if (Result.Val) break;
3926 // Fall Thru
3927 case TargetLowering::Legal:
3928 // If this operation is not supported, lower it to constant 1
3929 Result = DAG.getConstant(1, VT);
3930 break;
3931 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00003932 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003933 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003934 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003935 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003936 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3937 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00003938 case TargetLowering::Legal:
3939 Tmp1 = LegalizeOp(Node->getOperand(0));
3940 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3941 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003942 case TargetLowering::Custom:
3943 Result = TLI.LowerOperation(Op, DAG);
3944 if (Result.Val) break;
3945 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00003946 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003947 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00003948 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003949 TargetLowering::ArgListTy Args;
Dan Gohman475871a2008-07-27 21:46:04 +00003950 std::pair<SDValue,SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00003951 TLI.LowerCallTo(Tmp1, Type::VoidTy,
3952 false, false, false, CallingConv::C, false,
Chris Lattner034f12e2008-01-15 22:09:33 +00003953 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3954 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003955 Result = CallResult.second;
3956 break;
3957 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003958 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003959 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003960 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003961
Chris Lattner4ddd2832006-04-08 04:13:17 +00003962 assert(Result.getValueType() == Op.getValueType() &&
3963 "Bad legalization!");
3964
Chris Lattner456a93a2006-01-28 07:39:30 +00003965 // Make sure that the generated code is itself legal.
3966 if (Result != Op)
3967 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003968
Chris Lattner45982da2005-05-12 16:53:42 +00003969 // Note that LegalizeOp may be reentered even from single-use nodes, which
3970 // means that we always must cache transformed nodes.
3971 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003972 return Result;
3973}
3974
Chris Lattner8b6fa222005-01-15 22:16:26 +00003975/// PromoteOp - Given an operation that produces a value in an invalid type,
3976/// promote it to compute the value into a larger type. The produced value will
3977/// have the correct bits for the low portion of the register, but no guarantee
3978/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00003979SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003980 MVT VT = Op.getValueType();
3981 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003982 assert(getTypeAction(VT) == Promote &&
3983 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00003984 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00003985 "Cannot promote to smaller type!");
3986
Dan Gohman475871a2008-07-27 21:46:04 +00003987 SDValue Tmp1, Tmp2, Tmp3;
3988 SDValue Result;
Chris Lattner03c85462005-01-15 05:21:40 +00003989 SDNode *Node = Op.Val;
3990
Dan Gohman475871a2008-07-27 21:46:04 +00003991 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003992 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003993
Chris Lattner03c85462005-01-15 05:21:40 +00003994 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003995 case ISD::CopyFromReg:
3996 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003997 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003998#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003999 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004000#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004001 assert(0 && "Do not know how to promote this operator!");
4002 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004003 case ISD::UNDEF:
4004 Result = DAG.getNode(ISD::UNDEF, NVT);
4005 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004006 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004007 if (VT != MVT::i1)
4008 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4009 else
4010 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004011 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4012 break;
4013 case ISD::ConstantFP:
4014 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4015 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4016 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004017
Chris Lattner82fbfb62005-01-18 02:59:52 +00004018 case ISD::SETCC:
Scott Michel5b8f82e2008-03-10 15:42:14 +00004019 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman5922f562008-03-14 00:53:31 +00004020 && "SetCC type is not legal??");
Scott Michel5b8f82e2008-03-10 15:42:14 +00004021 Result = DAG.getNode(ISD::SETCC,
Nate Begeman5922f562008-03-14 00:53:31 +00004022 TLI.getSetCCResultType(Node->getOperand(0)),
4023 Node->getOperand(0), Node->getOperand(1),
4024 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004025 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00004026
Chris Lattner03c85462005-01-15 05:21:40 +00004027 case ISD::TRUNCATE:
4028 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4029 case Legal:
4030 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004031 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004032 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004033 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Chris Lattner03c85462005-01-15 05:21:40 +00004034 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4035 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004036 case Promote:
4037 // The truncation is not required, because we don't guarantee anything
4038 // about high bits anyway.
4039 Result = PromoteOp(Node->getOperand(0));
4040 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004041 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004042 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4043 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004044 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004045 }
4046 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004047 case ISD::SIGN_EXTEND:
4048 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004049 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004050 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4051 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4052 case Legal:
4053 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004054 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004055 break;
4056 case Promote:
4057 // Promote the reg if it's smaller.
4058 Result = PromoteOp(Node->getOperand(0));
4059 // The high bits are not guaranteed to be anything. Insert an extend.
4060 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004061 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004062 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004063 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004064 Result = DAG.getZeroExtendInReg(Result,
4065 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004066 break;
4067 }
4068 break;
Chris Lattner35481892005-12-23 00:16:34 +00004069 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004070 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4071 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004072 Result = PromoteOp(Result);
4073 break;
4074
Chris Lattner8b6fa222005-01-15 22:16:26 +00004075 case ISD::FP_EXTEND:
4076 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4077 case ISD::FP_ROUND:
4078 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4079 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4080 case Promote: assert(0 && "Unreachable with 2 FP types!");
4081 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004082 if (Node->getConstantOperandVal(1) == 0) {
4083 // Input is legal? Do an FP_ROUND_INREG.
4084 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4085 DAG.getValueType(VT));
4086 } else {
4087 // Just remove the truncate, it isn't affecting the value.
4088 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4089 Node->getOperand(1));
4090 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004091 break;
4092 }
4093 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004094 case ISD::SINT_TO_FP:
4095 case ISD::UINT_TO_FP:
4096 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4097 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004098 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004099 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004100 break;
4101
4102 case Promote:
4103 Result = PromoteOp(Node->getOperand(0));
4104 if (Node->getOpcode() == ISD::SINT_TO_FP)
4105 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004106 Result,
4107 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004108 else
Chris Lattner23993562005-04-13 02:38:47 +00004109 Result = DAG.getZeroExtendInReg(Result,
4110 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004111 // No extra round required here.
4112 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004113 break;
4114 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004115 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4116 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004117 // Round if we cannot tolerate excess precision.
4118 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004119 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4120 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004121 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004122 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004123 break;
4124
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004125 case ISD::SIGN_EXTEND_INREG:
4126 Result = PromoteOp(Node->getOperand(0));
4127 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4128 Node->getOperand(1));
4129 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004130 case ISD::FP_TO_SINT:
4131 case ISD::FP_TO_UINT:
4132 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4133 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004134 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004135 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004136 break;
4137 case Promote:
4138 // The input result is prerounded, so we don't have to do anything
4139 // special.
4140 Tmp1 = PromoteOp(Node->getOperand(0));
4141 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004142 }
Nate Begemand2558e32005-08-14 01:20:53 +00004143 // If we're promoting a UINT to a larger size, check to see if the new node
4144 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4145 // we can use that instead. This allows us to generate better code for
4146 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4147 // legal, such as PowerPC.
4148 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004149 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004150 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4151 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004152 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4153 } else {
4154 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4155 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004156 break;
4157
Chris Lattner2c8086f2005-04-02 05:00:07 +00004158 case ISD::FABS:
4159 case ISD::FNEG:
4160 Tmp1 = PromoteOp(Node->getOperand(0));
4161 assert(Tmp1.getValueType() == NVT);
4162 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4163 // NOTE: we do not have to do any extra rounding here for
4164 // NoExcessFPPrecision, because we know the input will have the appropriate
4165 // precision, and these operations don't modify precision at all.
4166 break;
4167
Chris Lattnerda6ba872005-04-28 21:44:33 +00004168 case ISD::FSQRT:
4169 case ISD::FSIN:
4170 case ISD::FCOS:
4171 Tmp1 = PromoteOp(Node->getOperand(0));
4172 assert(Tmp1.getValueType() == NVT);
4173 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004174 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004175 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4176 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004177 break;
4178
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004179 case ISD::FPOWI: {
4180 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4181 // directly as well, which may be better.
4182 Tmp1 = PromoteOp(Node->getOperand(0));
4183 assert(Tmp1.getValueType() == NVT);
4184 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4185 if (NoExcessFPPrecision)
4186 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4187 DAG.getValueType(VT));
4188 break;
4189 }
4190
Mon P Wang28873102008-06-25 08:15:39 +00004191 case ISD::ATOMIC_CMP_SWAP: {
4192 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004193 Tmp2 = PromoteOp(Node->getOperand(2));
4194 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang28873102008-06-25 08:15:39 +00004195 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4196 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004197 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004198 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004199 // Remember that we legalized the chain.
4200 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4201 break;
4202 }
Mon P Wang28873102008-06-25 08:15:39 +00004203 case ISD::ATOMIC_LOAD_ADD:
4204 case ISD::ATOMIC_LOAD_SUB:
Mon P Wang63307c32008-05-05 19:05:59 +00004205 case ISD::ATOMIC_LOAD_AND:
4206 case ISD::ATOMIC_LOAD_OR:
4207 case ISD::ATOMIC_LOAD_XOR:
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004208 case ISD::ATOMIC_LOAD_NAND:
Mon P Wang63307c32008-05-05 19:05:59 +00004209 case ISD::ATOMIC_LOAD_MIN:
4210 case ISD::ATOMIC_LOAD_MAX:
4211 case ISD::ATOMIC_LOAD_UMIN:
4212 case ISD::ATOMIC_LOAD_UMAX:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004213 case ISD::ATOMIC_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004214 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004215 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang28873102008-06-25 08:15:39 +00004216 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4217 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004218 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004219 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004220 // Remember that we legalized the chain.
4221 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4222 break;
4223 }
4224
Chris Lattner03c85462005-01-15 05:21:40 +00004225 case ISD::AND:
4226 case ISD::OR:
4227 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004228 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004229 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004230 case ISD::MUL:
4231 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004232 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004233 // that too is okay if they are integer operations.
4234 Tmp1 = PromoteOp(Node->getOperand(0));
4235 Tmp2 = PromoteOp(Node->getOperand(1));
4236 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4237 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004238 break;
4239 case ISD::FADD:
4240 case ISD::FSUB:
4241 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004242 Tmp1 = PromoteOp(Node->getOperand(0));
4243 Tmp2 = PromoteOp(Node->getOperand(1));
4244 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4245 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4246
4247 // Floating point operations will give excess precision that we may not be
4248 // able to tolerate. If we DO allow excess precision, just leave it,
4249 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004250 // FIXME: Why would we need to round FP ops more than integer ones?
4251 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004252 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004253 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4254 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004255 break;
4256
Chris Lattner8b6fa222005-01-15 22:16:26 +00004257 case ISD::SDIV:
4258 case ISD::SREM:
4259 // These operators require that their input be sign extended.
4260 Tmp1 = PromoteOp(Node->getOperand(0));
4261 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004262 if (NVT.isInteger()) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004263 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4264 DAG.getValueType(VT));
4265 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4266 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004267 }
4268 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4269
4270 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004271 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004272 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4273 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004274 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004275 case ISD::FDIV:
4276 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004277 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004278 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004279 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004280 case Expand: assert(0 && "not implemented");
4281 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4282 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004283 }
4284 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004285 case Expand: assert(0 && "not implemented");
4286 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4287 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004288 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004289 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4290
4291 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004292 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004293 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4294 DAG.getValueType(VT));
4295 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004296
4297 case ISD::UDIV:
4298 case ISD::UREM:
4299 // These operators require that their input be zero extended.
4300 Tmp1 = PromoteOp(Node->getOperand(0));
4301 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004302 assert(NVT.isInteger() && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004303 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4304 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004305 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4306 break;
4307
4308 case ISD::SHL:
4309 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004310 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004311 break;
4312 case ISD::SRA:
4313 // The input value must be properly sign extended.
4314 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004315 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4316 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004317 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004318 break;
4319 case ISD::SRL:
4320 // The input value must be properly zero extended.
4321 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004322 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004323 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004324 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004325
4326 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004327 Tmp1 = Node->getOperand(0); // Get the chain.
4328 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004329 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4330 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004331 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004332 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004333 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004334 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004335 // Increment the pointer, VAList, to the next vaarg
4336 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004337 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004338 TLI.getPointerTy()));
4339 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004340 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004341 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004342 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004343 }
4344 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004345 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004346 break;
4347
Evan Cheng466685d2006-10-09 20:57:25 +00004348 case ISD::LOAD: {
4349 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004350 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4351 ? ISD::EXTLOAD : LD->getExtensionType();
4352 Result = DAG.getExtLoad(ExtType, NVT,
4353 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004354 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004355 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004356 LD->isVolatile(),
4357 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004358 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004359 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004360 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004361 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004362 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004363 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4364 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004365
Duncan Sands83ec4b62008-06-06 12:08:01 +00004366 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004367 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004368 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4369 // Ensure that the resulting node is at least the same size as the operands'
4370 // value types, because we cannot assume that TLI.getSetCCValueType() is
4371 // constant.
4372 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004373 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004374 }
Nate Begeman9373a812005-08-10 20:51:12 +00004375 case ISD::SELECT_CC:
4376 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4377 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4378 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004379 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004380 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004381 case ISD::BSWAP:
4382 Tmp1 = Node->getOperand(0);
4383 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4384 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4385 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004386 DAG.getConstant(NVT.getSizeInBits() -
4387 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004388 TLI.getShiftAmountTy()));
4389 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004390 case ISD::CTPOP:
4391 case ISD::CTTZ:
4392 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004393 // Zero extend the argument
4394 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004395 // Perform the larger operation, then subtract if needed.
4396 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004397 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004398 case ISD::CTPOP:
4399 Result = Tmp1;
4400 break;
4401 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004402 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004403 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004404 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004405 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004406 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004407 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004408 break;
4409 case ISD::CTLZ:
4410 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004411 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004412 DAG.getConstant(NVT.getSizeInBits() -
4413 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004414 break;
4415 }
4416 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004417 case ISD::EXTRACT_SUBVECTOR:
4418 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004419 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004420 case ISD::EXTRACT_VECTOR_ELT:
4421 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4422 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004423 }
4424
4425 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004426
4427 // Make sure the result is itself legal.
4428 Result = LegalizeOp(Result);
4429
4430 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004431 AddPromotedOperand(Op, Result);
4432 return Result;
4433}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004434
Dan Gohman7f321562007-06-25 16:23:39 +00004435/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4436/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4437/// based on the vector type. The return type of this matches the element type
4438/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004439SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004440 // We know that operand #0 is the Vec vector. If the index is a constant
4441 // or if the invec is a supported hardware type, we can use it. Otherwise,
4442 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00004443 SDValue Vec = Op.getOperand(0);
4444 SDValue Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004445
Duncan Sands83ec4b62008-06-06 12:08:01 +00004446 MVT TVT = Vec.getValueType();
4447 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner15972212006-03-31 17:55:51 +00004448
Dan Gohman7f321562007-06-25 16:23:39 +00004449 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4450 default: assert(0 && "This action is not supported yet!");
4451 case TargetLowering::Custom: {
4452 Vec = LegalizeOp(Vec);
4453 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004454 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Dan Gohman7f321562007-06-25 16:23:39 +00004455 if (Tmp3.Val)
4456 return Tmp3;
4457 break;
4458 }
4459 case TargetLowering::Legal:
4460 if (isTypeLegal(TVT)) {
4461 Vec = LegalizeOp(Vec);
4462 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004463 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004464 }
4465 break;
4466 case TargetLowering::Expand:
4467 break;
4468 }
4469
4470 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004471 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004472 Op = ScalarizeVectorOp(Vec);
4473 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00004474 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00004475 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004476 SDValue Lo, Hi;
Chris Lattner15972212006-03-31 17:55:51 +00004477 SplitVectorOp(Vec, Lo, Hi);
Nate Begeman55030dc2008-01-29 02:24:00 +00004478 if (CIdx->getValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00004479 Vec = Lo;
4480 } else {
4481 Vec = Hi;
Nate Begeman55030dc2008-01-29 02:24:00 +00004482 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00004483 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004484 }
Dan Gohman7f321562007-06-25 16:23:39 +00004485
Chris Lattner15972212006-03-31 17:55:51 +00004486 // It's now an extract from the appropriate high or low part. Recurse.
4487 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004488 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004489 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004490 // Store the value to a temporary stack slot, then LOAD the scalar
4491 // element back out.
Dan Gohman475871a2008-07-27 21:46:04 +00004492 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4493 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohman7f321562007-06-25 16:23:39 +00004494
4495 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004496 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohman7f321562007-06-25 16:23:39 +00004497 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4498 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004499
Duncan Sands8e4eb092008-06-08 20:54:56 +00004500 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004501 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004502 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004503 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004504
Dan Gohman7f321562007-06-25 16:23:39 +00004505 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4506
4507 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004508 }
Dan Gohman7f321562007-06-25 16:23:39 +00004509 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004510}
4511
Dan Gohman7f321562007-06-25 16:23:39 +00004512/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004513/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00004514SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004515 // We know that operand #0 is the Vec vector. For now we assume the index
4516 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00004517 SDValue Vec = Op.getOperand(0);
4518 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00004519
Duncan Sands83ec4b62008-06-06 12:08:01 +00004520 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman65956352007-06-13 15:12:02 +00004521
Duncan Sands83ec4b62008-06-06 12:08:01 +00004522 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00004523 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004524 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004525 }
4526
4527 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004528 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00004529 SplitVectorOp(Vec, Lo, Hi);
4530 if (CIdx->getValue() < NumElems/2) {
4531 Vec = Lo;
4532 } else {
4533 Vec = Hi;
4534 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4535 }
4536
4537 // It's now an extract from the appropriate high or low part. Recurse.
4538 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004539 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004540}
4541
Nate Begeman750ac1b2006-02-01 07:19:44 +00004542/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4543/// with condition CC on the current target. This usually involves legalizing
4544/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4545/// there may be no choice but to create a new SetCC node to represent the
4546/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00004547/// LHS, and the SDValue returned in RHS has a nil SDNode value.
4548void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4549 SDValue &RHS,
4550 SDValue &CC) {
4551 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004552
4553 switch (getTypeAction(LHS.getValueType())) {
4554 case Legal:
4555 Tmp1 = LegalizeOp(LHS); // LHS
4556 Tmp2 = LegalizeOp(RHS); // RHS
4557 break;
4558 case Promote:
4559 Tmp1 = PromoteOp(LHS); // LHS
4560 Tmp2 = PromoteOp(RHS); // RHS
4561
4562 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004563 if (LHS.getValueType().isInteger()) {
4564 MVT VT = LHS.getValueType();
4565 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004566
4567 // Otherwise, we have to insert explicit sign or zero extends. Note
4568 // that we could insert sign extends for ALL conditions, but zero extend
4569 // is cheaper on many machines (an AND instead of two shifts), so prefer
4570 // it.
4571 switch (cast<CondCodeSDNode>(CC)->get()) {
4572 default: assert(0 && "Unknown integer comparison!");
4573 case ISD::SETEQ:
4574 case ISD::SETNE:
4575 case ISD::SETUGE:
4576 case ISD::SETUGT:
4577 case ISD::SETULE:
4578 case ISD::SETULT:
4579 // ALL of these operations will work if we either sign or zero extend
4580 // the operands (including the unsigned comparisons!). Zero extend is
4581 // usually a simpler/cheaper operation, so prefer it.
4582 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4583 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4584 break;
4585 case ISD::SETGE:
4586 case ISD::SETGT:
4587 case ISD::SETLT:
4588 case ISD::SETLE:
4589 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4590 DAG.getValueType(VT));
4591 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4592 DAG.getValueType(VT));
4593 break;
4594 }
4595 }
4596 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004597 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004598 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00004599 if (VT == MVT::f32 || VT == MVT::f64) {
4600 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00004601 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004602 switch (cast<CondCodeSDNode>(CC)->get()) {
4603 case ISD::SETEQ:
4604 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004605 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004606 break;
4607 case ISD::SETNE:
4608 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004609 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004610 break;
4611 case ISD::SETGE:
4612 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004613 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004614 break;
4615 case ISD::SETLT:
4616 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004617 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004618 break;
4619 case ISD::SETLE:
4620 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004621 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004622 break;
4623 case ISD::SETGT:
4624 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004625 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004626 break;
4627 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004628 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4629 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004630 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004631 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004632 break;
4633 default:
Evan Cheng56966222007-01-12 02:11:51 +00004634 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004635 switch (cast<CondCodeSDNode>(CC)->get()) {
4636 case ISD::SETONE:
4637 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004638 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004639 // Fallthrough
4640 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004641 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004642 break;
4643 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004644 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004645 break;
4646 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004647 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004648 break;
4649 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004650 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004651 break;
Evan Cheng56966222007-01-12 02:11:51 +00004652 case ISD::SETUEQ:
4653 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004654 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004655 default: assert(0 && "Unsupported FP setcc!");
4656 }
4657 }
Duncan Sandsf9516202008-06-30 10:19:09 +00004658
Dan Gohman475871a2008-07-27 21:46:04 +00004659 SDValue Dummy;
4660 SDValue Ops[2] = { LHS, RHS };
Duncan Sands4bdcb612008-07-02 17:40:58 +00004661 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004662 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004663 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004664 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004665 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00004666 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00004667 CC);
Duncan Sands4bdcb612008-07-02 17:40:58 +00004668 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004669 false /*sign irrelevant*/, Dummy);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004670 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004671 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004672 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00004673 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00004674 }
Evan Cheng21cacc42008-07-07 07:18:09 +00004675 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00004676 RHS = Tmp2;
4677 return;
4678 }
4679
Dan Gohman475871a2008-07-27 21:46:04 +00004680 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004681 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004682 ExpandOp(RHS, RHSLo, RHSHi);
4683 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4684
4685 if (VT==MVT::ppcf128) {
4686 // FIXME: This generated code sucks. We want to generate
4687 // FCMP crN, hi1, hi2
4688 // BNE crN, L:
4689 // FCMP crN, lo1, lo2
4690 // The following can be improved, but not that much.
Scott Michel5b8f82e2008-03-10 15:42:14 +00004691 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
4692 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004693 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004694 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
4695 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004696 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4697 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00004698 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00004699 break;
4700 }
4701
4702 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004703 case ISD::SETEQ:
4704 case ISD::SETNE:
4705 if (RHSLo == RHSHi)
4706 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4707 if (RHSCST->isAllOnesValue()) {
4708 // Comparison to -1.
4709 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4710 Tmp2 = RHSLo;
4711 break;
4712 }
4713
4714 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4715 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4716 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4717 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4718 break;
4719 default:
4720 // If this is a comparison of the sign bit, just look at the top part.
4721 // X > -1, x < 0
4722 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4723 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004724 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00004725 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4726 CST->isAllOnesValue())) { // X > -1
4727 Tmp1 = LHSHi;
4728 Tmp2 = RHSHi;
4729 break;
4730 }
4731
4732 // FIXME: This generated code sucks.
4733 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004734 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004735 default: assert(0 && "Unknown integer setcc!");
4736 case ISD::SETLT:
4737 case ISD::SETULT: LowCC = ISD::SETULT; break;
4738 case ISD::SETGT:
4739 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4740 case ISD::SETLE:
4741 case ISD::SETULE: LowCC = ISD::SETULE; break;
4742 case ISD::SETGE:
4743 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4744 }
4745
4746 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4747 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4748 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4749
4750 // NOTE: on targets without efficient SELECT of bools, we can always use
4751 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004752 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004753 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman5922f562008-03-14 00:53:31 +00004754 LowCC, false, DagCombineInfo);
Evan Cheng2e677812007-02-08 22:16:19 +00004755 if (!Tmp1.Val)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004756 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
4757 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00004758 CCCode, false, DagCombineInfo);
4759 if (!Tmp2.Val)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004760 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00004761 RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004762
4763 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4764 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
Dan Gohman002e5d02008-03-13 22:13:53 +00004765 if ((Tmp1C && Tmp1C->isNullValue()) ||
4766 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00004767 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4768 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00004769 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00004770 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4771 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4772 // low part is known false, returns high part.
4773 // For LE / GE, if high part is known false, ignore the low part.
4774 // For LT / GT, if high part is known true, ignore the low part.
4775 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00004776 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00004777 } else {
Scott Michel5b8f82e2008-03-10 15:42:14 +00004778 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00004779 ISD::SETEQ, false, DagCombineInfo);
4780 if (!Result.Val)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004781 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00004782 ISD::SETEQ);
Evan Cheng2e677812007-02-08 22:16:19 +00004783 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4784 Result, Tmp1, Tmp2));
4785 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00004786 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00004787 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004788 }
4789 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004790 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004791 LHS = Tmp1;
4792 RHS = Tmp2;
4793}
4794
Chris Lattner1401d152008-01-16 07:45:30 +00004795/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4796/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4797/// a load from the stack slot to DestVT, extending it if needed.
4798/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00004799SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
4800 MVT SlotVT,
4801 MVT DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00004802 // Create the stack frame object.
Mon P Wang364d73d2008-07-05 20:40:31 +00004803 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
4804 SrcOp.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00004805 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang364d73d2008-07-05 20:40:31 +00004806
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00004807 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00004808 int SPFI = StackPtrFI->getIndex();
Mon P Wang364d73d2008-07-05 20:40:31 +00004809
Duncan Sands83ec4b62008-06-06 12:08:01 +00004810 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
4811 unsigned SlotSize = SlotVT.getSizeInBits();
4812 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang364d73d2008-07-05 20:40:31 +00004813 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
4814 DestVT.getTypeForMVT());
Chris Lattner35481892005-12-23 00:16:34 +00004815
Chris Lattner1401d152008-01-16 07:45:30 +00004816 // Emit a store to the stack slot. Use a truncstore if the input value is
4817 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00004818 SDValue Store;
Mon P Wang364d73d2008-07-05 20:40:31 +00004819
Chris Lattner1401d152008-01-16 07:45:30 +00004820 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00004821 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00004822 PseudoSourceValue::getFixedStack(SPFI), 0,
4823 SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00004824 else {
4825 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00004826 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00004827 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang364d73d2008-07-05 20:40:31 +00004828 false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00004829 }
4830
Chris Lattner35481892005-12-23 00:16:34 +00004831 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00004832 if (SlotSize == DestSize)
Mon P Wang364d73d2008-07-05 20:40:31 +00004833 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00004834
4835 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang364d73d2008-07-05 20:40:31 +00004836 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
4837 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00004838}
4839
Dan Gohman475871a2008-07-27 21:46:04 +00004840SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Chris Lattner4352cc92006-04-04 17:23:26 +00004841 // Create a vector sized/aligned stack slot, store the value to element #0,
4842 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00004843 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00004844
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00004845 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00004846 int SPFI = StackPtrFI->getIndex();
4847
Dan Gohman475871a2008-07-27 21:46:04 +00004848 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00004849 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00004850 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00004851 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004852}
4853
4854
Chris Lattnerce872152006-03-19 06:31:19 +00004855/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004856/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00004857SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattnerce872152006-03-19 06:31:19 +00004858
4859 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004860 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004861 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004862 bool isOnlyLowElement = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004863 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004864
Dan Gohman475871a2008-07-27 21:46:04 +00004865 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004866 // and use a bitmask instead of a list of elements.
Dan Gohman475871a2008-07-27 21:46:04 +00004867 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00004868 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004869 bool isConstant = true;
4870 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4871 SplatValue.getOpcode() != ISD::UNDEF)
4872 isConstant = false;
4873
Evan Cheng033e6812006-03-24 01:17:21 +00004874 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004875 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004876 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004877 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004878 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004879 if (SplatValue != V)
Dan Gohman475871a2008-07-27 21:46:04 +00004880 SplatValue = SDValue(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004881
4882 // If this isn't a constant element or an undef, we can't use a constant
4883 // pool load.
4884 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4885 V.getOpcode() != ISD::UNDEF)
4886 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004887 }
4888
4889 if (isOnlyLowElement) {
4890 // If the low element is an undef too, then this whole things is an undef.
4891 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4892 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4893 // Otherwise, turn this into a scalar_to_vector node.
4894 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4895 Node->getOperand(0));
4896 }
4897
Chris Lattner2eb86532006-03-24 07:29:17 +00004898 // If all elements are constants, create a load from the constant pool.
4899 if (isConstant) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004900 MVT VT = Node->getValueType(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004901 std::vector<Constant*> CV;
4902 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4903 if (ConstantFPSDNode *V =
4904 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004905 CV.push_back(ConstantFP::get(V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004906 } else if (ConstantSDNode *V =
Chris Lattner02a260a2008-04-20 00:41:09 +00004907 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
4908 CV.push_back(ConstantInt::get(V->getAPIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004909 } else {
4910 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner02a260a2008-04-20 00:41:09 +00004911 const Type *OpNTy =
Duncan Sands83ec4b62008-06-06 12:08:01 +00004912 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00004913 CV.push_back(UndefValue::get(OpNTy));
4914 }
4915 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004916 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004917 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00004918 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004919 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004920 }
4921
Chris Lattner87100e02006-03-20 01:52:29 +00004922 if (SplatValue.Val) { // Splat of one value?
4923 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands83ec4b62008-06-06 12:08:01 +00004924 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman475871a2008-07-27 21:46:04 +00004925 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
4926 std::vector<SDValue> ZeroVec(NumElems, Zero);
4927 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004928 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004929
4930 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004931 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004932 // Get the splatted value into the low element of a vector register.
Dan Gohman475871a2008-07-27 21:46:04 +00004933 SDValue LowValVec =
Chris Lattner87100e02006-03-20 01:52:29 +00004934 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4935
4936 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4937 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4938 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4939 SplatMask);
4940 }
4941 }
4942
Evan Cheng033e6812006-03-24 01:17:21 +00004943 // If there are only two unique elements, we may be able to turn this into a
4944 // vector shuffle.
4945 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004946 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00004947 SDValue Val1 = Node->getOperand(1);
4948 SDValue Val2;
4949 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004950 if (MI->first != Val1)
4951 Val2 = MI->first;
4952 else
4953 Val2 = (++MI)->first;
4954
4955 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
4956 // vector shuffle has the undef vector on the RHS.
4957 if (Val1.getOpcode() == ISD::UNDEF)
4958 std::swap(Val1, Val2);
4959
Evan Cheng033e6812006-03-24 01:17:21 +00004960 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands83ec4b62008-06-06 12:08:01 +00004961 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
4962 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00004963 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004964
4965 // Set elements of the shuffle mask for Val1.
4966 std::vector<unsigned> &Val1Elts = Values[Val1];
4967 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
4968 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
4969
4970 // Set elements of the shuffle mask for Val2.
4971 std::vector<unsigned> &Val2Elts = Values[Val2];
4972 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
4973 if (Val2.getOpcode() != ISD::UNDEF)
4974 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
4975 else
4976 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
4977
Dan Gohman475871a2008-07-27 21:46:04 +00004978 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004979 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004980
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004981 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004982 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4983 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004984 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
4985 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman475871a2008-07-27 21:46:04 +00004986 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00004987
4988 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004989 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00004990 }
4991 }
Chris Lattnerce872152006-03-19 06:31:19 +00004992
4993 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4994 // aligned object on the stack, store each element into it, then load
4995 // the result as a vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004996 MVT VT = Node->getValueType(0);
Chris Lattnerce872152006-03-19 06:31:19 +00004997 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00004998 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00004999
5000 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005001 SmallVector<SDValue, 8> Stores;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005002 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005003 // Store (in the right endianness) the elements to memory.
5004 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5005 // Ignore undef elements.
5006 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5007
Chris Lattner841c8822006-03-22 01:46:54 +00005008 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005009
Dan Gohman475871a2008-07-27 21:46:04 +00005010 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Chris Lattnerce872152006-03-19 06:31:19 +00005011 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5012
Evan Cheng786225a2006-10-05 23:01:46 +00005013 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00005014 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00005015 }
5016
Dan Gohman475871a2008-07-27 21:46:04 +00005017 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005018 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005019 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5020 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005021 else
5022 StoreChain = DAG.getEntryNode();
5023
5024 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00005025 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005026}
5027
Chris Lattner5b359c62005-04-02 04:00:59 +00005028void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005029 SDValue Op, SDValue Amt,
5030 SDValue &Lo, SDValue &Hi) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005031 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005032 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005033 ExpandOp(Op, LHSL, LHSH);
5034
Dan Gohman475871a2008-07-27 21:46:04 +00005035 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005036 MVT VT = LHSL.getValueType();
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005037 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005038 Hi = Lo.getValue(1);
5039}
5040
5041
Chris Lattnere34b3962005-01-19 04:19:40 +00005042/// ExpandShift - Try to find a clever way to expand this shift operation out to
5043/// smaller elements. If we can't find a way that is more efficient than a
5044/// libcall on this target, return false. Otherwise, return true with the
5045/// low-parts expanded into Lo and Hi.
Dan Gohman475871a2008-07-27 21:46:04 +00005046bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5047 SDValue &Lo, SDValue &Hi) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005048 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5049 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005050
Duncan Sands83ec4b62008-06-06 12:08:01 +00005051 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005052 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005053 MVT ShTy = ShAmt.getValueType();
5054 unsigned ShBits = ShTy.getSizeInBits();
5055 unsigned VTBits = Op.getValueType().getSizeInBits();
5056 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005057
Chris Lattner7a3c8552007-10-14 20:35:12 +00005058 // Handle the case when Amt is an immediate.
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005059 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5060 unsigned Cst = CN->getValue();
5061 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005062 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005063 ExpandOp(Op, InL, InH);
5064 switch(Opc) {
5065 case ISD::SHL:
5066 if (Cst > VTBits) {
5067 Lo = DAG.getConstant(0, NVT);
5068 Hi = DAG.getConstant(0, NVT);
5069 } else if (Cst > NVTBits) {
5070 Lo = DAG.getConstant(0, NVT);
5071 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005072 } else if (Cst == NVTBits) {
5073 Lo = DAG.getConstant(0, NVT);
5074 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005075 } else {
5076 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5077 Hi = DAG.getNode(ISD::OR, NVT,
5078 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5079 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5080 }
5081 return true;
5082 case ISD::SRL:
5083 if (Cst > VTBits) {
5084 Lo = DAG.getConstant(0, NVT);
5085 Hi = DAG.getConstant(0, NVT);
5086 } else if (Cst > NVTBits) {
5087 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5088 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005089 } else if (Cst == NVTBits) {
5090 Lo = InH;
5091 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005092 } else {
5093 Lo = DAG.getNode(ISD::OR, NVT,
5094 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5095 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5096 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5097 }
5098 return true;
5099 case ISD::SRA:
5100 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005101 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005102 DAG.getConstant(NVTBits-1, ShTy));
5103 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005104 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005105 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005106 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005107 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005108 } else if (Cst == NVTBits) {
5109 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005110 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005111 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005112 } else {
5113 Lo = DAG.getNode(ISD::OR, NVT,
5114 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5115 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5116 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5117 }
5118 return true;
5119 }
5120 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005121
5122 // Okay, the shift amount isn't constant. However, if we can tell that it is
5123 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005124 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5125 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005126 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005127
Dan Gohman9e255b72008-02-22 01:12:31 +00005128 // If we know that if any of the high bits of the shift amount are one, then
5129 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005130 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005131 // Mask out the high bit, which we know is set.
5132 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005133 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005134
5135 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005136 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005137 ExpandOp(Op, InL, InH);
5138 switch(Opc) {
5139 case ISD::SHL:
5140 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5141 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5142 return true;
5143 case ISD::SRL:
5144 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5145 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5146 return true;
5147 case ISD::SRA:
5148 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5149 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5150 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5151 return true;
5152 }
5153 }
5154
Dan Gohman9e255b72008-02-22 01:12:31 +00005155 // If we know that the high bits of the shift amount are all zero, then we can
5156 // do this as a couple of simple shifts.
5157 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005158 // Compute 32-amt.
Dan Gohman475871a2008-07-27 21:46:04 +00005159 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005160 DAG.getConstant(NVTBits, Amt.getValueType()),
5161 Amt);
5162
5163 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005164 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005165 ExpandOp(Op, InL, InH);
5166 switch(Opc) {
5167 case ISD::SHL:
5168 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5169 Hi = DAG.getNode(ISD::OR, NVT,
5170 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5171 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5172 return true;
5173 case ISD::SRL:
5174 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5175 Lo = DAG.getNode(ISD::OR, NVT,
5176 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5177 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5178 return true;
5179 case ISD::SRA:
5180 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5181 Lo = DAG.getNode(ISD::OR, NVT,
5182 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5183 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5184 return true;
5185 }
5186 }
5187
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005188 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005189}
Chris Lattner77e77a62005-01-21 06:05:23 +00005190
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005191
Chris Lattner77e77a62005-01-21 06:05:23 +00005192// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5193// does not fit into a register, return the lo part and set the hi part to the
5194// by-reg argument. If it does fit into a single register, return the result
5195// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005196SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5197 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005198 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5199 // The input chain to this libcall is the entry node of the function.
5200 // Legalizing the call will automatically add the previous call to the
5201 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005202 SDValue InChain = DAG.getEntryNode();
Chris Lattner6831a812006-02-13 09:18:02 +00005203
Chris Lattner77e77a62005-01-21 06:05:23 +00005204 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005205 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005206 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005207 MVT ArgVT = Node->getOperand(i).getValueType();
5208 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencer47857812006-12-31 05:55:36 +00005209 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005210 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005211 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005212 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005213 }
Dan Gohman475871a2008-07-27 21:46:04 +00005214 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Duncan Sands460a14e2008-04-12 17:14:18 +00005215 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005216
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005217 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005218 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman475871a2008-07-27 21:46:04 +00005219 std::pair<SDValue,SDValue> CallInfo =
Duncan Sands00fee652008-02-14 17:28:50 +00005220 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5221 false, Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005222
Chris Lattner6831a812006-02-13 09:18:02 +00005223 // Legalize the call sequence, starting with the chain. This will advance
5224 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5225 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5226 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005227 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005228 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005229 default: assert(0 && "Unknown thing");
5230 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005231 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005232 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005233 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005234 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005235 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005236 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005237 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005238}
5239
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005240
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005241/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5242///
Dan Gohman475871a2008-07-27 21:46:04 +00005243SDValue SelectionDAGLegalize::
5244ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005245 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005246 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005247
Evan Cheng9845eb52008-04-01 02:18:22 +00005248 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5249 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005250 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005251 // incoming integer is set. To handle this, we dynamically test to see if
5252 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005253 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005254 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005255 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005256 ExpandOp(Source, Lo, Hi);
5257 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5258 } else {
5259 // The comparison for the sign bit will use the entire operand.
5260 Hi = Source;
5261 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005262
Chris Lattner66de05b2005-05-13 04:45:13 +00005263 // If this is unsigned, and not supported, first perform the conversion to
5264 // signed, then adjust the result if the sign bit is set.
Dan Gohman475871a2008-07-27 21:46:04 +00005265 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner66de05b2005-05-13 04:45:13 +00005266
Dan Gohman475871a2008-07-27 21:46:04 +00005267 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005268 DAG.getConstant(0, Hi.getValueType()),
5269 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005270 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5271 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00005272 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005273 uint64_t FF = 0x5f800000ULL;
5274 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00005275 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005276
Dan Gohman475871a2008-07-27 21:46:04 +00005277 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00005278 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman475871a2008-07-27 21:46:04 +00005279 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00005280 if (DestTy == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005281 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005282 PseudoSourceValue::getConstantPool(), 0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005283 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005284 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005285 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005286 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005287 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman69de1932008-02-06 22:27:42 +00005288 MVT::f32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005289 else
5290 assert(0 && "Unexpected conversion");
5291
Duncan Sands83ec4b62008-06-06 12:08:01 +00005292 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005293 if (SCVT != DestTy) {
5294 // Destination type needs to be expanded as well. The FADD now we are
5295 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005296 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5297 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmand91446d2008-03-05 01:08:17 +00005298 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005299 SignedConv, SignedConv.getValue(1));
5300 }
5301 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5302 }
Chris Lattner473a9902005-09-29 06:44:39 +00005303 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005304 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005305
Chris Lattnera88a2602005-05-14 05:33:54 +00005306 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005307 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005308 default: assert(0 && "This action not implemented for this operation!");
5309 case TargetLowering::Legal:
5310 case TargetLowering::Expand:
5311 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005312 case TargetLowering::Custom: {
Dan Gohman475871a2008-07-27 21:46:04 +00005313 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Chris Lattner07dffd62005-08-26 00:14:16 +00005314 Source), DAG);
5315 if (NV.Val)
5316 return LegalizeOp(NV);
5317 break; // The target decided this was legal after all
5318 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005319 }
5320
Chris Lattner13689e22005-05-12 07:00:44 +00005321 // Expand the source, then glue it back together for the call. We must expand
5322 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00005323 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005324 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005325 ExpandOp(Source, SrcLo, SrcHi);
5326 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5327 }
Chris Lattner13689e22005-05-12 07:00:44 +00005328
Duncan Sandsb2ff8852008-07-17 02:36:29 +00005329 RTLIB::Libcall LC = isSigned ?
5330 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5331 RTLIB::getUINTTOFP(SourceVT, DestTy);
5332 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5333
Chris Lattner6831a812006-02-13 09:18:02 +00005334 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00005335 SDValue HiPart;
5336 SDValue Result = ExpandLibCall(LC, Source.Val, isSigned, HiPart);
Evan Cheng110cf482008-04-01 01:50:16 +00005337 if (Result.getValueType() != DestTy && HiPart.Val)
Dan Gohmana2e94852008-03-10 23:03:31 +00005338 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5339 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005340}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005341
Chris Lattner22cde6a2006-01-28 08:25:58 +00005342/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5343/// INT_TO_FP operation of the specified operand when the target requests that
5344/// we expand it. At this point, we know that the result and operand types are
5345/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00005346SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5347 SDValue Op0,
5348 MVT DestVT) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005349 if (Op0.getValueType() == MVT::i32) {
5350 // simple 32-bit [signed|unsigned] integer to float/double expansion
5351
Chris Lattner23594d42008-01-16 07:03:22 +00005352 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00005353 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner23594d42008-01-16 07:03:22 +00005354
Chris Lattner22cde6a2006-01-28 08:25:58 +00005355 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00005356 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00005357 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00005358 SDValue Hi = StackSlot;
5359 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00005360 if (TLI.isLittleEndian())
5361 std::swap(Hi, Lo);
5362
Chris Lattner22cde6a2006-01-28 08:25:58 +00005363 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00005364 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005365 if (isSigned) {
5366 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00005367 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005368 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5369 } else {
5370 Op0Mapped = Op0;
5371 }
5372 // store the lo of the constructed double - based on integer input
Dan Gohman475871a2008-07-27 21:46:04 +00005373 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005374 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005375 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00005376 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005377 // store the hi of the constructed double - biased exponent
Dan Gohman475871a2008-07-27 21:46:04 +00005378 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005379 // load the constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00005380 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005381 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00005382 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner22cde6a2006-01-28 08:25:58 +00005383 BitsToDouble(0x4330000080000000ULL)
5384 : BitsToDouble(0x4330000000000000ULL),
5385 MVT::f64);
5386 // subtract the bias
Dan Gohman475871a2008-07-27 21:46:04 +00005387 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005388 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00005389 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005390 // handle final rounding
5391 if (DestVT == MVT::f64) {
5392 // do nothing
5393 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00005394 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00005395 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5396 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00005397 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005398 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005399 }
5400 return Result;
5401 }
5402 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman475871a2008-07-27 21:46:04 +00005403 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005404
Dan Gohman475871a2008-07-27 21:46:04 +00005405 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005406 DAG.getConstant(0, Op0.getValueType()),
5407 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005408 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5409 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005410 SignSet, Four, Zero);
5411
5412 // If the sign bit of the integer is set, the large number will be treated
5413 // as a negative number. To counteract this, the dynamic code adds an
5414 // offset depending on the data type.
5415 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005416 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005417 default: assert(0 && "Unsupported integer type!");
5418 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5419 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5420 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5421 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5422 }
5423 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005424 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005425
Dan Gohman475871a2008-07-27 21:46:04 +00005426 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00005427 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman475871a2008-07-27 21:46:04 +00005428 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005429 if (DestVT == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005430 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005431 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005432 else {
Dan Gohman69de1932008-02-06 22:27:42 +00005433 FudgeInReg =
5434 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5435 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005436 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman69de1932008-02-06 22:27:42 +00005437 MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005438 }
5439
5440 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5441}
5442
5443/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5444/// *INT_TO_FP operation of the specified operand when the target requests that
5445/// we promote it. At this point, we know that the result and operand types are
5446/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5447/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00005448SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
5449 MVT DestVT,
5450 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005451 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005452 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005453
5454 unsigned OpToUse = 0;
5455
5456 // Scan for the appropriate larger type to use.
5457 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005458 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
5459 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00005460
5461 // If the target supports SINT_TO_FP of this type, use it.
5462 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5463 default: break;
5464 case TargetLowering::Legal:
5465 if (!TLI.isTypeLegal(NewInTy))
5466 break; // Can't use this datatype.
5467 // FALL THROUGH.
5468 case TargetLowering::Custom:
5469 OpToUse = ISD::SINT_TO_FP;
5470 break;
5471 }
5472 if (OpToUse) break;
5473 if (isSigned) continue;
5474
5475 // If the target supports UINT_TO_FP of this type, use it.
5476 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5477 default: break;
5478 case TargetLowering::Legal:
5479 if (!TLI.isTypeLegal(NewInTy))
5480 break; // Can't use this datatype.
5481 // FALL THROUGH.
5482 case TargetLowering::Custom:
5483 OpToUse = ISD::UINT_TO_FP;
5484 break;
5485 }
5486 if (OpToUse) break;
5487
5488 // Otherwise, try a larger type.
5489 }
5490
5491 // Okay, we found the operation and type to use. Zero extend our input to the
5492 // desired type then run the operation on it.
5493 return DAG.getNode(OpToUse, DestVT,
5494 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5495 NewInTy, LegalOp));
5496}
5497
5498/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5499/// FP_TO_*INT operation of the specified operand when the target requests that
5500/// we promote it. At this point, we know that the result and operand types are
5501/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5502/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00005503SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
5504 MVT DestVT,
5505 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005506 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005507 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005508
5509 unsigned OpToUse = 0;
5510
5511 // Scan for the appropriate larger type to use.
5512 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005513 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
5514 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00005515
5516 // If the target supports FP_TO_SINT returning this type, use it.
5517 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5518 default: break;
5519 case TargetLowering::Legal:
5520 if (!TLI.isTypeLegal(NewOutTy))
5521 break; // Can't use this datatype.
5522 // FALL THROUGH.
5523 case TargetLowering::Custom:
5524 OpToUse = ISD::FP_TO_SINT;
5525 break;
5526 }
5527 if (OpToUse) break;
5528
5529 // If the target supports FP_TO_UINT of this type, use it.
5530 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5531 default: break;
5532 case TargetLowering::Legal:
5533 if (!TLI.isTypeLegal(NewOutTy))
5534 break; // Can't use this datatype.
5535 // FALL THROUGH.
5536 case TargetLowering::Custom:
5537 OpToUse = ISD::FP_TO_UINT;
5538 break;
5539 }
5540 if (OpToUse) break;
5541
5542 // Otherwise, try a larger type.
5543 }
5544
Chris Lattner27a6c732007-11-24 07:07:01 +00005545
5546 // Okay, we found the operation and type to use.
Dan Gohman475871a2008-07-27 21:46:04 +00005547 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00005548
Chris Lattner27a6c732007-11-24 07:07:01 +00005549 // If the operation produces an invalid type, it must be custom lowered. Use
5550 // the target lowering hooks to expand it. Just keep the low part of the
5551 // expanded operation, we know that we're truncating anyway.
5552 if (getTypeAction(NewOutTy) == Expand) {
Dan Gohman475871a2008-07-27 21:46:04 +00005553 Operation = SDValue(TLI.ReplaceNodeResults(Operation.Val, DAG), 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005554 assert(Operation.Val && "Didn't return anything");
5555 }
Duncan Sands126d9072008-07-04 11:47:58 +00005556
Chris Lattner27a6c732007-11-24 07:07:01 +00005557 // Truncate the result of the extended FP_TO_*INT operation to the desired
5558 // size.
5559 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005560}
5561
5562/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5563///
Dan Gohman475871a2008-07-27 21:46:04 +00005564SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005565 MVT VT = Op.getValueType();
5566 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00005567 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005568 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005569 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5570 case MVT::i16:
5571 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5572 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5573 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5574 case MVT::i32:
5575 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5576 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5577 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5578 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5579 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5580 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5581 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5582 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5583 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5584 case MVT::i64:
5585 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5586 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5587 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5588 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5589 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5590 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5591 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5592 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5593 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5594 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5595 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5596 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5597 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5598 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5599 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5600 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5601 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5602 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5603 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5604 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5605 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5606 }
5607}
5608
5609/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5610///
Dan Gohman475871a2008-07-27 21:46:04 +00005611SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005612 switch (Opc) {
5613 default: assert(0 && "Cannot expand this yet!");
5614 case ISD::CTPOP: {
5615 static const uint64_t mask[6] = {
5616 0x5555555555555555ULL, 0x3333333333333333ULL,
5617 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5618 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5619 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005620 MVT VT = Op.getValueType();
5621 MVT ShVT = TLI.getShiftAmountTy();
5622 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005623 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5624 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman475871a2008-07-27 21:46:04 +00005625 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
5626 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005627 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5628 DAG.getNode(ISD::AND, VT,
5629 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5630 }
5631 return Op;
5632 }
5633 case ISD::CTLZ: {
5634 // for now, we do this:
5635 // x = x | (x >> 1);
5636 // x = x | (x >> 2);
5637 // ...
5638 // x = x | (x >>16);
5639 // x = x | (x >>32); // for 64-bit input
5640 // return popcount(~x);
5641 //
5642 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00005643 MVT VT = Op.getValueType();
5644 MVT ShVT = TLI.getShiftAmountTy();
5645 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005646 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005647 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005648 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5649 }
5650 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5651 return DAG.getNode(ISD::CTPOP, VT, Op);
5652 }
5653 case ISD::CTTZ: {
5654 // for now, we use: { return popcount(~x & (x - 1)); }
5655 // unless the target has ctlz but not ctpop, in which case we use:
5656 // { return 32 - nlz(~x & (x-1)); }
5657 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00005658 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005659 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
5660 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005661 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5662 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5663 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5664 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5665 TLI.isOperationLegal(ISD::CTLZ, VT))
5666 return DAG.getNode(ISD::SUB, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005667 DAG.getConstant(VT.getSizeInBits(), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005668 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5669 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5670 }
5671 }
5672}
Chris Lattnere34b3962005-01-19 04:19:40 +00005673
Dan Gohman475871a2008-07-27 21:46:04 +00005674/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00005675/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5676/// LegalizeNodes map is filled in for any results that are not expanded, the
5677/// ExpandedNodes map is filled in for any results that are expanded, and the
5678/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00005679void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands83ec4b62008-06-06 12:08:01 +00005680 MVT VT = Op.getValueType();
5681 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005682 SDNode *Node = Op.Val;
5683 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00005684 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00005685 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005686
Chris Lattner6fdcb252005-09-02 20:32:45 +00005687 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00005688 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005689 = ExpandedNodes.find(Op);
5690 if (I != ExpandedNodes.end()) {
5691 Lo = I->second.first;
5692 Hi = I->second.second;
5693 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005694 }
5695
Chris Lattner3e928bb2005-01-07 07:47:09 +00005696 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005697 case ISD::CopyFromReg:
5698 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005699 case ISD::FP_ROUND_INREG:
5700 if (VT == MVT::ppcf128 &&
5701 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5702 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00005703 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005704 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5705 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman475871a2008-07-27 21:46:04 +00005706 SDValue Result = TLI.LowerOperation(
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005707 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005708 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5709 Lo = Result.Val->getOperand(0);
5710 Hi = Result.Val->getOperand(1);
5711 break;
5712 }
5713 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005714 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005715#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005716 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005717#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005718 assert(0 && "Do not know how to expand this operator!");
5719 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00005720 case ISD::EXTRACT_ELEMENT:
5721 ExpandOp(Node->getOperand(0), Lo, Hi);
5722 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5723 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00005724 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00005725 case ISD::EXTRACT_VECTOR_ELT:
5726 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5727 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5728 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5729 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005730 case ISD::UNDEF:
5731 Lo = DAG.getNode(ISD::UNDEF, NVT);
5732 Hi = DAG.getNode(ISD::UNDEF, NVT);
5733 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005734 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005735 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00005736 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
5737 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
5738 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005739 break;
5740 }
Evan Cheng00495212006-12-12 21:32:44 +00005741 case ISD::ConstantFP: {
5742 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005743 if (CFP->getValueType(0) == MVT::ppcf128) {
5744 APInt api = CFP->getValueAPF().convertToAPInt();
5745 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5746 MVT::f64);
5747 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5748 MVT::f64);
5749 break;
5750 }
Evan Cheng279101e2006-12-12 22:19:28 +00005751 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005752 if (getTypeAction(Lo.getValueType()) == Expand)
5753 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005754 break;
5755 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005756 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005757 // Return the operands.
5758 Lo = Node->getOperand(0);
5759 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005760 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005761
5762 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00005763 if (Node->getNumValues() == 1) {
5764 ExpandOp(Op.getOperand(0), Lo, Hi);
5765 break;
5766 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005767 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5768 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5769 Op.getValue(1).getValueType() == MVT::Other &&
5770 "unhandled MERGE_VALUES");
5771 ExpandOp(Op.getOperand(0), Lo, Hi);
5772 // Remember that we legalized the chain.
5773 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5774 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005775
5776 case ISD::SIGN_EXTEND_INREG:
5777 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005778 // sext_inreg the low part if needed.
5779 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5780
5781 // The high part gets the sign extension from the lo-part. This handles
5782 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005783 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005784 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00005785 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005786 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005787
Nate Begemand88fc032006-01-14 03:14:10 +00005788 case ISD::BSWAP: {
5789 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00005790 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Nate Begemand88fc032006-01-14 03:14:10 +00005791 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5792 Lo = TempLo;
5793 break;
5794 }
5795
Chris Lattneredb1add2005-05-11 04:51:16 +00005796 case ISD::CTPOP:
5797 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005798 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5799 DAG.getNode(ISD::CTPOP, NVT, Lo),
5800 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005801 Hi = DAG.getConstant(0, NVT);
5802 break;
5803
Chris Lattner39a8f332005-05-12 19:05:01 +00005804 case ISD::CTLZ: {
5805 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005806 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00005807 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
5808 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
5809 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005810 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00005811 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Chris Lattner39a8f332005-05-12 19:05:01 +00005812 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5813
5814 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5815 Hi = DAG.getConstant(0, NVT);
5816 break;
5817 }
5818
5819 case ISD::CTTZ: {
5820 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005821 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00005822 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
5823 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
5824 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005825 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00005826 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005827 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5828
5829 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5830 Hi = DAG.getConstant(0, NVT);
5831 break;
5832 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005833
Nate Begemanacc398c2006-01-25 18:21:52 +00005834 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00005835 SDValue Ch = Node->getOperand(0); // Legalize the chain.
5836 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005837 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5838 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5839
5840 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005841 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005842 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00005843 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00005844 std::swap(Lo, Hi);
5845 break;
5846 }
5847
Chris Lattner3e928bb2005-01-07 07:47:09 +00005848 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005849 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00005850 SDValue Ch = LD->getChain(); // Legalize the chain.
5851 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00005852 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005853 int SVOffset = LD->getSrcValueOffset();
5854 unsigned Alignment = LD->getAlignment();
5855 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005856
Evan Cheng466685d2006-10-09 20:57:25 +00005857 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005858 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5859 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005860 if (VT == MVT::f32 || VT == MVT::f64) {
5861 // f32->i32 or f64->i64 one to one expansion.
5862 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005863 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005864 // Recursively expand the new load.
5865 if (getTypeAction(NVT) == Expand)
5866 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005867 break;
5868 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005869
Evan Cheng466685d2006-10-09 20:57:25 +00005870 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005871 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Evan Cheng466685d2006-10-09 20:57:25 +00005872 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00005873 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005874 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00005875 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005876 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5877 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005878
Evan Cheng466685d2006-10-09 20:57:25 +00005879 // Build a factor node to remember that this load is independent of the
5880 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00005881 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Evan Cheng466685d2006-10-09 20:57:25 +00005882 Hi.getValue(1));
5883
5884 // Remember that we legalized the chain.
5885 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00005886 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00005887 std::swap(Lo, Hi);
5888 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005889 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005890
Dale Johannesenb6210fc2007-10-19 20:29:00 +00005891 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5892 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00005893 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman475871a2008-07-27 21:46:04 +00005894 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005895 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005896 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005897 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Evan Cheng548f6112006-12-13 03:19:57 +00005898 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5899 break;
5900 }
Evan Cheng466685d2006-10-09 20:57:25 +00005901
5902 if (EVT == NVT)
5903 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005904 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005905 else
5906 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005907 SVOffset, EVT, isVolatile,
5908 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005909
5910 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005911 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00005912
5913 if (ExtType == ISD::SEXTLOAD) {
5914 // The high part is obtained by SRA'ing all but one of the bits of the
5915 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005916 unsigned LoSize = Lo.getValueType().getSizeInBits();
Evan Cheng466685d2006-10-09 20:57:25 +00005917 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5918 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5919 } else if (ExtType == ISD::ZEXTLOAD) {
5920 // The high part is just a zero.
5921 Hi = DAG.getConstant(0, NVT);
5922 } else /* if (ExtType == ISD::EXTLOAD) */ {
5923 // The high part is undefined.
5924 Hi = DAG.getNode(ISD::UNDEF, NVT);
5925 }
5926 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005927 break;
5928 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005929 case ISD::AND:
5930 case ISD::OR:
5931 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00005932 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005933 ExpandOp(Node->getOperand(0), LL, LH);
5934 ExpandOp(Node->getOperand(1), RL, RH);
5935 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5936 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5937 break;
5938 }
5939 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00005940 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005941 ExpandOp(Node->getOperand(1), LL, LH);
5942 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005943 if (getTypeAction(NVT) == Expand)
5944 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005945 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005946 if (VT != MVT::f32)
5947 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005948 break;
5949 }
Nate Begeman9373a812005-08-10 20:51:12 +00005950 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00005951 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00005952 ExpandOp(Node->getOperand(2), TL, TH);
5953 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005954 if (getTypeAction(NVT) == Expand)
5955 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005956 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5957 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005958 if (VT != MVT::f32)
5959 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5960 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005961 break;
5962 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005963 case ISD::ANY_EXTEND:
5964 // The low part is any extension of the input (which degenerates to a copy).
5965 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5966 // The high part is undefined.
5967 Hi = DAG.getNode(ISD::UNDEF, NVT);
5968 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005969 case ISD::SIGN_EXTEND: {
5970 // The low part is just a sign extension of the input (which degenerates to
5971 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005972 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005973
Chris Lattner3e928bb2005-01-07 07:47:09 +00005974 // The high part is obtained by SRA'ing all but one of the bits of the lo
5975 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005976 unsigned LoSize = Lo.getValueType().getSizeInBits();
Chris Lattner8137c9e2006-01-28 05:07:51 +00005977 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5978 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005979 break;
5980 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005981 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005982 // The low part is just a zero extension of the input (which degenerates to
5983 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005984 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005985
Chris Lattner3e928bb2005-01-07 07:47:09 +00005986 // The high part is just a zero.
5987 Hi = DAG.getConstant(0, NVT);
5988 break;
Chris Lattner35481892005-12-23 00:16:34 +00005989
Chris Lattner4c948eb2007-02-13 23:55:16 +00005990 case ISD::TRUNCATE: {
5991 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00005992 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00005993 ExpandOp(Node->getOperand(0), NewLo, Hi);
5994
5995 // The low part is now either the right size, or it is closer. If not the
5996 // right size, make an illegal truncate so we recursively expand it.
5997 if (NewLo.getValueType() != Node->getValueType(0))
5998 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5999 ExpandOp(NewLo, Lo, Hi);
6000 break;
6001 }
6002
Chris Lattner35481892005-12-23 00:16:34 +00006003 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006004 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006005 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6006 // If the target wants to, allow it to lower this itself.
6007 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6008 case Expand: assert(0 && "cannot expand FP!");
6009 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6010 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6011 }
6012 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6013 }
6014
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006015 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006016 if (VT == MVT::f32 || VT == MVT::f64) {
6017 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006018 if (getTypeAction(NVT) == Expand)
6019 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006020 break;
6021 }
6022
6023 // If source operand will be expanded to the same type as VT, i.e.
6024 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006025 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006026 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6027 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006028 break;
6029 }
6030
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006031 // Turn this into a load/store pair by default.
6032 if (Tmp.Val == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006033 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006034
Chris Lattner35481892005-12-23 00:16:34 +00006035 ExpandOp(Tmp, Lo, Hi);
6036 break;
6037 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006038
Chris Lattner27a6c732007-11-24 07:07:01 +00006039 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006040 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6041 TargetLowering::Custom &&
6042 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006043 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00006044 assert(Tmp.Val && "Node must be custom expanded!");
6045 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006046 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006047 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006048 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006049 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006050
Mon P Wang28873102008-06-25 08:15:39 +00006051 case ISD::ATOMIC_CMP_SWAP: {
Dan Gohman475871a2008-07-27 21:46:04 +00006052 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006053 assert(Tmp.Val && "Node must be custom expanded!");
6054 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006055 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006056 LegalizeOp(Tmp.getValue(1)));
6057 break;
6058 }
6059
6060
6061
Chris Lattner4e6c7462005-01-08 19:27:05 +00006062 // These operators cannot be expanded directly, emit them as calls to
6063 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006064 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006065 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006066 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006067 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6068 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006069 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6070 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006071 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006072
Chris Lattnerf20d1832005-07-30 01:40:57 +00006073 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6074
Chris Lattner80a3e942005-07-29 00:33:32 +00006075 // Now that the custom expander is done, expand the result, which is still
6076 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00006077 if (Op.Val) {
6078 ExpandOp(Op, Lo, Hi);
6079 break;
6080 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006081 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006082
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006083 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6084 VT);
6085 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6086 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006087 break;
Evan Cheng56966222007-01-12 02:11:51 +00006088 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006089
Evan Cheng56966222007-01-12 02:11:51 +00006090 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006091 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006092 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006093 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6094 case Expand: assert(0 && "cannot expand FP!");
6095 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6096 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6097 }
6098
6099 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6100
6101 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00006102 if (Op.Val) {
6103 ExpandOp(Op, Lo, Hi);
6104 break;
6105 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006106 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006107
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006108 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6109 VT);
6110 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6111 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006112 break;
Evan Cheng56966222007-01-12 02:11:51 +00006113 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006114
Evan Cheng05a2d562006-01-09 18:31:59 +00006115 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006116 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006117 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006118 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006119 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006120 Op = TLI.LowerOperation(Op, DAG);
6121 if (Op.Val) {
6122 // Now that the custom expander is done, expand the result, which is
6123 // still VT.
6124 ExpandOp(Op, Lo, Hi);
6125 break;
6126 }
6127 }
6128
Chris Lattner79980b02006-09-13 03:50:39 +00006129 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6130 // this X << 1 as X+X.
6131 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00006132 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner79980b02006-09-13 03:50:39 +00006133 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006134 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006135 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6136 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6137 LoOps[1] = LoOps[0];
6138 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6139
6140 HiOps[1] = HiOps[0];
6141 HiOps[2] = Lo.getValue(1);
6142 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6143 break;
6144 }
6145 }
6146
Chris Lattnere34b3962005-01-19 04:19:40 +00006147 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006148 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006149 break;
Chris Lattner47599822005-04-02 03:38:53 +00006150
6151 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006152 TargetLowering::LegalizeAction Action =
6153 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6154 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6155 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006156 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006157 break;
6158 }
6159
Chris Lattnere34b3962005-01-19 04:19:40 +00006160 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006161 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006162 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006163 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006164
Evan Cheng05a2d562006-01-09 18:31:59 +00006165 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006166 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006167 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006168 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006169 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006170 Op = TLI.LowerOperation(Op, DAG);
6171 if (Op.Val) {
6172 // Now that the custom expander is done, expand the result, which is
6173 // still VT.
6174 ExpandOp(Op, Lo, Hi);
6175 break;
6176 }
6177 }
6178
Chris Lattnere34b3962005-01-19 04:19:40 +00006179 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006180 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006181 break;
Chris Lattner47599822005-04-02 03:38:53 +00006182
6183 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006184 TargetLowering::LegalizeAction Action =
6185 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6186 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6187 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006188 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006189 break;
6190 }
6191
Chris Lattnere34b3962005-01-19 04:19:40 +00006192 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006193 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006194 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006195 }
6196
6197 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006198 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006199 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006200 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006201 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006202 Op = TLI.LowerOperation(Op, DAG);
6203 if (Op.Val) {
6204 // Now that the custom expander is done, expand the result, which is
6205 // still VT.
6206 ExpandOp(Op, Lo, Hi);
6207 break;
6208 }
6209 }
6210
Chris Lattnere34b3962005-01-19 04:19:40 +00006211 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006212 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006213 break;
Chris Lattner47599822005-04-02 03:38:53 +00006214
6215 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006216 TargetLowering::LegalizeAction Action =
6217 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6218 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6219 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006220 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006221 break;
6222 }
6223
Chris Lattnere34b3962005-01-19 04:19:40 +00006224 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006225 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006226 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006227 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006228
Misha Brukmanedf128a2005-04-21 22:36:52 +00006229 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006230 case ISD::SUB: {
6231 // If the target wants to custom expand this, let them.
6232 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6233 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006234 SDValue Result = TLI.LowerOperation(Op, DAG);
Duncan Sands69bfb152008-06-22 09:42:16 +00006235 if (Result.Val) {
6236 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006237 break;
6238 }
6239 }
6240
6241 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006242 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006243 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6244 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00006245 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006246 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006247 LoOps[0] = LHSL;
6248 LoOps[1] = RHSL;
6249 HiOps[0] = LHSH;
6250 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006251 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00006252 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006253 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006254 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006255 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00006256 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006257 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006258 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006259 }
Chris Lattner84f67882005-01-20 18:52:28 +00006260 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006261 }
Chris Lattnerb429f732007-05-17 18:15:41 +00006262
6263 case ISD::ADDC:
6264 case ISD::SUBC: {
6265 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006266 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006267 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6268 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6269 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006270 SDValue LoOps[2] = { LHSL, RHSL };
6271 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00006272
6273 if (Node->getOpcode() == ISD::ADDC) {
6274 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6275 HiOps[2] = Lo.getValue(1);
6276 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6277 } else {
6278 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6279 HiOps[2] = Lo.getValue(1);
6280 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6281 }
6282 // Remember that we legalized the flag.
6283 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6284 break;
6285 }
6286 case ISD::ADDE:
6287 case ISD::SUBE: {
6288 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006289 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006290 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6291 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6292 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006293 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6294 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00006295
6296 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6297 HiOps[2] = Lo.getValue(1);
6298 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6299
6300 // Remember that we legalized the flag.
6301 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6302 break;
6303 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006304 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006305 // If the target wants to custom expand this, let them.
6306 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006307 SDValue New = TLI.LowerOperation(Op, DAG);
Chris Lattner8829dc82006-09-16 05:08:34 +00006308 if (New.Val) {
6309 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006310 break;
6311 }
6312 }
6313
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006314 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6315 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006316 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6317 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6318 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00006319 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00006320 ExpandOp(Node->getOperand(0), LL, LH);
6321 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006322 unsigned OuterBitSize = Op.getValueSizeInBits();
6323 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00006324 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6325 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00006326 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6327 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6328 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00006329 // The inputs are both zero-extended.
6330 if (HasUMUL_LOHI) {
6331 // We can emit a umul_lohi.
6332 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Dan Gohman475871a2008-07-27 21:46:04 +00006333 Hi = SDValue(Lo.Val, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006334 break;
6335 }
6336 if (HasMULHU) {
6337 // We can emit a mulhu+mul.
6338 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6339 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6340 break;
6341 }
Dan Gohman525178c2007-10-08 18:33:35 +00006342 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006343 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00006344 // The input values are both sign-extended.
6345 if (HasSMUL_LOHI) {
6346 // We can emit a smul_lohi.
6347 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Dan Gohman475871a2008-07-27 21:46:04 +00006348 Hi = SDValue(Lo.Val, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006349 break;
6350 }
6351 if (HasMULHS) {
6352 // We can emit a mulhs+mul.
6353 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6354 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6355 break;
6356 }
6357 }
6358 if (HasUMUL_LOHI) {
6359 // Lo,Hi = umul LHS, RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00006360 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman525178c2007-10-08 18:33:35 +00006361 DAG.getVTList(NVT, NVT), LL, RL);
6362 Lo = UMulLOHI;
6363 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006364 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6365 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6366 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6367 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006368 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006369 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006370 if (HasMULHU) {
6371 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6372 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6373 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6374 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6375 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6376 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6377 break;
6378 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006379 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006380
Dan Gohman525178c2007-10-08 18:33:35 +00006381 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006382 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006383 break;
6384 }
Evan Cheng56966222007-01-12 02:11:51 +00006385 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006386 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006387 break;
6388 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006389 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006390 break;
6391 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00006392 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006393 break;
6394 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00006395 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006396 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006397
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006398 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00006399 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6400 RTLIB::ADD_F64,
6401 RTLIB::ADD_F80,
6402 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006403 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006404 break;
6405 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00006406 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
6407 RTLIB::SUB_F64,
6408 RTLIB::SUB_F80,
6409 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006410 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006411 break;
6412 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00006413 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
6414 RTLIB::MUL_F64,
6415 RTLIB::MUL_F80,
6416 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006417 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006418 break;
6419 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006420 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
6421 RTLIB::DIV_F64,
6422 RTLIB::DIV_F80,
6423 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006424 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006425 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006426 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006427 if (VT == MVT::ppcf128) {
6428 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6429 Node->getOperand(0).getValueType()==MVT::f64);
6430 const uint64_t zero = 0;
6431 if (Node->getOperand(0).getValueType()==MVT::f32)
6432 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6433 else
6434 Hi = Node->getOperand(0);
6435 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6436 break;
6437 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006438 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
6439 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
6440 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006441 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006442 }
6443 case ISD::FP_ROUND: {
6444 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
6445 VT);
6446 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
6447 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006448 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006449 }
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006450 case ISD::FPOWI:
Duncan Sands460a14e2008-04-12 17:14:18 +00006451 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::POWI_F32,
6452 RTLIB::POWI_F64,
6453 RTLIB::POWI_F80,
6454 RTLIB::POWI_PPCF128),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006455 Node, false, Hi);
6456 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006457 case ISD::FSQRT:
6458 case ISD::FSIN:
6459 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00006460 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006461 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006462 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00006463 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6464 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006465 break;
6466 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00006467 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6468 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006469 break;
6470 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00006471 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6472 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006473 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006474 default: assert(0 && "Unreachable!");
6475 }
Duncan Sands460a14e2008-04-12 17:14:18 +00006476 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006477 break;
6478 }
Evan Cheng966bf242006-12-16 00:52:40 +00006479 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006480 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00006481 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00006482 ExpandOp(Node->getOperand(0), Lo, Tmp);
6483 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6484 // lo = hi==fabs(hi) ? lo : -lo;
6485 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6486 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6487 DAG.getCondCode(ISD::SETEQ));
6488 break;
6489 }
Dan Gohman475871a2008-07-27 21:46:04 +00006490 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00006491 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6492 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6493 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6494 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6495 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6496 if (getTypeAction(NVT) == Expand)
6497 ExpandOp(Lo, Lo, Hi);
6498 break;
6499 }
6500 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006501 if (VT == MVT::ppcf128) {
6502 ExpandOp(Node->getOperand(0), Lo, Hi);
6503 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6504 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6505 break;
6506 }
Dan Gohman475871a2008-07-27 21:46:04 +00006507 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00006508 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6509 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6510 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6511 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6512 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6513 if (getTypeAction(NVT) == Expand)
6514 ExpandOp(Lo, Lo, Hi);
6515 break;
6516 }
Evan Cheng912095b2007-01-04 21:56:39 +00006517 case ISD::FCOPYSIGN: {
6518 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6519 if (getTypeAction(NVT) == Expand)
6520 ExpandOp(Lo, Lo, Hi);
6521 break;
6522 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006523 case ISD::SINT_TO_FP:
6524 case ISD::UINT_TO_FP: {
6525 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006526 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00006527
6528 // Promote the operand if needed. Do this before checking for
6529 // ppcf128 so conversions of i16 and i8 work.
6530 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00006531 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00006532 Tmp = isSigned
6533 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6534 DAG.getValueType(SrcVT))
6535 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6536 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6537 SrcVT = Node->getOperand(0).getValueType();
6538 }
6539
Dan Gohmana2e94852008-03-10 23:03:31 +00006540 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006541 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006542 if (isSigned) {
6543 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6544 Node->getOperand(0)));
6545 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6546 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006547 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006548 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6549 Node->getOperand(0)));
6550 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6551 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006552 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006553 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6554 DAG.getConstant(0, MVT::i32),
6555 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6556 DAG.getConstantFP(
6557 APFloat(APInt(128, 2, TwoE32)),
6558 MVT::ppcf128)),
6559 Hi,
6560 DAG.getCondCode(ISD::SETLT)),
6561 Lo, Hi);
6562 }
6563 break;
6564 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00006565 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6566 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006567 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00006568 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6569 Lo, Hi);
6570 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6571 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6572 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6573 DAG.getConstant(0, MVT::i64),
6574 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6575 DAG.getConstantFP(
6576 APFloat(APInt(128, 2, TwoE64)),
6577 MVT::ppcf128)),
6578 Hi,
6579 DAG.getCondCode(ISD::SETLT)),
6580 Lo, Hi);
6581 break;
6582 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006583
Dan Gohmana2e94852008-03-10 23:03:31 +00006584 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6585 Node->getOperand(0));
Evan Cheng110cf482008-04-01 01:50:16 +00006586 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00006587 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00006588 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00006589 break;
6590 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006591 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006592
Chris Lattner83397362005-12-21 18:02:52 +00006593 // Make sure the resultant values have been legalized themselves, unless this
6594 // is a type that requires multi-step expansion.
6595 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6596 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006597 if (Hi.Val)
6598 // Don't legalize the high part if it is expanded to a single node.
6599 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006600 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006601
6602 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00006603 bool isNew =
6604 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00006605 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006606}
6607
Dan Gohman7f321562007-06-25 16:23:39 +00006608/// SplitVectorOp - Given an operand of vector type, break it down into
6609/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00006610void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
6611 SDValue &Hi) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006612 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006613 SDNode *Node = Op.Val;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006614 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00006615 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00006616
Duncan Sands83ec4b62008-06-06 12:08:01 +00006617 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00006618
6619 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6620 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6621
Duncan Sands83ec4b62008-06-06 12:08:01 +00006622 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
6623 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006624
Chris Lattnerc7029802006-03-18 01:44:44 +00006625 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00006626 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00006627 = SplitNodes.find(Op);
6628 if (I != SplitNodes.end()) {
6629 Lo = I->second.first;
6630 Hi = I->second.second;
6631 return;
6632 }
6633
6634 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006635 default:
6636#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006637 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006638#endif
6639 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00006640 case ISD::UNDEF:
6641 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6642 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6643 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006644 case ISD::BUILD_PAIR:
6645 Lo = Node->getOperand(0);
6646 Hi = Node->getOperand(1);
6647 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006648 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00006649 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
6650 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6651 unsigned Index = Idx->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006652 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00006653 if (Index < NewNumElts_Lo)
6654 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
6655 DAG.getIntPtrConstant(Index));
6656 else
6657 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6658 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
6659 break;
6660 }
Dan Gohman475871a2008-07-27 21:46:04 +00006661 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman68679912008-04-25 18:07:40 +00006662 Node->getOperand(1),
6663 Node->getOperand(2));
6664 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00006665 break;
6666 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00006667 case ISD::VECTOR_SHUFFLE: {
6668 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00006669 SDValue Mask = Node->getOperand(2);
6670 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006671 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6c9c6802007-11-19 21:16:54 +00006672
6673 // Insert all of the elements from the input that are needed. We use
6674 // buildvector of extractelement here because the input vectors will have
6675 // to be legalized, so this makes the code simpler.
6676 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006677 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00006678 if (IdxNode.getOpcode() == ISD::UNDEF) {
6679 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6680 continue;
6681 }
6682 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006683 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00006684 if (Idx >= NumElements) {
6685 InVec = Node->getOperand(1);
6686 Idx -= NumElements;
6687 }
6688 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6689 DAG.getConstant(Idx, PtrVT)));
6690 }
6691 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6692 Ops.clear();
6693
6694 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006695 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00006696 if (IdxNode.getOpcode() == ISD::UNDEF) {
6697 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6698 continue;
6699 }
6700 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006701 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00006702 if (Idx >= NumElements) {
6703 InVec = Node->getOperand(1);
6704 Idx -= NumElements;
6705 }
6706 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6707 DAG.getConstant(Idx, PtrVT)));
6708 }
Mon P Wang92879f32008-07-25 01:30:26 +00006709 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00006710 break;
6711 }
Dan Gohman7f321562007-06-25 16:23:39 +00006712 case ISD::BUILD_VECTOR: {
Dan Gohman475871a2008-07-27 21:46:04 +00006713 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00006714 Node->op_begin()+NewNumElts_Lo);
6715 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006716
Dan Gohman475871a2008-07-27 21:46:04 +00006717 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00006718 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006719 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006720 break;
6721 }
Dan Gohman7f321562007-06-25 16:23:39 +00006722 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00006723 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00006724 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6725 if (NewNumSubvectors == 1) {
6726 Lo = Node->getOperand(0);
6727 Hi = Node->getOperand(1);
6728 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00006729 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Dan Gohman7f321562007-06-25 16:23:39 +00006730 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006731 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006732
Dan Gohman475871a2008-07-27 21:46:04 +00006733 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohman7f321562007-06-25 16:23:39 +00006734 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006735 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00006736 }
Dan Gohman65956352007-06-13 15:12:02 +00006737 break;
6738 }
Dan Gohmanc6230962007-10-17 14:48:28 +00006739 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006740 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00006741
Dan Gohman475871a2008-07-27 21:46:04 +00006742 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00006743 SplitVectorOp(Node->getOperand(1), LL, LH);
6744 SplitVectorOp(Node->getOperand(2), RL, RH);
6745
Duncan Sands83ec4b62008-06-06 12:08:01 +00006746 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00006747 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00006748 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00006749 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006750 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6751 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006752 } else {
6753 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00006754 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6755 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006756 }
6757 break;
6758 }
Chris Lattner80c1a562008-06-30 02:43:01 +00006759 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006760 SDValue CondLHS = Node->getOperand(0);
6761 SDValue CondRHS = Node->getOperand(1);
6762 SDValue CondCode = Node->getOperand(4);
Chris Lattner80c1a562008-06-30 02:43:01 +00006763
Dan Gohman475871a2008-07-27 21:46:04 +00006764 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00006765 SplitVectorOp(Node->getOperand(2), LL, LH);
6766 SplitVectorOp(Node->getOperand(3), RL, RH);
6767
6768 // Handle a simple select with vector operands.
6769 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
6770 LL, RL, CondCode);
6771 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
6772 LH, RH, CondCode);
6773 break;
6774 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00006775 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006776 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00006777 SplitVectorOp(Node->getOperand(0), LL, LH);
6778 SplitVectorOp(Node->getOperand(1), RL, RH);
6779 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
6780 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
6781 break;
6782 }
Dan Gohman7f321562007-06-25 16:23:39 +00006783 case ISD::ADD:
6784 case ISD::SUB:
6785 case ISD::MUL:
6786 case ISD::FADD:
6787 case ISD::FSUB:
6788 case ISD::FMUL:
6789 case ISD::SDIV:
6790 case ISD::UDIV:
6791 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006792 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006793 case ISD::AND:
6794 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00006795 case ISD::XOR:
6796 case ISD::UREM:
6797 case ISD::SREM:
6798 case ISD::FREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00006799 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00006800 SplitVectorOp(Node->getOperand(0), LL, LH);
6801 SplitVectorOp(Node->getOperand(1), RL, RH);
6802
Nate Begeman5db1afb2007-11-15 21:15:26 +00006803 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6804 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006805 break;
6806 }
Dan Gohman82669522007-10-11 23:57:53 +00006807 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00006808 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00006809 SplitVectorOp(Node->getOperand(0), L, H);
6810
Nate Begeman5db1afb2007-11-15 21:15:26 +00006811 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6812 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00006813 break;
6814 }
6815 case ISD::CTTZ:
6816 case ISD::CTLZ:
6817 case ISD::CTPOP:
6818 case ISD::FNEG:
6819 case ISD::FABS:
6820 case ISD::FSQRT:
6821 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00006822 case ISD::FCOS:
6823 case ISD::FP_TO_SINT:
6824 case ISD::FP_TO_UINT:
6825 case ISD::SINT_TO_FP:
6826 case ISD::UINT_TO_FP: {
Dan Gohman475871a2008-07-27 21:46:04 +00006827 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00006828 SplitVectorOp(Node->getOperand(0), L, H);
6829
Nate Begeman5db1afb2007-11-15 21:15:26 +00006830 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6831 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00006832 break;
6833 }
Dan Gohman7f321562007-06-25 16:23:39 +00006834 case ISD::LOAD: {
6835 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006836 SDValue Ch = LD->getChain();
6837 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f321562007-06-25 16:23:39 +00006838 const Value *SV = LD->getSrcValue();
6839 int SVOffset = LD->getSrcValueOffset();
6840 unsigned Alignment = LD->getAlignment();
6841 bool isVolatile = LD->isVolatile();
6842
Nate Begeman5db1afb2007-11-15 21:15:26 +00006843 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006844 unsigned IncrementSize = NewNumElts_Lo * NewEltVT.getSizeInBits()/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00006845 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006846 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006847 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006848 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006849 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00006850
6851 // Build a factor node to remember that this load is independent of the
6852 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00006853 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Chris Lattnerc7029802006-03-18 01:44:44 +00006854 Hi.getValue(1));
6855
6856 // Remember that we legalized the chain.
6857 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00006858 break;
6859 }
Dan Gohman7f321562007-06-25 16:23:39 +00006860 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00006861 // We know the result is a vector. The input may be either a vector or a
6862 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00006863 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006864 if (!InOp.getValueType().isVector() ||
6865 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00006866 // The input is a scalar or single-element vector.
6867 // Lower to a store/load so that it can be split.
6868 // FIXME: this could be improved probably.
Mon P Wang2920d2b2008-07-15 05:28:34 +00006869 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
6870 Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00006871 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Dan Gohmana54cf172008-07-11 22:44:52 +00006872 int FI = cast<FrameIndexSDNode>(Ptr.Val)->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00006873
Dan Gohman475871a2008-07-27 21:46:04 +00006874 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00006875 InOp, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00006876 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00006877 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00006878 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00006879 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00006880 // Split the vector and convert each of the pieces now.
6881 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006882 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6883 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00006884 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00006885 }
Chris Lattnerc7029802006-03-18 01:44:44 +00006886 }
6887
6888 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006889 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00006890 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00006891 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006892}
6893
6894
Dan Gohman89b20c02007-06-27 14:06:22 +00006895/// ScalarizeVectorOp - Given an operand of single-element vector type
6896/// (e.g. v1f32), convert it into the equivalent operation that returns a
6897/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00006898SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006899 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006900 SDNode *Node = Op.Val;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006901 MVT NewVT = Op.getValueType().getVectorElementType();
6902 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00006903
Dan Gohman7f321562007-06-25 16:23:39 +00006904 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00006905 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00006906 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00006907
Dan Gohman475871a2008-07-27 21:46:04 +00006908 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00006909 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00006910 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006911#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006912 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006913#endif
Dan Gohman7f321562007-06-25 16:23:39 +00006914 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6915 case ISD::ADD:
6916 case ISD::FADD:
6917 case ISD::SUB:
6918 case ISD::FSUB:
6919 case ISD::MUL:
6920 case ISD::FMUL:
6921 case ISD::SDIV:
6922 case ISD::UDIV:
6923 case ISD::FDIV:
6924 case ISD::SREM:
6925 case ISD::UREM:
6926 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00006927 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006928 case ISD::AND:
6929 case ISD::OR:
6930 case ISD::XOR:
6931 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00006932 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00006933 ScalarizeVectorOp(Node->getOperand(0)),
6934 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00006935 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006936 case ISD::FNEG:
6937 case ISD::FABS:
6938 case ISD::FSQRT:
6939 case ISD::FSIN:
6940 case ISD::FCOS:
6941 Result = DAG.getNode(Node->getOpcode(),
6942 NewVT,
6943 ScalarizeVectorOp(Node->getOperand(0)));
6944 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00006945 case ISD::FPOWI:
6946 Result = DAG.getNode(Node->getOpcode(),
6947 NewVT,
6948 ScalarizeVectorOp(Node->getOperand(0)),
6949 Node->getOperand(1));
6950 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006951 case ISD::LOAD: {
6952 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006953 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6954 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00006955
Dan Gohman7f321562007-06-25 16:23:39 +00006956 const Value *SV = LD->getSrcValue();
6957 int SVOffset = LD->getSrcValueOffset();
6958 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6959 LD->isVolatile(), LD->getAlignment());
6960
Chris Lattnerc7029802006-03-18 01:44:44 +00006961 // Remember that we legalized the chain.
6962 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6963 break;
6964 }
Dan Gohman7f321562007-06-25 16:23:39 +00006965 case ISD::BUILD_VECTOR:
6966 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00006967 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006968 case ISD::INSERT_VECTOR_ELT:
6969 // Returning the inserted scalar element.
6970 Result = Node->getOperand(1);
6971 break;
6972 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00006973 assert(Node->getOperand(0).getValueType() == NewVT &&
6974 "Concat of non-legal vectors not yet supported!");
6975 Result = Node->getOperand(0);
6976 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006977 case ISD::VECTOR_SHUFFLE: {
6978 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00006979 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00006980 if (cast<ConstantSDNode>(EltNum)->getValue())
6981 Result = ScalarizeVectorOp(Node->getOperand(1));
6982 else
6983 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00006984 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006985 }
6986 case ISD::EXTRACT_SUBVECTOR:
6987 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00006988 assert(Result.getValueType() == NewVT);
6989 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00006990 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006991 SDValue Op0 = Op.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006992 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00006993 Op0 = ScalarizeVectorOp(Op0);
6994 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00006995 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00006996 }
Dan Gohman7f321562007-06-25 16:23:39 +00006997 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00006998 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00006999 ScalarizeVectorOp(Op.getOperand(1)),
7000 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007001 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007002 case ISD::SELECT_CC:
7003 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7004 Node->getOperand(1),
7005 ScalarizeVectorOp(Op.getOperand(2)),
7006 ScalarizeVectorOp(Op.getOperand(3)),
7007 Node->getOperand(4));
7008 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007009 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007010 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7011 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begeman0d1704b2008-05-12 23:09:43 +00007012 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7013 Op.getOperand(2));
7014 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7015 DAG.getConstant(-1ULL, NewVT),
7016 DAG.getConstant(0ULL, NewVT));
7017 break;
7018 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007019 }
7020
7021 if (TLI.isTypeLegal(NewVT))
7022 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007023 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7024 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00007025 return Result;
7026}
7027
Chris Lattner3e928bb2005-01-07 07:47:09 +00007028
7029// SelectionDAG::Legalize - This is the entry point for the file.
7030//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00007031void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00007032 /// run - This is the main entry point to this class.
7033 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00007034 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00007035}
7036