blob: 2df363e5be1eaa064ef587579ef41faa7701754c [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
Chris Lattner5e46a192006-04-02 03:07:27 +000038#ifndef NDEBUG
39static cl::opt<bool>
40ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
41 cl::desc("Pop up a window to show dags before legalize"));
42#else
43static const bool ViewLegalizeDAGs = 0;
44#endif
45
Chris Lattner3e928bb2005-01-07 07:47:09 +000046//===----------------------------------------------------------------------===//
47/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
48/// hacks on it until the target machine can handle it. This involves
49/// eliminating value sizes the machine cannot handle (promoting small sizes to
50/// large sizes or splitting up large values into small values) as well as
51/// eliminating operations the machine cannot handle.
52///
53/// This code also does a small amount of optimization and recognition of idioms
54/// as part of its processing. For example, if a target does not support a
55/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
56/// will attempt merge setcc and brc instructions into brcc's.
57///
58namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000059class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000060 TargetLowering &TLI;
61 SelectionDAG &DAG;
62
Chris Lattner6831a812006-02-13 09:18:02 +000063 // Libcall insertion helpers.
64
65 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
66 /// legalized. We use this to ensure that calls are properly serialized
67 /// against each other, including inserted libcalls.
68 SDOperand LastCALLSEQ_END;
69
70 /// IsLegalizingCall - This member is used *only* for purposes of providing
71 /// helpful assertions that a libcall isn't created while another call is
72 /// being legalized (which could lead to non-serialized call sequences).
73 bool IsLegalizingCall;
74
Chris Lattner3e928bb2005-01-07 07:47:09 +000075 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000076 Legal, // The target natively supports this operation.
77 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000078 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000079 };
Chris Lattner6831a812006-02-13 09:18:02 +000080
Chris Lattner3e928bb2005-01-07 07:47:09 +000081 /// ValueTypeActions - This is a bitvector that contains two bits for each
82 /// value type, where the two bits correspond to the LegalizeAction enum.
83 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000084 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000085
Chris Lattner3e928bb2005-01-07 07:47:09 +000086 /// LegalizedNodes - For nodes that are of legal width, and that have more
87 /// than one use, this map indicates what regularized operand to use. This
88 /// allows us to avoid legalizing the same thing more than once.
Roman Levensteindc1adac2008-04-07 10:06:32 +000089 DenseMap<SDOperandImpl, SDOperand> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000090
Chris Lattner03c85462005-01-15 05:21:40 +000091 /// PromotedNodes - For nodes that are below legal width, and that have more
92 /// than one use, this map indicates what promoted value to use. This allows
93 /// us to avoid promoting the same thing more than once.
Roman Levensteindc1adac2008-04-07 10:06:32 +000094 DenseMap<SDOperandImpl, SDOperand> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000095
Chris Lattnerc7029802006-03-18 01:44:44 +000096 /// ExpandedNodes - For nodes that need to be expanded this map indicates
97 /// which which operands are the expanded version of the input. This allows
98 /// us to avoid expanding the same node more than once.
Roman Levensteindc1adac2008-04-07 10:06:32 +000099 DenseMap<SDOperandImpl, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000100
Chris Lattnerc7029802006-03-18 01:44:44 +0000101 /// SplitNodes - For vector nodes that need to be split, this map indicates
102 /// which which operands are the split version of the input. This allows us
103 /// to avoid splitting the same node more than once.
104 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
105
Dan Gohman7f321562007-06-25 16:23:39 +0000106 /// ScalarizedNodes - For nodes that need to be converted from vector types to
107 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000108 /// processed to the result.
Dan Gohman7f321562007-06-25 16:23:39 +0000109 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000110
Chris Lattner8afc48e2005-01-07 22:28:47 +0000111 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000112 LegalizedNodes.insert(std::make_pair(From, To));
113 // If someone requests legalization of the new node, return itself.
114 if (From != To)
115 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000116 }
Chris Lattner03c85462005-01-15 05:21:40 +0000117 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner40030bf2007-02-04 01:17:38 +0000118 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000119 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000120 // If someone requests legalization of the new node, return itself.
121 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000122 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000123
Chris Lattner3e928bb2005-01-07 07:47:09 +0000124public:
125
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000126 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000127
Chris Lattner3e928bb2005-01-07 07:47:09 +0000128 /// getTypeAction - Return how we should legalize values of this type, either
129 /// it is already legal or we need to expand it into multiple registers of
130 /// smaller integer type, or we need to promote it to a larger type.
131 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000132 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000133 }
134
135 /// isTypeLegal - Return true if this type is legal on this target.
136 ///
137 bool isTypeLegal(MVT::ValueType VT) const {
138 return getTypeAction(VT) == Legal;
139 }
140
Chris Lattner3e928bb2005-01-07 07:47:09 +0000141 void LegalizeDAG();
142
Chris Lattner456a93a2006-01-28 07:39:30 +0000143private:
Dan Gohman7f321562007-06-25 16:23:39 +0000144 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000145 /// appropriate for its type.
146 void HandleOp(SDOperand Op);
147
148 /// LegalizeOp - We know that the specified value has a legal type.
149 /// Recursively ensure that the operands have legal types, then return the
150 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000151 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000152
Dan Gohman82669522007-10-11 23:57:53 +0000153 /// UnrollVectorOp - We know that the given vector has a legal type, however
154 /// the operation it performs is not legal and is an operation that we have
155 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
156 /// operating on each element individually.
157 SDOperand UnrollVectorOp(SDOperand O);
158
Chris Lattnerc7029802006-03-18 01:44:44 +0000159 /// PromoteOp - Given an operation that produces a value in an invalid type,
160 /// promote it to compute the value into a larger type. The produced value
161 /// will have the correct bits for the low portion of the register, but no
162 /// guarantee is made about the top bits: it may be zero, sign-extended, or
163 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000164 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000165
Chris Lattnerc7029802006-03-18 01:44:44 +0000166 /// ExpandOp - Expand the specified SDOperand into its two component pieces
167 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
168 /// the LegalizeNodes map is filled in for any results that are not expanded,
169 /// the ExpandedNodes map is filled in for any results that are expanded, and
170 /// the Lo/Hi values are returned. This applies to integer types and Vector
171 /// types.
172 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
173
Dan Gohman7f321562007-06-25 16:23:39 +0000174 /// SplitVectorOp - Given an operand of vector type, break it down into
175 /// two smaller values.
Chris Lattnerc7029802006-03-18 01:44:44 +0000176 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
177
Dan Gohman89b20c02007-06-27 14:06:22 +0000178 /// ScalarizeVectorOp - Given an operand of single-element vector type
179 /// (e.g. v1f32), convert it into the equivalent operation that returns a
180 /// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +0000181 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000182
Chris Lattner4352cc92006-04-04 17:23:26 +0000183 /// isShuffleLegal - Return true if a vector shuffle is legal with the
184 /// specified mask and type. Targets can specify exactly which masks they
185 /// support and the code generator is tasked with not creating illegal masks.
186 ///
187 /// Note that this will also return true for shuffles that are promoted to a
188 /// different type.
189 ///
190 /// If this is a legal shuffle, this method returns the (possibly promoted)
191 /// build_vector Mask. If it's not a legal shuffle, it returns null.
192 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
193
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000194 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000195 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000196
Nate Begeman750ac1b2006-02-01 07:19:44 +0000197 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
198
Reid Spencer47857812006-12-31 05:55:36 +0000199 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000200 SDOperand &Hi);
201 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
202 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000203
Chris Lattner1401d152008-01-16 07:45:30 +0000204 SDOperand EmitStackConvert(SDOperand SrcOp, MVT::ValueType SlotVT,
205 MVT::ValueType DestVT);
Chris Lattnerce872152006-03-19 06:31:19 +0000206 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000207 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000208 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
209 SDOperand LegalOp,
210 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000211 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
212 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000213 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
214 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000215
Chris Lattner456a93a2006-01-28 07:39:30 +0000216 SDOperand ExpandBSWAP(SDOperand Op);
217 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000218 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
219 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000220 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
221 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000222
Dan Gohman7f321562007-06-25 16:23:39 +0000223 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000224 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000225};
226}
227
Chris Lattner4352cc92006-04-04 17:23:26 +0000228/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
229/// specified mask and type. Targets can specify exactly which masks they
230/// support and the code generator is tasked with not creating illegal masks.
231///
232/// Note that this will also return true for shuffles that are promoted to a
233/// different type.
234SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
235 SDOperand Mask) const {
236 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
237 default: return 0;
238 case TargetLowering::Legal:
239 case TargetLowering::Custom:
240 break;
241 case TargetLowering::Promote: {
242 // If this is promoted to a different type, convert the shuffle mask and
243 // ask if it is legal in the promoted type!
244 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
245
246 // If we changed # elements, change the shuffle mask.
247 unsigned NumEltsGrowth =
248 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
249 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
250 if (NumEltsGrowth > 1) {
251 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000252 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000253 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
254 SDOperand InOp = Mask.getOperand(i);
255 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
256 if (InOp.getOpcode() == ISD::UNDEF)
257 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
258 else {
259 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
260 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
261 }
262 }
263 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000264 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000265 }
266 VT = NVT;
267 break;
268 }
269 }
270 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
271}
272
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000273SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
274 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
275 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000276 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000277 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000278}
279
Chris Lattnere10e6f72007-06-18 21:28:10 +0000280/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
281/// contains all of a nodes operands before it contains the node.
282static void ComputeTopDownOrdering(SelectionDAG &DAG,
283 SmallVector<SDNode*, 64> &Order) {
284
285 DenseMap<SDNode*, unsigned> Visited;
286 std::vector<SDNode*> Worklist;
287 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000288
Chris Lattnere10e6f72007-06-18 21:28:10 +0000289 // Compute ordering from all of the leaves in the graphs, those (like the
290 // entry node) that have no operands.
291 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
292 E = DAG.allnodes_end(); I != E; ++I) {
293 if (I->getNumOperands() == 0) {
294 Visited[I] = 0 - 1U;
295 Worklist.push_back(I);
296 }
Chris Lattner32fca002005-10-06 01:20:27 +0000297 }
298
Chris Lattnere10e6f72007-06-18 21:28:10 +0000299 while (!Worklist.empty()) {
300 SDNode *N = Worklist.back();
301 Worklist.pop_back();
302
303 if (++Visited[N] != N->getNumOperands())
304 continue; // Haven't visited all operands yet
305
306 Order.push_back(N);
307
308 // Now that we have N in, add anything that uses it if all of their operands
309 // are now done.
310 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
311 UI != E; ++UI)
Roman Levensteindc1adac2008-04-07 10:06:32 +0000312 Worklist.push_back(UI->getUser());
Chris Lattnere10e6f72007-06-18 21:28:10 +0000313 }
314
315 assert(Order.size() == Visited.size() &&
316 Order.size() ==
317 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
318 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000319}
320
Chris Lattner1618beb2005-07-29 00:11:56 +0000321
Chris Lattner3e928bb2005-01-07 07:47:09 +0000322void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000323 LastCALLSEQ_END = DAG.getEntryNode();
324 IsLegalizingCall = false;
325
Chris Lattnerab510a72005-10-02 17:49:46 +0000326 // The legalize process is inherently a bottom-up recursive process (users
327 // legalize their uses before themselves). Given infinite stack space, we
328 // could just start legalizing on the root and traverse the whole graph. In
329 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000330 // blocks. To avoid this problem, compute an ordering of the nodes where each
331 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000332 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000333 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000334
Chris Lattnerc7029802006-03-18 01:44:44 +0000335 for (unsigned i = 0, e = Order.size(); i != e; ++i)
336 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000337
338 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000339 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000340 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
341 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000342
343 ExpandedNodes.clear();
344 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000345 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000346 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000347 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000348
349 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000350 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000351}
352
Chris Lattner6831a812006-02-13 09:18:02 +0000353
354/// FindCallEndFromCallStart - Given a chained node that is part of a call
355/// sequence, find the CALLSEQ_END node that terminates the call sequence.
356static SDNode *FindCallEndFromCallStart(SDNode *Node) {
357 if (Node->getOpcode() == ISD::CALLSEQ_END)
358 return Node;
359 if (Node->use_empty())
360 return 0; // No CallSeqEnd
361
362 // The chain is usually at the end.
363 SDOperand TheChain(Node, Node->getNumValues()-1);
364 if (TheChain.getValueType() != MVT::Other) {
365 // Sometimes it's at the beginning.
366 TheChain = SDOperand(Node, 0);
367 if (TheChain.getValueType() != MVT::Other) {
368 // Otherwise, hunt for it.
369 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
370 if (Node->getValueType(i) == MVT::Other) {
371 TheChain = SDOperand(Node, i);
372 break;
373 }
374
375 // Otherwise, we walked into a node without a chain.
376 if (TheChain.getValueType() != MVT::Other)
377 return 0;
378 }
379 }
380
381 for (SDNode::use_iterator UI = Node->use_begin(),
382 E = Node->use_end(); UI != E; ++UI) {
383
384 // Make sure to only follow users of our token chain.
Roman Levensteindc1adac2008-04-07 10:06:32 +0000385 SDNode *User = UI->getUser();
Chris Lattner6831a812006-02-13 09:18:02 +0000386 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
387 if (User->getOperand(i) == TheChain)
388 if (SDNode *Result = FindCallEndFromCallStart(User))
389 return Result;
390 }
391 return 0;
392}
393
394/// FindCallStartFromCallEnd - Given a chained node that is part of a call
395/// sequence, find the CALLSEQ_START node that initiates the call sequence.
396static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
397 assert(Node && "Didn't find callseq_start for a call??");
398 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
399
400 assert(Node->getOperand(0).getValueType() == MVT::Other &&
401 "Node doesn't have a token chain argument!");
402 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
403}
404
405/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
406/// see if any uses can reach Dest. If no dest operands can get to dest,
407/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000408///
409/// Keep track of the nodes we fine that actually do lead to Dest in
410/// NodesLeadingTo. This avoids retraversing them exponential number of times.
411///
412bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000413 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000414 if (N == Dest) return true; // N certainly leads to Dest :)
415
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000416 // If we've already processed this node and it does lead to Dest, there is no
417 // need to reprocess it.
418 if (NodesLeadingTo.count(N)) return true;
419
Chris Lattner6831a812006-02-13 09:18:02 +0000420 // If the first result of this node has been already legalized, then it cannot
421 // reach N.
422 switch (getTypeAction(N->getValueType(0))) {
423 case Legal:
424 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
425 break;
426 case Promote:
427 if (PromotedNodes.count(SDOperand(N, 0))) return false;
428 break;
429 case Expand:
430 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
431 break;
432 }
433
434 // Okay, this node has not already been legalized. Check and legalize all
435 // operands. If none lead to Dest, then we can legalize this node.
436 bool OperandsLeadToDest = false;
437 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
438 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000439 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000440
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000441 if (OperandsLeadToDest) {
442 NodesLeadingTo.insert(N);
443 return true;
444 }
Chris Lattner6831a812006-02-13 09:18:02 +0000445
446 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000447 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000448 return false;
449}
450
Dan Gohman7f321562007-06-25 16:23:39 +0000451/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000452/// appropriate for its type.
453void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000454 MVT::ValueType VT = Op.getValueType();
455 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000456 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000457 case Legal: (void)LegalizeOp(Op); break;
458 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000459 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000460 if (!MVT::isVector(VT)) {
461 // If this is an illegal scalar, expand it into its two component
462 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000463 SDOperand X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000464 if (Op.getOpcode() == ISD::TargetConstant)
465 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000466 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000467 } else if (MVT::getVectorNumElements(VT) == 1) {
468 // If this is an illegal single element vector, convert it to a
469 // scalar operation.
470 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000471 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000472 // Otherwise, this is an illegal multiple element vector.
473 // Split it in half and legalize both parts.
474 SDOperand X, Y;
475 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000476 }
477 break;
478 }
479}
Chris Lattner6831a812006-02-13 09:18:02 +0000480
Evan Cheng9f877882006-12-13 20:57:08 +0000481/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
482/// a load from the constant pool.
483static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000484 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000485 bool Extend = false;
486
487 // If a FP immediate is precise when represented as a float and if the
488 // target can do an extending load from float to double, we put it into
489 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000490 // double. This shrinks FP constants and canonicalizes them for targets where
491 // an FP extending load is the same cost as a normal load (such as on the x87
492 // fp stack or PPC FP unit).
Evan Cheng00495212006-12-12 21:32:44 +0000493 MVT::ValueType VT = CFP->getValueType(0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +0000494 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000495 CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000496 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000497 if (VT!=MVT::f64 && VT!=MVT::f32)
498 assert(0 && "Invalid type expansion");
Dan Gohman6cf9b8a2008-03-11 00:11:06 +0000499 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000500 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000501 }
502
Evan Chengef120572008-03-04 08:05:30 +0000503 MVT::ValueType OrigVT = VT;
504 MVT::ValueType SVT = VT;
505 while (SVT != MVT::f32) {
506 SVT = (unsigned)SVT - 1;
507 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
508 // Only do this if the target has a native EXTLOAD instruction from
509 // smaller type.
Evan Cheng6fd599f2008-03-05 01:30:59 +0000510 TLI.isLoadXLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000511 TLI.ShouldShrinkFPConstant(OrigVT)) {
Evan Chengef120572008-03-04 08:05:30 +0000512 const Type *SType = MVT::getTypeForValueType(SVT);
513 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
514 VT = SVT;
515 Extend = true;
516 }
Evan Cheng00495212006-12-12 21:32:44 +0000517 }
518
519 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Chengef120572008-03-04 08:05:30 +0000520 if (Extend)
521 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000522 CPIdx, PseudoSourceValue::getConstantPool(),
Evan Chengef120572008-03-04 08:05:30 +0000523 0, VT);
524 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
525 PseudoSourceValue::getConstantPool(), 0);
Evan Cheng00495212006-12-12 21:32:44 +0000526}
527
Chris Lattner6831a812006-02-13 09:18:02 +0000528
Evan Cheng912095b2007-01-04 21:56:39 +0000529/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
530/// operations.
531static
532SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
533 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000534 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000535 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000536 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
537 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000538 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000539
Evan Cheng912095b2007-01-04 21:56:39 +0000540 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000541 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000542 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
543 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000544 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000545 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000546 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000547 // Shift right or sign-extend it if the two operands have different types.
548 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
549 if (SizeDiff > 0) {
550 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
551 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
552 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
553 } else if (SizeDiff < 0)
554 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000555
556 // Clear the sign bit of first operand.
557 SDOperand Mask2 = (VT == MVT::f64)
558 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
559 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
560 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000561 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000562 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
563
564 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000565 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
566 return Result;
567}
568
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000569/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
570static
571SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
572 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000573 SDOperand Chain = ST->getChain();
574 SDOperand Ptr = ST->getBasePtr();
575 SDOperand Val = ST->getValue();
576 MVT::ValueType VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000577 int Alignment = ST->getAlignment();
578 int SVOffset = ST->getSrcValueOffset();
Dale Johannesen8155d642008-02-27 22:36:00 +0000579 if (MVT::isFloatingPoint(ST->getMemoryVT()) ||
580 MVT::isVector(ST->getMemoryVT())) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000581 // Expand to a bitconvert of the value to the integer type of the
582 // same size, then a (misaligned) int store.
583 MVT::ValueType intVT;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000584 if (MVT::is128BitVector(VT) || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000585 intVT = MVT::i128;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000586 else if (MVT::is64BitVector(VT) || VT==MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000587 intVT = MVT::i64;
588 else if (VT==MVT::f32)
589 intVT = MVT::i32;
590 else
Dale Johannesencd9f1742008-02-28 18:36:51 +0000591 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000592
593 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
594 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
595 SVOffset, ST->isVolatile(), Alignment);
596 }
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000597 assert(MVT::isInteger(ST->getMemoryVT()) &&
Dale Johannesen8155d642008-02-27 22:36:00 +0000598 !MVT::isVector(ST->getMemoryVT()) &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000599 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000600 // Get the half-size VT
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000601 MVT::ValueType NewStoredVT = ST->getMemoryVT() - 1;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000602 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000603 int IncrementSize = NumBits / 8;
604
605 // Divide the stored value in two parts.
606 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
607 SDOperand Lo = Val;
608 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
609
610 // Store the two parts
611 SDOperand Store1, Store2;
612 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
613 ST->getSrcValue(), SVOffset, NewStoredVT,
614 ST->isVolatile(), Alignment);
615 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
616 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000617 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000618 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
619 ST->getSrcValue(), SVOffset + IncrementSize,
620 NewStoredVT, ST->isVolatile(), Alignment);
621
622 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
623}
624
625/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
626static
627SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
628 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000629 int SVOffset = LD->getSrcValueOffset();
630 SDOperand Chain = LD->getChain();
631 SDOperand Ptr = LD->getBasePtr();
632 MVT::ValueType VT = LD->getValueType(0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000633 MVT::ValueType LoadedVT = LD->getMemoryVT();
Dale Johannesen8155d642008-02-27 22:36:00 +0000634 if (MVT::isFloatingPoint(VT) || MVT::isVector(VT)) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000635 // Expand to a (misaligned) integer load of the same size,
Dale Johannesen8155d642008-02-27 22:36:00 +0000636 // then bitconvert to floating point or vector.
Dale Johannesen907f28c2007-09-08 19:29:23 +0000637 MVT::ValueType intVT;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000638 if (MVT::is128BitVector(LoadedVT) ||
639 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000640 intVT = MVT::i128;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000641 else if (MVT::is64BitVector(LoadedVT) || LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000642 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000643 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000644 intVT = MVT::i32;
645 else
Dale Johannesen8155d642008-02-27 22:36:00 +0000646 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000647
648 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
649 SVOffset, LD->isVolatile(),
650 LD->getAlignment());
651 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Dale Johannesen8155d642008-02-27 22:36:00 +0000652 if (MVT::isFloatingPoint(VT) && LoadedVT != VT)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000653 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
654
655 SDOperand Ops[] = { Result, Chain };
656 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
657 Ops, 2);
658 }
Dale Johannesen8155d642008-02-27 22:36:00 +0000659 assert(MVT::isInteger(LoadedVT) && !MVT::isVector(LoadedVT) &&
Chris Lattnere400af82007-11-19 21:38:03 +0000660 "Unaligned load of unsupported type.");
661
Dale Johannesen8155d642008-02-27 22:36:00 +0000662 // Compute the new VT that is half the size of the old one. This is an
663 // integer MVT.
Chris Lattnere400af82007-11-19 21:38:03 +0000664 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
665 MVT::ValueType NewLoadedVT;
Dale Johannesen8155d642008-02-27 22:36:00 +0000666 NewLoadedVT = MVT::getIntegerType(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000667 NumBits >>= 1;
668
669 unsigned Alignment = LD->getAlignment();
670 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000671 ISD::LoadExtType HiExtType = LD->getExtensionType();
672
673 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
674 if (HiExtType == ISD::NON_EXTLOAD)
675 HiExtType = ISD::ZEXTLOAD;
676
677 // Load the value in two parts
678 SDOperand Lo, Hi;
679 if (TLI.isLittleEndian()) {
680 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
681 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
682 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
683 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
684 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
685 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000686 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000687 } else {
688 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
689 NewLoadedVT,LD->isVolatile(), Alignment);
690 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
691 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
692 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
693 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000694 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000695 }
696
697 // aggregate the two parts
698 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
699 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
700 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
701
702 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
703 Hi.getValue(1));
704
705 SDOperand Ops[] = { Result, TF };
706 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
707}
Evan Cheng912095b2007-01-04 21:56:39 +0000708
Dan Gohman82669522007-10-11 23:57:53 +0000709/// UnrollVectorOp - We know that the given vector has a legal type, however
710/// the operation it performs is not legal and is an operation that we have
711/// no way of lowering. "Unroll" the vector, splitting out the scalars and
712/// operating on each element individually.
713SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
714 MVT::ValueType VT = Op.getValueType();
715 assert(isTypeLegal(VT) &&
716 "Caller should expand or promote operands that are not legal!");
717 assert(Op.Val->getNumValues() == 1 &&
718 "Can't unroll a vector with multiple results!");
719 unsigned NE = MVT::getVectorNumElements(VT);
720 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
721
722 SmallVector<SDOperand, 8> Scalars;
723 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
724 for (unsigned i = 0; i != NE; ++i) {
725 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
726 SDOperand Operand = Op.getOperand(j);
727 MVT::ValueType OperandVT = Operand.getValueType();
728 if (MVT::isVector(OperandVT)) {
729 // A vector operand; extract a single element.
730 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
731 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
732 OperandEltVT,
733 Operand,
734 DAG.getConstant(i, MVT::i32));
735 } else {
736 // A scalar operand; just use it as is.
737 Operands[j] = Operand;
738 }
739 }
740 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
741 &Operands[0], Operands.size()));
742 }
743
744 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
745}
746
Duncan Sands007f9842008-01-10 10:28:30 +0000747/// GetFPLibCall - Return the right libcall for the given floating point type.
748static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
749 RTLIB::Libcall Call_F32,
750 RTLIB::Libcall Call_F64,
751 RTLIB::Libcall Call_F80,
752 RTLIB::Libcall Call_PPCF128) {
753 return
754 VT == MVT::f32 ? Call_F32 :
755 VT == MVT::f64 ? Call_F64 :
756 VT == MVT::f80 ? Call_F80 :
757 VT == MVT::ppcf128 ? Call_PPCF128 :
758 RTLIB::UNKNOWN_LIBCALL;
759}
760
Dan Gohmana3466152007-07-13 20:14:11 +0000761/// LegalizeOp - We know that the specified value has a legal type, and
762/// that its operands are legal. Now ensure that the operation itself
763/// is legal, recursively ensuring that the operands' operations remain
764/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000765SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000766 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
767 return Op;
768
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000769 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000770 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000771 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000772
Chris Lattner3e928bb2005-01-07 07:47:09 +0000773 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000774 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000775 if (Node->getNumValues() > 1) {
776 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000777 if (getTypeAction(Node->getValueType(i)) != Legal) {
778 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000779 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000780 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000781 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000782 }
783 }
784
Chris Lattner45982da2005-05-12 16:53:42 +0000785 // Note that LegalizeOp may be reentered even from single-use nodes, which
786 // means that we always must cache transformed nodes.
Roman Levensteindc1adac2008-04-07 10:06:32 +0000787 DenseMap<SDOperandImpl, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000788 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000789
Nate Begeman9373a812005-08-10 20:51:12 +0000790 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000791 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000792 bool isCustom = false;
793
Chris Lattner3e928bb2005-01-07 07:47:09 +0000794 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000795 case ISD::FrameIndex:
796 case ISD::EntryToken:
797 case ISD::Register:
798 case ISD::BasicBlock:
799 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000800 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000801 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000802 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000803 case ISD::TargetConstantPool:
804 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000805 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000806 case ISD::TargetExternalSymbol:
807 case ISD::VALUETYPE:
808 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +0000809 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +0000810 case ISD::STRING:
811 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +0000812 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +0000813 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000814 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000815 "This must be legal!");
816 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000817 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000818 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
819 // If this is a target node, legalize it by legalizing the operands then
820 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000821 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000822 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000823 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000824
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000825 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000826
827 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
828 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
829 return Result.getValue(Op.ResNo);
830 }
831 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000832#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000833 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000834#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000835 assert(0 && "Do not know how to legalize this operator!");
836 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000837 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000838 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000839 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000840 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000841 case ISD::ConstantPool:
842 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000843 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
844 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000845 case TargetLowering::Custom:
846 Tmp1 = TLI.LowerOperation(Op, DAG);
847 if (Tmp1.Val) Result = Tmp1;
848 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000849 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000850 break;
851 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000852 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000853 case ISD::FRAMEADDR:
854 case ISD::RETURNADDR:
855 // The only option for these nodes is to custom lower them. If the target
856 // does not custom lower them, then return zero.
857 Tmp1 = TLI.LowerOperation(Op, DAG);
858 if (Tmp1.Val)
859 Result = Tmp1;
860 else
861 Result = DAG.getConstant(0, TLI.getPointerTy());
862 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000863 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000864 MVT::ValueType VT = Node->getValueType(0);
865 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
866 default: assert(0 && "This action is not supported yet!");
867 case TargetLowering::Custom:
868 Result = TLI.LowerOperation(Op, DAG);
869 if (Result.Val) break;
870 // Fall Thru
871 case TargetLowering::Legal:
872 Result = DAG.getConstant(0, VT);
873 break;
874 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000875 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000876 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000877 case ISD::EXCEPTIONADDR: {
878 Tmp1 = LegalizeOp(Node->getOperand(0));
879 MVT::ValueType VT = Node->getValueType(0);
880 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
881 default: assert(0 && "This action is not supported yet!");
882 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000883 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000884 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000885 }
886 break;
887 case TargetLowering::Custom:
888 Result = TLI.LowerOperation(Op, DAG);
889 if (Result.Val) break;
890 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000891 case TargetLowering::Legal: {
892 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
893 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000894 Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000895 break;
896 }
897 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000898 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000899 if (Result.Val->getNumValues() == 1) break;
900
901 assert(Result.Val->getNumValues() == 2 &&
902 "Cannot return more than two values!");
903
904 // Since we produced two values, make sure to remember that we
905 // legalized both of them.
906 Tmp1 = LegalizeOp(Result);
907 Tmp2 = LegalizeOp(Result.getValue(1));
908 AddLegalizedOperand(Op.getValue(0), Tmp1);
909 AddLegalizedOperand(Op.getValue(1), Tmp2);
910 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000911 case ISD::EHSELECTION: {
912 Tmp1 = LegalizeOp(Node->getOperand(0));
913 Tmp2 = LegalizeOp(Node->getOperand(1));
914 MVT::ValueType VT = Node->getValueType(0);
915 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
916 default: assert(0 && "This action is not supported yet!");
917 case TargetLowering::Expand: {
918 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000919 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000920 }
921 break;
922 case TargetLowering::Custom:
923 Result = TLI.LowerOperation(Op, DAG);
924 if (Result.Val) break;
925 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000926 case TargetLowering::Legal: {
927 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
928 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000929 Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +0000930 break;
931 }
932 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000933 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000934 if (Result.Val->getNumValues() == 1) break;
935
936 assert(Result.Val->getNumValues() == 2 &&
937 "Cannot return more than two values!");
938
939 // Since we produced two values, make sure to remember that we
940 // legalized both of them.
941 Tmp1 = LegalizeOp(Result);
942 Tmp2 = LegalizeOp(Result.getValue(1));
943 AddLegalizedOperand(Op.getValue(0), Tmp1);
944 AddLegalizedOperand(Op.getValue(1), Tmp2);
945 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000946 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000947 MVT::ValueType VT = Node->getValueType(0);
948 // The only "good" option for this node is to custom lower it.
949 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
950 default: assert(0 && "This action is not supported at all!");
951 case TargetLowering::Custom:
952 Result = TLI.LowerOperation(Op, DAG);
953 if (Result.Val) break;
954 // Fall Thru
955 case TargetLowering::Legal:
956 // Target does not know, how to lower this, lower to noop
957 Result = LegalizeOp(Node->getOperand(0));
958 break;
959 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000960 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000961 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000962 case ISD::AssertSext:
963 case ISD::AssertZext:
964 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000965 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000966 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000967 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000968 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000969 Result = Node->getOperand(Op.ResNo);
970 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000971 case ISD::CopyFromReg:
972 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000973 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000974 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000975 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000976 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000977 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000978 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000979 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000980 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
981 } else {
982 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
983 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000984 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
985 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000986 // Since CopyFromReg produces two values, make sure to remember that we
987 // legalized both of them.
988 AddLegalizedOperand(Op.getValue(0), Result);
989 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
990 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000991 case ISD::UNDEF: {
992 MVT::ValueType VT = Op.getValueType();
993 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000994 default: assert(0 && "This action is not supported yet!");
995 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000996 if (MVT::isInteger(VT))
997 Result = DAG.getConstant(0, VT);
998 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000999 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
1000 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001001 else
1002 assert(0 && "Unknown value type!");
1003 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001004 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001005 break;
1006 }
1007 break;
1008 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001009
Chris Lattner48b61a72006-03-28 00:40:33 +00001010 case ISD::INTRINSIC_W_CHAIN:
1011 case ISD::INTRINSIC_WO_CHAIN:
1012 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001013 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001014 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1015 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001016 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001017
1018 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001019 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001020 TargetLowering::Custom) {
1021 Tmp3 = TLI.LowerOperation(Result, DAG);
1022 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001023 }
1024
1025 if (Result.Val->getNumValues() == 1) break;
1026
1027 // Must have return value and chain result.
1028 assert(Result.Val->getNumValues() == 2 &&
1029 "Cannot return more than two values!");
1030
1031 // Since loads produce two values, make sure to remember that we
1032 // legalized both of them.
1033 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1034 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1035 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001036 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001037
1038 case ISD::LOCATION:
1039 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1040 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1041
1042 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1043 case TargetLowering::Promote:
1044 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001045 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001046 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001047 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +00001048 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001049
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001050 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001051 const std::string &FName =
1052 cast<StringSDNode>(Node->getOperand(3))->getValue();
1053 const std::string &DirName =
1054 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001055 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001056
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001057 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +00001058 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +00001059 SDOperand LineOp = Node->getOperand(1);
1060 SDOperand ColOp = Node->getOperand(2);
1061
1062 if (useDEBUG_LOC) {
1063 Ops.push_back(LineOp); // line #
1064 Ops.push_back(ColOp); // col #
1065 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001066 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001067 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +00001068 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1069 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Evan Chenga647c922008-02-01 02:05:57 +00001070 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001071 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Evan Chengbb81d972008-01-31 09:59:15 +00001072 Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label
1073 Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001074 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001075 } else {
1076 Result = Tmp1; // chain
1077 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001078 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001079 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001080 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +00001081 if (Tmp1 != Node->getOperand(0) ||
1082 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001083 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001084 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +00001085 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1086 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1087 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1088 } else {
1089 // Otherwise promote them.
1090 Ops.push_back(PromoteOp(Node->getOperand(1)));
1091 Ops.push_back(PromoteOp(Node->getOperand(2)));
1092 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001093 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1094 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001095 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001096 }
1097 break;
1098 }
1099 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001100
1101 case ISD::DECLARE:
1102 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1103 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1104 default: assert(0 && "This action is not supported yet!");
1105 case TargetLowering::Legal:
1106 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1107 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1108 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1109 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1110 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001111 case TargetLowering::Expand:
1112 Result = LegalizeOp(Node->getOperand(0));
1113 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001114 }
1115 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001116
1117 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001118 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001119 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001120 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001121 case TargetLowering::Legal:
1122 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1123 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1124 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1125 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001127 break;
1128 }
1129 break;
1130
Jim Laskey1ee29252007-01-26 14:34:52 +00001131 case ISD::LABEL:
Evan Chengbb81d972008-01-31 09:59:15 +00001132 assert(Node->getNumOperands() == 3 && "Invalid LABEL node!");
Jim Laskey1ee29252007-01-26 14:34:52 +00001133 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001134 default: assert(0 && "This action is not supported yet!");
1135 case TargetLowering::Legal:
1136 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1137 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Evan Chengbb81d972008-01-31 09:59:15 +00001138 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand.
1139 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001140 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001141 case TargetLowering::Expand:
1142 Result = LegalizeOp(Node->getOperand(0));
1143 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001144 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001145 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001146
Evan Cheng27b7db52008-03-08 00:58:38 +00001147 case ISD::PREFETCH:
1148 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1149 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1150 default: assert(0 && "This action is not supported yet!");
1151 case TargetLowering::Legal:
1152 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1153 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1154 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1155 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1156 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1157 break;
1158 case TargetLowering::Expand:
1159 // It's a noop.
1160 Result = LegalizeOp(Node->getOperand(0));
1161 break;
1162 }
1163 break;
1164
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001165 case ISD::MEMBARRIER: {
1166 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001167 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1168 default: assert(0 && "This action is not supported yet!");
1169 case TargetLowering::Legal: {
1170 SDOperand Ops[6];
1171 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001172 for (int x = 1; x < 6; ++x) {
1173 Ops[x] = Node->getOperand(x);
1174 if (!isTypeLegal(Ops[x].getValueType()))
1175 Ops[x] = PromoteOp(Ops[x]);
1176 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001177 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1178 break;
1179 }
1180 case TargetLowering::Expand:
1181 //There is no libgcc call for this op
1182 Result = Node->getOperand(0); // Noop
1183 break;
1184 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001185 break;
1186 }
1187
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001188 case ISD::ATOMIC_LCS:
1189 case ISD::ATOMIC_LAS:
1190 case ISD::ATOMIC_SWAP: {
1191 assert(((Node->getNumOperands() == 4 && Node->getOpcode() == ISD::ATOMIC_LCS) ||
1192 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_LAS) ||
1193 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_SWAP)) &&
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001194 "Invalid Atomic node!");
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001195 int num = Node->getOpcode() == ISD::ATOMIC_LCS ? 4 : 3;
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001196 SDOperand Ops[4];
1197 for (int x = 0; x < num; ++x)
1198 Ops[x] = LegalizeOp(Node->getOperand(x));
1199 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num);
1200
1201 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001202 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001203 case TargetLowering::Custom:
1204 Result = TLI.LowerOperation(Result, DAG);
1205 break;
1206 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001207 break;
1208 }
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001209 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1210 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1211 return Result.getValue(Op.ResNo);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001212 }
1213
Scott Michelc1513d22007-08-08 23:23:31 +00001214 case ISD::Constant: {
1215 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1216 unsigned opAction =
1217 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1218
Chris Lattner3e928bb2005-01-07 07:47:09 +00001219 // We know we don't need to expand constants here, constants only have one
1220 // value and we check that it is fine above.
1221
Scott Michelc1513d22007-08-08 23:23:31 +00001222 if (opAction == TargetLowering::Custom) {
1223 Tmp1 = TLI.LowerOperation(Result, DAG);
1224 if (Tmp1.Val)
1225 Result = Tmp1;
1226 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001227 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001228 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001229 case ISD::ConstantFP: {
1230 // Spill FP immediates to the constant pool if the target cannot directly
1231 // codegen them. Targets often have some immediate values that can be
1232 // efficiently generated into an FP register without a load. We explicitly
1233 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001234 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1235
Chris Lattner3181a772006-01-29 06:26:56 +00001236 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1237 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001238 case TargetLowering::Legal:
1239 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001240 case TargetLowering::Custom:
1241 Tmp3 = TLI.LowerOperation(Result, DAG);
1242 if (Tmp3.Val) {
1243 Result = Tmp3;
1244 break;
1245 }
1246 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001247 case TargetLowering::Expand: {
1248 // Check to see if this FP immediate is already legal.
1249 bool isLegal = false;
1250 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1251 E = TLI.legal_fpimm_end(); I != E; ++I) {
1252 if (CFP->isExactlyValue(*I)) {
1253 isLegal = true;
1254 break;
1255 }
1256 }
1257 // If this is a legal constant, turn it into a TargetConstantFP node.
1258 if (isLegal)
1259 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001260 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001261 }
Nate Begemane1795842008-02-14 08:57:00 +00001262 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001263 break;
1264 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001265 case ISD::TokenFactor:
1266 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001267 Tmp1 = LegalizeOp(Node->getOperand(0));
1268 Tmp2 = LegalizeOp(Node->getOperand(1));
1269 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1270 } else if (Node->getNumOperands() == 3) {
1271 Tmp1 = LegalizeOp(Node->getOperand(0));
1272 Tmp2 = LegalizeOp(Node->getOperand(1));
1273 Tmp3 = LegalizeOp(Node->getOperand(2));
1274 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001275 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001276 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001277 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001278 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1279 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001280 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001281 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001282 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001283
1284 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001285 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001286 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001287 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1288 assert(Tmp3.Val && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001289 // A call within a calling sequence must be legalized to something
1290 // other than the normal CALLSEQ_END. Violating this gets Legalize
1291 // into an infinite loop.
1292 assert ((!IsLegalizingCall ||
1293 Node->getOpcode() != ISD::CALL ||
1294 Tmp3.Val->getOpcode() != ISD::CALLSEQ_END) &&
1295 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001296
1297 // The number of incoming and outgoing values should match; unless the final
1298 // outgoing value is a flag.
1299 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1300 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1301 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1302 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001303 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001304
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001305 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001306 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001307 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001308 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1309 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001310 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001311 if (Op.ResNo == i)
1312 Tmp2 = Tmp1;
1313 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1314 }
1315 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001316 case ISD::EXTRACT_SUBREG: {
1317 Tmp1 = LegalizeOp(Node->getOperand(0));
1318 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1319 assert(idx && "Operand must be a constant");
1320 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1321 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1322 }
1323 break;
1324 case ISD::INSERT_SUBREG: {
1325 Tmp1 = LegalizeOp(Node->getOperand(0));
1326 Tmp2 = LegalizeOp(Node->getOperand(1));
1327 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1328 assert(idx && "Operand must be a constant");
1329 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1330 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1331 }
1332 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001333 case ISD::BUILD_VECTOR:
1334 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001335 default: assert(0 && "This action is not supported yet!");
1336 case TargetLowering::Custom:
1337 Tmp3 = TLI.LowerOperation(Result, DAG);
1338 if (Tmp3.Val) {
1339 Result = Tmp3;
1340 break;
1341 }
1342 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001343 case TargetLowering::Expand:
1344 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001345 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001346 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001347 break;
1348 case ISD::INSERT_VECTOR_ELT:
1349 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001350 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001351
1352 // The type of the value to insert may not be legal, even though the vector
1353 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1354 // here.
1355 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1356 default: assert(0 && "Cannot expand insert element operand");
1357 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1358 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1359 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001360 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1361
1362 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1363 Node->getValueType(0))) {
1364 default: assert(0 && "This action is not supported yet!");
1365 case TargetLowering::Legal:
1366 break;
1367 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001368 Tmp4 = TLI.LowerOperation(Result, DAG);
1369 if (Tmp4.Val) {
1370 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001371 break;
1372 }
1373 // FALLTHROUGH
1374 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001375 // If the insert index is a constant, codegen this as a scalar_to_vector,
1376 // then a shuffle that inserts it into the right position in the vector.
1377 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001378 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1379 // match the element type of the vector being created.
1380 if (Tmp2.getValueType() ==
1381 MVT::getVectorElementType(Op.getValueType())) {
1382 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1383 Tmp1.getValueType(), Tmp2);
1384
1385 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1386 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1387 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1388
1389 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1390 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1391 // elt 0 of the RHS.
1392 SmallVector<SDOperand, 8> ShufOps;
1393 for (unsigned i = 0; i != NumElts; ++i) {
1394 if (i != InsertPos->getValue())
1395 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1396 else
1397 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1398 }
1399 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1400 &ShufOps[0], ShufOps.size());
1401
1402 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1403 Tmp1, ScVec, ShufMask);
1404 Result = LegalizeOp(Result);
1405 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001406 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001407 }
1408
Chris Lattner2332b9f2006-03-19 01:17:20 +00001409 // If the target doesn't support this, we have to spill the input vector
1410 // to a temporary stack slot, update the element, then reload it. This is
1411 // badness. We could also load the value into a vector register (either
1412 // with a "move to register" or "extload into register" instruction, then
1413 // permute it into place, if the idx is a constant and if the idx is
1414 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001415 MVT::ValueType VT = Tmp1.getValueType();
Nate Begeman0325d902008-02-13 06:43:04 +00001416 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Chengf6bf87f2006-04-08 01:46:37 +00001417 MVT::ValueType IdxVT = Tmp3.getValueType();
1418 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner85dd3be2007-10-15 17:48:57 +00001419 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohman69de1932008-02-06 22:27:42 +00001420
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00001421 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val);
Dan Gohman69de1932008-02-06 22:27:42 +00001422 int SPFI = StackPtrFI->getIndex();
1423
Evan Chengeb0b4612006-03-31 01:27:51 +00001424 // Store the vector.
Dan Gohman69de1932008-02-06 22:27:42 +00001425 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00001426 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00001427 SPFI);
Evan Chengeb0b4612006-03-31 01:27:51 +00001428
1429 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001430 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1431 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001432 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001433 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1434 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1435 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001436 // Store the scalar value.
Nate Begeman0325d902008-02-13 06:43:04 +00001437 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
1438 PseudoSourceValue::getFixedStack(), SPFI, EltVT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001439 // Load the updated vector.
Dan Gohman69de1932008-02-06 22:27:42 +00001440 Result = DAG.getLoad(VT, Ch, StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00001441 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001442 break;
1443 }
1444 }
1445 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001446 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001447 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1448 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1449 break;
1450 }
1451
Chris Lattnerce872152006-03-19 06:31:19 +00001452 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1453 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1454 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1455 Node->getValueType(0))) {
1456 default: assert(0 && "This action is not supported yet!");
1457 case TargetLowering::Legal:
1458 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001459 case TargetLowering::Custom:
1460 Tmp3 = TLI.LowerOperation(Result, DAG);
1461 if (Tmp3.Val) {
1462 Result = Tmp3;
1463 break;
1464 }
1465 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001466 case TargetLowering::Expand:
1467 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001468 break;
1469 }
Chris Lattnerce872152006-03-19 06:31:19 +00001470 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001471 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001472 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1473 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1474 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1475
1476 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001477 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1478 default: assert(0 && "Unknown operation action!");
1479 case TargetLowering::Legal:
1480 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1481 "vector shuffle should not be created if not legal!");
1482 break;
1483 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001484 Tmp3 = TLI.LowerOperation(Result, DAG);
1485 if (Tmp3.Val) {
1486 Result = Tmp3;
1487 break;
1488 }
1489 // FALLTHROUGH
1490 case TargetLowering::Expand: {
1491 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001492 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001493 MVT::ValueType PtrVT = TLI.getPointerTy();
1494 SDOperand Mask = Node->getOperand(2);
1495 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001496 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001497 for (unsigned i = 0; i != NumElems; ++i) {
1498 SDOperand Arg = Mask.getOperand(i);
1499 if (Arg.getOpcode() == ISD::UNDEF) {
1500 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1501 } else {
1502 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1503 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1504 if (Idx < NumElems)
1505 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1506 DAG.getConstant(Idx, PtrVT)));
1507 else
1508 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1509 DAG.getConstant(Idx - NumElems, PtrVT)));
1510 }
1511 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001512 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001513 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001514 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001515 case TargetLowering::Promote: {
1516 // Change base type to a different vector type.
1517 MVT::ValueType OVT = Node->getValueType(0);
1518 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1519
1520 // Cast the two input vectors.
1521 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1522 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1523
1524 // Convert the shuffle mask to the right # elements.
1525 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1526 assert(Tmp3.Val && "Shuffle not legal?");
1527 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1528 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1529 break;
1530 }
Chris Lattner87100e02006-03-20 01:52:29 +00001531 }
1532 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001533
1534 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001535 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001536 Tmp2 = LegalizeOp(Node->getOperand(1));
1537 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001538 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001539 break;
1540
Dan Gohman7f321562007-06-25 16:23:39 +00001541 case ISD::EXTRACT_SUBVECTOR:
1542 Tmp1 = Node->getOperand(0);
1543 Tmp2 = LegalizeOp(Node->getOperand(1));
1544 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1545 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001546 break;
1547
Chris Lattner6831a812006-02-13 09:18:02 +00001548 case ISD::CALLSEQ_START: {
1549 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1550
1551 // Recursively Legalize all of the inputs of the call end that do not lead
1552 // to this call start. This ensures that any libcalls that need be inserted
1553 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001554 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001555 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001556 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1557 NodesLeadingTo);
1558 }
Chris Lattner6831a812006-02-13 09:18:02 +00001559
1560 // Now that we legalized all of the inputs (which may have inserted
1561 // libcalls) create the new CALLSEQ_START node.
1562 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1563
1564 // Merge in the last call, to ensure that this call start after the last
1565 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001566 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001567 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1568 Tmp1 = LegalizeOp(Tmp1);
1569 }
Chris Lattner6831a812006-02-13 09:18:02 +00001570
1571 // Do not try to legalize the target-specific arguments (#1+).
1572 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001573 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001574 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001575 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001576 }
1577
1578 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001579 AddLegalizedOperand(Op.getValue(0), Result);
1580 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1581 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1582
Chris Lattner6831a812006-02-13 09:18:02 +00001583 // Now that the callseq_start and all of the non-call nodes above this call
1584 // sequence have been legalized, legalize the call itself. During this
1585 // process, no libcalls can/will be inserted, guaranteeing that no calls
1586 // can overlap.
1587 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1588 SDOperand InCallSEQ = LastCALLSEQ_END;
1589 // Note that we are selecting this call!
1590 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1591 IsLegalizingCall = true;
1592
1593 // Legalize the call, starting from the CALLSEQ_END.
1594 LegalizeOp(LastCALLSEQ_END);
1595 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1596 return Result;
1597 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001598 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001599 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1600 // will cause this node to be legalized as well as handling libcalls right.
1601 if (LastCALLSEQ_END.Val != Node) {
1602 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Roman Levensteindc1adac2008-04-07 10:06:32 +00001603 DenseMap<SDOperandImpl, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001604 assert(I != LegalizedNodes.end() &&
1605 "Legalizing the call start should have legalized this node!");
1606 return I->second;
1607 }
1608
1609 // Otherwise, the call start has been legalized and everything is going
1610 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001611 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001612 // Do not try to legalize the target-specific arguments (#1+), except for
1613 // an optional flag input.
1614 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1615 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001616 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001617 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001618 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001619 }
1620 } else {
1621 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1622 if (Tmp1 != Node->getOperand(0) ||
1623 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001624 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001625 Ops[0] = Tmp1;
1626 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001627 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001628 }
Chris Lattner6a542892006-01-24 05:48:21 +00001629 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001630 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001631 // This finishes up call legalization.
1632 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001633
1634 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1635 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1636 if (Node->getNumValues() == 2)
1637 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1638 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001639 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001640 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001641 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1642 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1643 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001644 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001645
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001646 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001647 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001648 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001649 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001650 case TargetLowering::Expand: {
1651 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1652 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1653 " not tell us which reg is the stack pointer!");
1654 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001655
1656 // Chain the dynamic stack allocation so that it doesn't modify the stack
1657 // pointer when other instructions are using the stack.
1658 Chain = DAG.getCALLSEQ_START(Chain,
1659 DAG.getConstant(0, TLI.getPointerTy()));
1660
Chris Lattner903d2782006-01-15 08:54:32 +00001661 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001662 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1663 Chain = SP.getValue(1);
1664 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1665 unsigned StackAlign =
1666 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1667 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001668 SP = DAG.getNode(ISD::AND, VT, SP,
1669 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001670 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001671 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1672
1673 Tmp2 =
1674 DAG.getCALLSEQ_END(Chain,
1675 DAG.getConstant(0, TLI.getPointerTy()),
1676 DAG.getConstant(0, TLI.getPointerTy()),
1677 SDOperand());
1678
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001679 Tmp1 = LegalizeOp(Tmp1);
1680 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001681 break;
1682 }
1683 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001684 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1685 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001686 Tmp1 = LegalizeOp(Tmp3);
1687 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001688 }
Chris Lattner903d2782006-01-15 08:54:32 +00001689 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001690 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001691 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001692 }
Chris Lattner903d2782006-01-15 08:54:32 +00001693 // Since this op produce two values, make sure to remember that we
1694 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001695 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1696 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001697 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001698 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001699 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001700 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001701 bool Changed = false;
1702 // Legalize all of the operands of the inline asm, in case they are nodes
1703 // that need to be expanded or something. Note we skip the asm string and
1704 // all of the TargetConstant flags.
1705 SDOperand Op = LegalizeOp(Ops[0]);
1706 Changed = Op != Ops[0];
1707 Ops[0] = Op;
1708
1709 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1710 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1711 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1712 for (++i; NumVals; ++i, --NumVals) {
1713 SDOperand Op = LegalizeOp(Ops[i]);
1714 if (Op != Ops[i]) {
1715 Changed = true;
1716 Ops[i] = Op;
1717 }
1718 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001719 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001720
1721 if (HasInFlag) {
1722 Op = LegalizeOp(Ops.back());
1723 Changed |= Op != Ops.back();
1724 Ops.back() = Op;
1725 }
1726
1727 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001728 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001729
1730 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001731 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001732 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1733 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001734 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001735 case ISD::BR:
1736 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001737 // Ensure that libcalls are emitted before a branch.
1738 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1739 Tmp1 = LegalizeOp(Tmp1);
1740 LastCALLSEQ_END = DAG.getEntryNode();
1741
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001742 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001743 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001744 case ISD::BRIND:
1745 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1746 // Ensure that libcalls are emitted before a branch.
1747 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1748 Tmp1 = LegalizeOp(Tmp1);
1749 LastCALLSEQ_END = DAG.getEntryNode();
1750
1751 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1752 default: assert(0 && "Indirect target must be legal type (pointer)!");
1753 case Legal:
1754 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1755 break;
1756 }
1757 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1758 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001759 case ISD::BR_JT:
1760 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1761 // Ensure that libcalls are emitted before a branch.
1762 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1763 Tmp1 = LegalizeOp(Tmp1);
1764 LastCALLSEQ_END = DAG.getEntryNode();
1765
1766 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1767 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1768
1769 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1770 default: assert(0 && "This action is not supported yet!");
1771 case TargetLowering::Legal: break;
1772 case TargetLowering::Custom:
1773 Tmp1 = TLI.LowerOperation(Result, DAG);
1774 if (Tmp1.Val) Result = Tmp1;
1775 break;
1776 case TargetLowering::Expand: {
1777 SDOperand Chain = Result.getOperand(0);
1778 SDOperand Table = Result.getOperand(1);
1779 SDOperand Index = Result.getOperand(2);
1780
1781 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001782 MachineFunction &MF = DAG.getMachineFunction();
1783 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001784 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1785 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001786
1787 SDOperand LD;
1788 switch (EntrySize) {
1789 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001790 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001791 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001792 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001793 PseudoSourceValue::getJumpTable(), 0); break;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001794 }
1795
Evan Chengcc415862007-11-09 01:32:10 +00001796 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001797 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001798 // For PIC, the sequence is:
1799 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001800 // RelocBase can be JumpTable, GOT or some sort of global base.
1801 if (PTy != MVT::i32)
1802 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1803 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1804 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001805 }
Evan Chengcc415862007-11-09 01:32:10 +00001806 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001807 }
1808 }
1809 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001810 case ISD::BRCOND:
1811 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001812 // Ensure that libcalls are emitted before a return.
1813 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1814 Tmp1 = LegalizeOp(Tmp1);
1815 LastCALLSEQ_END = DAG.getEntryNode();
1816
Chris Lattner47e92232005-01-18 19:27:06 +00001817 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1818 case Expand: assert(0 && "It's impossible to expand bools");
1819 case Legal:
1820 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1821 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001822 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00001823 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001824
1825 // The top bits of the promoted condition are not necessarily zero, ensure
1826 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001827 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001828 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001829 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00001830 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001831 break;
1832 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001833 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001834
1835 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001836 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001837
1838 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1839 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001840 case TargetLowering::Legal: break;
1841 case TargetLowering::Custom:
1842 Tmp1 = TLI.LowerOperation(Result, DAG);
1843 if (Tmp1.Val) Result = Tmp1;
1844 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001845 case TargetLowering::Expand:
1846 // Expand brcond's setcc into its constituent parts and create a BR_CC
1847 // Node.
1848 if (Tmp2.getOpcode() == ISD::SETCC) {
1849 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1850 Tmp2.getOperand(0), Tmp2.getOperand(1),
1851 Node->getOperand(2));
1852 } else {
1853 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1854 DAG.getCondCode(ISD::SETNE), Tmp2,
1855 DAG.getConstant(0, Tmp2.getValueType()),
1856 Node->getOperand(2));
1857 }
1858 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001859 }
1860 break;
1861 case ISD::BR_CC:
1862 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001863 // Ensure that libcalls are emitted before a branch.
1864 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1865 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001866 Tmp2 = Node->getOperand(2); // LHS
1867 Tmp3 = Node->getOperand(3); // RHS
1868 Tmp4 = Node->getOperand(1); // CC
1869
1870 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001871 LastCALLSEQ_END = DAG.getEntryNode();
1872
Nate Begeman750ac1b2006-02-01 07:19:44 +00001873 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1874 // the LHS is a legal SETCC itself. In this case, we need to compare
1875 // the result against zero to select between true and false values.
1876 if (Tmp3.Val == 0) {
1877 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1878 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001879 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001880
1881 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1882 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001883
Chris Lattner181b7a32005-12-17 23:46:46 +00001884 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1885 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001886 case TargetLowering::Legal: break;
1887 case TargetLowering::Custom:
1888 Tmp4 = TLI.LowerOperation(Result, DAG);
1889 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001890 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001891 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001892 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001893 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001894 LoadSDNode *LD = cast<LoadSDNode>(Node);
1895 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1896 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001897
Evan Cheng466685d2006-10-09 20:57:25 +00001898 ISD::LoadExtType ExtType = LD->getExtensionType();
1899 if (ExtType == ISD::NON_EXTLOAD) {
1900 MVT::ValueType VT = Node->getValueType(0);
1901 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1902 Tmp3 = Result.getValue(0);
1903 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001904
Evan Cheng466685d2006-10-09 20:57:25 +00001905 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1906 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001907 case TargetLowering::Legal:
1908 // If this is an unaligned load and the target doesn't support it,
1909 // expand it.
1910 if (!TLI.allowsUnalignedMemoryAccesses()) {
1911 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001912 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001913 if (LD->getAlignment() < ABIAlignment){
1914 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1915 TLI);
1916 Tmp3 = Result.getOperand(0);
1917 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001918 Tmp3 = LegalizeOp(Tmp3);
1919 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001920 }
1921 }
1922 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001923 case TargetLowering::Custom:
1924 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1925 if (Tmp1.Val) {
1926 Tmp3 = LegalizeOp(Tmp1);
1927 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001928 }
Evan Cheng466685d2006-10-09 20:57:25 +00001929 break;
1930 case TargetLowering::Promote: {
1931 // Only promote a load of vector type to another.
1932 assert(MVT::isVector(VT) && "Cannot promote this load!");
1933 // Change base type to a different vector type.
1934 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1935
1936 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001937 LD->getSrcValueOffset(),
1938 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001939 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1940 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001941 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001942 }
Evan Cheng466685d2006-10-09 20:57:25 +00001943 }
1944 // Since loads produce two values, make sure to remember that we
1945 // legalized both of them.
1946 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1947 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1948 return Op.ResNo ? Tmp4 : Tmp3;
1949 } else {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001950 MVT::ValueType SrcVT = LD->getMemoryVT();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001951 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1952 int SVOffset = LD->getSrcValueOffset();
1953 unsigned Alignment = LD->getAlignment();
1954 bool isVolatile = LD->isVolatile();
1955
1956 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1957 // Some targets pretend to have an i1 loading operation, and actually
1958 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1959 // bits are guaranteed to be zero; it helps the optimizers understand
1960 // that these bits are zero. It is also useful for EXTLOAD, since it
1961 // tells the optimizers that those bits are undefined. It would be
1962 // nice to have an effective generic way of getting these benefits...
1963 // Until such a way is found, don't insist on promoting i1 here.
1964 (SrcVT != MVT::i1 ||
1965 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1966 // Promote to a byte-sized load if not loading an integral number of
1967 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1968 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1969 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1970 SDOperand Ch;
1971
1972 // The extra bits are guaranteed to be zero, since we stored them that
1973 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1974
1975 ISD::LoadExtType NewExtType =
1976 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1977
1978 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1979 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1980 NVT, isVolatile, Alignment);
1981
1982 Ch = Result.getValue(1); // The chain.
1983
1984 if (ExtType == ISD::SEXTLOAD)
1985 // Having the top bits zero doesn't help when sign extending.
1986 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1987 Result, DAG.getValueType(SrcVT));
1988 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1989 // All the top bits are guaranteed to be zero - inform the optimizers.
1990 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1991 DAG.getValueType(SrcVT));
1992
1993 Tmp1 = LegalizeOp(Result);
1994 Tmp2 = LegalizeOp(Ch);
1995 } else if (SrcWidth & (SrcWidth - 1)) {
1996 // If not loading a power-of-2 number of bits, expand as two loads.
1997 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1998 "Unsupported extload!");
1999 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2000 assert(RoundWidth < SrcWidth);
2001 unsigned ExtraWidth = SrcWidth - RoundWidth;
2002 assert(ExtraWidth < RoundWidth);
2003 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2004 "Load size not an integral number of bytes!");
2005 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2006 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2007 SDOperand Lo, Hi, Ch;
2008 unsigned IncrementSize;
2009
2010 if (TLI.isLittleEndian()) {
2011 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2012 // Load the bottom RoundWidth bits.
2013 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2014 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2015 Alignment);
2016
2017 // Load the remaining ExtraWidth bits.
2018 IncrementSize = RoundWidth / 8;
2019 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2020 DAG.getIntPtrConstant(IncrementSize));
2021 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2022 LD->getSrcValue(), SVOffset + IncrementSize,
2023 ExtraVT, isVolatile,
2024 MinAlign(Alignment, IncrementSize));
2025
2026 // Build a factor node to remember that this load is independent of the
2027 // other one.
2028 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2029 Hi.getValue(1));
2030
2031 // Move the top bits to the right place.
2032 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2033 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2034
2035 // Join the hi and lo parts.
2036 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002037 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002038 // Big endian - avoid unaligned loads.
2039 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2040 // Load the top RoundWidth bits.
2041 Hi = DAG.getExtLoad(ExtType, 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 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, 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(ExtraWidth, TLI.getShiftAmountTy()));
2062
2063 // Join the hi and lo parts.
2064 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2065 }
2066
2067 Tmp1 = LegalizeOp(Result);
2068 Tmp2 = LegalizeOp(Ch);
2069 } else {
2070 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2071 default: assert(0 && "This action is not supported yet!");
2072 case TargetLowering::Custom:
2073 isCustom = true;
2074 // FALLTHROUGH
2075 case TargetLowering::Legal:
2076 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2077 Tmp1 = Result.getValue(0);
2078 Tmp2 = Result.getValue(1);
2079
2080 if (isCustom) {
2081 Tmp3 = TLI.LowerOperation(Result, DAG);
2082 if (Tmp3.Val) {
2083 Tmp1 = LegalizeOp(Tmp3);
2084 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2085 }
2086 } else {
2087 // If this is an unaligned load and the target doesn't support it,
2088 // expand it.
2089 if (!TLI.allowsUnalignedMemoryAccesses()) {
2090 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002091 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002092 if (LD->getAlignment() < ABIAlignment){
2093 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2094 TLI);
2095 Tmp1 = Result.getOperand(0);
2096 Tmp2 = Result.getOperand(1);
2097 Tmp1 = LegalizeOp(Tmp1);
2098 Tmp2 = LegalizeOp(Tmp2);
2099 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002100 }
2101 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002102 break;
2103 case TargetLowering::Expand:
2104 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2105 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2106 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2107 LD->getSrcValueOffset(),
2108 LD->isVolatile(), LD->getAlignment());
2109 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2110 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2111 Tmp2 = LegalizeOp(Load.getValue(1));
2112 break;
2113 }
2114 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2115 // Turn the unsupported load into an EXTLOAD followed by an explicit
2116 // zero/sign extend inreg.
2117 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2118 Tmp1, Tmp2, LD->getSrcValue(),
2119 LD->getSrcValueOffset(), SrcVT,
2120 LD->isVolatile(), LD->getAlignment());
2121 SDOperand ValRes;
2122 if (ExtType == ISD::SEXTLOAD)
2123 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2124 Result, DAG.getValueType(SrcVT));
2125 else
2126 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2127 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2128 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002129 break;
2130 }
Evan Cheng466685d2006-10-09 20:57:25 +00002131 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002132
Evan Cheng466685d2006-10-09 20:57:25 +00002133 // Since loads produce two values, make sure to remember that we legalized
2134 // both of them.
2135 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2136 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2137 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002138 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002139 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002140 case ISD::EXTRACT_ELEMENT: {
2141 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2142 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002143 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002144 case Legal:
2145 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2146 // 1 -> Hi
2147 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2148 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2149 TLI.getShiftAmountTy()));
2150 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2151 } else {
2152 // 0 -> Lo
2153 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2154 Node->getOperand(0));
2155 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002156 break;
2157 case Expand:
2158 // Get both the low and high parts.
2159 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2160 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2161 Result = Tmp2; // 1 -> Hi
2162 else
2163 Result = Tmp1; // 0 -> Lo
2164 break;
2165 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002166 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002167 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002168
2169 case ISD::CopyToReg:
2170 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002171
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002172 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002173 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002174 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002175 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002176 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002177 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002178 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002179 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002180 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002181 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002182 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2183 Tmp3);
2184 } else {
2185 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002186 }
2187
2188 // Since this produces two values, make sure to remember that we legalized
2189 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002190 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00002191 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002192 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002193 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002194 break;
2195
2196 case ISD::RET:
2197 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002198
2199 // Ensure that libcalls are emitted before a return.
2200 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2201 Tmp1 = LegalizeOp(Tmp1);
2202 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002203
Chris Lattner3e928bb2005-01-07 07:47:09 +00002204 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002205 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002206 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002207 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002208 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002209 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002210 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002211 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002212 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00002213 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00002214 SDOperand Lo, Hi;
2215 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002216
2217 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002218 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002219 std::swap(Lo, Hi);
2220
Evan Cheng13acce32006-12-11 19:27:14 +00002221 if (Hi.Val)
2222 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2223 else
2224 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002225 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002226 } else {
2227 SDNode *InVal = Tmp2.Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002228 int InIx = Tmp2.ResNo;
2229 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2230 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattnerf87324e2006-04-11 01:31:51 +00002231
Dan Gohman7f321562007-06-25 16:23:39 +00002232 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002233 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00002234 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002235 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002236 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002237 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002238 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002239 } else if (NumElems == 1) {
2240 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002241 Tmp2 = ScalarizeVectorOp(Tmp2);
2242 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002243 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002244
2245 // FIXME: Returns of gcc generic vectors smaller than a legal type
2246 // should be returned in integer registers!
2247
Chris Lattnerf87324e2006-04-11 01:31:51 +00002248 // The scalarized value type may not be legal, e.g. it might require
2249 // promotion or expansion. Relegalize the return.
2250 Result = LegalizeOp(Result);
2251 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002252 // FIXME: Returns of gcc generic vectors larger than a legal vector
2253 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00002254 SDOperand Lo, Hi;
2255 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002256 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002257 Result = LegalizeOp(Result);
2258 }
2259 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002260 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002261 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002262 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002263 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002264 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002265 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002266 }
2267 break;
2268 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002269 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002270 break;
2271 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002272 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002273 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002274 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002275 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2276 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002277 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002278 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002279 break;
2280 case Expand: {
2281 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00002282 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002283 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002284 ExpandOp(Node->getOperand(i), Lo, Hi);
2285 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002286 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00002287 if (Hi.Val) {
2288 NewValues.push_back(Hi);
2289 NewValues.push_back(Node->getOperand(i+1));
2290 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002291 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002292 }
2293 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002294 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002295 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002296
2297 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002298 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002299 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002300 Result = DAG.getNode(ISD::RET, MVT::Other,
2301 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002302 break;
2303 }
2304 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002305
Chris Lattner6862dbc2006-01-29 21:02:23 +00002306 if (Result.getOpcode() == ISD::RET) {
2307 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2308 default: assert(0 && "This action is not supported yet!");
2309 case TargetLowering::Legal: break;
2310 case TargetLowering::Custom:
2311 Tmp1 = TLI.LowerOperation(Result, DAG);
2312 if (Tmp1.Val) Result = Tmp1;
2313 break;
2314 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002315 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002316 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002317 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002318 StoreSDNode *ST = cast<StoreSDNode>(Node);
2319 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2320 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002321 int SVOffset = ST->getSrcValueOffset();
2322 unsigned Alignment = ST->getAlignment();
2323 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002324
Evan Cheng8b2794a2006-10-13 21:14:26 +00002325 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002326 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2327 // FIXME: We shouldn't do this for TargetConstantFP's.
2328 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2329 // to phase ordering between legalized code and the dag combiner. This
2330 // probably means that we need to integrate dag combiner and legalizer
2331 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002332 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002333 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002334 if (CFP->getValueType(0) == MVT::f32 &&
2335 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002336 Tmp3 = DAG.getConstant(CFP->getValueAPF().
2337 convertToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002338 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002339 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2340 SVOffset, isVolatile, Alignment);
2341 break;
2342 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002343 // If this target supports 64-bit registers, do a single 64-bit store.
2344 if (getTypeAction(MVT::i64) == Legal) {
2345 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002346 zextOrTrunc(64), MVT::i64);
Chris Lattner3cb93512007-10-15 05:46:06 +00002347 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2348 SVOffset, isVolatile, Alignment);
2349 break;
2350 } else if (getTypeAction(MVT::i32) == Legal) {
2351 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2352 // stores. If the target supports neither 32- nor 64-bits, this
2353 // xform is certainly not worth it.
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002354 const APInt &IntVal =CFP->getValueAPF().convertToAPInt();
2355 SDOperand Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2356 SDOperand Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002357 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002358
2359 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2360 SVOffset, isVolatile, Alignment);
2361 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002362 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002363 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002364 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002365
2366 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2367 break;
2368 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002369 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002370 }
2371
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002372 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002373 case Legal: {
2374 Tmp3 = LegalizeOp(ST->getValue());
2375 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2376 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002377
Evan Cheng8b2794a2006-10-13 21:14:26 +00002378 MVT::ValueType VT = Tmp3.getValueType();
2379 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2380 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002381 case TargetLowering::Legal:
2382 // If this is an unaligned store and the target doesn't support it,
2383 // expand it.
2384 if (!TLI.allowsUnalignedMemoryAccesses()) {
2385 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002386 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002387 if (ST->getAlignment() < ABIAlignment)
2388 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2389 TLI);
2390 }
2391 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002392 case TargetLowering::Custom:
2393 Tmp1 = TLI.LowerOperation(Result, DAG);
2394 if (Tmp1.Val) Result = Tmp1;
2395 break;
2396 case TargetLowering::Promote:
2397 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2398 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2399 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2400 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002401 ST->getSrcValue(), SVOffset, isVolatile,
2402 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002403 break;
2404 }
2405 break;
2406 }
2407 case Promote:
2408 // Truncate the value and store the result.
2409 Tmp3 = PromoteOp(ST->getValue());
2410 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002411 SVOffset, ST->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002412 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002413 break;
2414
2415 case Expand:
2416 unsigned IncrementSize = 0;
2417 SDOperand Lo, Hi;
2418
2419 // If this is a vector type, then we have to calculate the increment as
2420 // the product of the element size in bytes, and the number of elements
2421 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002422 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002423 SDNode *InVal = ST->getValue().Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002424 int InIx = ST->getValue().ResNo;
Chris Lattner0bd48932008-01-17 07:00:52 +00002425 MVT::ValueType InVT = InVal->getValueType(InIx);
2426 unsigned NumElems = MVT::getVectorNumElements(InVT);
2427 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002428
Dan Gohman7f321562007-06-25 16:23:39 +00002429 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002430 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002431 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002432 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002433 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002434 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002435 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002436 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002437 Result = LegalizeOp(Result);
2438 break;
2439 } else if (NumElems == 1) {
2440 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002441 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002442 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002443 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002444 // The scalarized value type may not be legal, e.g. it might require
2445 // promotion or expansion. Relegalize the scalar store.
2446 Result = LegalizeOp(Result);
2447 break;
2448 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002449 SplitVectorOp(ST->getValue(), Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00002450 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2451 MVT::getSizeInBits(EVT)/8;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002452 }
2453 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002454 ExpandOp(ST->getValue(), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002455 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002456
Duncan Sands0753fc12008-02-11 10:37:04 +00002457 if (TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002458 std::swap(Lo, Hi);
2459 }
2460
2461 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002462 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002463
2464 if (Hi.Val == NULL) {
2465 // Must be int <-> float one-to-one expansion.
2466 Result = Lo;
2467 break;
2468 }
2469
Evan Cheng8b2794a2006-10-13 21:14:26 +00002470 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002471 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002472 assert(isTypeLegal(Tmp2.getValueType()) &&
2473 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002474 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002475 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002476 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002477 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002478 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2479 break;
2480 }
2481 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002482 switch (getTypeAction(ST->getValue().getValueType())) {
2483 case Legal:
2484 Tmp3 = LegalizeOp(ST->getValue());
2485 break;
2486 case Promote:
2487 // We can promote the value, the truncstore will still take care of it.
2488 Tmp3 = PromoteOp(ST->getValue());
2489 break;
2490 case Expand:
2491 // Just store the low part. This may become a non-trunc store, so make
2492 // sure to use getTruncStore, not UpdateNodeOperands below.
2493 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2494 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2495 SVOffset, MVT::i8, isVolatile, Alignment);
2496 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002497
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002498 MVT::ValueType StVT = ST->getMemoryVT();
Duncan Sands7e857202008-01-22 07:17:34 +00002499 unsigned StWidth = MVT::getSizeInBits(StVT);
2500
2501 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2502 // Promote to a byte-sized store with upper bits zero if not
2503 // storing an integral number of bytes. For example, promote
2504 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2505 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2506 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2507 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2508 SVOffset, NVT, isVolatile, Alignment);
2509 } else if (StWidth & (StWidth - 1)) {
2510 // If not storing a power-of-2 number of bits, expand as two stores.
2511 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2512 "Unsupported truncstore!");
2513 unsigned RoundWidth = 1 << Log2_32(StWidth);
2514 assert(RoundWidth < StWidth);
2515 unsigned ExtraWidth = StWidth - RoundWidth;
2516 assert(ExtraWidth < RoundWidth);
2517 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2518 "Store size not an integral number of bytes!");
2519 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2520 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2521 SDOperand Lo, Hi;
2522 unsigned IncrementSize;
2523
2524 if (TLI.isLittleEndian()) {
2525 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2526 // Store the bottom RoundWidth bits.
2527 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2528 SVOffset, RoundVT,
2529 isVolatile, Alignment);
2530
2531 // Store the remaining ExtraWidth bits.
2532 IncrementSize = RoundWidth / 8;
2533 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2534 DAG.getIntPtrConstant(IncrementSize));
2535 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2536 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2537 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2538 SVOffset + IncrementSize, ExtraVT, isVolatile,
2539 MinAlign(Alignment, IncrementSize));
2540 } else {
2541 // Big endian - avoid unaligned stores.
2542 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2543 // Store the top RoundWidth bits.
2544 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2545 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2546 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2547 RoundVT, isVolatile, Alignment);
2548
2549 // Store the remaining ExtraWidth bits.
2550 IncrementSize = RoundWidth / 8;
2551 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2552 DAG.getIntPtrConstant(IncrementSize));
2553 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2554 SVOffset + IncrementSize, ExtraVT, isVolatile,
2555 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002556 }
Duncan Sands7e857202008-01-22 07:17:34 +00002557
2558 // The order of the stores doesn't matter.
2559 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2560 } else {
2561 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2562 Tmp2 != ST->getBasePtr())
2563 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2564 ST->getOffset());
2565
2566 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2567 default: assert(0 && "This action is not supported yet!");
2568 case TargetLowering::Legal:
2569 // If this is an unaligned store and the target doesn't support it,
2570 // expand it.
2571 if (!TLI.allowsUnalignedMemoryAccesses()) {
2572 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002573 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Duncan Sands7e857202008-01-22 07:17:34 +00002574 if (ST->getAlignment() < ABIAlignment)
2575 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2576 TLI);
2577 }
2578 break;
2579 case TargetLowering::Custom:
2580 Result = TLI.LowerOperation(Result, DAG);
2581 break;
2582 case Expand:
2583 // TRUNCSTORE:i16 i32 -> STORE i16
2584 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2585 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2586 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2587 isVolatile, Alignment);
2588 break;
2589 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002590 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002591 }
2592 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002593 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002594 case ISD::PCMARKER:
2595 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002596 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002597 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002598 case ISD::STACKSAVE:
2599 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002600 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2601 Tmp1 = Result.getValue(0);
2602 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002603
Chris Lattner140d53c2006-01-13 02:50:02 +00002604 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2605 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002606 case TargetLowering::Legal: break;
2607 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002608 Tmp3 = TLI.LowerOperation(Result, DAG);
2609 if (Tmp3.Val) {
2610 Tmp1 = LegalizeOp(Tmp3);
2611 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002612 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002613 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002614 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002615 // Expand to CopyFromReg if the target set
2616 // StackPointerRegisterToSaveRestore.
2617 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002618 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002619 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002620 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002621 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002622 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2623 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002624 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002625 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002626 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002627
2628 // Since stacksave produce two values, make sure to remember that we
2629 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002630 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2631 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2632 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002633
Chris Lattner140d53c2006-01-13 02:50:02 +00002634 case ISD::STACKRESTORE:
2635 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2636 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002637 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002638
2639 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2640 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002641 case TargetLowering::Legal: break;
2642 case TargetLowering::Custom:
2643 Tmp1 = TLI.LowerOperation(Result, DAG);
2644 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002645 break;
2646 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002647 // Expand to CopyToReg if the target set
2648 // StackPointerRegisterToSaveRestore.
2649 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2650 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2651 } else {
2652 Result = Tmp1;
2653 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002654 break;
2655 }
2656 break;
2657
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002658 case ISD::READCYCLECOUNTER:
2659 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002660 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002661 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2662 Node->getValueType(0))) {
2663 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002664 case TargetLowering::Legal:
2665 Tmp1 = Result.getValue(0);
2666 Tmp2 = Result.getValue(1);
2667 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002668 case TargetLowering::Custom:
2669 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002670 Tmp1 = LegalizeOp(Result.getValue(0));
2671 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002672 break;
2673 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002674
2675 // Since rdcc produce two values, make sure to remember that we legalized
2676 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002677 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2678 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002679 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002680
Chris Lattner2ee743f2005-01-14 22:08:15 +00002681 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002682 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2683 case Expand: assert(0 && "It's impossible to expand bools");
2684 case Legal:
2685 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2686 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002687 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00002688 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002689 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002690 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002691 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002692 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002693 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002694 break;
2695 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002696 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002697 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002698 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002699
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002700 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002701
Nate Begemanb942a3d2005-08-23 04:29:48 +00002702 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002703 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002704 case TargetLowering::Legal: break;
2705 case TargetLowering::Custom: {
2706 Tmp1 = TLI.LowerOperation(Result, DAG);
2707 if (Tmp1.Val) Result = Tmp1;
2708 break;
2709 }
Nate Begeman9373a812005-08-10 20:51:12 +00002710 case TargetLowering::Expand:
2711 if (Tmp1.getOpcode() == ISD::SETCC) {
2712 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2713 Tmp2, Tmp3,
2714 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2715 } else {
2716 Result = DAG.getSelectCC(Tmp1,
2717 DAG.getConstant(0, Tmp1.getValueType()),
2718 Tmp2, Tmp3, ISD::SETNE);
2719 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002720 break;
2721 case TargetLowering::Promote: {
2722 MVT::ValueType NVT =
2723 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2724 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002725 if (MVT::isVector(Tmp2.getValueType())) {
2726 ExtOp = ISD::BIT_CONVERT;
2727 TruncOp = ISD::BIT_CONVERT;
2728 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002729 ExtOp = ISD::ANY_EXTEND;
2730 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002731 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002732 ExtOp = ISD::FP_EXTEND;
2733 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002734 }
2735 // Promote each of the values to the new type.
2736 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2737 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2738 // Perform the larger operation, then round down.
2739 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002740 if (TruncOp != ISD::FP_ROUND)
2741 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2742 else
2743 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2744 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002745 break;
2746 }
2747 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002748 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002749 case ISD::SELECT_CC: {
2750 Tmp1 = Node->getOperand(0); // LHS
2751 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002752 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2753 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002754 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002755
Nate Begeman750ac1b2006-02-01 07:19:44 +00002756 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2757
2758 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2759 // the LHS is a legal SETCC itself. In this case, we need to compare
2760 // the result against zero to select between true and false values.
2761 if (Tmp2.Val == 0) {
2762 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2763 CC = DAG.getCondCode(ISD::SETNE);
2764 }
2765 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2766
2767 // Everything is legal, see if we should expand this op or something.
2768 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2769 default: assert(0 && "This action is not supported yet!");
2770 case TargetLowering::Legal: break;
2771 case TargetLowering::Custom:
2772 Tmp1 = TLI.LowerOperation(Result, DAG);
2773 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002774 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002775 }
2776 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002777 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002778 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002779 Tmp1 = Node->getOperand(0);
2780 Tmp2 = Node->getOperand(1);
2781 Tmp3 = Node->getOperand(2);
2782 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2783
2784 // If we had to Expand the SetCC operands into a SELECT node, then it may
2785 // not always be possible to return a true LHS & RHS. In this case, just
2786 // return the value we legalized, returned in the LHS
2787 if (Tmp2.Val == 0) {
2788 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002789 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002790 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002791
Chris Lattner73e142f2006-01-30 22:43:50 +00002792 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002793 default: assert(0 && "Cannot handle this action for SETCC yet!");
2794 case TargetLowering::Custom:
2795 isCustom = true;
2796 // FALLTHROUGH.
2797 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002798 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002799 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002800 Tmp4 = TLI.LowerOperation(Result, DAG);
2801 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002802 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002803 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002804 case TargetLowering::Promote: {
2805 // First step, figure out the appropriate operation to use.
2806 // Allow SETCC to not be supported for all legal data types
2807 // Mostly this targets FP
2808 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002809 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002810
2811 // Scan for the appropriate larger type to use.
2812 while (1) {
2813 NewInTy = (MVT::ValueType)(NewInTy+1);
2814
2815 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2816 "Fell off of the edge of the integer world");
2817 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2818 "Fell off of the edge of the floating point world");
2819
2820 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002821 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002822 break;
2823 }
2824 if (MVT::isInteger(NewInTy))
2825 assert(0 && "Cannot promote Legal Integer SETCC yet");
2826 else {
2827 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2828 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2829 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002830 Tmp1 = LegalizeOp(Tmp1);
2831 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002832 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002833 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002834 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002835 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002836 case TargetLowering::Expand:
2837 // Expand a setcc node into a select_cc of the same condition, lhs, and
2838 // rhs that selects between const 1 (true) and const 0 (false).
2839 MVT::ValueType VT = Node->getValueType(0);
2840 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2841 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002842 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002843 break;
2844 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002845 break;
Chris Lattner52d08bd2005-05-09 20:23:03 +00002846
Chris Lattner5b359c62005-04-02 04:00:59 +00002847 case ISD::SHL_PARTS:
2848 case ISD::SRA_PARTS:
2849 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002850 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002851 bool Changed = false;
2852 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2853 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2854 Changed |= Ops.back() != Node->getOperand(i);
2855 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002856 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002857 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002858
Evan Cheng05a2d562006-01-09 18:31:59 +00002859 switch (TLI.getOperationAction(Node->getOpcode(),
2860 Node->getValueType(0))) {
2861 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002862 case TargetLowering::Legal: break;
2863 case TargetLowering::Custom:
2864 Tmp1 = TLI.LowerOperation(Result, DAG);
2865 if (Tmp1.Val) {
2866 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002867 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002868 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002869 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2870 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002871 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002872 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002873 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002874 return RetVal;
2875 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002876 break;
2877 }
2878
Chris Lattner2c8086f2005-04-02 05:00:07 +00002879 // Since these produce multiple values, make sure to remember that we
2880 // legalized all of them.
2881 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2882 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2883 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002884 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002885
2886 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002887 case ISD::ADD:
2888 case ISD::SUB:
2889 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002890 case ISD::MULHS:
2891 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002892 case ISD::UDIV:
2893 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002894 case ISD::AND:
2895 case ISD::OR:
2896 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002897 case ISD::SHL:
2898 case ISD::SRL:
2899 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002900 case ISD::FADD:
2901 case ISD::FSUB:
2902 case ISD::FMUL:
2903 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00002904 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002905 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002906 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2907 case Expand: assert(0 && "Not possible");
2908 case Legal:
2909 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2910 break;
2911 case Promote:
2912 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2913 break;
2914 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002915
2916 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002917
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002918 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002919 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002920 case TargetLowering::Legal: break;
2921 case TargetLowering::Custom:
2922 Tmp1 = TLI.LowerOperation(Result, DAG);
2923 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002924 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002925 case TargetLowering::Expand: {
Dan Gohman525178c2007-10-08 18:33:35 +00002926 MVT::ValueType VT = Op.getValueType();
2927
2928 // See if multiply or divide can be lowered using two-result operations.
2929 SDVTList VTs = DAG.getVTList(VT, VT);
2930 if (Node->getOpcode() == ISD::MUL) {
2931 // We just need the low half of the multiply; try both the signed
2932 // and unsigned forms. If the target supports both SMUL_LOHI and
2933 // UMUL_LOHI, form a preference by checking which forms of plain
2934 // MULH it supports.
2935 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2936 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2937 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2938 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2939 unsigned OpToUse = 0;
2940 if (HasSMUL_LOHI && !HasMULHS) {
2941 OpToUse = ISD::SMUL_LOHI;
2942 } else if (HasUMUL_LOHI && !HasMULHU) {
2943 OpToUse = ISD::UMUL_LOHI;
2944 } else if (HasSMUL_LOHI) {
2945 OpToUse = ISD::SMUL_LOHI;
2946 } else if (HasUMUL_LOHI) {
2947 OpToUse = ISD::UMUL_LOHI;
2948 }
2949 if (OpToUse) {
2950 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2951 break;
2952 }
2953 }
2954 if (Node->getOpcode() == ISD::MULHS &&
2955 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2956 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2957 break;
2958 }
2959 if (Node->getOpcode() == ISD::MULHU &&
2960 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2961 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2962 break;
2963 }
2964 if (Node->getOpcode() == ISD::SDIV &&
2965 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2966 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2967 break;
2968 }
2969 if (Node->getOpcode() == ISD::UDIV &&
2970 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2971 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2972 break;
2973 }
2974
Dan Gohman82669522007-10-11 23:57:53 +00002975 // Check to see if we have a libcall for this operator.
2976 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2977 bool isSigned = false;
2978 switch (Node->getOpcode()) {
2979 case ISD::UDIV:
2980 case ISD::SDIV:
2981 if (VT == MVT::i32) {
2982 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00002983 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00002984 isSigned = Node->getOpcode() == ISD::SDIV;
2985 }
2986 break;
2987 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00002988 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
2989 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00002990 break;
2991 default: break;
2992 }
2993 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2994 SDOperand Dummy;
2995 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002996 break;
2997 }
2998
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002999 assert(MVT::isVector(Node->getValueType(0)) &&
3000 "Cannot expand this binary operator!");
3001 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003002 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003003 break;
3004 }
Evan Chengcc987612006-04-12 21:20:24 +00003005 case TargetLowering::Promote: {
3006 switch (Node->getOpcode()) {
3007 default: assert(0 && "Do not know how to promote this BinOp!");
3008 case ISD::AND:
3009 case ISD::OR:
3010 case ISD::XOR: {
3011 MVT::ValueType OVT = Node->getValueType(0);
3012 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3013 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3014 // Bit convert each of the values to the new type.
3015 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3016 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3017 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3018 // Bit convert the result back the original type.
3019 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3020 break;
3021 }
3022 }
3023 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003024 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003025 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003026
Dan Gohmane14ea862007-10-05 14:17:22 +00003027 case ISD::SMUL_LOHI:
3028 case ISD::UMUL_LOHI:
3029 case ISD::SDIVREM:
3030 case ISD::UDIVREM:
3031 // These nodes will only be produced by target-specific lowering, so
3032 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003033 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003034 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003035
3036 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3037 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3038 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003039 break;
3040
Chris Lattnera09f8482006-03-05 05:09:38 +00003041 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3042 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3043 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3044 case Expand: assert(0 && "Not possible");
3045 case Legal:
3046 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3047 break;
3048 case Promote:
3049 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3050 break;
3051 }
3052
3053 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3054
3055 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3056 default: assert(0 && "Operation not supported");
3057 case TargetLowering::Custom:
3058 Tmp1 = TLI.LowerOperation(Result, DAG);
3059 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003060 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003061 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003062 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003063 // If this target supports fabs/fneg natively and select is cheap,
3064 // do this efficiently.
3065 if (!TLI.isSelectExpensive() &&
3066 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3067 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003068 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003069 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003070 // Get the sign bit of the RHS.
3071 MVT::ValueType IVT =
3072 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3073 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003074 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003075 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3076 // Get the absolute value of the result.
3077 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3078 // Select between the nabs and abs value based on the sign bit of
3079 // the input.
3080 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3081 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3082 AbsVal),
3083 AbsVal);
3084 Result = LegalizeOp(Result);
3085 break;
3086 }
3087
3088 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00003089 MVT::ValueType NVT =
3090 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3091 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3092 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3093 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003094 break;
3095 }
Evan Cheng912095b2007-01-04 21:56:39 +00003096 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003097 break;
3098
Nate Begeman551bf3f2006-02-17 05:43:56 +00003099 case ISD::ADDC:
3100 case ISD::SUBC:
3101 Tmp1 = LegalizeOp(Node->getOperand(0));
3102 Tmp2 = LegalizeOp(Node->getOperand(1));
3103 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3104 // Since this produces two values, make sure to remember that we legalized
3105 // both of them.
3106 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3107 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3108 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003109
Nate Begeman551bf3f2006-02-17 05:43:56 +00003110 case ISD::ADDE:
3111 case ISD::SUBE:
3112 Tmp1 = LegalizeOp(Node->getOperand(0));
3113 Tmp2 = LegalizeOp(Node->getOperand(1));
3114 Tmp3 = LegalizeOp(Node->getOperand(2));
3115 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3116 // Since this produces two values, make sure to remember that we legalized
3117 // both of them.
3118 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3119 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3120 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003121
Nate Begeman419f8b62005-10-18 00:27:41 +00003122 case ISD::BUILD_PAIR: {
3123 MVT::ValueType PairTy = Node->getValueType(0);
3124 // TODO: handle the case where the Lo and Hi operands are not of legal type
3125 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3126 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3127 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003128 case TargetLowering::Promote:
3129 case TargetLowering::Custom:
3130 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003131 case TargetLowering::Legal:
3132 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3133 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3134 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003135 case TargetLowering::Expand:
3136 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3137 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3138 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3139 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3140 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003141 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003142 break;
3143 }
3144 break;
3145 }
3146
Nate Begemanc105e192005-04-06 00:23:54 +00003147 case ISD::UREM:
3148 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003149 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003150 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3151 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003152
Nate Begemanc105e192005-04-06 00:23:54 +00003153 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003154 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3155 case TargetLowering::Custom:
3156 isCustom = true;
3157 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003158 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003159 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003160 if (isCustom) {
3161 Tmp1 = TLI.LowerOperation(Result, DAG);
3162 if (Tmp1.Val) Result = Tmp1;
3163 }
Nate Begemanc105e192005-04-06 00:23:54 +00003164 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003165 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003166 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003167 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman525178c2007-10-08 18:33:35 +00003168 MVT::ValueType VT = Node->getValueType(0);
3169
3170 // See if remainder can be lowered using two-result operations.
3171 SDVTList VTs = DAG.getVTList(VT, VT);
3172 if (Node->getOpcode() == ISD::SREM &&
3173 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3174 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3175 break;
3176 }
3177 if (Node->getOpcode() == ISD::UREM &&
3178 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3179 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3180 break;
3181 }
3182
3183 if (MVT::isInteger(VT)) {
3184 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003185 TargetLowering::Legal) {
3186 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003187 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003188 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3189 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman80176312007-11-05 23:35:22 +00003190 } else if (MVT::isVector(VT)) {
3191 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003192 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003193 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003194 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003195 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3196 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003197 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003198 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003199 }
Dan Gohman0d974262007-11-06 22:11:54 +00003200 } else {
3201 assert(MVT::isFloatingPoint(VT) &&
3202 "remainder op must have integer or floating-point type");
Dan Gohman80176312007-11-05 23:35:22 +00003203 if (MVT::isVector(VT)) {
3204 Result = LegalizeOp(UnrollVectorOp(Op));
3205 } else {
3206 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003207 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3208 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman80176312007-11-05 23:35:22 +00003209 SDOperand Dummy;
3210 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3211 false/*sign irrelevant*/, Dummy);
3212 }
Nate Begemanc105e192005-04-06 00:23:54 +00003213 }
3214 break;
3215 }
Dan Gohman525178c2007-10-08 18:33:35 +00003216 }
Nate Begemanc105e192005-04-06 00:23:54 +00003217 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003218 case ISD::VAARG: {
3219 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3220 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3221
Chris Lattner5c62f332006-01-28 07:42:08 +00003222 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003223 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3224 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003225 case TargetLowering::Custom:
3226 isCustom = true;
3227 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003228 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003229 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3230 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003231 Tmp1 = Result.getValue(1);
3232
3233 if (isCustom) {
3234 Tmp2 = TLI.LowerOperation(Result, DAG);
3235 if (Tmp2.Val) {
3236 Result = LegalizeOp(Tmp2);
3237 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3238 }
3239 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003240 break;
3241 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003242 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3243 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003244 // Increment the pointer, VAList, to the next vaarg
3245 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3246 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3247 TLI.getPointerTy()));
3248 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003249 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003250 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003251 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003252 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003253 Result = LegalizeOp(Result);
3254 break;
3255 }
3256 }
3257 // Since VAARG produces two values, make sure to remember that we
3258 // legalized both of them.
3259 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00003260 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3261 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003262 }
3263
3264 case ISD::VACOPY:
3265 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3266 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3267 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3268
3269 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3270 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003271 case TargetLowering::Custom:
3272 isCustom = true;
3273 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003274 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003275 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3276 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003277 if (isCustom) {
3278 Tmp1 = TLI.LowerOperation(Result, DAG);
3279 if (Tmp1.Val) Result = Tmp1;
3280 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003281 break;
3282 case TargetLowering::Expand:
3283 // This defaults to loading a pointer from the input and storing it to the
3284 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003285 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3286 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3287 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VD, 0);
3288 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VS, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003289 break;
3290 }
3291 break;
3292
3293 case ISD::VAEND:
3294 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3295 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3296
3297 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3298 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003299 case TargetLowering::Custom:
3300 isCustom = true;
3301 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003302 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003303 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003304 if (isCustom) {
3305 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3306 if (Tmp1.Val) Result = Tmp1;
3307 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003308 break;
3309 case TargetLowering::Expand:
3310 Result = Tmp1; // Default to a no-op, return the chain
3311 break;
3312 }
3313 break;
3314
3315 case ISD::VASTART:
3316 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3317 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3318
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003319 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3320
Nate Begemanacc398c2006-01-25 18:21:52 +00003321 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3322 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003323 case TargetLowering::Legal: break;
3324 case TargetLowering::Custom:
3325 Tmp1 = TLI.LowerOperation(Result, DAG);
3326 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003327 break;
3328 }
3329 break;
3330
Nate Begeman35ef9132006-01-11 21:21:00 +00003331 case ISD::ROTL:
3332 case ISD::ROTR:
3333 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3334 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003335 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003336 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3337 default:
3338 assert(0 && "ROTL/ROTR legalize operation not supported");
3339 break;
3340 case TargetLowering::Legal:
3341 break;
3342 case TargetLowering::Custom:
3343 Tmp1 = TLI.LowerOperation(Result, DAG);
3344 if (Tmp1.Val) Result = Tmp1;
3345 break;
3346 case TargetLowering::Promote:
3347 assert(0 && "Do not know how to promote ROTL/ROTR");
3348 break;
3349 case TargetLowering::Expand:
3350 assert(0 && "Do not know how to expand ROTL/ROTR");
3351 break;
3352 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003353 break;
3354
Nate Begemand88fc032006-01-14 03:14:10 +00003355 case ISD::BSWAP:
3356 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3357 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003358 case TargetLowering::Custom:
3359 assert(0 && "Cannot custom legalize this yet!");
3360 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003361 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003362 break;
3363 case TargetLowering::Promote: {
3364 MVT::ValueType OVT = Tmp1.getValueType();
3365 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00003366 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00003367
Chris Lattner456a93a2006-01-28 07:39:30 +00003368 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3369 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3370 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3371 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3372 break;
3373 }
3374 case TargetLowering::Expand:
3375 Result = ExpandBSWAP(Tmp1);
3376 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003377 }
3378 break;
3379
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003380 case ISD::CTPOP:
3381 case ISD::CTTZ:
3382 case ISD::CTLZ:
3383 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3384 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003385 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003386 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003387 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003388 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003389 TargetLowering::Custom) {
3390 Tmp1 = TLI.LowerOperation(Result, DAG);
3391 if (Tmp1.Val) {
3392 Result = Tmp1;
3393 }
Scott Michel910b66d2007-07-30 21:00:31 +00003394 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003395 break;
3396 case TargetLowering::Promote: {
3397 MVT::ValueType OVT = Tmp1.getValueType();
3398 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003399
3400 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003401 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3402 // Perform the larger operation, then subtract if needed.
3403 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003404 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003405 case ISD::CTPOP:
3406 Result = Tmp1;
3407 break;
3408 case ISD::CTTZ:
3409 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00003410 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003411 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003412 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003413 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00003414 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003415 break;
3416 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003417 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003418 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003419 DAG.getConstant(MVT::getSizeInBits(NVT) -
3420 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003421 break;
3422 }
3423 break;
3424 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003425 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003426 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003427 break;
3428 }
3429 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003430
Chris Lattner2c8086f2005-04-02 05:00:07 +00003431 // Unary operators
3432 case ISD::FABS:
3433 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003434 case ISD::FSQRT:
3435 case ISD::FSIN:
3436 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003437 Tmp1 = LegalizeOp(Node->getOperand(0));
3438 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003439 case TargetLowering::Promote:
3440 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003441 isCustom = true;
3442 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003443 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003444 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003445 if (isCustom) {
3446 Tmp1 = TLI.LowerOperation(Result, DAG);
3447 if (Tmp1.Val) Result = Tmp1;
3448 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003449 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003450 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003451 switch (Node->getOpcode()) {
3452 default: assert(0 && "Unreachable!");
3453 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003454 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3455 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003456 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003457 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003458 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003459 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3460 MVT::ValueType VT = Node->getValueType(0);
3461 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003462 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00003463 ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003464 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3465 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003466 break;
3467 }
3468 case ISD::FSQRT:
3469 case ISD::FSIN:
3470 case ISD::FCOS: {
3471 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003472
3473 // Expand unsupported unary vector operators by unrolling them.
3474 if (MVT::isVector(VT)) {
3475 Result = LegalizeOp(UnrollVectorOp(Op));
3476 break;
3477 }
3478
Evan Cheng56966222007-01-12 02:11:51 +00003479 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003480 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003481 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003482 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3483 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003484 break;
3485 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003486 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3487 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003488 break;
3489 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003490 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3491 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003492 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003493 default: assert(0 && "Unreachable!");
3494 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003495 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003496 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3497 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003498 break;
3499 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003500 }
3501 break;
3502 }
3503 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003504 case ISD::FPOWI: {
Dan Gohman82669522007-10-11 23:57:53 +00003505 MVT::ValueType VT = Node->getValueType(0);
3506
3507 // Expand unsupported unary vector operators by unrolling them.
3508 if (MVT::isVector(VT)) {
3509 Result = LegalizeOp(UnrollVectorOp(Op));
3510 break;
3511 }
3512
3513 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003514 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3515 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003516 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003517 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3518 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003519 break;
3520 }
Chris Lattner35481892005-12-23 00:16:34 +00003521 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003522 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003523 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3524 Node->getValueType(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003525 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3526 // The input has to be a vector type, we have to either scalarize it, pack
3527 // it, or convert it based on whether the input vector type is legal.
3528 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesene5269622007-10-20 00:07:52 +00003529 int InIx = Node->getOperand(0).ResNo;
3530 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3531 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohman7f321562007-06-25 16:23:39 +00003532
3533 // Figure out if there is a simple type corresponding to this Vector
3534 // type. If so, convert to the vector type.
3535 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3536 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003537 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003538 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3539 LegalizeOp(Node->getOperand(0)));
3540 break;
3541 } else if (NumElems == 1) {
3542 // Turn this into a bit convert of the scalar input.
3543 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3544 ScalarizeVectorOp(Node->getOperand(0)));
3545 break;
3546 } else {
3547 // FIXME: UNIMP! Store then reload
3548 assert(0 && "Cast from unsupported vector type not implemented yet!");
3549 }
Chris Lattner67993f72006-01-23 07:30:46 +00003550 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003551 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3552 Node->getOperand(0).getValueType())) {
3553 default: assert(0 && "Unknown operation action!");
3554 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003555 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3556 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003557 break;
3558 case TargetLowering::Legal:
3559 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003560 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003561 break;
3562 }
3563 }
3564 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003565
Chris Lattner2c8086f2005-04-02 05:00:07 +00003566 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003567 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003568 case ISD::UINT_TO_FP: {
3569 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003570 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3571 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003572 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003573 Node->getOperand(0).getValueType())) {
3574 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003575 case TargetLowering::Custom:
3576 isCustom = true;
3577 // FALLTHROUGH
3578 case TargetLowering::Legal:
3579 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003580 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003581 if (isCustom) {
3582 Tmp1 = TLI.LowerOperation(Result, DAG);
3583 if (Tmp1.Val) Result = Tmp1;
3584 }
3585 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003586 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003587 Result = ExpandLegalINT_TO_FP(isSigned,
3588 LegalizeOp(Node->getOperand(0)),
3589 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003590 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003591 case TargetLowering::Promote:
3592 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3593 Node->getValueType(0),
3594 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003595 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003596 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003597 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003598 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003599 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3600 Node->getValueType(0), Node->getOperand(0));
3601 break;
3602 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003603 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003604 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003605 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3606 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003607 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003608 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3609 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003610 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003611 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3612 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003613 break;
3614 }
3615 break;
3616 }
3617 case ISD::TRUNCATE:
3618 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3619 case Legal:
3620 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003621 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003622 break;
3623 case Expand:
3624 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3625
3626 // Since the result is legal, we should just be able to truncate the low
3627 // part of the source.
3628 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3629 break;
3630 case Promote:
3631 Result = PromoteOp(Node->getOperand(0));
3632 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3633 break;
3634 }
3635 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003636
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003637 case ISD::FP_TO_SINT:
3638 case ISD::FP_TO_UINT:
3639 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3640 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003641 Tmp1 = LegalizeOp(Node->getOperand(0));
3642
Chris Lattner1618beb2005-07-29 00:11:56 +00003643 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3644 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003645 case TargetLowering::Custom:
3646 isCustom = true;
3647 // FALLTHROUGH
3648 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003649 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003650 if (isCustom) {
3651 Tmp1 = TLI.LowerOperation(Result, DAG);
3652 if (Tmp1.Val) Result = Tmp1;
3653 }
3654 break;
3655 case TargetLowering::Promote:
3656 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3657 Node->getOpcode() == ISD::FP_TO_SINT);
3658 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003659 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003660 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3661 SDOperand True, False;
3662 MVT::ValueType VT = Node->getOperand(0).getValueType();
3663 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00003664 const uint64_t zero[] = {0, 0};
3665 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003666 APInt x = APInt::getSignBit(MVT::getSizeInBits(NVT));
3667 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003668 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003669 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Nate Begemand2558e32005-08-14 01:20:53 +00003670 Node->getOperand(0), Tmp2, ISD::SETLT);
3671 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3672 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003673 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003674 Tmp2));
3675 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003676 DAG.getConstant(x, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003677 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3678 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003679 } else {
3680 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3681 }
3682 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003683 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003684 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003685 case Expand: {
Evan Cheng6af00d52006-12-13 01:57:55 +00003686 MVT::ValueType VT = Op.getValueType();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003687 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003688 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003689 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003690 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3691 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3692 Node->getOperand(0), DAG.getValueType(MVT::f64));
3693 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3694 DAG.getIntPtrConstant(1));
3695 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3696 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003697 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3698 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3699 Tmp2 = DAG.getConstantFP(apf, OVT);
3700 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3701 // FIXME: generated code sucks.
3702 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3703 DAG.getNode(ISD::ADD, MVT::i32,
3704 DAG.getNode(ISD::FP_TO_SINT, VT,
3705 DAG.getNode(ISD::FSUB, OVT,
3706 Node->getOperand(0), Tmp2)),
3707 DAG.getConstant(0x80000000, MVT::i32)),
3708 DAG.getNode(ISD::FP_TO_SINT, VT,
3709 Node->getOperand(0)),
3710 DAG.getCondCode(ISD::SETGE));
3711 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003712 break;
3713 }
Dan Gohmana2e94852008-03-10 23:03:31 +00003714 // Convert f32 / f64 to i32 / i64 / i128.
Evan Cheng56966222007-01-12 02:11:51 +00003715 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003716 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003717 case ISD::FP_TO_SINT: {
Dan Gohmana2e94852008-03-10 23:03:31 +00003718 if (VT == MVT::i32) {
3719 if (OVT == MVT::f32)
3720 LC = RTLIB::FPTOSINT_F32_I32;
3721 else if (OVT == MVT::f64)
3722 LC = RTLIB::FPTOSINT_F64_I32;
3723 else
3724 assert(0 && "Unexpected i32-to-fp conversion!");
3725 } else if (VT == MVT::i64) {
3726 if (OVT == MVT::f32)
3727 LC = RTLIB::FPTOSINT_F32_I64;
3728 else if (OVT == MVT::f64)
3729 LC = RTLIB::FPTOSINT_F64_I64;
3730 else if (OVT == MVT::f80)
3731 LC = RTLIB::FPTOSINT_F80_I64;
3732 else if (OVT == MVT::ppcf128)
3733 LC = RTLIB::FPTOSINT_PPCF128_I64;
3734 else
3735 assert(0 && "Unexpected i64-to-fp conversion!");
3736 } else if (VT == MVT::i128) {
3737 if (OVT == MVT::f32)
3738 LC = RTLIB::FPTOSINT_F32_I128;
3739 else if (OVT == MVT::f64)
3740 LC = RTLIB::FPTOSINT_F64_I128;
3741 else if (OVT == MVT::f80)
3742 LC = RTLIB::FPTOSINT_F80_I128;
3743 else if (OVT == MVT::ppcf128)
3744 LC = RTLIB::FPTOSINT_PPCF128_I128;
3745 else
3746 assert(0 && "Unexpected i128-to-fp conversion!");
3747 } else {
3748 assert(0 && "Unexpectd int-to-fp conversion!");
Dale Johannesen73328d12007-09-19 23:55:34 +00003749 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003750 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003751 }
3752 case ISD::FP_TO_UINT: {
Dan Gohmana2e94852008-03-10 23:03:31 +00003753 if (VT == MVT::i32) {
3754 if (OVT == MVT::f32)
3755 LC = RTLIB::FPTOUINT_F32_I32;
3756 else if (OVT == MVT::f64)
3757 LC = RTLIB::FPTOUINT_F64_I32;
3758 else if (OVT == MVT::f80)
3759 LC = RTLIB::FPTOUINT_F80_I32;
3760 else
3761 assert(0 && "Unexpected i32-to-fp conversion!");
3762 } else if (VT == MVT::i64) {
3763 if (OVT == MVT::f32)
3764 LC = RTLIB::FPTOUINT_F32_I64;
3765 else if (OVT == MVT::f64)
3766 LC = RTLIB::FPTOUINT_F64_I64;
3767 else if (OVT == MVT::f80)
3768 LC = RTLIB::FPTOUINT_F80_I64;
3769 else if (OVT == MVT::ppcf128)
3770 LC = RTLIB::FPTOUINT_PPCF128_I64;
3771 else
3772 assert(0 && "Unexpected i64-to-fp conversion!");
3773 } else if (VT == MVT::i128) {
3774 if (OVT == MVT::f32)
3775 LC = RTLIB::FPTOUINT_F32_I128;
3776 else if (OVT == MVT::f64)
3777 LC = RTLIB::FPTOUINT_F64_I128;
3778 else if (OVT == MVT::f80)
3779 LC = RTLIB::FPTOUINT_F80_I128;
3780 else if (OVT == MVT::ppcf128)
3781 LC = RTLIB::FPTOUINT_PPCF128_I128;
3782 else
3783 assert(0 && "Unexpected i128-to-fp conversion!");
3784 } else {
3785 assert(0 && "Unexpectd int-to-fp conversion!");
Dale Johannesen73328d12007-09-19 23:55:34 +00003786 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003787 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003788 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003789 default: assert(0 && "Unreachable!");
3790 }
3791 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003792 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3793 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003794 break;
3795 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003796 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003797 Tmp1 = PromoteOp(Node->getOperand(0));
3798 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3799 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003800 break;
3801 }
3802 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003803
Chris Lattnerf2670a82008-01-16 06:57:07 +00003804 case ISD::FP_EXTEND: {
Chris Lattner0bd48932008-01-17 07:00:52 +00003805 MVT::ValueType DstVT = Op.getValueType();
3806 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3807 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3808 // The only other way we can lower this is to turn it into a STORE,
3809 // LOAD pair, targetting a temporary location (a stack slot).
3810 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3811 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00003812 }
3813 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3814 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3815 case Legal:
3816 Tmp1 = LegalizeOp(Node->getOperand(0));
3817 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3818 break;
3819 case Promote:
3820 Tmp1 = PromoteOp(Node->getOperand(0));
3821 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3822 break;
3823 }
3824 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003825 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003826 case ISD::FP_ROUND: {
Chris Lattner0bd48932008-01-17 07:00:52 +00003827 MVT::ValueType DstVT = Op.getValueType();
3828 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3829 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3830 if (SrcVT == MVT::ppcf128) {
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003831 SDOperand Lo;
3832 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00003833 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003834 if (DstVT!=MVT::f64)
3835 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00003836 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00003837 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003838 // The only other way we can lower this is to turn it into a STORE,
3839 // LOAD pair, targetting a temporary location (a stack slot).
3840 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3841 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00003842 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00003843 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3844 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3845 case Legal:
3846 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003847 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003848 break;
3849 case Promote:
3850 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003851 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3852 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003853 break;
3854 }
3855 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003856 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003857 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003858 case ISD::ZERO_EXTEND:
3859 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003860 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003861 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003862 case Legal:
3863 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel0123b7d2008-02-15 23:05:48 +00003864 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3865 TargetLowering::Custom) {
3866 Tmp2 = TLI.LowerOperation(Result, DAG);
3867 if (Tmp2.Val) {
3868 Tmp1 = Tmp2;
3869 }
3870 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003871 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003872 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003873 case Promote:
3874 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003875 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003876 Tmp1 = PromoteOp(Node->getOperand(0));
3877 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003878 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003879 case ISD::ZERO_EXTEND:
3880 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003881 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003882 Result = DAG.getZeroExtendInReg(Result,
3883 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003884 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003885 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003886 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003887 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003888 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003889 Result,
3890 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003891 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003892 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003893 }
3894 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003895 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003896 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003897 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003898 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003899
3900 // If this operation is not supported, convert it to a shl/shr or load/store
3901 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003902 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3903 default: assert(0 && "This action not supported for this op yet!");
3904 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003905 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003906 break;
3907 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003908 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003909 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003910 // NOTE: we could fall back on load/store here too for targets without
3911 // SAR. However, it is doubtful that any exist.
3912 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3913 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003914 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003915 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3916 Node->getOperand(0), ShiftCst);
3917 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3918 Result, ShiftCst);
3919 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003920 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003921 // EXTLOAD pair, targetting a temporary location (a stack slot).
3922
3923 // NOTE: there is a choice here between constantly creating new stack
3924 // slots and always reusing the same one. We currently always create
3925 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00003926 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3927 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00003928 } else {
3929 assert(0 && "Unknown op");
3930 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003931 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003932 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003933 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003934 }
Duncan Sands36397f52007-07-27 12:58:54 +00003935 case ISD::TRAMPOLINE: {
3936 SDOperand Ops[6];
3937 for (unsigned i = 0; i != 6; ++i)
3938 Ops[i] = LegalizeOp(Node->getOperand(i));
3939 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3940 // The only option for this node is to custom lower it.
3941 Result = TLI.LowerOperation(Result, DAG);
3942 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00003943
3944 // Since trampoline produces two values, make sure to remember that we
3945 // legalized both of them.
3946 Tmp1 = LegalizeOp(Result.getValue(1));
3947 Result = LegalizeOp(Result);
3948 AddLegalizedOperand(SDOperand(Node, 0), Result);
3949 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3950 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00003951 }
Dan Gohman1a024862008-01-31 00:41:03 +00003952 case ISD::FLT_ROUNDS_: {
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00003953 MVT::ValueType VT = Node->getValueType(0);
3954 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3955 default: assert(0 && "This action not supported for this op yet!");
3956 case TargetLowering::Custom:
3957 Result = TLI.LowerOperation(Op, DAG);
3958 if (Result.Val) break;
3959 // Fall Thru
3960 case TargetLowering::Legal:
3961 // If this operation is not supported, lower it to constant 1
3962 Result = DAG.getConstant(1, VT);
3963 break;
3964 }
3965 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003966 case ISD::TRAP: {
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003967 MVT::ValueType VT = Node->getValueType(0);
3968 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3969 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00003970 case TargetLowering::Legal:
3971 Tmp1 = LegalizeOp(Node->getOperand(0));
3972 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3973 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003974 case TargetLowering::Custom:
3975 Result = TLI.LowerOperation(Op, DAG);
3976 if (Result.Val) break;
3977 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00003978 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003979 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00003980 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003981 TargetLowering::ArgListTy Args;
3982 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00003983 TLI.LowerCallTo(Tmp1, Type::VoidTy,
3984 false, false, false, CallingConv::C, false,
Chris Lattner034f12e2008-01-15 22:09:33 +00003985 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3986 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003987 Result = CallResult.second;
3988 break;
3989 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00003990 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00003991 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003992 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003993
Chris Lattner4ddd2832006-04-08 04:13:17 +00003994 assert(Result.getValueType() == Op.getValueType() &&
3995 "Bad legalization!");
3996
Chris Lattner456a93a2006-01-28 07:39:30 +00003997 // Make sure that the generated code is itself legal.
3998 if (Result != Op)
3999 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004000
Chris Lattner45982da2005-05-12 16:53:42 +00004001 // Note that LegalizeOp may be reentered even from single-use nodes, which
4002 // means that we always must cache transformed nodes.
4003 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004004 return Result;
4005}
4006
Chris Lattner8b6fa222005-01-15 22:16:26 +00004007/// PromoteOp - Given an operation that produces a value in an invalid type,
4008/// promote it to compute the value into a larger type. The produced value will
4009/// have the correct bits for the low portion of the register, but no guarantee
4010/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00004011SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
4012 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004013 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004014 assert(getTypeAction(VT) == Promote &&
4015 "Caller should expand or legalize operands that are not promotable!");
4016 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
4017 "Cannot promote to smaller type!");
4018
Chris Lattner03c85462005-01-15 05:21:40 +00004019 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00004020 SDOperand Result;
4021 SDNode *Node = Op.Val;
4022
Roman Levensteindc1adac2008-04-07 10:06:32 +00004023 DenseMap<SDOperandImpl, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004024 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004025
Chris Lattner03c85462005-01-15 05:21:40 +00004026 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004027 case ISD::CopyFromReg:
4028 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004029 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004030#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004031 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004032#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004033 assert(0 && "Do not know how to promote this operator!");
4034 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004035 case ISD::UNDEF:
4036 Result = DAG.getNode(ISD::UNDEF, NVT);
4037 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004038 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004039 if (VT != MVT::i1)
4040 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4041 else
4042 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004043 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4044 break;
4045 case ISD::ConstantFP:
4046 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4047 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4048 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004049
Chris Lattner82fbfb62005-01-18 02:59:52 +00004050 case ISD::SETCC:
Scott Michel5b8f82e2008-03-10 15:42:14 +00004051 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman5922f562008-03-14 00:53:31 +00004052 && "SetCC type is not legal??");
Scott Michel5b8f82e2008-03-10 15:42:14 +00004053 Result = DAG.getNode(ISD::SETCC,
Nate Begeman5922f562008-03-14 00:53:31 +00004054 TLI.getSetCCResultType(Node->getOperand(0)),
4055 Node->getOperand(0), Node->getOperand(1),
4056 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004057 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00004058
Chris Lattner03c85462005-01-15 05:21:40 +00004059 case ISD::TRUNCATE:
4060 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4061 case Legal:
4062 Result = LegalizeOp(Node->getOperand(0));
4063 assert(Result.getValueType() >= NVT &&
4064 "This truncation doesn't make sense!");
4065 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4066 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4067 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004068 case Promote:
4069 // The truncation is not required, because we don't guarantee anything
4070 // about high bits anyway.
4071 Result = PromoteOp(Node->getOperand(0));
4072 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004073 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004074 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4075 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004076 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004077 }
4078 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004079 case ISD::SIGN_EXTEND:
4080 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004081 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004082 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4083 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4084 case Legal:
4085 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004086 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004087 break;
4088 case Promote:
4089 // Promote the reg if it's smaller.
4090 Result = PromoteOp(Node->getOperand(0));
4091 // The high bits are not guaranteed to be anything. Insert an extend.
4092 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004093 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004094 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004095 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004096 Result = DAG.getZeroExtendInReg(Result,
4097 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004098 break;
4099 }
4100 break;
Chris Lattner35481892005-12-23 00:16:34 +00004101 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004102 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4103 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004104 Result = PromoteOp(Result);
4105 break;
4106
Chris Lattner8b6fa222005-01-15 22:16:26 +00004107 case ISD::FP_EXTEND:
4108 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4109 case ISD::FP_ROUND:
4110 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4111 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4112 case Promote: assert(0 && "Unreachable with 2 FP types!");
4113 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004114 if (Node->getConstantOperandVal(1) == 0) {
4115 // Input is legal? Do an FP_ROUND_INREG.
4116 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4117 DAG.getValueType(VT));
4118 } else {
4119 // Just remove the truncate, it isn't affecting the value.
4120 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4121 Node->getOperand(1));
4122 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004123 break;
4124 }
4125 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004126 case ISD::SINT_TO_FP:
4127 case ISD::UINT_TO_FP:
4128 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4129 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004130 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004131 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004132 break;
4133
4134 case Promote:
4135 Result = PromoteOp(Node->getOperand(0));
4136 if (Node->getOpcode() == ISD::SINT_TO_FP)
4137 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004138 Result,
4139 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004140 else
Chris Lattner23993562005-04-13 02:38:47 +00004141 Result = DAG.getZeroExtendInReg(Result,
4142 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004143 // No extra round required here.
4144 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004145 break;
4146 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004147 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4148 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004149 // Round if we cannot tolerate excess precision.
4150 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004151 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4152 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004153 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004154 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004155 break;
4156
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004157 case ISD::SIGN_EXTEND_INREG:
4158 Result = PromoteOp(Node->getOperand(0));
4159 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4160 Node->getOperand(1));
4161 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004162 case ISD::FP_TO_SINT:
4163 case ISD::FP_TO_UINT:
4164 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4165 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004166 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004167 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004168 break;
4169 case Promote:
4170 // The input result is prerounded, so we don't have to do anything
4171 // special.
4172 Tmp1 = PromoteOp(Node->getOperand(0));
4173 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004174 }
Nate Begemand2558e32005-08-14 01:20:53 +00004175 // If we're promoting a UINT to a larger size, check to see if the new node
4176 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4177 // we can use that instead. This allows us to generate better code for
4178 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4179 // legal, such as PowerPC.
4180 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004181 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004182 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4183 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004184 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4185 } else {
4186 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4187 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004188 break;
4189
Chris Lattner2c8086f2005-04-02 05:00:07 +00004190 case ISD::FABS:
4191 case ISD::FNEG:
4192 Tmp1 = PromoteOp(Node->getOperand(0));
4193 assert(Tmp1.getValueType() == NVT);
4194 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4195 // NOTE: we do not have to do any extra rounding here for
4196 // NoExcessFPPrecision, because we know the input will have the appropriate
4197 // precision, and these operations don't modify precision at all.
4198 break;
4199
Chris Lattnerda6ba872005-04-28 21:44:33 +00004200 case ISD::FSQRT:
4201 case ISD::FSIN:
4202 case ISD::FCOS:
4203 Tmp1 = PromoteOp(Node->getOperand(0));
4204 assert(Tmp1.getValueType() == NVT);
4205 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004206 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004207 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4208 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004209 break;
4210
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004211 case ISD::FPOWI: {
4212 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4213 // directly as well, which may be better.
4214 Tmp1 = PromoteOp(Node->getOperand(0));
4215 assert(Tmp1.getValueType() == NVT);
4216 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4217 if (NoExcessFPPrecision)
4218 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4219 DAG.getValueType(VT));
4220 break;
4221 }
4222
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004223 case ISD::ATOMIC_LCS: {
4224 Tmp2 = PromoteOp(Node->getOperand(2));
4225 Tmp3 = PromoteOp(Node->getOperand(3));
4226 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4227 Node->getOperand(1), Tmp2, Tmp3,
4228 cast<AtomicSDNode>(Node)->getVT());
4229 // Remember that we legalized the chain.
4230 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4231 break;
4232 }
4233 case ISD::ATOMIC_LAS:
4234 case ISD::ATOMIC_SWAP: {
4235 Tmp2 = PromoteOp(Node->getOperand(2));
4236 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4237 Node->getOperand(1), Tmp2,
4238 cast<AtomicSDNode>(Node)->getVT());
4239 // Remember that we legalized the chain.
4240 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4241 break;
4242 }
4243
Chris Lattner03c85462005-01-15 05:21:40 +00004244 case ISD::AND:
4245 case ISD::OR:
4246 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004247 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004248 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004249 case ISD::MUL:
4250 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004251 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004252 // that too is okay if they are integer operations.
4253 Tmp1 = PromoteOp(Node->getOperand(0));
4254 Tmp2 = PromoteOp(Node->getOperand(1));
4255 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4256 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004257 break;
4258 case ISD::FADD:
4259 case ISD::FSUB:
4260 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004261 Tmp1 = PromoteOp(Node->getOperand(0));
4262 Tmp2 = PromoteOp(Node->getOperand(1));
4263 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4264 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4265
4266 // Floating point operations will give excess precision that we may not be
4267 // able to tolerate. If we DO allow excess precision, just leave it,
4268 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004269 // FIXME: Why would we need to round FP ops more than integer ones?
4270 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004271 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004272 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4273 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004274 break;
4275
Chris Lattner8b6fa222005-01-15 22:16:26 +00004276 case ISD::SDIV:
4277 case ISD::SREM:
4278 // These operators require that their input be sign extended.
4279 Tmp1 = PromoteOp(Node->getOperand(0));
4280 Tmp2 = PromoteOp(Node->getOperand(1));
4281 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004282 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4283 DAG.getValueType(VT));
4284 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4285 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004286 }
4287 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4288
4289 // Perform FP_ROUND: this is probably overly pessimistic.
4290 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004291 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4292 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004293 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004294 case ISD::FDIV:
4295 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004296 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004297 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004298 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004299 case Expand: assert(0 && "not implemented");
4300 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4301 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004302 }
4303 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004304 case Expand: assert(0 && "not implemented");
4305 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4306 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004307 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004308 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4309
4310 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004311 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004312 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4313 DAG.getValueType(VT));
4314 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004315
4316 case ISD::UDIV:
4317 case ISD::UREM:
4318 // These operators require that their input be zero extended.
4319 Tmp1 = PromoteOp(Node->getOperand(0));
4320 Tmp2 = PromoteOp(Node->getOperand(1));
4321 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004322 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4323 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004324 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4325 break;
4326
4327 case ISD::SHL:
4328 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004329 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004330 break;
4331 case ISD::SRA:
4332 // The input value must be properly sign extended.
4333 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004334 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4335 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004336 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004337 break;
4338 case ISD::SRL:
4339 // The input value must be properly zero extended.
4340 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004341 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004342 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004343 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004344
4345 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004346 Tmp1 = Node->getOperand(0); // Get the chain.
4347 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004348 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4349 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4350 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4351 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004352 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4353 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004354 // Increment the pointer, VAList, to the next vaarg
4355 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4356 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4357 TLI.getPointerTy()));
4358 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004359 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004360 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004361 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004362 }
4363 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004364 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004365 break;
4366
Evan Cheng466685d2006-10-09 20:57:25 +00004367 case ISD::LOAD: {
4368 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004369 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4370 ? ISD::EXTLOAD : LD->getExtensionType();
4371 Result = DAG.getExtLoad(ExtType, NVT,
4372 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004373 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004374 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004375 LD->isVolatile(),
4376 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004377 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004378 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004379 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004380 }
Chris Lattner03c85462005-01-15 05:21:40 +00004381 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00004382 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4383 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00004384 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004385 break;
Nate Begeman9373a812005-08-10 20:51:12 +00004386 case ISD::SELECT_CC:
4387 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4388 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4389 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004390 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004391 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004392 case ISD::BSWAP:
4393 Tmp1 = Node->getOperand(0);
4394 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4395 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4396 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004397 DAG.getConstant(MVT::getSizeInBits(NVT) -
4398 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00004399 TLI.getShiftAmountTy()));
4400 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004401 case ISD::CTPOP:
4402 case ISD::CTTZ:
4403 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004404 // Zero extend the argument
4405 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004406 // Perform the larger operation, then subtract if needed.
4407 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004408 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004409 case ISD::CTPOP:
4410 Result = Tmp1;
4411 break;
4412 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004413 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004414 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004415 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4416 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004417 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004418 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004419 break;
4420 case ISD::CTLZ:
4421 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004422 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004423 DAG.getConstant(MVT::getSizeInBits(NVT) -
4424 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004425 break;
4426 }
4427 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004428 case ISD::EXTRACT_SUBVECTOR:
4429 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004430 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004431 case ISD::EXTRACT_VECTOR_ELT:
4432 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4433 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004434 }
4435
4436 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004437
4438 // Make sure the result is itself legal.
4439 Result = LegalizeOp(Result);
4440
4441 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004442 AddPromotedOperand(Op, Result);
4443 return Result;
4444}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004445
Dan Gohman7f321562007-06-25 16:23:39 +00004446/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4447/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4448/// based on the vector type. The return type of this matches the element type
4449/// of the vector, which may not be legal for the target.
4450SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004451 // We know that operand #0 is the Vec vector. If the index is a constant
4452 // or if the invec is a supported hardware type, we can use it. Otherwise,
4453 // lower to a store then an indexed load.
4454 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004455 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004456
Dan Gohmane40c7b02007-09-24 15:54:53 +00004457 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00004458 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00004459
Dan Gohman7f321562007-06-25 16:23:39 +00004460 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4461 default: assert(0 && "This action is not supported yet!");
4462 case TargetLowering::Custom: {
4463 Vec = LegalizeOp(Vec);
4464 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4465 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4466 if (Tmp3.Val)
4467 return Tmp3;
4468 break;
4469 }
4470 case TargetLowering::Legal:
4471 if (isTypeLegal(TVT)) {
4472 Vec = LegalizeOp(Vec);
4473 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004474 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004475 }
4476 break;
4477 case TargetLowering::Expand:
4478 break;
4479 }
4480
4481 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004482 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004483 Op = ScalarizeVectorOp(Vec);
4484 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00004485 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00004486 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00004487 SDOperand Lo, Hi;
4488 SplitVectorOp(Vec, Lo, Hi);
Nate Begeman55030dc2008-01-29 02:24:00 +00004489 if (CIdx->getValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00004490 Vec = Lo;
4491 } else {
4492 Vec = Hi;
Nate Begeman55030dc2008-01-29 02:24:00 +00004493 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00004494 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004495 }
Dan Gohman7f321562007-06-25 16:23:39 +00004496
Chris Lattner15972212006-03-31 17:55:51 +00004497 // It's now an extract from the appropriate high or low part. Recurse.
4498 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004499 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004500 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004501 // Store the value to a temporary stack slot, then LOAD the scalar
4502 // element back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004503 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00004504 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4505
4506 // Add the offset to the index.
4507 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4508 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4509 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004510
4511 if (MVT::getSizeInBits(Idx.getValueType()) >
4512 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004513 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004514 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004515 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004516
Dan Gohman7f321562007-06-25 16:23:39 +00004517 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4518
4519 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004520 }
Dan Gohman7f321562007-06-25 16:23:39 +00004521 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004522}
4523
Dan Gohman7f321562007-06-25 16:23:39 +00004524/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004525/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00004526SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004527 // We know that operand #0 is the Vec vector. For now we assume the index
4528 // is a constant and that the extracted result is a supported hardware type.
4529 SDOperand Vec = Op.getOperand(0);
4530 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4531
Dan Gohman7f321562007-06-25 16:23:39 +00004532 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004533
4534 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4535 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004536 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004537 }
4538
4539 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4540 SDOperand Lo, Hi;
4541 SplitVectorOp(Vec, Lo, Hi);
4542 if (CIdx->getValue() < NumElems/2) {
4543 Vec = Lo;
4544 } else {
4545 Vec = Hi;
4546 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4547 }
4548
4549 // It's now an extract from the appropriate high or low part. Recurse.
4550 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004551 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004552}
4553
Nate Begeman750ac1b2006-02-01 07:19:44 +00004554/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4555/// with condition CC on the current target. This usually involves legalizing
4556/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4557/// there may be no choice but to create a new SetCC node to represent the
4558/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4559/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4560void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4561 SDOperand &RHS,
4562 SDOperand &CC) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00004563 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004564
4565 switch (getTypeAction(LHS.getValueType())) {
4566 case Legal:
4567 Tmp1 = LegalizeOp(LHS); // LHS
4568 Tmp2 = LegalizeOp(RHS); // RHS
4569 break;
4570 case Promote:
4571 Tmp1 = PromoteOp(LHS); // LHS
4572 Tmp2 = PromoteOp(RHS); // RHS
4573
4574 // If this is an FP compare, the operands have already been extended.
4575 if (MVT::isInteger(LHS.getValueType())) {
4576 MVT::ValueType VT = LHS.getValueType();
4577 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4578
4579 // Otherwise, we have to insert explicit sign or zero extends. Note
4580 // that we could insert sign extends for ALL conditions, but zero extend
4581 // is cheaper on many machines (an AND instead of two shifts), so prefer
4582 // it.
4583 switch (cast<CondCodeSDNode>(CC)->get()) {
4584 default: assert(0 && "Unknown integer comparison!");
4585 case ISD::SETEQ:
4586 case ISD::SETNE:
4587 case ISD::SETUGE:
4588 case ISD::SETUGT:
4589 case ISD::SETULE:
4590 case ISD::SETULT:
4591 // ALL of these operations will work if we either sign or zero extend
4592 // the operands (including the unsigned comparisons!). Zero extend is
4593 // usually a simpler/cheaper operation, so prefer it.
4594 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4595 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4596 break;
4597 case ISD::SETGE:
4598 case ISD::SETGT:
4599 case ISD::SETLT:
4600 case ISD::SETLE:
4601 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4602 DAG.getValueType(VT));
4603 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4604 DAG.getValueType(VT));
4605 break;
4606 }
4607 }
4608 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004609 case Expand: {
4610 MVT::ValueType VT = LHS.getValueType();
4611 if (VT == MVT::f32 || VT == MVT::f64) {
4612 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004613 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004614 switch (cast<CondCodeSDNode>(CC)->get()) {
4615 case ISD::SETEQ:
4616 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004617 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004618 break;
4619 case ISD::SETNE:
4620 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004621 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004622 break;
4623 case ISD::SETGE:
4624 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004625 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004626 break;
4627 case ISD::SETLT:
4628 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004629 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004630 break;
4631 case ISD::SETLE:
4632 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004633 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004634 break;
4635 case ISD::SETGT:
4636 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004637 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004638 break;
4639 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004640 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4641 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004642 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004643 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004644 break;
4645 default:
Evan Cheng56966222007-01-12 02:11:51 +00004646 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004647 switch (cast<CondCodeSDNode>(CC)->get()) {
4648 case ISD::SETONE:
4649 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004650 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004651 // Fallthrough
4652 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004653 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004654 break;
4655 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004656 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004657 break;
4658 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004659 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004660 break;
4661 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004662 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004663 break;
Evan Cheng56966222007-01-12 02:11:51 +00004664 case ISD::SETUEQ:
4665 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004666 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004667 default: assert(0 && "Unsupported FP setcc!");
4668 }
4669 }
4670
4671 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004672 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004673 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004674 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004675 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004676 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004677 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00004678 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00004679 CC);
Evan Cheng56966222007-01-12 02:11:51 +00004680 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004681 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004682 false /*sign irrelevant*/, Dummy);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004683 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004684 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004685 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4686 Tmp2 = SDOperand();
4687 }
4688 LHS = Tmp1;
4689 RHS = Tmp2;
4690 return;
4691 }
4692
Nate Begeman750ac1b2006-02-01 07:19:44 +00004693 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4694 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004695 ExpandOp(RHS, RHSLo, RHSHi);
4696 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4697
4698 if (VT==MVT::ppcf128) {
4699 // FIXME: This generated code sucks. We want to generate
4700 // FCMP crN, hi1, hi2
4701 // BNE crN, L:
4702 // FCMP crN, lo1, lo2
4703 // The following can be improved, but not that much.
Scott Michel5b8f82e2008-03-10 15:42:14 +00004704 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
4705 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004706 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004707 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
4708 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004709 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4710 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4711 Tmp2 = SDOperand();
4712 break;
4713 }
4714
4715 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004716 case ISD::SETEQ:
4717 case ISD::SETNE:
4718 if (RHSLo == RHSHi)
4719 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4720 if (RHSCST->isAllOnesValue()) {
4721 // Comparison to -1.
4722 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4723 Tmp2 = RHSLo;
4724 break;
4725 }
4726
4727 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4728 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4729 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4730 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4731 break;
4732 default:
4733 // If this is a comparison of the sign bit, just look at the top part.
4734 // X > -1, x < 0
4735 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4736 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004737 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00004738 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4739 CST->isAllOnesValue())) { // X > -1
4740 Tmp1 = LHSHi;
4741 Tmp2 = RHSHi;
4742 break;
4743 }
4744
4745 // FIXME: This generated code sucks.
4746 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004747 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004748 default: assert(0 && "Unknown integer setcc!");
4749 case ISD::SETLT:
4750 case ISD::SETULT: LowCC = ISD::SETULT; break;
4751 case ISD::SETGT:
4752 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4753 case ISD::SETLE:
4754 case ISD::SETULE: LowCC = ISD::SETULE; break;
4755 case ISD::SETGE:
4756 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4757 }
4758
4759 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4760 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4761 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4762
4763 // NOTE: on targets without efficient SELECT of bools, we can always use
4764 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004765 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004766 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman5922f562008-03-14 00:53:31 +00004767 LowCC, false, DagCombineInfo);
Evan Cheng2e677812007-02-08 22:16:19 +00004768 if (!Tmp1.Val)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004769 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
4770 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00004771 CCCode, false, DagCombineInfo);
4772 if (!Tmp2.Val)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004773 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00004774 RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004775
4776 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4777 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
Dan Gohman002e5d02008-03-13 22:13:53 +00004778 if ((Tmp1C && Tmp1C->isNullValue()) ||
4779 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00004780 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4781 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00004782 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00004783 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4784 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4785 // low part is known false, returns high part.
4786 // For LE / GE, if high part is known false, ignore the low part.
4787 // For LT / GT, if high part is known true, ignore the low part.
4788 Tmp1 = Tmp2;
4789 Tmp2 = SDOperand();
4790 } else {
Scott Michel5b8f82e2008-03-10 15:42:14 +00004791 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00004792 ISD::SETEQ, false, DagCombineInfo);
4793 if (!Result.Val)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004794 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00004795 ISD::SETEQ);
Evan Cheng2e677812007-02-08 22:16:19 +00004796 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4797 Result, Tmp1, Tmp2));
4798 Tmp1 = Result;
4799 Tmp2 = SDOperand();
4800 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004801 }
4802 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004803 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004804 LHS = Tmp1;
4805 RHS = Tmp2;
4806}
4807
Chris Lattner1401d152008-01-16 07:45:30 +00004808/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4809/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4810/// a load from the stack slot to DestVT, extending it if needed.
4811/// The resultant code need not be legal.
4812SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4813 MVT::ValueType SlotVT,
4814 MVT::ValueType DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00004815 // Create the stack frame object.
Chris Lattner1401d152008-01-16 07:45:30 +00004816 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4817
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00004818 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00004819 int SPFI = StackPtrFI->getIndex();
4820
Chris Lattner1401d152008-01-16 07:45:30 +00004821 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4822 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4823 unsigned DestSize = MVT::getSizeInBits(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004824
Chris Lattner1401d152008-01-16 07:45:30 +00004825 // Emit a store to the stack slot. Use a truncstore if the input value is
4826 // later than DestVT.
4827 SDOperand Store;
4828 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00004829 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004830 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00004831 SPFI, SlotVT);
Chris Lattner1401d152008-01-16 07:45:30 +00004832 else {
4833 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00004834 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004835 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00004836 SPFI, SlotVT);
Chris Lattner1401d152008-01-16 07:45:30 +00004837 }
4838
Chris Lattner35481892005-12-23 00:16:34 +00004839 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00004840 if (SlotSize == DestSize)
4841 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4842
4843 assert(SlotSize < DestSize && "Unknown extension!");
4844 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Chris Lattner35481892005-12-23 00:16:34 +00004845}
4846
Chris Lattner4352cc92006-04-04 17:23:26 +00004847SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4848 // Create a vector sized/aligned stack slot, store the value to element #0,
4849 // then load the whole vector back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004850 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00004851
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00004852 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00004853 int SPFI = StackPtrFI->getIndex();
4854
Evan Cheng786225a2006-10-05 23:01:46 +00004855 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004856 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohman69de1932008-02-06 22:27:42 +00004857 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004858 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner4352cc92006-04-04 17:23:26 +00004859}
4860
4861
Chris Lattnerce872152006-03-19 06:31:19 +00004862/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004863/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004864SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4865
4866 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004867 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004868 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004869 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004870 SDOperand SplatValue = Node->getOperand(0);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004871
4872 // FIXME: it would be far nicer to change this into map<SDOperand,uint64_t>
4873 // and use a bitmask instead of a list of elements.
Evan Cheng033e6812006-03-24 01:17:21 +00004874 std::map<SDOperand, std::vector<unsigned> > Values;
4875 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004876 bool isConstant = true;
4877 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4878 SplatValue.getOpcode() != ISD::UNDEF)
4879 isConstant = false;
4880
Evan Cheng033e6812006-03-24 01:17:21 +00004881 for (unsigned i = 1; i < NumElems; ++i) {
4882 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004883 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004884 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004885 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004886 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004887 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004888
4889 // If this isn't a constant element or an undef, we can't use a constant
4890 // pool load.
4891 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4892 V.getOpcode() != ISD::UNDEF)
4893 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004894 }
4895
4896 if (isOnlyLowElement) {
4897 // If the low element is an undef too, then this whole things is an undef.
4898 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4899 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4900 // Otherwise, turn this into a scalar_to_vector node.
4901 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4902 Node->getOperand(0));
4903 }
4904
Chris Lattner2eb86532006-03-24 07:29:17 +00004905 // If all elements are constants, create a load from the constant pool.
4906 if (isConstant) {
4907 MVT::ValueType VT = Node->getValueType(0);
4908 const Type *OpNTy =
4909 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4910 std::vector<Constant*> CV;
4911 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4912 if (ConstantFPSDNode *V =
4913 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00004914 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004915 } else if (ConstantSDNode *V =
4916 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004917 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004918 } else {
4919 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4920 CV.push_back(UndefValue::get(OpNTy));
4921 }
4922 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004923 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004924 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00004925 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004926 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004927 }
4928
Chris Lattner87100e02006-03-20 01:52:29 +00004929 if (SplatValue.Val) { // Splat of one value?
4930 // Build the shuffle constant vector: <0, 0, 0, 0>
4931 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004932 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004933 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004934 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004935 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4936 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004937
4938 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004939 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004940 // Get the splatted value into the low element of a vector register.
4941 SDOperand LowValVec =
4942 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4943
4944 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4945 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4946 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4947 SplatMask);
4948 }
4949 }
4950
Evan Cheng033e6812006-03-24 01:17:21 +00004951 // If there are only two unique elements, we may be able to turn this into a
4952 // vector shuffle.
4953 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004954 // Get the two values in deterministic order.
4955 SDOperand Val1 = Node->getOperand(1);
4956 SDOperand Val2;
4957 std::map<SDOperand, std::vector<unsigned> >::iterator MI = Values.begin();
4958 if (MI->first != Val1)
4959 Val2 = MI->first;
4960 else
4961 Val2 = (++MI)->first;
4962
4963 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
4964 // vector shuffle has the undef vector on the RHS.
4965 if (Val1.getOpcode() == ISD::UNDEF)
4966 std::swap(Val1, Val2);
4967
Evan Cheng033e6812006-03-24 01:17:21 +00004968 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004969 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
4970 MVT::ValueType MaskEltVT = MVT::getVectorElementType(MaskVT);
Evan Cheng033e6812006-03-24 01:17:21 +00004971 std::vector<SDOperand> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004972
4973 // Set elements of the shuffle mask for Val1.
4974 std::vector<unsigned> &Val1Elts = Values[Val1];
4975 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
4976 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
4977
4978 // Set elements of the shuffle mask for Val2.
4979 std::vector<unsigned> &Val2Elts = Values[Val2];
4980 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
4981 if (Val2.getOpcode() != ISD::UNDEF)
4982 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
4983 else
4984 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
4985
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004986 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4987 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004988
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004989 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004990 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4991 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004992 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
4993 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
4994 SDOperand Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00004995
4996 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004997 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00004998 }
4999 }
Chris Lattnerce872152006-03-19 06:31:19 +00005000
5001 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5002 // aligned object on the stack, store each element into it, then load
5003 // the result as a vector.
5004 MVT::ValueType VT = Node->getValueType(0);
5005 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00005006 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005007
5008 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005009 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00005010 unsigned TypeByteSize =
5011 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005012 // Store (in the right endianness) the elements to memory.
5013 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5014 // Ignore undef elements.
5015 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5016
Chris Lattner841c8822006-03-22 01:46:54 +00005017 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005018
5019 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5020 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5021
Evan Cheng786225a2006-10-05 23:01:46 +00005022 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00005023 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00005024 }
5025
5026 SDOperand StoreChain;
5027 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005028 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5029 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005030 else
5031 StoreChain = DAG.getEntryNode();
5032
5033 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00005034 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005035}
5036
Chris Lattner5b359c62005-04-02 04:00:59 +00005037void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5038 SDOperand Op, SDOperand Amt,
5039 SDOperand &Lo, SDOperand &Hi) {
5040 // Expand the subcomponents.
5041 SDOperand LHSL, LHSH;
5042 ExpandOp(Op, LHSL, LHSH);
5043
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005044 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005045 MVT::ValueType VT = LHSL.getValueType();
5046 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005047 Hi = Lo.getValue(1);
5048}
5049
5050
Chris Lattnere34b3962005-01-19 04:19:40 +00005051/// ExpandShift - Try to find a clever way to expand this shift operation out to
5052/// smaller elements. If we can't find a way that is more efficient than a
5053/// libcall on this target, return false. Otherwise, return true with the
5054/// low-parts expanded into Lo and Hi.
5055bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
5056 SDOperand &Lo, SDOperand &Hi) {
5057 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5058 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005059
Chris Lattnere34b3962005-01-19 04:19:40 +00005060 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005061 SDOperand ShAmt = LegalizeOp(Amt);
5062 MVT::ValueType ShTy = ShAmt.getValueType();
Dan Gohman91dc17b2008-02-20 16:57:27 +00005063 unsigned ShBits = MVT::getSizeInBits(ShTy);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005064 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
5065 unsigned NVTBits = MVT::getSizeInBits(NVT);
5066
Chris Lattner7a3c8552007-10-14 20:35:12 +00005067 // Handle the case when Amt is an immediate.
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005068 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5069 unsigned Cst = CN->getValue();
5070 // Expand the incoming operand to be shifted, so that we have its parts
5071 SDOperand InL, InH;
5072 ExpandOp(Op, InL, InH);
5073 switch(Opc) {
5074 case ISD::SHL:
5075 if (Cst > VTBits) {
5076 Lo = DAG.getConstant(0, NVT);
5077 Hi = DAG.getConstant(0, NVT);
5078 } else if (Cst > NVTBits) {
5079 Lo = DAG.getConstant(0, NVT);
5080 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005081 } else if (Cst == NVTBits) {
5082 Lo = DAG.getConstant(0, NVT);
5083 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005084 } else {
5085 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5086 Hi = DAG.getNode(ISD::OR, NVT,
5087 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5088 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5089 }
5090 return true;
5091 case ISD::SRL:
5092 if (Cst > VTBits) {
5093 Lo = DAG.getConstant(0, NVT);
5094 Hi = DAG.getConstant(0, NVT);
5095 } else if (Cst > NVTBits) {
5096 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5097 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005098 } else if (Cst == NVTBits) {
5099 Lo = InH;
5100 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005101 } else {
5102 Lo = DAG.getNode(ISD::OR, NVT,
5103 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5104 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5105 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5106 }
5107 return true;
5108 case ISD::SRA:
5109 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005110 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005111 DAG.getConstant(NVTBits-1, ShTy));
5112 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005113 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005114 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005115 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005116 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005117 } else if (Cst == NVTBits) {
5118 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005119 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005120 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005121 } else {
5122 Lo = DAG.getNode(ISD::OR, NVT,
5123 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5124 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5125 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5126 }
5127 return true;
5128 }
5129 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005130
5131 // Okay, the shift amount isn't constant. However, if we can tell that it is
5132 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005133 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5134 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005135 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005136
Dan Gohman9e255b72008-02-22 01:12:31 +00005137 // If we know that if any of the high bits of the shift amount are one, then
5138 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005139 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005140 // Mask out the high bit, which we know is set.
5141 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005142 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005143
5144 // Expand the incoming operand to be shifted, so that we have its parts
5145 SDOperand InL, InH;
5146 ExpandOp(Op, InL, InH);
5147 switch(Opc) {
5148 case ISD::SHL:
5149 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5150 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5151 return true;
5152 case ISD::SRL:
5153 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5154 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5155 return true;
5156 case ISD::SRA:
5157 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5158 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5159 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5160 return true;
5161 }
5162 }
5163
Dan Gohman9e255b72008-02-22 01:12:31 +00005164 // If we know that the high bits of the shift amount are all zero, then we can
5165 // do this as a couple of simple shifts.
5166 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005167 // Compute 32-amt.
5168 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5169 DAG.getConstant(NVTBits, Amt.getValueType()),
5170 Amt);
5171
5172 // Expand the incoming operand to be shifted, so that we have its parts
5173 SDOperand InL, InH;
5174 ExpandOp(Op, InL, InH);
5175 switch(Opc) {
5176 case ISD::SHL:
5177 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5178 Hi = DAG.getNode(ISD::OR, NVT,
5179 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5180 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5181 return true;
5182 case ISD::SRL:
5183 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5184 Lo = DAG.getNode(ISD::OR, NVT,
5185 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5186 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5187 return true;
5188 case ISD::SRA:
5189 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5190 Lo = DAG.getNode(ISD::OR, NVT,
5191 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5192 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5193 return true;
5194 }
5195 }
5196
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005197 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005198}
Chris Lattner77e77a62005-01-21 06:05:23 +00005199
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005200
Chris Lattner77e77a62005-01-21 06:05:23 +00005201// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5202// does not fit into a register, return the lo part and set the hi part to the
5203// by-reg argument. If it does fit into a single register, return the result
5204// and leave the Hi part unset.
5205SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00005206 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005207 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5208 // The input chain to this libcall is the entry node of the function.
5209 // Legalizing the call will automatically add the previous call to the
5210 // dependence.
5211 SDOperand InChain = DAG.getEntryNode();
5212
Chris Lattner77e77a62005-01-21 06:05:23 +00005213 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005214 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005215 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5216 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5217 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00005218 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005219 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005220 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005221 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005222 }
5223 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005224
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005225 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00005226 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005227 std::pair<SDOperand,SDOperand> CallInfo =
Duncan Sands00fee652008-02-14 17:28:50 +00005228 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5229 false, Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005230
Chris Lattner6831a812006-02-13 09:18:02 +00005231 // Legalize the call sequence, starting with the chain. This will advance
5232 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5233 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5234 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00005235 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005236 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005237 default: assert(0 && "Unknown thing");
5238 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005239 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005240 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005241 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005242 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005243 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005244 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005245 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005246}
5247
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005248
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005249/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5250///
Chris Lattner77e77a62005-01-21 06:05:23 +00005251SDOperand SelectionDAGLegalize::
5252ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Dan Gohmand91446d2008-03-05 01:08:17 +00005253 MVT::ValueType SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005254 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005255
Evan Cheng9845eb52008-04-01 02:18:22 +00005256 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5257 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005258 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005259 // incoming integer is set. To handle this, we dynamically test to see if
5260 // it is set, and, if so, add a fudge factor.
Dan Gohman034f60e2008-03-11 01:59:03 +00005261 SDOperand Hi;
5262 if (ExpandSource) {
5263 SDOperand Lo;
5264 ExpandOp(Source, Lo, Hi);
5265 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5266 } else {
5267 // The comparison for the sign bit will use the entire operand.
5268 Hi = Source;
5269 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005270
Chris Lattner66de05b2005-05-13 04:45:13 +00005271 // If this is unsigned, and not supported, first perform the conversion to
5272 // signed, then adjust the result if the sign bit is set.
Dan Gohman034f60e2008-03-11 01:59:03 +00005273 SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner66de05b2005-05-13 04:45:13 +00005274
Scott Michel5b8f82e2008-03-10 15:42:14 +00005275 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005276 DAG.getConstant(0, Hi.getValueType()),
5277 ISD::SETLT);
Chris Lattner0bd48932008-01-17 07:00:52 +00005278 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005279 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5280 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005281 uint64_t FF = 0x5f800000ULL;
5282 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00005283 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005284
Chris Lattner5839bf22005-08-26 17:15:30 +00005285 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00005286 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5287 SDOperand FudgeInReg;
5288 if (DestTy == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005289 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005290 PseudoSourceValue::getConstantPool(), 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005291 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005292 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005293 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005294 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005295 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman69de1932008-02-06 22:27:42 +00005296 MVT::f32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005297 else
5298 assert(0 && "Unexpected conversion");
5299
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005300 MVT::ValueType SCVT = SignedConv.getValueType();
5301 if (SCVT != DestTy) {
5302 // Destination type needs to be expanded as well. The FADD now we are
5303 // constructing will be expanded into a libcall.
5304 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
Dan Gohmand91446d2008-03-05 01:08:17 +00005305 assert(MVT::getSizeInBits(SCVT) * 2 == MVT::getSizeInBits(DestTy));
5306 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005307 SignedConv, SignedConv.getValue(1));
5308 }
5309 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5310 }
Chris Lattner473a9902005-09-29 06:44:39 +00005311 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005312 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005313
Chris Lattnera88a2602005-05-14 05:33:54 +00005314 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005315 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005316 default: assert(0 && "This action not implemented for this operation!");
5317 case TargetLowering::Legal:
5318 case TargetLowering::Expand:
5319 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005320 case TargetLowering::Custom: {
5321 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5322 Source), DAG);
5323 if (NV.Val)
5324 return LegalizeOp(NV);
5325 break; // The target decided this was legal after all
5326 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005327 }
5328
Chris Lattner13689e22005-05-12 07:00:44 +00005329 // Expand the source, then glue it back together for the call. We must expand
5330 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00005331 if (ExpandSource) {
5332 SDOperand SrcLo, SrcHi;
5333 ExpandOp(Source, SrcLo, SrcHi);
5334 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5335 }
Chris Lattner13689e22005-05-12 07:00:44 +00005336
Evan Cheng56966222007-01-12 02:11:51 +00005337 RTLIB::Libcall LC;
Evan Cheng110cf482008-04-01 01:50:16 +00005338 if (SourceVT == MVT::i32) {
5339 if (DestTy == MVT::f32)
Evan Chengdb45d1c2008-04-01 02:00:09 +00005340 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng110cf482008-04-01 01:50:16 +00005341 else {
5342 assert(DestTy == MVT::f64 && "Unknown fp value type!");
5343 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
5344 }
5345 } else if (SourceVT == MVT::i64) {
Dan Gohmand91446d2008-03-05 01:08:17 +00005346 if (DestTy == MVT::f32)
5347 LC = RTLIB::SINTTOFP_I64_F32;
Dan Gohman034f60e2008-03-11 01:59:03 +00005348 else if (DestTy == MVT::f64)
Dan Gohmand91446d2008-03-05 01:08:17 +00005349 LC = RTLIB::SINTTOFP_I64_F64;
Dan Gohman034f60e2008-03-11 01:59:03 +00005350 else if (DestTy == MVT::f80)
5351 LC = RTLIB::SINTTOFP_I64_F80;
5352 else {
5353 assert(DestTy == MVT::ppcf128 && "Unknown fp value type!");
5354 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dan Gohmand91446d2008-03-05 01:08:17 +00005355 }
5356 } else if (SourceVT == MVT::i128) {
5357 if (DestTy == MVT::f32)
5358 LC = RTLIB::SINTTOFP_I128_F32;
5359 else if (DestTy == MVT::f64)
5360 LC = RTLIB::SINTTOFP_I128_F64;
5361 else if (DestTy == MVT::f80)
5362 LC = RTLIB::SINTTOFP_I128_F80;
5363 else {
5364 assert(DestTy == MVT::ppcf128 && "Unknown fp value type!");
5365 LC = RTLIB::SINTTOFP_I128_PPCF128;
5366 }
5367 } else {
5368 assert(0 && "Unknown int value type");
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005369 }
Chris Lattner6831a812006-02-13 09:18:02 +00005370
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005371 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00005372 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohmana2e94852008-03-10 23:03:31 +00005373 SDOperand HiPart;
5374 SDOperand Result = ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5375 HiPart);
Evan Cheng110cf482008-04-01 01:50:16 +00005376 if (Result.getValueType() != DestTy && HiPart.Val)
Dan Gohmana2e94852008-03-10 23:03:31 +00005377 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5378 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005379}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005380
Chris Lattner22cde6a2006-01-28 08:25:58 +00005381/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5382/// INT_TO_FP operation of the specified operand when the target requests that
5383/// we expand it. At this point, we know that the result and operand types are
5384/// legal for the target.
5385SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5386 SDOperand Op0,
5387 MVT::ValueType DestVT) {
5388 if (Op0.getValueType() == MVT::i32) {
5389 // simple 32-bit [signed|unsigned] integer to float/double expansion
5390
Chris Lattner23594d42008-01-16 07:03:22 +00005391 // Get the stack frame index of a 8 byte buffer.
5392 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5393
Chris Lattner22cde6a2006-01-28 08:25:58 +00005394 // word offset constant for Hi/Lo address computation
5395 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5396 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00005397 SDOperand Hi = StackSlot;
5398 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5399 if (TLI.isLittleEndian())
5400 std::swap(Hi, Lo);
5401
Chris Lattner22cde6a2006-01-28 08:25:58 +00005402 // if signed map to unsigned space
5403 SDOperand Op0Mapped;
5404 if (isSigned) {
5405 // constant used to invert sign bit (signed to unsigned mapping)
5406 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5407 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5408 } else {
5409 Op0Mapped = Op0;
5410 }
5411 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00005412 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005413 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005414 // initial hi portion of constructed double
5415 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5416 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00005417 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005418 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00005419 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005420 // FP constant to bias correct the final result
5421 SDOperand Bias = DAG.getConstantFP(isSigned ?
5422 BitsToDouble(0x4330000080000000ULL)
5423 : BitsToDouble(0x4330000000000000ULL),
5424 MVT::f64);
5425 // subtract the bias
5426 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5427 // final result
5428 SDOperand Result;
5429 // handle final rounding
5430 if (DestVT == MVT::f64) {
5431 // do nothing
5432 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005433 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00005434 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5435 DAG.getIntPtrConstant(0));
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005436 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5437 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005438 }
5439 return Result;
5440 }
5441 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5442 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5443
Scott Michel5b8f82e2008-03-10 15:42:14 +00005444 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005445 DAG.getConstant(0, Op0.getValueType()),
5446 ISD::SETLT);
Chris Lattner0bd48932008-01-17 07:00:52 +00005447 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005448 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5449 SignSet, Four, Zero);
5450
5451 // If the sign bit of the integer is set, the large number will be treated
5452 // as a negative number. To counteract this, the dynamic code adds an
5453 // offset depending on the data type.
5454 uint64_t FF;
5455 switch (Op0.getValueType()) {
5456 default: assert(0 && "Unsupported integer type!");
5457 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5458 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5459 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5460 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5461 }
5462 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005463 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005464
5465 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5466 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5467 SDOperand FudgeInReg;
5468 if (DestVT == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005469 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005470 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005471 else {
Dan Gohman69de1932008-02-06 22:27:42 +00005472 FudgeInReg =
5473 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5474 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005475 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman69de1932008-02-06 22:27:42 +00005476 MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005477 }
5478
5479 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5480}
5481
5482/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5483/// *INT_TO_FP operation of the specified operand when the target requests that
5484/// we promote it. At this point, we know that the result and operand types are
5485/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5486/// operation that takes a larger input.
5487SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5488 MVT::ValueType DestVT,
5489 bool isSigned) {
5490 // First step, figure out the appropriate *INT_TO_FP operation to use.
5491 MVT::ValueType NewInTy = LegalOp.getValueType();
5492
5493 unsigned OpToUse = 0;
5494
5495 // Scan for the appropriate larger type to use.
5496 while (1) {
5497 NewInTy = (MVT::ValueType)(NewInTy+1);
5498 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5499
5500 // If the target supports SINT_TO_FP of this type, use it.
5501 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5502 default: break;
5503 case TargetLowering::Legal:
5504 if (!TLI.isTypeLegal(NewInTy))
5505 break; // Can't use this datatype.
5506 // FALL THROUGH.
5507 case TargetLowering::Custom:
5508 OpToUse = ISD::SINT_TO_FP;
5509 break;
5510 }
5511 if (OpToUse) break;
5512 if (isSigned) continue;
5513
5514 // If the target supports UINT_TO_FP of this type, use it.
5515 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5516 default: break;
5517 case TargetLowering::Legal:
5518 if (!TLI.isTypeLegal(NewInTy))
5519 break; // Can't use this datatype.
5520 // FALL THROUGH.
5521 case TargetLowering::Custom:
5522 OpToUse = ISD::UINT_TO_FP;
5523 break;
5524 }
5525 if (OpToUse) break;
5526
5527 // Otherwise, try a larger type.
5528 }
5529
5530 // Okay, we found the operation and type to use. Zero extend our input to the
5531 // desired type then run the operation on it.
5532 return DAG.getNode(OpToUse, DestVT,
5533 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5534 NewInTy, LegalOp));
5535}
5536
5537/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5538/// FP_TO_*INT operation of the specified operand when the target requests that
5539/// we promote it. At this point, we know that the result and operand types are
5540/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5541/// operation that returns a larger result.
5542SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5543 MVT::ValueType DestVT,
5544 bool isSigned) {
5545 // First step, figure out the appropriate FP_TO*INT operation to use.
5546 MVT::ValueType NewOutTy = DestVT;
5547
5548 unsigned OpToUse = 0;
5549
5550 // Scan for the appropriate larger type to use.
5551 while (1) {
5552 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5553 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5554
5555 // If the target supports FP_TO_SINT returning this type, use it.
5556 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5557 default: break;
5558 case TargetLowering::Legal:
5559 if (!TLI.isTypeLegal(NewOutTy))
5560 break; // Can't use this datatype.
5561 // FALL THROUGH.
5562 case TargetLowering::Custom:
5563 OpToUse = ISD::FP_TO_SINT;
5564 break;
5565 }
5566 if (OpToUse) break;
5567
5568 // If the target supports FP_TO_UINT of this type, use it.
5569 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5570 default: break;
5571 case TargetLowering::Legal:
5572 if (!TLI.isTypeLegal(NewOutTy))
5573 break; // Can't use this datatype.
5574 // FALL THROUGH.
5575 case TargetLowering::Custom:
5576 OpToUse = ISD::FP_TO_UINT;
5577 break;
5578 }
5579 if (OpToUse) break;
5580
5581 // Otherwise, try a larger type.
5582 }
5583
Chris Lattner27a6c732007-11-24 07:07:01 +00005584
5585 // Okay, we found the operation and type to use.
5586 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5587
5588 // If the operation produces an invalid type, it must be custom lowered. Use
5589 // the target lowering hooks to expand it. Just keep the low part of the
5590 // expanded operation, we know that we're truncating anyway.
5591 if (getTypeAction(NewOutTy) == Expand) {
5592 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5593 assert(Operation.Val && "Didn't return anything");
5594 }
5595
5596 // Truncate the result of the extended FP_TO_*INT operation to the desired
5597 // size.
5598 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005599}
5600
5601/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5602///
5603SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5604 MVT::ValueType VT = Op.getValueType();
5605 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5606 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5607 switch (VT) {
5608 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5609 case MVT::i16:
5610 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5611 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5612 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5613 case MVT::i32:
5614 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5615 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5616 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5617 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5618 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5619 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5620 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5621 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5622 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5623 case MVT::i64:
5624 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5625 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5626 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5627 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5628 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5629 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5630 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5631 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5632 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5633 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5634 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5635 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5636 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5637 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5638 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5639 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5640 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5641 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5642 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5643 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5644 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5645 }
5646}
5647
5648/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5649///
5650SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5651 switch (Opc) {
5652 default: assert(0 && "Cannot expand this yet!");
5653 case ISD::CTPOP: {
5654 static const uint64_t mask[6] = {
5655 0x5555555555555555ULL, 0x3333333333333333ULL,
5656 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5657 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5658 };
5659 MVT::ValueType VT = Op.getValueType();
5660 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005661 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005662 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5663 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5664 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5665 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5666 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5667 DAG.getNode(ISD::AND, VT,
5668 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5669 }
5670 return Op;
5671 }
5672 case ISD::CTLZ: {
5673 // for now, we do this:
5674 // x = x | (x >> 1);
5675 // x = x | (x >> 2);
5676 // ...
5677 // x = x | (x >>16);
5678 // x = x | (x >>32); // for 64-bit input
5679 // return popcount(~x);
5680 //
5681 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5682 MVT::ValueType VT = Op.getValueType();
5683 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005684 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005685 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5686 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5687 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5688 }
5689 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5690 return DAG.getNode(ISD::CTPOP, VT, Op);
5691 }
5692 case ISD::CTTZ: {
5693 // for now, we use: { return popcount(~x & (x - 1)); }
5694 // unless the target has ctlz but not ctpop, in which case we use:
5695 // { return 32 - nlz(~x & (x-1)); }
5696 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5697 MVT::ValueType VT = Op.getValueType();
5698 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5699 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5700 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5701 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5702 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5703 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5704 TLI.isOperationLegal(ISD::CTLZ, VT))
5705 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005706 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005707 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5708 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5709 }
5710 }
5711}
Chris Lattnere34b3962005-01-19 04:19:40 +00005712
Chris Lattner3e928bb2005-01-07 07:47:09 +00005713/// ExpandOp - Expand the specified SDOperand into its two component pieces
5714/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5715/// LegalizeNodes map is filled in for any results that are not expanded, the
5716/// ExpandedNodes map is filled in for any results that are expanded, and the
5717/// Lo/Hi values are returned.
5718void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5719 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005720 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005721 SDNode *Node = Op.Val;
5722 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005723 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005724 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005725 "Cannot expand to FP value or to larger int value!");
5726
Chris Lattner6fdcb252005-09-02 20:32:45 +00005727 // See if we already expanded it.
Roman Levensteindc1adac2008-04-07 10:06:32 +00005728 DenseMap<SDOperandImpl, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005729 = ExpandedNodes.find(Op);
5730 if (I != ExpandedNodes.end()) {
5731 Lo = I->second.first;
5732 Hi = I->second.second;
5733 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005734 }
5735
Chris Lattner3e928bb2005-01-07 07:47:09 +00005736 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005737 case ISD::CopyFromReg:
5738 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005739 case ISD::FP_ROUND_INREG:
5740 if (VT == MVT::ppcf128 &&
5741 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5742 TargetLowering::Custom) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005743 SDOperand SrcLo, SrcHi, Src;
5744 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5745 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5746 SDOperand Result = TLI.LowerOperation(
5747 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005748 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5749 Lo = Result.Val->getOperand(0);
5750 Hi = Result.Val->getOperand(1);
5751 break;
5752 }
5753 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005754 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005755#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005756 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005757#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005758 assert(0 && "Do not know how to expand this operator!");
5759 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00005760 case ISD::EXTRACT_ELEMENT:
5761 ExpandOp(Node->getOperand(0), Lo, Hi);
5762 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5763 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00005764 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00005765 case ISD::EXTRACT_VECTOR_ELT:
5766 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5767 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5768 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5769 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005770 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005771 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005772 Lo = DAG.getNode(ISD::UNDEF, NVT);
5773 Hi = DAG.getNode(ISD::UNDEF, NVT);
5774 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005775 case ISD::Constant: {
Dan Gohman050f5502008-03-03 22:20:46 +00005776 unsigned NVTBits = MVT::getSizeInBits(NVT);
5777 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
5778 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
5779 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005780 break;
5781 }
Evan Cheng00495212006-12-12 21:32:44 +00005782 case ISD::ConstantFP: {
5783 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005784 if (CFP->getValueType(0) == MVT::ppcf128) {
5785 APInt api = CFP->getValueAPF().convertToAPInt();
5786 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5787 MVT::f64);
5788 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5789 MVT::f64);
5790 break;
5791 }
Evan Cheng279101e2006-12-12 22:19:28 +00005792 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005793 if (getTypeAction(Lo.getValueType()) == Expand)
5794 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005795 break;
5796 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005797 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005798 // Return the operands.
5799 Lo = Node->getOperand(0);
5800 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005801 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005802
5803 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00005804 if (Node->getNumValues() == 1) {
5805 ExpandOp(Op.getOperand(0), Lo, Hi);
5806 break;
5807 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005808 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5809 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5810 Op.getValue(1).getValueType() == MVT::Other &&
5811 "unhandled MERGE_VALUES");
5812 ExpandOp(Op.getOperand(0), Lo, Hi);
5813 // Remember that we legalized the chain.
5814 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5815 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005816
5817 case ISD::SIGN_EXTEND_INREG:
5818 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005819 // sext_inreg the low part if needed.
5820 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5821
5822 // The high part gets the sign extension from the lo-part. This handles
5823 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005824 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5825 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5826 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005827 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005828
Nate Begemand88fc032006-01-14 03:14:10 +00005829 case ISD::BSWAP: {
5830 ExpandOp(Node->getOperand(0), Lo, Hi);
5831 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5832 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5833 Lo = TempLo;
5834 break;
5835 }
5836
Chris Lattneredb1add2005-05-11 04:51:16 +00005837 case ISD::CTPOP:
5838 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005839 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5840 DAG.getNode(ISD::CTPOP, NVT, Lo),
5841 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005842 Hi = DAG.getConstant(0, NVT);
5843 break;
5844
Chris Lattner39a8f332005-05-12 19:05:01 +00005845 case ISD::CTLZ: {
5846 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005847 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005848 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5849 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005850 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005851 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005852 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5853 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5854
5855 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5856 Hi = DAG.getConstant(0, NVT);
5857 break;
5858 }
5859
5860 case ISD::CTTZ: {
5861 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005862 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005863 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5864 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005865 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005866 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005867 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5868 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5869
5870 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5871 Hi = DAG.getConstant(0, NVT);
5872 break;
5873 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005874
Nate Begemanacc398c2006-01-25 18:21:52 +00005875 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005876 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5877 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005878 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5879 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5880
5881 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005882 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005883 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00005884 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00005885 std::swap(Lo, Hi);
5886 break;
5887 }
5888
Chris Lattner3e928bb2005-01-07 07:47:09 +00005889 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005890 LoadSDNode *LD = cast<LoadSDNode>(Node);
5891 SDOperand Ch = LD->getChain(); // Legalize the chain.
5892 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5893 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005894 int SVOffset = LD->getSrcValueOffset();
5895 unsigned Alignment = LD->getAlignment();
5896 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005897
Evan Cheng466685d2006-10-09 20:57:25 +00005898 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005899 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5900 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005901 if (VT == MVT::f32 || VT == MVT::f64) {
5902 // f32->i32 or f64->i64 one to one expansion.
5903 // Remember that we legalized the chain.
5904 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005905 // Recursively expand the new load.
5906 if (getTypeAction(NVT) == Expand)
5907 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005908 break;
5909 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005910
Evan Cheng466685d2006-10-09 20:57:25 +00005911 // Increment the pointer to the other half.
5912 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5913 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00005914 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005915 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00005916 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005917 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5918 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005919
Evan Cheng466685d2006-10-09 20:57:25 +00005920 // Build a factor node to remember that this load is independent of the
5921 // other one.
5922 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5923 Hi.getValue(1));
5924
5925 // Remember that we legalized the chain.
5926 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00005927 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00005928 std::swap(Lo, Hi);
5929 } else {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005930 MVT::ValueType EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005931
Dale Johannesenb6210fc2007-10-19 20:29:00 +00005932 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5933 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00005934 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5935 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005936 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005937 // Remember that we legalized the chain.
5938 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5939 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5940 break;
5941 }
Evan Cheng466685d2006-10-09 20:57:25 +00005942
5943 if (EVT == NVT)
5944 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005945 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005946 else
5947 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005948 SVOffset, EVT, isVolatile,
5949 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005950
5951 // Remember that we legalized the chain.
5952 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5953
5954 if (ExtType == ISD::SEXTLOAD) {
5955 // The high part is obtained by SRA'ing all but one of the bits of the
5956 // lo part.
5957 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5958 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5959 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5960 } else if (ExtType == ISD::ZEXTLOAD) {
5961 // The high part is just a zero.
5962 Hi = DAG.getConstant(0, NVT);
5963 } else /* if (ExtType == ISD::EXTLOAD) */ {
5964 // The high part is undefined.
5965 Hi = DAG.getNode(ISD::UNDEF, NVT);
5966 }
5967 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005968 break;
5969 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005970 case ISD::AND:
5971 case ISD::OR:
5972 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5973 SDOperand LL, LH, RL, RH;
5974 ExpandOp(Node->getOperand(0), LL, LH);
5975 ExpandOp(Node->getOperand(1), RL, RH);
5976 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5977 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5978 break;
5979 }
5980 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005981 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005982 ExpandOp(Node->getOperand(1), LL, LH);
5983 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005984 if (getTypeAction(NVT) == Expand)
5985 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005986 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005987 if (VT != MVT::f32)
5988 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005989 break;
5990 }
Nate Begeman9373a812005-08-10 20:51:12 +00005991 case ISD::SELECT_CC: {
5992 SDOperand TL, TH, FL, FH;
5993 ExpandOp(Node->getOperand(2), TL, TH);
5994 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005995 if (getTypeAction(NVT) == Expand)
5996 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005997 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5998 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005999 if (VT != MVT::f32)
6000 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6001 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006002 break;
6003 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006004 case ISD::ANY_EXTEND:
6005 // The low part is any extension of the input (which degenerates to a copy).
6006 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6007 // The high part is undefined.
6008 Hi = DAG.getNode(ISD::UNDEF, NVT);
6009 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006010 case ISD::SIGN_EXTEND: {
6011 // The low part is just a sign extension of the input (which degenerates to
6012 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006013 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006014
Chris Lattner3e928bb2005-01-07 07:47:09 +00006015 // The high part is obtained by SRA'ing all but one of the bits of the lo
6016 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00006017 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00006018 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6019 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006020 break;
6021 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006022 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006023 // The low part is just a zero extension of the input (which degenerates to
6024 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006025 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006026
Chris Lattner3e928bb2005-01-07 07:47:09 +00006027 // The high part is just a zero.
6028 Hi = DAG.getConstant(0, NVT);
6029 break;
Chris Lattner35481892005-12-23 00:16:34 +00006030
Chris Lattner4c948eb2007-02-13 23:55:16 +00006031 case ISD::TRUNCATE: {
6032 // The input value must be larger than this value. Expand *it*.
6033 SDOperand NewLo;
6034 ExpandOp(Node->getOperand(0), NewLo, Hi);
6035
6036 // The low part is now either the right size, or it is closer. If not the
6037 // right size, make an illegal truncate so we recursively expand it.
6038 if (NewLo.getValueType() != Node->getValueType(0))
6039 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6040 ExpandOp(NewLo, Lo, Hi);
6041 break;
6042 }
6043
Chris Lattner35481892005-12-23 00:16:34 +00006044 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006045 SDOperand Tmp;
6046 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6047 // If the target wants to, allow it to lower this itself.
6048 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6049 case Expand: assert(0 && "cannot expand FP!");
6050 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6051 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6052 }
6053 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6054 }
6055
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006056 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006057 if (VT == MVT::f32 || VT == MVT::f64) {
6058 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006059 if (getTypeAction(NVT) == Expand)
6060 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006061 break;
6062 }
6063
6064 // If source operand will be expanded to the same type as VT, i.e.
6065 // i64 <- f64, i32 <- f32, expand the source operand instead.
6066 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
6067 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6068 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006069 break;
6070 }
6071
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006072 // Turn this into a load/store pair by default.
6073 if (Tmp.Val == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006074 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006075
Chris Lattner35481892005-12-23 00:16:34 +00006076 ExpandOp(Tmp, Lo, Hi);
6077 break;
6078 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006079
Chris Lattner27a6c732007-11-24 07:07:01 +00006080 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006081 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6082 TargetLowering::Custom &&
6083 "Must custom expand ReadCycleCounter");
Chris Lattner27a6c732007-11-24 07:07:01 +00006084 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6085 assert(Tmp.Val && "Node must be custom expanded!");
6086 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner308575b2005-11-20 22:56:56 +00006087 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006088 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006089 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006090 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006091
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006092 case ISD::ATOMIC_LCS: {
6093 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6094 assert(Tmp.Val && "Node must be custom expanded!");
6095 ExpandOp(Tmp.getValue(0), Lo, Hi);
6096 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
6097 LegalizeOp(Tmp.getValue(1)));
6098 break;
6099 }
6100
6101
6102
Chris Lattner4e6c7462005-01-08 19:27:05 +00006103 // These operators cannot be expanded directly, emit them as calls to
6104 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006105 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006106 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00006107 SDOperand Op;
6108 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6109 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006110 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6111 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006112 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006113
Chris Lattnerf20d1832005-07-30 01:40:57 +00006114 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6115
Chris Lattner80a3e942005-07-29 00:33:32 +00006116 // Now that the custom expander is done, expand the result, which is still
6117 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00006118 if (Op.Val) {
6119 ExpandOp(Op, Lo, Hi);
6120 break;
6121 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006122 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006123
Dale Johannesen161e8972007-10-05 20:04:43 +00006124 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmana2e94852008-03-10 23:03:31 +00006125 if (VT == MVT::i64) {
6126 if (Node->getOperand(0).getValueType() == MVT::f32)
6127 LC = RTLIB::FPTOSINT_F32_I64;
6128 else if (Node->getOperand(0).getValueType() == MVT::f64)
6129 LC = RTLIB::FPTOSINT_F64_I64;
6130 else if (Node->getOperand(0).getValueType() == MVT::f80)
6131 LC = RTLIB::FPTOSINT_F80_I64;
6132 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6133 LC = RTLIB::FPTOSINT_PPCF128_I64;
6134 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6135 false/*sign irrelevant*/, Hi);
6136 } else if (VT == MVT::i128) {
6137 if (Node->getOperand(0).getValueType() == MVT::f32)
6138 LC = RTLIB::FPTOSINT_F32_I128;
6139 else if (Node->getOperand(0).getValueType() == MVT::f64)
6140 LC = RTLIB::FPTOSINT_F64_I128;
6141 else if (Node->getOperand(0).getValueType() == MVT::f80)
6142 LC = RTLIB::FPTOSINT_F80_I128;
6143 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6144 LC = RTLIB::FPTOSINT_PPCF128_I128;
6145 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6146 false/*sign irrelevant*/, Hi);
6147 } else {
6148 assert(0 && "Unexpected uint-to-fp conversion!");
6149 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006150 break;
Evan Cheng56966222007-01-12 02:11:51 +00006151 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006152
Evan Cheng56966222007-01-12 02:11:51 +00006153 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006154 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006155 SDOperand Op;
6156 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6157 case Expand: assert(0 && "cannot expand FP!");
6158 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6159 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6160 }
6161
6162 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6163
6164 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00006165 if (Op.Val) {
6166 ExpandOp(Op, Lo, Hi);
6167 break;
6168 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006169 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006170
Evan Chengdaccea182007-10-05 01:09:32 +00006171 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmana2e94852008-03-10 23:03:31 +00006172 if (VT == MVT::i64) {
6173 if (Node->getOperand(0).getValueType() == MVT::f32)
6174 LC = RTLIB::FPTOUINT_F32_I64;
6175 else if (Node->getOperand(0).getValueType() == MVT::f64)
6176 LC = RTLIB::FPTOUINT_F64_I64;
6177 else if (Node->getOperand(0).getValueType() == MVT::f80)
6178 LC = RTLIB::FPTOUINT_F80_I64;
6179 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6180 LC = RTLIB::FPTOUINT_PPCF128_I64;
6181 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6182 false/*sign irrelevant*/, Hi);
6183 } else if (VT == MVT::i128) {
6184 if (Node->getOperand(0).getValueType() == MVT::f32)
6185 LC = RTLIB::FPTOUINT_F32_I128;
6186 else if (Node->getOperand(0).getValueType() == MVT::f64)
6187 LC = RTLIB::FPTOUINT_F64_I128;
6188 else if (Node->getOperand(0).getValueType() == MVT::f80)
6189 LC = RTLIB::FPTOUINT_F80_I128;
6190 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6191 LC = RTLIB::FPTOUINT_PPCF128_I128;
6192 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6193 false/*sign irrelevant*/, Hi);
6194 } else {
6195 assert(0 && "Unexpected uint-to-fp conversion!");
6196 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006197 break;
Evan Cheng56966222007-01-12 02:11:51 +00006198 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006199
Evan Cheng05a2d562006-01-09 18:31:59 +00006200 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006201 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006202 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006203 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006204 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006205 Op = TLI.LowerOperation(Op, DAG);
6206 if (Op.Val) {
6207 // Now that the custom expander is done, expand the result, which is
6208 // still VT.
6209 ExpandOp(Op, Lo, Hi);
6210 break;
6211 }
6212 }
6213
Chris Lattner79980b02006-09-13 03:50:39 +00006214 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6215 // this X << 1 as X+X.
6216 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00006217 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner79980b02006-09-13 03:50:39 +00006218 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6219 SDOperand LoOps[2], HiOps[3];
6220 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6221 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6222 LoOps[1] = LoOps[0];
6223 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6224
6225 HiOps[1] = HiOps[0];
6226 HiOps[2] = Lo.getValue(1);
6227 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6228 break;
6229 }
6230 }
6231
Chris Lattnere34b3962005-01-19 04:19:40 +00006232 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006233 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006234 break;
Chris Lattner47599822005-04-02 03:38:53 +00006235
6236 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006237 TargetLowering::LegalizeAction Action =
6238 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6239 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6240 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006241 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006242 break;
6243 }
6244
Chris Lattnere34b3962005-01-19 04:19:40 +00006245 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006246 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
6247 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006248 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006249 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006250
Evan Cheng05a2d562006-01-09 18:31:59 +00006251 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006252 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006253 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006254 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006255 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006256 Op = TLI.LowerOperation(Op, DAG);
6257 if (Op.Val) {
6258 // Now that the custom expander is done, expand the result, which is
6259 // still VT.
6260 ExpandOp(Op, Lo, Hi);
6261 break;
6262 }
6263 }
6264
Chris Lattnere34b3962005-01-19 04:19:40 +00006265 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006266 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006267 break;
Chris Lattner47599822005-04-02 03:38:53 +00006268
6269 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006270 TargetLowering::LegalizeAction Action =
6271 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6272 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6273 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006274 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006275 break;
6276 }
6277
Chris Lattnere34b3962005-01-19 04:19:40 +00006278 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006279 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
6280 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006281 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006282 }
6283
6284 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006285 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006286 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006287 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006288 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006289 Op = TLI.LowerOperation(Op, DAG);
6290 if (Op.Val) {
6291 // Now that the custom expander is done, expand the result, which is
6292 // still VT.
6293 ExpandOp(Op, Lo, Hi);
6294 break;
6295 }
6296 }
6297
Chris Lattnere34b3962005-01-19 04:19:40 +00006298 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006299 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006300 break;
Chris Lattner47599822005-04-02 03:38:53 +00006301
6302 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006303 TargetLowering::LegalizeAction Action =
6304 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6305 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6306 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006307 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006308 break;
6309 }
6310
Chris Lattnere34b3962005-01-19 04:19:40 +00006311 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006312 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
6313 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006314 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006315 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006316
Misha Brukmanedf128a2005-04-21 22:36:52 +00006317 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006318 case ISD::SUB: {
6319 // If the target wants to custom expand this, let them.
6320 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6321 TargetLowering::Custom) {
6322 Op = TLI.LowerOperation(Op, DAG);
6323 if (Op.Val) {
6324 ExpandOp(Op, Lo, Hi);
6325 break;
6326 }
6327 }
6328
6329 // Expand the subcomponents.
6330 SDOperand LHSL, LHSH, RHSL, RHSH;
6331 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6332 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00006333 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6334 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006335 LoOps[0] = LHSL;
6336 LoOps[1] = RHSL;
6337 HiOps[0] = LHSH;
6338 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006339 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00006340 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006341 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006342 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006343 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00006344 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006345 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006346 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006347 }
Chris Lattner84f67882005-01-20 18:52:28 +00006348 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006349 }
Chris Lattnerb429f732007-05-17 18:15:41 +00006350
6351 case ISD::ADDC:
6352 case ISD::SUBC: {
6353 // Expand the subcomponents.
6354 SDOperand LHSL, LHSH, RHSL, RHSH;
6355 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6356 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6357 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6358 SDOperand LoOps[2] = { LHSL, RHSL };
6359 SDOperand HiOps[3] = { LHSH, RHSH };
6360
6361 if (Node->getOpcode() == ISD::ADDC) {
6362 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6363 HiOps[2] = Lo.getValue(1);
6364 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6365 } else {
6366 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6367 HiOps[2] = Lo.getValue(1);
6368 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6369 }
6370 // Remember that we legalized the flag.
6371 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6372 break;
6373 }
6374 case ISD::ADDE:
6375 case ISD::SUBE: {
6376 // Expand the subcomponents.
6377 SDOperand LHSL, LHSH, RHSL, RHSH;
6378 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6379 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6380 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6381 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6382 SDOperand HiOps[3] = { LHSH, RHSH };
6383
6384 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6385 HiOps[2] = Lo.getValue(1);
6386 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6387
6388 // Remember that we legalized the flag.
6389 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6390 break;
6391 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006392 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006393 // If the target wants to custom expand this, let them.
6394 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006395 SDOperand New = TLI.LowerOperation(Op, DAG);
6396 if (New.Val) {
6397 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006398 break;
6399 }
6400 }
6401
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006402 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6403 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006404 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6405 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6406 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanc7c16572005-04-11 03:01:51 +00006407 SDOperand LL, LH, RL, RH;
6408 ExpandOp(Node->getOperand(0), LL, LH);
6409 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006410 unsigned OuterBitSize = Op.getValueSizeInBits();
6411 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00006412 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6413 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00006414 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6415 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6416 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00006417 // The inputs are both zero-extended.
6418 if (HasUMUL_LOHI) {
6419 // We can emit a umul_lohi.
6420 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6421 Hi = SDOperand(Lo.Val, 1);
6422 break;
6423 }
6424 if (HasMULHU) {
6425 // We can emit a mulhu+mul.
6426 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6427 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6428 break;
6429 }
Dan Gohman525178c2007-10-08 18:33:35 +00006430 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006431 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00006432 // The input values are both sign-extended.
6433 if (HasSMUL_LOHI) {
6434 // We can emit a smul_lohi.
6435 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6436 Hi = SDOperand(Lo.Val, 1);
6437 break;
6438 }
6439 if (HasMULHS) {
6440 // We can emit a mulhs+mul.
6441 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6442 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6443 break;
6444 }
6445 }
6446 if (HasUMUL_LOHI) {
6447 // Lo,Hi = umul LHS, RHS.
6448 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6449 DAG.getVTList(NVT, NVT), LL, RL);
6450 Lo = UMulLOHI;
6451 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006452 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6453 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6454 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6455 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006456 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006457 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006458 if (HasMULHU) {
6459 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6460 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6461 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6462 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6463 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6464 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6465 break;
6466 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006467 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006468
Dan Gohman525178c2007-10-08 18:33:35 +00006469 // If nothing else, we can make a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006470 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6471 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006472 break;
6473 }
Evan Cheng56966222007-01-12 02:11:51 +00006474 case ISD::SDIV:
6475 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6476 break;
6477 case ISD::UDIV:
6478 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6479 break;
6480 case ISD::SREM:
6481 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6482 break;
6483 case ISD::UREM:
6484 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6485 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006486
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006487 case ISD::FADD:
Duncan Sands007f9842008-01-10 10:28:30 +00006488 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6489 RTLIB::ADD_F64,
6490 RTLIB::ADD_F80,
6491 RTLIB::ADD_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006492 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006493 break;
6494 case ISD::FSUB:
Duncan Sands007f9842008-01-10 10:28:30 +00006495 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6496 RTLIB::SUB_F64,
6497 RTLIB::SUB_F80,
6498 RTLIB::SUB_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006499 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006500 break;
6501 case ISD::FMUL:
Duncan Sands007f9842008-01-10 10:28:30 +00006502 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6503 RTLIB::MUL_F64,
6504 RTLIB::MUL_F80,
6505 RTLIB::MUL_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006506 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006507 break;
6508 case ISD::FDIV:
Duncan Sands007f9842008-01-10 10:28:30 +00006509 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6510 RTLIB::DIV_F64,
6511 RTLIB::DIV_F80,
6512 RTLIB::DIV_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006513 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006514 break;
6515 case ISD::FP_EXTEND:
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006516 if (VT == MVT::ppcf128) {
6517 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6518 Node->getOperand(0).getValueType()==MVT::f64);
6519 const uint64_t zero = 0;
6520 if (Node->getOperand(0).getValueType()==MVT::f32)
6521 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6522 else
6523 Hi = Node->getOperand(0);
6524 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6525 break;
6526 }
Evan Cheng56966222007-01-12 02:11:51 +00006527 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006528 break;
6529 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00006530 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006531 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006532 case ISD::FPOWI:
Duncan Sands007f9842008-01-10 10:28:30 +00006533 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6534 RTLIB::POWI_F64,
6535 RTLIB::POWI_F80,
6536 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006537 Node, false, Hi);
6538 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006539 case ISD::FSQRT:
6540 case ISD::FSIN:
6541 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00006542 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006543 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006544 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00006545 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6546 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006547 break;
6548 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00006549 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6550 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006551 break;
6552 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00006553 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6554 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006555 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006556 default: assert(0 && "Unreachable!");
6557 }
Evan Cheng56966222007-01-12 02:11:51 +00006558 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006559 break;
6560 }
Evan Cheng966bf242006-12-16 00:52:40 +00006561 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006562 if (VT == MVT::ppcf128) {
6563 SDOperand Tmp;
6564 ExpandOp(Node->getOperand(0), Lo, Tmp);
6565 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6566 // lo = hi==fabs(hi) ? lo : -lo;
6567 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6568 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6569 DAG.getCondCode(ISD::SETEQ));
6570 break;
6571 }
Evan Cheng966bf242006-12-16 00:52:40 +00006572 SDOperand Mask = (VT == MVT::f64)
6573 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6574 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6575 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6576 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6577 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6578 if (getTypeAction(NVT) == Expand)
6579 ExpandOp(Lo, Lo, Hi);
6580 break;
6581 }
6582 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006583 if (VT == MVT::ppcf128) {
6584 ExpandOp(Node->getOperand(0), Lo, Hi);
6585 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6586 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6587 break;
6588 }
Evan Cheng966bf242006-12-16 00:52:40 +00006589 SDOperand Mask = (VT == MVT::f64)
6590 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6591 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6592 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6593 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6594 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6595 if (getTypeAction(NVT) == Expand)
6596 ExpandOp(Lo, Lo, Hi);
6597 break;
6598 }
Evan Cheng912095b2007-01-04 21:56:39 +00006599 case ISD::FCOPYSIGN: {
6600 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6601 if (getTypeAction(NVT) == Expand)
6602 ExpandOp(Lo, Lo, Hi);
6603 break;
6604 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006605 case ISD::SINT_TO_FP:
6606 case ISD::UINT_TO_FP: {
6607 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6608 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00006609
6610 // Promote the operand if needed. Do this before checking for
6611 // ppcf128 so conversions of i16 and i8 work.
6612 if (getTypeAction(SrcVT) == Promote) {
6613 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6614 Tmp = isSigned
6615 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6616 DAG.getValueType(SrcVT))
6617 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6618 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6619 SrcVT = Node->getOperand(0).getValueType();
6620 }
6621
Dan Gohmana2e94852008-03-10 23:03:31 +00006622 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006623 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006624 if (isSigned) {
6625 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6626 Node->getOperand(0)));
6627 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6628 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006629 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006630 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6631 Node->getOperand(0)));
6632 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6633 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006634 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006635 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6636 DAG.getConstant(0, MVT::i32),
6637 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6638 DAG.getConstantFP(
6639 APFloat(APInt(128, 2, TwoE32)),
6640 MVT::ppcf128)),
6641 Hi,
6642 DAG.getCondCode(ISD::SETLT)),
6643 Lo, Hi);
6644 }
6645 break;
6646 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00006647 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6648 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006649 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00006650 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6651 Lo, Hi);
6652 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6653 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6654 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6655 DAG.getConstant(0, MVT::i64),
6656 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6657 DAG.getConstantFP(
6658 APFloat(APInt(128, 2, TwoE64)),
6659 MVT::ppcf128)),
6660 Hi,
6661 DAG.getCondCode(ISD::SETLT)),
6662 Lo, Hi);
6663 break;
6664 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006665
Dan Gohmana2e94852008-03-10 23:03:31 +00006666 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6667 Node->getOperand(0));
Evan Cheng110cf482008-04-01 01:50:16 +00006668 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00006669 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00006670 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00006671 break;
6672 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006673 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006674
Chris Lattner83397362005-12-21 18:02:52 +00006675 // Make sure the resultant values have been legalized themselves, unless this
6676 // is a type that requires multi-step expansion.
6677 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6678 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006679 if (Hi.Val)
6680 // Don't legalize the high part if it is expanded to a single node.
6681 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006682 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006683
6684 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006685 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00006686 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006687}
6688
Dan Gohman7f321562007-06-25 16:23:39 +00006689/// SplitVectorOp - Given an operand of vector type, break it down into
6690/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00006691void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6692 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00006693 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006694 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006695 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00006696 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00006697
Dan Gohmane40c7b02007-09-24 15:54:53 +00006698 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006699
6700 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6701 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6702
6703 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6704 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6705
Chris Lattnerc7029802006-03-18 01:44:44 +00006706 // See if we already split it.
6707 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6708 = SplitNodes.find(Op);
6709 if (I != SplitNodes.end()) {
6710 Lo = I->second.first;
6711 Hi = I->second.second;
6712 return;
6713 }
6714
6715 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006716 default:
6717#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006718 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006719#endif
6720 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00006721 case ISD::UNDEF:
6722 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6723 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6724 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006725 case ISD::BUILD_PAIR:
6726 Lo = Node->getOperand(0);
6727 Hi = Node->getOperand(1);
6728 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006729 case ISD::INSERT_VECTOR_ELT: {
6730 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6731 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6732 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006733 if (Index < NewNumElts_Lo)
6734 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Nate Begeman5922f562008-03-14 00:53:31 +00006735 DAG.getIntPtrConstant(Index));
Dan Gohman9fe46622007-09-28 23:53:40 +00006736 else
Nate Begeman5db1afb2007-11-15 21:15:26 +00006737 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
Nate Begeman5922f562008-03-14 00:53:31 +00006738 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
Dan Gohman9fe46622007-09-28 23:53:40 +00006739 break;
6740 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00006741 case ISD::VECTOR_SHUFFLE: {
6742 // Build the low part.
6743 SDOperand Mask = Node->getOperand(2);
6744 SmallVector<SDOperand, 8> Ops;
6745 MVT::ValueType PtrVT = TLI.getPointerTy();
6746
6747 // Insert all of the elements from the input that are needed. We use
6748 // buildvector of extractelement here because the input vectors will have
6749 // to be legalized, so this makes the code simpler.
6750 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Nate Begeman5922f562008-03-14 00:53:31 +00006751 SDOperand IdxNode = Mask.getOperand(i);
6752 if (IdxNode.getOpcode() == ISD::UNDEF) {
6753 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6754 continue;
6755 }
6756 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Chris Lattner6c9c6802007-11-19 21:16:54 +00006757 SDOperand InVec = Node->getOperand(0);
6758 if (Idx >= NumElements) {
6759 InVec = Node->getOperand(1);
6760 Idx -= NumElements;
6761 }
6762 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6763 DAG.getConstant(Idx, PtrVT)));
6764 }
6765 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6766 Ops.clear();
6767
6768 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Nate Begeman5922f562008-03-14 00:53:31 +00006769 SDOperand IdxNode = Mask.getOperand(i);
6770 if (IdxNode.getOpcode() == ISD::UNDEF) {
6771 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6772 continue;
6773 }
6774 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Chris Lattner6c9c6802007-11-19 21:16:54 +00006775 SDOperand InVec = Node->getOperand(0);
6776 if (Idx >= NumElements) {
6777 InVec = Node->getOperand(1);
6778 Idx -= NumElements;
6779 }
6780 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6781 DAG.getConstant(Idx, PtrVT)));
6782 }
6783 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6784 break;
6785 }
Dan Gohman7f321562007-06-25 16:23:39 +00006786 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006787 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00006788 Node->op_begin()+NewNumElts_Lo);
6789 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006790
Nate Begeman5db1afb2007-11-15 21:15:26 +00006791 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00006792 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006793 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006794 break;
6795 }
Dan Gohman7f321562007-06-25 16:23:39 +00006796 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00006797 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00006798 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6799 if (NewNumSubvectors == 1) {
6800 Lo = Node->getOperand(0);
6801 Hi = Node->getOperand(1);
6802 } else {
6803 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6804 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006805 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006806
Dan Gohman7f321562007-06-25 16:23:39 +00006807 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6808 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006809 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00006810 }
Dan Gohman65956352007-06-13 15:12:02 +00006811 break;
6812 }
Dan Gohmanc6230962007-10-17 14:48:28 +00006813 case ISD::SELECT: {
6814 SDOperand Cond = Node->getOperand(0);
6815
6816 SDOperand LL, LH, RL, RH;
6817 SplitVectorOp(Node->getOperand(1), LL, LH);
6818 SplitVectorOp(Node->getOperand(2), RL, RH);
6819
6820 if (MVT::isVector(Cond.getValueType())) {
6821 // Handle a vector merge.
6822 SDOperand CL, CH;
6823 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006824 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6825 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006826 } else {
6827 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00006828 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6829 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006830 }
6831 break;
6832 }
Dan Gohman7f321562007-06-25 16:23:39 +00006833 case ISD::ADD:
6834 case ISD::SUB:
6835 case ISD::MUL:
6836 case ISD::FADD:
6837 case ISD::FSUB:
6838 case ISD::FMUL:
6839 case ISD::SDIV:
6840 case ISD::UDIV:
6841 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006842 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006843 case ISD::AND:
6844 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00006845 case ISD::XOR:
6846 case ISD::UREM:
6847 case ISD::SREM:
6848 case ISD::FREM: {
Chris Lattnerc7029802006-03-18 01:44:44 +00006849 SDOperand LL, LH, RL, RH;
6850 SplitVectorOp(Node->getOperand(0), LL, LH);
6851 SplitVectorOp(Node->getOperand(1), RL, RH);
6852
Nate Begeman5db1afb2007-11-15 21:15:26 +00006853 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6854 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006855 break;
6856 }
Dan Gohman82669522007-10-11 23:57:53 +00006857 case ISD::FPOWI: {
6858 SDOperand L, H;
6859 SplitVectorOp(Node->getOperand(0), L, H);
6860
Nate Begeman5db1afb2007-11-15 21:15:26 +00006861 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6862 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00006863 break;
6864 }
6865 case ISD::CTTZ:
6866 case ISD::CTLZ:
6867 case ISD::CTPOP:
6868 case ISD::FNEG:
6869 case ISD::FABS:
6870 case ISD::FSQRT:
6871 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00006872 case ISD::FCOS:
6873 case ISD::FP_TO_SINT:
6874 case ISD::FP_TO_UINT:
6875 case ISD::SINT_TO_FP:
6876 case ISD::UINT_TO_FP: {
Dan Gohman82669522007-10-11 23:57:53 +00006877 SDOperand L, H;
6878 SplitVectorOp(Node->getOperand(0), L, H);
6879
Nate Begeman5db1afb2007-11-15 21:15:26 +00006880 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6881 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00006882 break;
6883 }
Dan Gohman7f321562007-06-25 16:23:39 +00006884 case ISD::LOAD: {
6885 LoadSDNode *LD = cast<LoadSDNode>(Node);
6886 SDOperand Ch = LD->getChain();
6887 SDOperand Ptr = LD->getBasePtr();
6888 const Value *SV = LD->getSrcValue();
6889 int SVOffset = LD->getSrcValueOffset();
6890 unsigned Alignment = LD->getAlignment();
6891 bool isVolatile = LD->isVolatile();
6892
Nate Begeman5db1afb2007-11-15 21:15:26 +00006893 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6894 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00006895 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006896 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006897 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006898 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006899 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00006900
6901 // Build a factor node to remember that this load is independent of the
6902 // other one.
6903 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6904 Hi.getValue(1));
6905
6906 // Remember that we legalized the chain.
6907 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00006908 break;
6909 }
Dan Gohman7f321562007-06-25 16:23:39 +00006910 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00006911 // We know the result is a vector. The input may be either a vector or a
6912 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00006913 SDOperand InOp = Node->getOperand(0);
6914 if (!MVT::isVector(InOp.getValueType()) ||
6915 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6916 // The input is a scalar or single-element vector.
6917 // Lower to a store/load so that it can be split.
6918 // FIXME: this could be improved probably.
Chris Lattner85dd3be2007-10-15 17:48:57 +00006919 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00006920 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val);
Chris Lattner7692eb42006-03-23 21:16:34 +00006921
Evan Cheng786225a2006-10-05 23:01:46 +00006922 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00006923 InOp, Ptr,
Dan Gohman3069b872008-02-07 18:41:25 +00006924 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00006925 FI->getIndex());
6926 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman3069b872008-02-07 18:41:25 +00006927 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00006928 FI->getIndex());
Chris Lattner7692eb42006-03-23 21:16:34 +00006929 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00006930 // Split the vector and convert each of the pieces now.
6931 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006932 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6933 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00006934 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00006935 }
Chris Lattnerc7029802006-03-18 01:44:44 +00006936 }
6937
6938 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006939 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00006940 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00006941 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00006942}
6943
6944
Dan Gohman89b20c02007-06-27 14:06:22 +00006945/// ScalarizeVectorOp - Given an operand of single-element vector type
6946/// (e.g. v1f32), convert it into the equivalent operation that returns a
6947/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00006948SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6949 assert(MVT::isVector(Op.getValueType()) &&
6950 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006951 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00006952 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6953 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00006954
Dan Gohman7f321562007-06-25 16:23:39 +00006955 // See if we already scalarized it.
6956 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6957 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00006958
6959 SDOperand Result;
6960 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00006961 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006962#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006963 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006964#endif
Dan Gohman7f321562007-06-25 16:23:39 +00006965 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6966 case ISD::ADD:
6967 case ISD::FADD:
6968 case ISD::SUB:
6969 case ISD::FSUB:
6970 case ISD::MUL:
6971 case ISD::FMUL:
6972 case ISD::SDIV:
6973 case ISD::UDIV:
6974 case ISD::FDIV:
6975 case ISD::SREM:
6976 case ISD::UREM:
6977 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00006978 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006979 case ISD::AND:
6980 case ISD::OR:
6981 case ISD::XOR:
6982 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00006983 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00006984 ScalarizeVectorOp(Node->getOperand(0)),
6985 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00006986 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006987 case ISD::FNEG:
6988 case ISD::FABS:
6989 case ISD::FSQRT:
6990 case ISD::FSIN:
6991 case ISD::FCOS:
6992 Result = DAG.getNode(Node->getOpcode(),
6993 NewVT,
6994 ScalarizeVectorOp(Node->getOperand(0)));
6995 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00006996 case ISD::FPOWI:
6997 Result = DAG.getNode(Node->getOpcode(),
6998 NewVT,
6999 ScalarizeVectorOp(Node->getOperand(0)),
7000 Node->getOperand(1));
7001 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007002 case ISD::LOAD: {
7003 LoadSDNode *LD = cast<LoadSDNode>(Node);
7004 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7005 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00007006
Dan Gohman7f321562007-06-25 16:23:39 +00007007 const Value *SV = LD->getSrcValue();
7008 int SVOffset = LD->getSrcValueOffset();
7009 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
7010 LD->isVolatile(), LD->getAlignment());
7011
Chris Lattnerc7029802006-03-18 01:44:44 +00007012 // Remember that we legalized the chain.
7013 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7014 break;
7015 }
Dan Gohman7f321562007-06-25 16:23:39 +00007016 case ISD::BUILD_VECTOR:
7017 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007018 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007019 case ISD::INSERT_VECTOR_ELT:
7020 // Returning the inserted scalar element.
7021 Result = Node->getOperand(1);
7022 break;
7023 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007024 assert(Node->getOperand(0).getValueType() == NewVT &&
7025 "Concat of non-legal vectors not yet supported!");
7026 Result = Node->getOperand(0);
7027 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007028 case ISD::VECTOR_SHUFFLE: {
7029 // Figure out if the scalar is the LHS or RHS and return it.
7030 SDOperand EltNum = Node->getOperand(2).getOperand(0);
7031 if (cast<ConstantSDNode>(EltNum)->getValue())
7032 Result = ScalarizeVectorOp(Node->getOperand(1));
7033 else
7034 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007035 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007036 }
7037 case ISD::EXTRACT_SUBVECTOR:
7038 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00007039 assert(Result.getValueType() == NewVT);
7040 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007041 case ISD::BIT_CONVERT:
7042 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00007043 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007044 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007045 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007046 ScalarizeVectorOp(Op.getOperand(1)),
7047 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007048 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00007049 }
7050
7051 if (TLI.isTypeLegal(NewVT))
7052 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007053 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7054 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00007055 return Result;
7056}
7057
Chris Lattner3e928bb2005-01-07 07:47:09 +00007058
7059// SelectionDAG::Legalize - This is the entry point for the file.
7060//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00007061void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00007062 if (ViewLegalizeDAGs) viewGraph();
7063
Chris Lattner3e928bb2005-01-07 07:47:09 +00007064 /// run - This is the main entry point to this class.
7065 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00007066 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00007067}
7068