blob: 78c0263c14e2862bcd03f6dd4b52bb4e4766c9ed [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000019#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000020#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000022#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000025#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000026#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000027#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000029#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000030#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000031#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000032#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000033#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000034#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000035using namespace llvm;
36
Chris Lattner5e46a192006-04-02 03:07:27 +000037#ifndef NDEBUG
38static cl::opt<bool>
39ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
40 cl::desc("Pop up a window to show dags before legalize"));
41#else
42static const bool ViewLegalizeDAGs = 0;
43#endif
44
Chris Lattner3e928bb2005-01-07 07:47:09 +000045//===----------------------------------------------------------------------===//
46/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
47/// hacks on it until the target machine can handle it. This involves
48/// eliminating value sizes the machine cannot handle (promoting small sizes to
49/// large sizes or splitting up large values into small values) as well as
50/// eliminating operations the machine cannot handle.
51///
52/// This code also does a small amount of optimization and recognition of idioms
53/// as part of its processing. For example, if a target does not support a
54/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
55/// will attempt merge setcc and brc instructions into brcc's.
56///
57namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000058class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000059 TargetLowering &TLI;
60 SelectionDAG &DAG;
61
Chris Lattner6831a812006-02-13 09:18:02 +000062 // Libcall insertion helpers.
63
64 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
65 /// legalized. We use this to ensure that calls are properly serialized
66 /// against each other, including inserted libcalls.
67 SDOperand LastCALLSEQ_END;
68
69 /// IsLegalizingCall - This member is used *only* for purposes of providing
70 /// helpful assertions that a libcall isn't created while another call is
71 /// being legalized (which could lead to non-serialized call sequences).
72 bool IsLegalizingCall;
73
Chris Lattner3e928bb2005-01-07 07:47:09 +000074 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000075 Legal, // The target natively supports this operation.
76 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000077 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 };
Chris Lattner6831a812006-02-13 09:18:02 +000079
Chris Lattner3e928bb2005-01-07 07:47:09 +000080 /// ValueTypeActions - This is a bitvector that contains two bits for each
81 /// value type, where the two bits correspond to the LegalizeAction enum.
82 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000083 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000084
Chris Lattner3e928bb2005-01-07 07:47:09 +000085 /// LegalizedNodes - For nodes that are of legal width, and that have more
86 /// than one use, this map indicates what regularized operand to use. This
87 /// allows us to avoid legalizing the same thing more than once.
Roman Levensteine3263322008-03-26 12:39:26 +000088 DenseMap<SDOperandImpl, SDOperand> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000089
Chris Lattner03c85462005-01-15 05:21:40 +000090 /// PromotedNodes - For nodes that are below legal width, and that have more
91 /// than one use, this map indicates what promoted value to use. This allows
92 /// us to avoid promoting the same thing more than once.
Roman Levensteine3263322008-03-26 12:39:26 +000093 DenseMap<SDOperandImpl, SDOperand> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000094
Chris Lattnerc7029802006-03-18 01:44:44 +000095 /// ExpandedNodes - For nodes that need to be expanded this map indicates
96 /// which which operands are the expanded version of the input. This allows
97 /// us to avoid expanding the same node more than once.
Roman Levensteine3263322008-03-26 12:39:26 +000098 DenseMap<SDOperandImpl, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000099
Chris Lattnerc7029802006-03-18 01:44:44 +0000100 /// SplitNodes - For vector nodes that need to be split, this map indicates
101 /// which which operands are the split version of the input. This allows us
102 /// to avoid splitting the same node more than once.
103 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
104
Dan Gohman7f321562007-06-25 16:23:39 +0000105 /// ScalarizedNodes - For nodes that need to be converted from vector types to
106 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000107 /// processed to the result.
Dan Gohman7f321562007-06-25 16:23:39 +0000108 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000109
Chris Lattner8afc48e2005-01-07 22:28:47 +0000110 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000111 LegalizedNodes.insert(std::make_pair(From, To));
112 // If someone requests legalization of the new node, return itself.
113 if (From != To)
114 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000115 }
Chris Lattner03c85462005-01-15 05:21:40 +0000116 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner40030bf2007-02-04 01:17:38 +0000117 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000118 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000119 // If someone requests legalization of the new node, return itself.
120 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000121 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000122
Chris Lattner3e928bb2005-01-07 07:47:09 +0000123public:
124
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000125 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000126
Chris Lattner3e928bb2005-01-07 07:47:09 +0000127 /// getTypeAction - Return how we should legalize values of this type, either
128 /// it is already legal or we need to expand it into multiple registers of
129 /// smaller integer type, or we need to promote it to a larger type.
130 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000131 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000132 }
133
134 /// isTypeLegal - Return true if this type is legal on this target.
135 ///
136 bool isTypeLegal(MVT::ValueType VT) const {
137 return getTypeAction(VT) == Legal;
138 }
139
Chris Lattner3e928bb2005-01-07 07:47:09 +0000140 void LegalizeDAG();
141
Chris Lattner456a93a2006-01-28 07:39:30 +0000142private:
Dan Gohman7f321562007-06-25 16:23:39 +0000143 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000144 /// appropriate for its type.
145 void HandleOp(SDOperand Op);
146
147 /// LegalizeOp - We know that the specified value has a legal type.
148 /// Recursively ensure that the operands have legal types, then return the
149 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000150 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000151
Dan Gohman82669522007-10-11 23:57:53 +0000152 /// UnrollVectorOp - We know that the given vector has a legal type, however
153 /// the operation it performs is not legal and is an operation that we have
154 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
155 /// operating on each element individually.
156 SDOperand UnrollVectorOp(SDOperand O);
157
Chris Lattnerc7029802006-03-18 01:44:44 +0000158 /// PromoteOp - Given an operation that produces a value in an invalid type,
159 /// promote it to compute the value into a larger type. The produced value
160 /// will have the correct bits for the low portion of the register, but no
161 /// guarantee is made about the top bits: it may be zero, sign-extended, or
162 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000163 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000164
Chris Lattnerc7029802006-03-18 01:44:44 +0000165 /// ExpandOp - Expand the specified SDOperand into its two component pieces
166 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
167 /// the LegalizeNodes map is filled in for any results that are not expanded,
168 /// the ExpandedNodes map is filled in for any results that are expanded, and
169 /// the Lo/Hi values are returned. This applies to integer types and Vector
170 /// types.
171 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
172
Dan Gohman7f321562007-06-25 16:23:39 +0000173 /// SplitVectorOp - Given an operand of vector type, break it down into
174 /// two smaller values.
Chris Lattnerc7029802006-03-18 01:44:44 +0000175 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
176
Dan Gohman89b20c02007-06-27 14:06:22 +0000177 /// ScalarizeVectorOp - Given an operand of single-element vector type
178 /// (e.g. v1f32), convert it into the equivalent operation that returns a
179 /// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +0000180 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000181
Chris Lattner4352cc92006-04-04 17:23:26 +0000182 /// isShuffleLegal - Return true if a vector shuffle is legal with the
183 /// specified mask and type. Targets can specify exactly which masks they
184 /// support and the code generator is tasked with not creating illegal masks.
185 ///
186 /// Note that this will also return true for shuffles that are promoted to a
187 /// different type.
188 ///
189 /// If this is a legal shuffle, this method returns the (possibly promoted)
190 /// build_vector Mask. If it's not a legal shuffle, it returns null.
191 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
192
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000193 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000194 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000195
Nate Begeman750ac1b2006-02-01 07:19:44 +0000196 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
197
Reid Spencer47857812006-12-31 05:55:36 +0000198 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000199 SDOperand &Hi);
200 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
201 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000202
Chris Lattner1401d152008-01-16 07:45:30 +0000203 SDOperand EmitStackConvert(SDOperand SrcOp, MVT::ValueType SlotVT,
204 MVT::ValueType DestVT);
Chris Lattnerce872152006-03-19 06:31:19 +0000205 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000206 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000207 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
208 SDOperand LegalOp,
209 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000210 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
211 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000212 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
213 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000214
Chris Lattner456a93a2006-01-28 07:39:30 +0000215 SDOperand ExpandBSWAP(SDOperand Op);
216 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000217 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
218 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000219 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
220 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000221
Dan Gohman7f321562007-06-25 16:23:39 +0000222 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000223 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000224};
225}
226
Chris Lattner4352cc92006-04-04 17:23:26 +0000227/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
228/// specified mask and type. Targets can specify exactly which masks they
229/// support and the code generator is tasked with not creating illegal masks.
230///
231/// Note that this will also return true for shuffles that are promoted to a
232/// different type.
233SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
234 SDOperand Mask) const {
235 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
236 default: return 0;
237 case TargetLowering::Legal:
238 case TargetLowering::Custom:
239 break;
240 case TargetLowering::Promote: {
241 // If this is promoted to a different type, convert the shuffle mask and
242 // ask if it is legal in the promoted type!
243 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
244
245 // If we changed # elements, change the shuffle mask.
246 unsigned NumEltsGrowth =
247 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
248 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
249 if (NumEltsGrowth > 1) {
250 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000251 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000252 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
253 SDOperand InOp = Mask.getOperand(i);
254 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
255 if (InOp.getOpcode() == ISD::UNDEF)
256 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
257 else {
258 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
259 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
260 }
261 }
262 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000263 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000264 }
265 VT = NVT;
266 break;
267 }
268 }
269 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
270}
271
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000272SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
273 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
274 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000275 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000276 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000277}
278
Chris Lattnere10e6f72007-06-18 21:28:10 +0000279/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
280/// contains all of a nodes operands before it contains the node.
281static void ComputeTopDownOrdering(SelectionDAG &DAG,
282 SmallVector<SDNode*, 64> &Order) {
283
284 DenseMap<SDNode*, unsigned> Visited;
285 std::vector<SDNode*> Worklist;
286 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000287
Chris Lattnere10e6f72007-06-18 21:28:10 +0000288 // Compute ordering from all of the leaves in the graphs, those (like the
289 // entry node) that have no operands.
290 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
291 E = DAG.allnodes_end(); I != E; ++I) {
292 if (I->getNumOperands() == 0) {
293 Visited[I] = 0 - 1U;
294 Worklist.push_back(I);
295 }
Chris Lattner32fca002005-10-06 01:20:27 +0000296 }
297
Chris Lattnere10e6f72007-06-18 21:28:10 +0000298 while (!Worklist.empty()) {
299 SDNode *N = Worklist.back();
300 Worklist.pop_back();
301
302 if (++Visited[N] != N->getNumOperands())
303 continue; // Haven't visited all operands yet
304
305 Order.push_back(N);
306
307 // Now that we have N in, add anything that uses it if all of their operands
308 // are now done.
309 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
310 UI != E; ++UI)
Roman Levensteine3263322008-03-26 12:39:26 +0000311 Worklist.push_back(UI->getUser());
Chris Lattnere10e6f72007-06-18 21:28:10 +0000312 }
313
314 assert(Order.size() == Visited.size() &&
315 Order.size() ==
316 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
317 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000318}
319
Chris Lattner1618beb2005-07-29 00:11:56 +0000320
Chris Lattner3e928bb2005-01-07 07:47:09 +0000321void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000322 LastCALLSEQ_END = DAG.getEntryNode();
323 IsLegalizingCall = false;
324
Chris Lattnerab510a72005-10-02 17:49:46 +0000325 // The legalize process is inherently a bottom-up recursive process (users
326 // legalize their uses before themselves). Given infinite stack space, we
327 // could just start legalizing on the root and traverse the whole graph. In
328 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000329 // blocks. To avoid this problem, compute an ordering of the nodes where each
330 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000331 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000332 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000333
Chris Lattnerc7029802006-03-18 01:44:44 +0000334 for (unsigned i = 0, e = Order.size(); i != e; ++i)
335 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000336
337 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000338 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000339 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
340 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000341
342 ExpandedNodes.clear();
343 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000344 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000345 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000346 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000347
348 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000349 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000350}
351
Chris Lattner6831a812006-02-13 09:18:02 +0000352
353/// FindCallEndFromCallStart - Given a chained node that is part of a call
354/// sequence, find the CALLSEQ_END node that terminates the call sequence.
355static SDNode *FindCallEndFromCallStart(SDNode *Node) {
356 if (Node->getOpcode() == ISD::CALLSEQ_END)
357 return Node;
358 if (Node->use_empty())
359 return 0; // No CallSeqEnd
360
361 // The chain is usually at the end.
362 SDOperand TheChain(Node, Node->getNumValues()-1);
363 if (TheChain.getValueType() != MVT::Other) {
364 // Sometimes it's at the beginning.
365 TheChain = SDOperand(Node, 0);
366 if (TheChain.getValueType() != MVT::Other) {
367 // Otherwise, hunt for it.
368 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
369 if (Node->getValueType(i) == MVT::Other) {
370 TheChain = SDOperand(Node, i);
371 break;
372 }
373
374 // Otherwise, we walked into a node without a chain.
375 if (TheChain.getValueType() != MVT::Other)
376 return 0;
377 }
378 }
379
380 for (SDNode::use_iterator UI = Node->use_begin(),
381 E = Node->use_end(); UI != E; ++UI) {
382
383 // Make sure to only follow users of our token chain.
Roman Levensteine3263322008-03-26 12:39:26 +0000384 SDNode *User = UI->getUser();
Chris Lattner6831a812006-02-13 09:18:02 +0000385 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
386 if (User->getOperand(i) == TheChain)
387 if (SDNode *Result = FindCallEndFromCallStart(User))
388 return Result;
389 }
390 return 0;
391}
392
393/// FindCallStartFromCallEnd - Given a chained node that is part of a call
394/// sequence, find the CALLSEQ_START node that initiates the call sequence.
395static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
396 assert(Node && "Didn't find callseq_start for a call??");
397 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
398
399 assert(Node->getOperand(0).getValueType() == MVT::Other &&
400 "Node doesn't have a token chain argument!");
401 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
402}
403
404/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
405/// see if any uses can reach Dest. If no dest operands can get to dest,
406/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000407///
408/// Keep track of the nodes we fine that actually do lead to Dest in
409/// NodesLeadingTo. This avoids retraversing them exponential number of times.
410///
411bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000412 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000413 if (N == Dest) return true; // N certainly leads to Dest :)
414
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000415 // If we've already processed this node and it does lead to Dest, there is no
416 // need to reprocess it.
417 if (NodesLeadingTo.count(N)) return true;
418
Chris Lattner6831a812006-02-13 09:18:02 +0000419 // If the first result of this node has been already legalized, then it cannot
420 // reach N.
421 switch (getTypeAction(N->getValueType(0))) {
422 case Legal:
423 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
424 break;
425 case Promote:
426 if (PromotedNodes.count(SDOperand(N, 0))) return false;
427 break;
428 case Expand:
429 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
430 break;
431 }
432
433 // Okay, this node has not already been legalized. Check and legalize all
434 // operands. If none lead to Dest, then we can legalize this node.
435 bool OperandsLeadToDest = false;
436 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
437 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000438 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000439
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000440 if (OperandsLeadToDest) {
441 NodesLeadingTo.insert(N);
442 return true;
443 }
Chris Lattner6831a812006-02-13 09:18:02 +0000444
445 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000446 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000447 return false;
448}
449
Dan Gohman7f321562007-06-25 16:23:39 +0000450/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000451/// appropriate for its type.
452void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000453 MVT::ValueType VT = Op.getValueType();
454 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000455 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000456 case Legal: (void)LegalizeOp(Op); break;
457 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000458 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000459 if (!MVT::isVector(VT)) {
460 // If this is an illegal scalar, expand it into its two component
461 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000462 SDOperand X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000463 if (Op.getOpcode() == ISD::TargetConstant)
464 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000465 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000466 } else if (MVT::getVectorNumElements(VT) == 1) {
467 // If this is an illegal single element vector, convert it to a
468 // scalar operation.
469 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000470 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000471 // Otherwise, this is an illegal multiple element vector.
472 // Split it in half and legalize both parts.
473 SDOperand X, Y;
474 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000475 }
476 break;
477 }
478}
Chris Lattner6831a812006-02-13 09:18:02 +0000479
Evan Cheng9f877882006-12-13 20:57:08 +0000480/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
481/// a load from the constant pool.
482static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000483 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000484 bool Extend = false;
485
486 // If a FP immediate is precise when represented as a float and if the
487 // target can do an extending load from float to double, we put it into
488 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000489 // double. This shrinks FP constants and canonicalizes them for targets where
490 // an FP extending load is the same cost as a normal load (such as on the x87
491 // fp stack or PPC FP unit).
Evan Cheng00495212006-12-12 21:32:44 +0000492 MVT::ValueType VT = CFP->getValueType(0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +0000493 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000494 CFP->getValueAPF());
Evan Cheng9f877882006-12-13 20:57:08 +0000495 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000496 if (VT!=MVT::f64 && VT!=MVT::f32)
497 assert(0 && "Invalid type expansion");
Dan Gohman6cf9b8a2008-03-11 00:11:06 +0000498 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000499 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000500 }
501
Evan Chengef120572008-03-04 08:05:30 +0000502 MVT::ValueType OrigVT = VT;
503 MVT::ValueType SVT = VT;
504 while (SVT != MVT::f32) {
505 SVT = (unsigned)SVT - 1;
506 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
507 // Only do this if the target has a native EXTLOAD instruction from
508 // smaller type.
Evan Cheng6fd599f2008-03-05 01:30:59 +0000509 TLI.isLoadXLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000510 TLI.ShouldShrinkFPConstant(OrigVT)) {
Evan Chengef120572008-03-04 08:05:30 +0000511 const Type *SType = MVT::getTypeForValueType(SVT);
512 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
513 VT = SVT;
514 Extend = true;
515 }
Evan Cheng00495212006-12-12 21:32:44 +0000516 }
517
518 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Chengef120572008-03-04 08:05:30 +0000519 if (Extend)
520 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000521 CPIdx, PseudoSourceValue::getConstantPool(),
Evan Chengef120572008-03-04 08:05:30 +0000522 0, VT);
523 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
524 PseudoSourceValue::getConstantPool(), 0);
Evan Cheng00495212006-12-12 21:32:44 +0000525}
526
Chris Lattner6831a812006-02-13 09:18:02 +0000527
Evan Cheng912095b2007-01-04 21:56:39 +0000528/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
529/// operations.
530static
531SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
532 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000533 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000534 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000535 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
536 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000537 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000538
Evan Cheng912095b2007-01-04 21:56:39 +0000539 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000540 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000541 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
542 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000543 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000544 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000545 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000546 // Shift right or sign-extend it if the two operands have different types.
547 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
548 if (SizeDiff > 0) {
549 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
550 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
551 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
552 } else if (SizeDiff < 0)
553 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000554
555 // Clear the sign bit of first operand.
556 SDOperand Mask2 = (VT == MVT::f64)
557 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
558 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
559 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000560 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000561 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
562
563 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000564 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
565 return Result;
566}
567
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000568/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
569static
570SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
571 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000572 SDOperand Chain = ST->getChain();
573 SDOperand Ptr = ST->getBasePtr();
574 SDOperand Val = ST->getValue();
575 MVT::ValueType VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000576 int Alignment = ST->getAlignment();
577 int SVOffset = ST->getSrcValueOffset();
Dale Johannesen8155d642008-02-27 22:36:00 +0000578 if (MVT::isFloatingPoint(ST->getMemoryVT()) ||
579 MVT::isVector(ST->getMemoryVT())) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000580 // Expand to a bitconvert of the value to the integer type of the
581 // same size, then a (misaligned) int store.
582 MVT::ValueType intVT;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000583 if (MVT::is128BitVector(VT) || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000584 intVT = MVT::i128;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000585 else if (MVT::is64BitVector(VT) || VT==MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000586 intVT = MVT::i64;
587 else if (VT==MVT::f32)
588 intVT = MVT::i32;
589 else
Dale Johannesencd9f1742008-02-28 18:36:51 +0000590 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000591
592 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
593 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
594 SVOffset, ST->isVolatile(), Alignment);
595 }
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000596 assert(MVT::isInteger(ST->getMemoryVT()) &&
Dale Johannesen8155d642008-02-27 22:36:00 +0000597 !MVT::isVector(ST->getMemoryVT()) &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000598 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000599 // Get the half-size VT
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000600 MVT::ValueType NewStoredVT = ST->getMemoryVT() - 1;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000601 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000602 int IncrementSize = NumBits / 8;
603
604 // Divide the stored value in two parts.
605 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
606 SDOperand Lo = Val;
607 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
608
609 // Store the two parts
610 SDOperand Store1, Store2;
611 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
612 ST->getSrcValue(), SVOffset, NewStoredVT,
613 ST->isVolatile(), Alignment);
614 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
615 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000616 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000617 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
618 ST->getSrcValue(), SVOffset + IncrementSize,
619 NewStoredVT, ST->isVolatile(), Alignment);
620
621 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
622}
623
624/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
625static
626SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
627 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000628 int SVOffset = LD->getSrcValueOffset();
629 SDOperand Chain = LD->getChain();
630 SDOperand Ptr = LD->getBasePtr();
631 MVT::ValueType VT = LD->getValueType(0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000632 MVT::ValueType LoadedVT = LD->getMemoryVT();
Dale Johannesen8155d642008-02-27 22:36:00 +0000633 if (MVT::isFloatingPoint(VT) || MVT::isVector(VT)) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000634 // Expand to a (misaligned) integer load of the same size,
Dale Johannesen8155d642008-02-27 22:36:00 +0000635 // then bitconvert to floating point or vector.
Dale Johannesen907f28c2007-09-08 19:29:23 +0000636 MVT::ValueType intVT;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000637 if (MVT::is128BitVector(LoadedVT) ||
638 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000639 intVT = MVT::i128;
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000640 else if (MVT::is64BitVector(LoadedVT) || LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000641 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000642 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000643 intVT = MVT::i32;
644 else
Dale Johannesen8155d642008-02-27 22:36:00 +0000645 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000646
647 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
648 SVOffset, LD->isVolatile(),
649 LD->getAlignment());
650 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Dale Johannesen8155d642008-02-27 22:36:00 +0000651 if (MVT::isFloatingPoint(VT) && LoadedVT != VT)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000652 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
653
654 SDOperand Ops[] = { Result, Chain };
655 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
656 Ops, 2);
657 }
Dale Johannesen8155d642008-02-27 22:36:00 +0000658 assert(MVT::isInteger(LoadedVT) && !MVT::isVector(LoadedVT) &&
Chris Lattnere400af82007-11-19 21:38:03 +0000659 "Unaligned load of unsupported type.");
660
Dale Johannesen8155d642008-02-27 22:36:00 +0000661 // Compute the new VT that is half the size of the old one. This is an
662 // integer MVT.
Chris Lattnere400af82007-11-19 21:38:03 +0000663 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
664 MVT::ValueType NewLoadedVT;
Dale Johannesen8155d642008-02-27 22:36:00 +0000665 NewLoadedVT = MVT::getIntegerType(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000666 NumBits >>= 1;
667
668 unsigned Alignment = LD->getAlignment();
669 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000670 ISD::LoadExtType HiExtType = LD->getExtensionType();
671
672 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
673 if (HiExtType == ISD::NON_EXTLOAD)
674 HiExtType = ISD::ZEXTLOAD;
675
676 // Load the value in two parts
677 SDOperand Lo, Hi;
678 if (TLI.isLittleEndian()) {
679 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
680 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
681 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
682 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
683 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
684 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000685 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000686 } else {
687 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
688 NewLoadedVT,LD->isVolatile(), Alignment);
689 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
690 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
691 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
692 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000693 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000694 }
695
696 // aggregate the two parts
697 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
698 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
699 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
700
701 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
702 Hi.getValue(1));
703
704 SDOperand Ops[] = { Result, TF };
705 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
706}
Evan Cheng912095b2007-01-04 21:56:39 +0000707
Dan Gohman82669522007-10-11 23:57:53 +0000708/// UnrollVectorOp - We know that the given vector has a legal type, however
709/// the operation it performs is not legal and is an operation that we have
710/// no way of lowering. "Unroll" the vector, splitting out the scalars and
711/// operating on each element individually.
712SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
713 MVT::ValueType VT = Op.getValueType();
714 assert(isTypeLegal(VT) &&
715 "Caller should expand or promote operands that are not legal!");
716 assert(Op.Val->getNumValues() == 1 &&
717 "Can't unroll a vector with multiple results!");
718 unsigned NE = MVT::getVectorNumElements(VT);
719 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
720
721 SmallVector<SDOperand, 8> Scalars;
722 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
723 for (unsigned i = 0; i != NE; ++i) {
724 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
725 SDOperand Operand = Op.getOperand(j);
726 MVT::ValueType OperandVT = Operand.getValueType();
727 if (MVT::isVector(OperandVT)) {
728 // A vector operand; extract a single element.
729 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
730 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
731 OperandEltVT,
732 Operand,
733 DAG.getConstant(i, MVT::i32));
734 } else {
735 // A scalar operand; just use it as is.
736 Operands[j] = Operand;
737 }
738 }
739 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
740 &Operands[0], Operands.size()));
741 }
742
743 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
744}
745
Duncan Sands007f9842008-01-10 10:28:30 +0000746/// GetFPLibCall - Return the right libcall for the given floating point type.
747static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
748 RTLIB::Libcall Call_F32,
749 RTLIB::Libcall Call_F64,
750 RTLIB::Libcall Call_F80,
751 RTLIB::Libcall Call_PPCF128) {
752 return
753 VT == MVT::f32 ? Call_F32 :
754 VT == MVT::f64 ? Call_F64 :
755 VT == MVT::f80 ? Call_F80 :
756 VT == MVT::ppcf128 ? Call_PPCF128 :
757 RTLIB::UNKNOWN_LIBCALL;
758}
759
Dan Gohmana3466152007-07-13 20:14:11 +0000760/// LegalizeOp - We know that the specified value has a legal type, and
761/// that its operands are legal. Now ensure that the operation itself
762/// is legal, recursively ensuring that the operands' operations remain
763/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000764SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000765 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
766 return Op;
767
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000768 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000769 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000770 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000771
Chris Lattner3e928bb2005-01-07 07:47:09 +0000772 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000773 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000774 if (Node->getNumValues() > 1) {
775 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000776 if (getTypeAction(Node->getValueType(i)) != Legal) {
777 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000778 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000779 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000780 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000781 }
782 }
783
Chris Lattner45982da2005-05-12 16:53:42 +0000784 // Note that LegalizeOp may be reentered even from single-use nodes, which
785 // means that we always must cache transformed nodes.
Roman Levensteine3263322008-03-26 12:39:26 +0000786 DenseMap<SDOperandImpl, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000787 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000788
Nate Begeman9373a812005-08-10 20:51:12 +0000789 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000790 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000791 bool isCustom = false;
792
Chris Lattner3e928bb2005-01-07 07:47:09 +0000793 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000794 case ISD::FrameIndex:
795 case ISD::EntryToken:
796 case ISD::Register:
797 case ISD::BasicBlock:
798 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000799 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000800 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000801 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000802 case ISD::TargetConstantPool:
803 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000804 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000805 case ISD::TargetExternalSymbol:
806 case ISD::VALUETYPE:
807 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +0000808 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +0000809 case ISD::STRING:
810 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +0000811 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +0000812 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000813 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000814 "This must be legal!");
815 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000816 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000817 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
818 // If this is a target node, legalize it by legalizing the operands then
819 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000820 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000821 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000822 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000823
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000824 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000825
826 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
827 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
828 return Result.getValue(Op.ResNo);
829 }
830 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000831#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000832 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000833#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000834 assert(0 && "Do not know how to legalize this operator!");
835 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000836 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000837 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000838 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000839 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000840 case ISD::ConstantPool:
841 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000842 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
843 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000844 case TargetLowering::Custom:
845 Tmp1 = TLI.LowerOperation(Op, DAG);
846 if (Tmp1.Val) Result = Tmp1;
847 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000848 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000849 break;
850 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000851 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000852 case ISD::FRAMEADDR:
853 case ISD::RETURNADDR:
854 // The only option for these nodes is to custom lower them. If the target
855 // does not custom lower them, then return zero.
856 Tmp1 = TLI.LowerOperation(Op, DAG);
857 if (Tmp1.Val)
858 Result = Tmp1;
859 else
860 Result = DAG.getConstant(0, TLI.getPointerTy());
861 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000862 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000863 MVT::ValueType VT = Node->getValueType(0);
864 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
865 default: assert(0 && "This action is not supported yet!");
866 case TargetLowering::Custom:
867 Result = TLI.LowerOperation(Op, DAG);
868 if (Result.Val) break;
869 // Fall Thru
870 case TargetLowering::Legal:
871 Result = DAG.getConstant(0, VT);
872 break;
873 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000874 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000875 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000876 case ISD::EXCEPTIONADDR: {
877 Tmp1 = LegalizeOp(Node->getOperand(0));
878 MVT::ValueType VT = Node->getValueType(0);
879 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
880 default: assert(0 && "This action is not supported yet!");
881 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000882 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000883 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000884 }
885 break;
886 case TargetLowering::Custom:
887 Result = TLI.LowerOperation(Op, DAG);
888 if (Result.Val) break;
889 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000890 case TargetLowering::Legal: {
891 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
892 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000893 Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000894 break;
895 }
896 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000897 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000898 if (Result.Val->getNumValues() == 1) break;
899
900 assert(Result.Val->getNumValues() == 2 &&
901 "Cannot return more than two values!");
902
903 // Since we produced two values, make sure to remember that we
904 // legalized both of them.
905 Tmp1 = LegalizeOp(Result);
906 Tmp2 = LegalizeOp(Result.getValue(1));
907 AddLegalizedOperand(Op.getValue(0), Tmp1);
908 AddLegalizedOperand(Op.getValue(1), Tmp2);
909 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000910 case ISD::EHSELECTION: {
911 Tmp1 = LegalizeOp(Node->getOperand(0));
912 Tmp2 = LegalizeOp(Node->getOperand(1));
913 MVT::ValueType VT = Node->getValueType(0);
914 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
915 default: assert(0 && "This action is not supported yet!");
916 case TargetLowering::Expand: {
917 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000918 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +0000919 }
920 break;
921 case TargetLowering::Custom:
922 Result = TLI.LowerOperation(Op, DAG);
923 if (Result.Val) break;
924 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000925 case TargetLowering::Legal: {
926 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
927 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sandsb027fa02007-12-31 18:35:50 +0000928 Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +0000929 break;
930 }
931 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000932 }
Duncan Sandsb027fa02007-12-31 18:35:50 +0000933 if (Result.Val->getNumValues() == 1) break;
934
935 assert(Result.Val->getNumValues() == 2 &&
936 "Cannot return more than two values!");
937
938 // Since we produced two values, make sure to remember that we
939 // legalized both of them.
940 Tmp1 = LegalizeOp(Result);
941 Tmp2 = LegalizeOp(Result.getValue(1));
942 AddLegalizedOperand(Op.getValue(0), Tmp1);
943 AddLegalizedOperand(Op.getValue(1), Tmp2);
944 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000945 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000946 MVT::ValueType VT = Node->getValueType(0);
947 // The only "good" option for this node is to custom lower it.
948 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
949 default: assert(0 && "This action is not supported at all!");
950 case TargetLowering::Custom:
951 Result = TLI.LowerOperation(Op, DAG);
952 if (Result.Val) break;
953 // Fall Thru
954 case TargetLowering::Legal:
955 // Target does not know, how to lower this, lower to noop
956 Result = LegalizeOp(Node->getOperand(0));
957 break;
958 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000959 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000960 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000961 case ISD::AssertSext:
962 case ISD::AssertZext:
963 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000964 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000965 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000966 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000967 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000968 Result = Node->getOperand(Op.ResNo);
969 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000970 case ISD::CopyFromReg:
971 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000972 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000973 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000975 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000976 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000977 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000978 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000979 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
980 } else {
981 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
982 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000983 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
984 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000985 // Since CopyFromReg produces two values, make sure to remember that we
986 // legalized both of them.
987 AddLegalizedOperand(Op.getValue(0), Result);
988 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
989 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000990 case ISD::UNDEF: {
991 MVT::ValueType VT = Op.getValueType();
992 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000993 default: assert(0 && "This action is not supported yet!");
994 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000995 if (MVT::isInteger(VT))
996 Result = DAG.getConstant(0, VT);
997 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf41db212007-09-26 17:26:49 +0000998 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
999 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001000 else
1001 assert(0 && "Unknown value type!");
1002 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001003 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001004 break;
1005 }
1006 break;
1007 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001008
Chris Lattner48b61a72006-03-28 00:40:33 +00001009 case ISD::INTRINSIC_W_CHAIN:
1010 case ISD::INTRINSIC_WO_CHAIN:
1011 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001012 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001013 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1014 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001015 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001016
1017 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001018 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001019 TargetLowering::Custom) {
1020 Tmp3 = TLI.LowerOperation(Result, DAG);
1021 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001022 }
1023
1024 if (Result.Val->getNumValues() == 1) break;
1025
1026 // Must have return value and chain result.
1027 assert(Result.Val->getNumValues() == 2 &&
1028 "Cannot return more than two values!");
1029
1030 // Since loads produce two values, make sure to remember that we
1031 // legalized both of them.
1032 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1033 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1034 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001035 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001036
1037 case ISD::LOCATION:
1038 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1039 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1040
1041 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1042 case TargetLowering::Promote:
1043 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001044 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001045 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001046 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +00001047 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001048
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001049 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001050 const std::string &FName =
1051 cast<StringSDNode>(Node->getOperand(3))->getValue();
1052 const std::string &DirName =
1053 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001054 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001055
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001056 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +00001057 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +00001058 SDOperand LineOp = Node->getOperand(1);
1059 SDOperand ColOp = Node->getOperand(2);
1060
1061 if (useDEBUG_LOC) {
1062 Ops.push_back(LineOp); // line #
1063 Ops.push_back(ColOp); // col #
1064 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001065 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001066 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +00001067 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1068 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Evan Chenga647c922008-02-01 02:05:57 +00001069 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001070 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Evan Chengbb81d972008-01-31 09:59:15 +00001071 Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label
1072 Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001073 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001074 } else {
1075 Result = Tmp1; // chain
1076 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001077 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001078 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001079 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +00001080 if (Tmp1 != Node->getOperand(0) ||
1081 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001082 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001083 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +00001084 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1085 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1086 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1087 } else {
1088 // Otherwise promote them.
1089 Ops.push_back(PromoteOp(Node->getOperand(1)));
1090 Ops.push_back(PromoteOp(Node->getOperand(2)));
1091 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001092 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1093 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001094 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001095 }
1096 break;
1097 }
1098 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001099
1100 case ISD::DECLARE:
1101 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1102 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1103 default: assert(0 && "This action is not supported yet!");
1104 case TargetLowering::Legal:
1105 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1106 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1107 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1108 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1109 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001110 case TargetLowering::Expand:
1111 Result = LegalizeOp(Node->getOperand(0));
1112 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001113 }
1114 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001115
1116 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001117 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001118 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001119 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +00001120 case TargetLowering::Legal:
1121 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1122 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1123 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1124 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001125 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001126 break;
1127 }
1128 break;
1129
Jim Laskey1ee29252007-01-26 14:34:52 +00001130 case ISD::LABEL:
Evan Chengbb81d972008-01-31 09:59:15 +00001131 assert(Node->getNumOperands() == 3 && "Invalid LABEL node!");
Jim Laskey1ee29252007-01-26 14:34:52 +00001132 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001133 default: assert(0 && "This action is not supported yet!");
1134 case TargetLowering::Legal:
1135 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1136 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Evan Chengbb81d972008-01-31 09:59:15 +00001137 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand.
1138 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001139 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001140 case TargetLowering::Expand:
1141 Result = LegalizeOp(Node->getOperand(0));
1142 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001143 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001144 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001145
Evan Cheng27b7db52008-03-08 00:58:38 +00001146 case ISD::PREFETCH:
1147 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1148 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1149 default: assert(0 && "This action is not supported yet!");
1150 case TargetLowering::Legal:
1151 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1152 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1153 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1154 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1155 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1156 break;
1157 case TargetLowering::Expand:
1158 // It's a noop.
1159 Result = LegalizeOp(Node->getOperand(0));
1160 break;
1161 }
1162 break;
1163
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001164 case ISD::MEMBARRIER: {
1165 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001166 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1167 default: assert(0 && "This action is not supported yet!");
1168 case TargetLowering::Legal: {
1169 SDOperand Ops[6];
1170 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001171 for (int x = 1; x < 6; ++x) {
1172 Ops[x] = Node->getOperand(x);
1173 if (!isTypeLegal(Ops[x].getValueType()))
1174 Ops[x] = PromoteOp(Ops[x]);
1175 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001176 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1177 break;
1178 }
1179 case TargetLowering::Expand:
1180 //There is no libgcc call for this op
1181 Result = Node->getOperand(0); // Noop
1182 break;
1183 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001184 break;
1185 }
1186
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001187 case ISD::ATOMIC_LCS:
1188 case ISD::ATOMIC_LAS:
1189 case ISD::ATOMIC_SWAP: {
1190 assert(((Node->getNumOperands() == 4 && Node->getOpcode() == ISD::ATOMIC_LCS) ||
1191 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_LAS) ||
1192 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_SWAP)) &&
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001193 "Invalid Atomic node!");
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001194 int num = Node->getOpcode() == ISD::ATOMIC_LCS ? 4 : 3;
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001195 SDOperand Ops[4];
1196 for (int x = 0; x < num; ++x)
1197 Ops[x] = LegalizeOp(Node->getOperand(x));
1198 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num);
1199
1200 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001201 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001202 case TargetLowering::Custom:
1203 Result = TLI.LowerOperation(Result, DAG);
1204 break;
1205 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001206 break;
1207 }
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001208 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1209 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1210 return Result.getValue(Op.ResNo);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001211 }
1212
Scott Michelc1513d22007-08-08 23:23:31 +00001213 case ISD::Constant: {
1214 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1215 unsigned opAction =
1216 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1217
Chris Lattner3e928bb2005-01-07 07:47:09 +00001218 // We know we don't need to expand constants here, constants only have one
1219 // value and we check that it is fine above.
1220
Scott Michelc1513d22007-08-08 23:23:31 +00001221 if (opAction == TargetLowering::Custom) {
1222 Tmp1 = TLI.LowerOperation(Result, DAG);
1223 if (Tmp1.Val)
1224 Result = Tmp1;
1225 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001226 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001227 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001228 case ISD::ConstantFP: {
1229 // Spill FP immediates to the constant pool if the target cannot directly
1230 // codegen them. Targets often have some immediate values that can be
1231 // efficiently generated into an FP register without a load. We explicitly
1232 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001233 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1234
Chris Lattner3181a772006-01-29 06:26:56 +00001235 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1236 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001237 case TargetLowering::Legal:
1238 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001239 case TargetLowering::Custom:
1240 Tmp3 = TLI.LowerOperation(Result, DAG);
1241 if (Tmp3.Val) {
1242 Result = Tmp3;
1243 break;
1244 }
1245 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001246 case TargetLowering::Expand: {
1247 // Check to see if this FP immediate is already legal.
1248 bool isLegal = false;
1249 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1250 E = TLI.legal_fpimm_end(); I != E; ++I) {
1251 if (CFP->isExactlyValue(*I)) {
1252 isLegal = true;
1253 break;
1254 }
1255 }
1256 // If this is a legal constant, turn it into a TargetConstantFP node.
1257 if (isLegal)
1258 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001259 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001260 }
Nate Begemane1795842008-02-14 08:57:00 +00001261 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001262 break;
1263 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001264 case ISD::TokenFactor:
1265 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001266 Tmp1 = LegalizeOp(Node->getOperand(0));
1267 Tmp2 = LegalizeOp(Node->getOperand(1));
1268 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1269 } else if (Node->getNumOperands() == 3) {
1270 Tmp1 = LegalizeOp(Node->getOperand(0));
1271 Tmp2 = LegalizeOp(Node->getOperand(1));
1272 Tmp3 = LegalizeOp(Node->getOperand(2));
1273 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001274 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001275 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001276 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001277 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1278 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001279 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001280 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001281 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001282
1283 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001284 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001285 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001286 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1287 assert(Tmp3.Val && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001288 // A call within a calling sequence must be legalized to something
1289 // other than the normal CALLSEQ_END. Violating this gets Legalize
1290 // into an infinite loop.
1291 assert ((!IsLegalizingCall ||
1292 Node->getOpcode() != ISD::CALL ||
1293 Tmp3.Val->getOpcode() != ISD::CALLSEQ_END) &&
1294 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001295
1296 // The number of incoming and outgoing values should match; unless the final
1297 // outgoing value is a flag.
1298 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1299 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1300 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1301 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001302 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001303
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001304 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001305 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001306 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001307 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1308 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001309 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001310 if (Op.ResNo == i)
1311 Tmp2 = Tmp1;
1312 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1313 }
1314 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001315 case ISD::EXTRACT_SUBREG: {
1316 Tmp1 = LegalizeOp(Node->getOperand(0));
1317 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1318 assert(idx && "Operand must be a constant");
1319 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1321 }
1322 break;
1323 case ISD::INSERT_SUBREG: {
1324 Tmp1 = LegalizeOp(Node->getOperand(0));
1325 Tmp2 = LegalizeOp(Node->getOperand(1));
1326 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1327 assert(idx && "Operand must be a constant");
1328 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1329 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1330 }
1331 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001332 case ISD::BUILD_VECTOR:
1333 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001334 default: assert(0 && "This action is not supported yet!");
1335 case TargetLowering::Custom:
1336 Tmp3 = TLI.LowerOperation(Result, DAG);
1337 if (Tmp3.Val) {
1338 Result = Tmp3;
1339 break;
1340 }
1341 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001342 case TargetLowering::Expand:
1343 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001344 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001345 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001346 break;
1347 case ISD::INSERT_VECTOR_ELT:
1348 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001349 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001350
1351 // The type of the value to insert may not be legal, even though the vector
1352 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1353 // here.
1354 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1355 default: assert(0 && "Cannot expand insert element operand");
1356 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1357 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1358 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001359 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1360
1361 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1362 Node->getValueType(0))) {
1363 default: assert(0 && "This action is not supported yet!");
1364 case TargetLowering::Legal:
1365 break;
1366 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001367 Tmp4 = TLI.LowerOperation(Result, DAG);
1368 if (Tmp4.Val) {
1369 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001370 break;
1371 }
1372 // FALLTHROUGH
1373 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001374 // If the insert index is a constant, codegen this as a scalar_to_vector,
1375 // then a shuffle that inserts it into the right position in the vector.
1376 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001377 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1378 // match the element type of the vector being created.
1379 if (Tmp2.getValueType() ==
1380 MVT::getVectorElementType(Op.getValueType())) {
1381 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1382 Tmp1.getValueType(), Tmp2);
1383
1384 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1385 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1386 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1387
1388 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1389 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1390 // elt 0 of the RHS.
1391 SmallVector<SDOperand, 8> ShufOps;
1392 for (unsigned i = 0; i != NumElts; ++i) {
1393 if (i != InsertPos->getValue())
1394 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1395 else
1396 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1397 }
1398 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1399 &ShufOps[0], ShufOps.size());
1400
1401 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1402 Tmp1, ScVec, ShufMask);
1403 Result = LegalizeOp(Result);
1404 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001405 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001406 }
1407
Chris Lattner2332b9f2006-03-19 01:17:20 +00001408 // If the target doesn't support this, we have to spill the input vector
1409 // to a temporary stack slot, update the element, then reload it. This is
1410 // badness. We could also load the value into a vector register (either
1411 // with a "move to register" or "extload into register" instruction, then
1412 // permute it into place, if the idx is a constant and if the idx is
1413 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001414 MVT::ValueType VT = Tmp1.getValueType();
Nate Begeman0325d902008-02-13 06:43:04 +00001415 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Chengf6bf87f2006-04-08 01:46:37 +00001416 MVT::ValueType IdxVT = Tmp3.getValueType();
1417 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattner85dd3be2007-10-15 17:48:57 +00001418 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohman69de1932008-02-06 22:27:42 +00001419
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00001420 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val);
Dan Gohman69de1932008-02-06 22:27:42 +00001421 int SPFI = StackPtrFI->getIndex();
1422
Evan Chengeb0b4612006-03-31 01:27:51 +00001423 // Store the vector.
Dan Gohman69de1932008-02-06 22:27:42 +00001424 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00001425 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00001426 SPFI);
Evan Chengeb0b4612006-03-31 01:27:51 +00001427
1428 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001429 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1430 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001431 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001432 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1433 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1434 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001435 // Store the scalar value.
Nate Begeman0325d902008-02-13 06:43:04 +00001436 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
1437 PseudoSourceValue::getFixedStack(), SPFI, EltVT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001438 // Load the updated vector.
Dan Gohman69de1932008-02-06 22:27:42 +00001439 Result = DAG.getLoad(VT, Ch, StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00001440 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001441 break;
1442 }
1443 }
1444 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001445 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001446 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1447 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1448 break;
1449 }
1450
Chris Lattnerce872152006-03-19 06:31:19 +00001451 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1452 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1453 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1454 Node->getValueType(0))) {
1455 default: assert(0 && "This action is not supported yet!");
1456 case TargetLowering::Legal:
1457 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001458 case TargetLowering::Custom:
1459 Tmp3 = TLI.LowerOperation(Result, DAG);
1460 if (Tmp3.Val) {
1461 Result = Tmp3;
1462 break;
1463 }
1464 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001465 case TargetLowering::Expand:
1466 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001467 break;
1468 }
Chris Lattnerce872152006-03-19 06:31:19 +00001469 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001470 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001471 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1472 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1473 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1474
1475 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001476 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1477 default: assert(0 && "Unknown operation action!");
1478 case TargetLowering::Legal:
1479 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1480 "vector shuffle should not be created if not legal!");
1481 break;
1482 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001483 Tmp3 = TLI.LowerOperation(Result, DAG);
1484 if (Tmp3.Val) {
1485 Result = Tmp3;
1486 break;
1487 }
1488 // FALLTHROUGH
1489 case TargetLowering::Expand: {
1490 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001491 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001492 MVT::ValueType PtrVT = TLI.getPointerTy();
1493 SDOperand Mask = Node->getOperand(2);
1494 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001495 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001496 for (unsigned i = 0; i != NumElems; ++i) {
1497 SDOperand Arg = Mask.getOperand(i);
1498 if (Arg.getOpcode() == ISD::UNDEF) {
1499 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1500 } else {
1501 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1502 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1503 if (Idx < NumElems)
1504 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1505 DAG.getConstant(Idx, PtrVT)));
1506 else
1507 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1508 DAG.getConstant(Idx - NumElems, PtrVT)));
1509 }
1510 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001511 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001512 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001513 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001514 case TargetLowering::Promote: {
1515 // Change base type to a different vector type.
1516 MVT::ValueType OVT = Node->getValueType(0);
1517 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1518
1519 // Cast the two input vectors.
1520 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1521 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1522
1523 // Convert the shuffle mask to the right # elements.
1524 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1525 assert(Tmp3.Val && "Shuffle not legal?");
1526 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1527 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1528 break;
1529 }
Chris Lattner87100e02006-03-20 01:52:29 +00001530 }
1531 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001532
1533 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001534 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001535 Tmp2 = LegalizeOp(Node->getOperand(1));
1536 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001537 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001538 break;
1539
Dan Gohman7f321562007-06-25 16:23:39 +00001540 case ISD::EXTRACT_SUBVECTOR:
1541 Tmp1 = Node->getOperand(0);
1542 Tmp2 = LegalizeOp(Node->getOperand(1));
1543 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1544 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001545 break;
1546
Chris Lattner6831a812006-02-13 09:18:02 +00001547 case ISD::CALLSEQ_START: {
1548 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1549
1550 // Recursively Legalize all of the inputs of the call end that do not lead
1551 // to this call start. This ensures that any libcalls that need be inserted
1552 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001553 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001554 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001555 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1556 NodesLeadingTo);
1557 }
Chris Lattner6831a812006-02-13 09:18:02 +00001558
1559 // Now that we legalized all of the inputs (which may have inserted
1560 // libcalls) create the new CALLSEQ_START node.
1561 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1562
1563 // Merge in the last call, to ensure that this call start after the last
1564 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001565 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001566 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1567 Tmp1 = LegalizeOp(Tmp1);
1568 }
Chris Lattner6831a812006-02-13 09:18:02 +00001569
1570 // Do not try to legalize the target-specific arguments (#1+).
1571 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001572 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001573 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001574 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001575 }
1576
1577 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001578 AddLegalizedOperand(Op.getValue(0), Result);
1579 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1580 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1581
Chris Lattner6831a812006-02-13 09:18:02 +00001582 // Now that the callseq_start and all of the non-call nodes above this call
1583 // sequence have been legalized, legalize the call itself. During this
1584 // process, no libcalls can/will be inserted, guaranteeing that no calls
1585 // can overlap.
1586 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1587 SDOperand InCallSEQ = LastCALLSEQ_END;
1588 // Note that we are selecting this call!
1589 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1590 IsLegalizingCall = true;
1591
1592 // Legalize the call, starting from the CALLSEQ_END.
1593 LegalizeOp(LastCALLSEQ_END);
1594 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1595 return Result;
1596 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001597 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001598 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1599 // will cause this node to be legalized as well as handling libcalls right.
1600 if (LastCALLSEQ_END.Val != Node) {
1601 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Roman Levensteine3263322008-03-26 12:39:26 +00001602 DenseMap<SDOperandImpl, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001603 assert(I != LegalizedNodes.end() &&
1604 "Legalizing the call start should have legalized this node!");
1605 return I->second;
1606 }
1607
1608 // Otherwise, the call start has been legalized and everything is going
1609 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001610 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001611 // Do not try to legalize the target-specific arguments (#1+), except for
1612 // an optional flag input.
1613 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1614 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001615 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001616 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001617 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001618 }
1619 } else {
1620 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1621 if (Tmp1 != Node->getOperand(0) ||
1622 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001623 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001624 Ops[0] = Tmp1;
1625 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001626 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001627 }
Chris Lattner6a542892006-01-24 05:48:21 +00001628 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001629 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001630 // This finishes up call legalization.
1631 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001632
1633 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1634 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1635 if (Node->getNumValues() == 2)
1636 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1637 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001638 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001639 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001640 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1641 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1642 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001643 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001644
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001645 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001646 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001647 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001648 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001649 case TargetLowering::Expand: {
1650 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1651 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1652 " not tell us which reg is the stack pointer!");
1653 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001654
1655 // Chain the dynamic stack allocation so that it doesn't modify the stack
1656 // pointer when other instructions are using the stack.
1657 Chain = DAG.getCALLSEQ_START(Chain,
1658 DAG.getConstant(0, TLI.getPointerTy()));
1659
Chris Lattner903d2782006-01-15 08:54:32 +00001660 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001661 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1662 Chain = SP.getValue(1);
1663 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1664 unsigned StackAlign =
1665 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1666 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001667 SP = DAG.getNode(ISD::AND, VT, SP,
1668 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001669 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001670 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1671
1672 Tmp2 =
1673 DAG.getCALLSEQ_END(Chain,
1674 DAG.getConstant(0, TLI.getPointerTy()),
1675 DAG.getConstant(0, TLI.getPointerTy()),
1676 SDOperand());
1677
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001678 Tmp1 = LegalizeOp(Tmp1);
1679 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001680 break;
1681 }
1682 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001683 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1684 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001685 Tmp1 = LegalizeOp(Tmp3);
1686 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001687 }
Chris Lattner903d2782006-01-15 08:54:32 +00001688 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001689 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001690 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001691 }
Chris Lattner903d2782006-01-15 08:54:32 +00001692 // Since this op produce two values, make sure to remember that we
1693 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001694 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1695 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001696 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001697 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001698 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001699 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001700 bool Changed = false;
1701 // Legalize all of the operands of the inline asm, in case they are nodes
1702 // that need to be expanded or something. Note we skip the asm string and
1703 // all of the TargetConstant flags.
1704 SDOperand Op = LegalizeOp(Ops[0]);
1705 Changed = Op != Ops[0];
1706 Ops[0] = Op;
1707
1708 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1709 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1710 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1711 for (++i; NumVals; ++i, --NumVals) {
1712 SDOperand Op = LegalizeOp(Ops[i]);
1713 if (Op != Ops[i]) {
1714 Changed = true;
1715 Ops[i] = Op;
1716 }
1717 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001718 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001719
1720 if (HasInFlag) {
1721 Op = LegalizeOp(Ops.back());
1722 Changed |= Op != Ops.back();
1723 Ops.back() = Op;
1724 }
1725
1726 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001727 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001728
1729 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001730 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001731 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1732 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001733 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001734 case ISD::BR:
1735 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001736 // Ensure that libcalls are emitted before a branch.
1737 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1738 Tmp1 = LegalizeOp(Tmp1);
1739 LastCALLSEQ_END = DAG.getEntryNode();
1740
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001741 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001742 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001743 case ISD::BRIND:
1744 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1745 // Ensure that libcalls are emitted before a branch.
1746 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1747 Tmp1 = LegalizeOp(Tmp1);
1748 LastCALLSEQ_END = DAG.getEntryNode();
1749
1750 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1751 default: assert(0 && "Indirect target must be legal type (pointer)!");
1752 case Legal:
1753 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1754 break;
1755 }
1756 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1757 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001758 case ISD::BR_JT:
1759 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1760 // Ensure that libcalls are emitted before a branch.
1761 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1762 Tmp1 = LegalizeOp(Tmp1);
1763 LastCALLSEQ_END = DAG.getEntryNode();
1764
1765 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1766 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1767
1768 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1769 default: assert(0 && "This action is not supported yet!");
1770 case TargetLowering::Legal: break;
1771 case TargetLowering::Custom:
1772 Tmp1 = TLI.LowerOperation(Result, DAG);
1773 if (Tmp1.Val) Result = Tmp1;
1774 break;
1775 case TargetLowering::Expand: {
1776 SDOperand Chain = Result.getOperand(0);
1777 SDOperand Table = Result.getOperand(1);
1778 SDOperand Index = Result.getOperand(2);
1779
1780 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001781 MachineFunction &MF = DAG.getMachineFunction();
1782 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001783 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1784 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001785
1786 SDOperand LD;
1787 switch (EntrySize) {
1788 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001789 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001790 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001791 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001792 PseudoSourceValue::getJumpTable(), 0); break;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001793 }
1794
Evan Chengcc415862007-11-09 01:32:10 +00001795 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001796 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001797 // For PIC, the sequence is:
1798 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001799 // RelocBase can be JumpTable, GOT or some sort of global base.
1800 if (PTy != MVT::i32)
1801 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1802 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1803 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001804 }
Evan Chengcc415862007-11-09 01:32:10 +00001805 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001806 }
1807 }
1808 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001809 case ISD::BRCOND:
1810 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001811 // Ensure that libcalls are emitted before a return.
1812 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1813 Tmp1 = LegalizeOp(Tmp1);
1814 LastCALLSEQ_END = DAG.getEntryNode();
1815
Chris Lattner47e92232005-01-18 19:27:06 +00001816 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1817 case Expand: assert(0 && "It's impossible to expand bools");
1818 case Legal:
1819 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1820 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001821 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00001822 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001823
1824 // The top bits of the promoted condition are not necessarily zero, ensure
1825 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001826 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001827 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001828 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00001829 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001830 break;
1831 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001832 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001833
1834 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001835 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001836
1837 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1838 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001839 case TargetLowering::Legal: break;
1840 case TargetLowering::Custom:
1841 Tmp1 = TLI.LowerOperation(Result, DAG);
1842 if (Tmp1.Val) Result = Tmp1;
1843 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001844 case TargetLowering::Expand:
1845 // Expand brcond's setcc into its constituent parts and create a BR_CC
1846 // Node.
1847 if (Tmp2.getOpcode() == ISD::SETCC) {
1848 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1849 Tmp2.getOperand(0), Tmp2.getOperand(1),
1850 Node->getOperand(2));
1851 } else {
1852 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1853 DAG.getCondCode(ISD::SETNE), Tmp2,
1854 DAG.getConstant(0, Tmp2.getValueType()),
1855 Node->getOperand(2));
1856 }
1857 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001858 }
1859 break;
1860 case ISD::BR_CC:
1861 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001862 // Ensure that libcalls are emitted before a branch.
1863 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1864 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001865 Tmp2 = Node->getOperand(2); // LHS
1866 Tmp3 = Node->getOperand(3); // RHS
1867 Tmp4 = Node->getOperand(1); // CC
1868
1869 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001870 LastCALLSEQ_END = DAG.getEntryNode();
1871
Nate Begeman750ac1b2006-02-01 07:19:44 +00001872 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1873 // the LHS is a legal SETCC itself. In this case, we need to compare
1874 // the result against zero to select between true and false values.
1875 if (Tmp3.Val == 0) {
1876 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1877 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001878 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001879
1880 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1881 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001882
Chris Lattner181b7a32005-12-17 23:46:46 +00001883 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1884 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001885 case TargetLowering::Legal: break;
1886 case TargetLowering::Custom:
1887 Tmp4 = TLI.LowerOperation(Result, DAG);
1888 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001889 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001890 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001891 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001892 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001893 LoadSDNode *LD = cast<LoadSDNode>(Node);
1894 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1895 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001896
Evan Cheng466685d2006-10-09 20:57:25 +00001897 ISD::LoadExtType ExtType = LD->getExtensionType();
1898 if (ExtType == ISD::NON_EXTLOAD) {
1899 MVT::ValueType VT = Node->getValueType(0);
1900 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1901 Tmp3 = Result.getValue(0);
1902 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001903
Evan Cheng466685d2006-10-09 20:57:25 +00001904 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1905 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001906 case TargetLowering::Legal:
1907 // If this is an unaligned load and the target doesn't support it,
1908 // expand it.
1909 if (!TLI.allowsUnalignedMemoryAccesses()) {
1910 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001911 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001912 if (LD->getAlignment() < ABIAlignment){
1913 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1914 TLI);
1915 Tmp3 = Result.getOperand(0);
1916 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00001917 Tmp3 = LegalizeOp(Tmp3);
1918 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001919 }
1920 }
1921 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001922 case TargetLowering::Custom:
1923 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1924 if (Tmp1.Val) {
1925 Tmp3 = LegalizeOp(Tmp1);
1926 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001927 }
Evan Cheng466685d2006-10-09 20:57:25 +00001928 break;
1929 case TargetLowering::Promote: {
1930 // Only promote a load of vector type to another.
1931 assert(MVT::isVector(VT) && "Cannot promote this load!");
1932 // Change base type to a different vector type.
1933 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1934
1935 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001936 LD->getSrcValueOffset(),
1937 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001938 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1939 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001940 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001941 }
Evan Cheng466685d2006-10-09 20:57:25 +00001942 }
1943 // Since loads produce two values, make sure to remember that we
1944 // legalized both of them.
1945 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1946 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1947 return Op.ResNo ? Tmp4 : Tmp3;
1948 } else {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001949 MVT::ValueType SrcVT = LD->getMemoryVT();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00001950 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1951 int SVOffset = LD->getSrcValueOffset();
1952 unsigned Alignment = LD->getAlignment();
1953 bool isVolatile = LD->isVolatile();
1954
1955 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1956 // Some targets pretend to have an i1 loading operation, and actually
1957 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1958 // bits are guaranteed to be zero; it helps the optimizers understand
1959 // that these bits are zero. It is also useful for EXTLOAD, since it
1960 // tells the optimizers that those bits are undefined. It would be
1961 // nice to have an effective generic way of getting these benefits...
1962 // Until such a way is found, don't insist on promoting i1 here.
1963 (SrcVT != MVT::i1 ||
1964 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1965 // Promote to a byte-sized load if not loading an integral number of
1966 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1967 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1968 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1969 SDOperand Ch;
1970
1971 // The extra bits are guaranteed to be zero, since we stored them that
1972 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1973
1974 ISD::LoadExtType NewExtType =
1975 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1976
1977 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1978 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1979 NVT, isVolatile, Alignment);
1980
1981 Ch = Result.getValue(1); // The chain.
1982
1983 if (ExtType == ISD::SEXTLOAD)
1984 // Having the top bits zero doesn't help when sign extending.
1985 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1986 Result, DAG.getValueType(SrcVT));
1987 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1988 // All the top bits are guaranteed to be zero - inform the optimizers.
1989 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1990 DAG.getValueType(SrcVT));
1991
1992 Tmp1 = LegalizeOp(Result);
1993 Tmp2 = LegalizeOp(Ch);
1994 } else if (SrcWidth & (SrcWidth - 1)) {
1995 // If not loading a power-of-2 number of bits, expand as two loads.
1996 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1997 "Unsupported extload!");
1998 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1999 assert(RoundWidth < SrcWidth);
2000 unsigned ExtraWidth = SrcWidth - RoundWidth;
2001 assert(ExtraWidth < RoundWidth);
2002 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2003 "Load size not an integral number of bytes!");
2004 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2005 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2006 SDOperand Lo, Hi, Ch;
2007 unsigned IncrementSize;
2008
2009 if (TLI.isLittleEndian()) {
2010 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2011 // Load the bottom RoundWidth bits.
2012 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2013 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2014 Alignment);
2015
2016 // Load the remaining ExtraWidth bits.
2017 IncrementSize = RoundWidth / 8;
2018 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2019 DAG.getIntPtrConstant(IncrementSize));
2020 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2021 LD->getSrcValue(), SVOffset + IncrementSize,
2022 ExtraVT, isVolatile,
2023 MinAlign(Alignment, IncrementSize));
2024
2025 // Build a factor node to remember that this load is independent of the
2026 // other one.
2027 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2028 Hi.getValue(1));
2029
2030 // Move the top bits to the right place.
2031 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2032 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2033
2034 // Join the hi and lo parts.
2035 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002036 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002037 // Big endian - avoid unaligned loads.
2038 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2039 // Load the top RoundWidth bits.
2040 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2041 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2042 Alignment);
2043
2044 // Load the remaining ExtraWidth bits.
2045 IncrementSize = RoundWidth / 8;
2046 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2047 DAG.getIntPtrConstant(IncrementSize));
2048 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2049 LD->getSrcValue(), SVOffset + IncrementSize,
2050 ExtraVT, isVolatile,
2051 MinAlign(Alignment, IncrementSize));
2052
2053 // Build a factor node to remember that this load is independent of the
2054 // other one.
2055 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2056 Hi.getValue(1));
2057
2058 // Move the top bits to the right place.
2059 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2060 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2061
2062 // Join the hi and lo parts.
2063 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2064 }
2065
2066 Tmp1 = LegalizeOp(Result);
2067 Tmp2 = LegalizeOp(Ch);
2068 } else {
2069 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2070 default: assert(0 && "This action is not supported yet!");
2071 case TargetLowering::Custom:
2072 isCustom = true;
2073 // FALLTHROUGH
2074 case TargetLowering::Legal:
2075 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2076 Tmp1 = Result.getValue(0);
2077 Tmp2 = Result.getValue(1);
2078
2079 if (isCustom) {
2080 Tmp3 = TLI.LowerOperation(Result, DAG);
2081 if (Tmp3.Val) {
2082 Tmp1 = LegalizeOp(Tmp3);
2083 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2084 }
2085 } else {
2086 // If this is an unaligned load and the target doesn't support it,
2087 // expand it.
2088 if (!TLI.allowsUnalignedMemoryAccesses()) {
2089 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002090 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002091 if (LD->getAlignment() < ABIAlignment){
2092 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2093 TLI);
2094 Tmp1 = Result.getOperand(0);
2095 Tmp2 = Result.getOperand(1);
2096 Tmp1 = LegalizeOp(Tmp1);
2097 Tmp2 = LegalizeOp(Tmp2);
2098 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002099 }
2100 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002101 break;
2102 case TargetLowering::Expand:
2103 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2104 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2105 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2106 LD->getSrcValueOffset(),
2107 LD->isVolatile(), LD->getAlignment());
2108 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2109 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2110 Tmp2 = LegalizeOp(Load.getValue(1));
2111 break;
2112 }
2113 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2114 // Turn the unsupported load into an EXTLOAD followed by an explicit
2115 // zero/sign extend inreg.
2116 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2117 Tmp1, Tmp2, LD->getSrcValue(),
2118 LD->getSrcValueOffset(), SrcVT,
2119 LD->isVolatile(), LD->getAlignment());
2120 SDOperand ValRes;
2121 if (ExtType == ISD::SEXTLOAD)
2122 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2123 Result, DAG.getValueType(SrcVT));
2124 else
2125 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2126 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2127 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002128 break;
2129 }
Evan Cheng466685d2006-10-09 20:57:25 +00002130 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002131
Evan Cheng466685d2006-10-09 20:57:25 +00002132 // Since loads produce two values, make sure to remember that we legalized
2133 // both of them.
2134 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2135 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2136 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002137 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002138 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002139 case ISD::EXTRACT_ELEMENT: {
2140 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2141 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002142 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002143 case Legal:
2144 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2145 // 1 -> Hi
2146 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2147 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2148 TLI.getShiftAmountTy()));
2149 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2150 } else {
2151 // 0 -> Lo
2152 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2153 Node->getOperand(0));
2154 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002155 break;
2156 case Expand:
2157 // Get both the low and high parts.
2158 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2159 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2160 Result = Tmp2; // 1 -> Hi
2161 else
2162 Result = Tmp1; // 0 -> Lo
2163 break;
2164 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002165 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002166 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002167
2168 case ISD::CopyToReg:
2169 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002170
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002171 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002172 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002173 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002174 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002175 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002176 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002177 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002178 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002179 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002180 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002181 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2182 Tmp3);
2183 } else {
2184 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002185 }
2186
2187 // Since this produces two values, make sure to remember that we legalized
2188 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002189 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00002190 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002191 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002192 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002193 break;
2194
2195 case ISD::RET:
2196 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002197
2198 // Ensure that libcalls are emitted before a return.
2199 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2200 Tmp1 = LegalizeOp(Tmp1);
2201 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002202
Chris Lattner3e928bb2005-01-07 07:47:09 +00002203 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002204 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002205 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002206 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002207 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002208 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002209 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002210 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002211 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00002212 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00002213 SDOperand Lo, Hi;
2214 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002215
2216 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002217 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002218 std::swap(Lo, Hi);
2219
Evan Cheng13acce32006-12-11 19:27:14 +00002220 if (Hi.Val)
2221 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2222 else
2223 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002224 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002225 } else {
2226 SDNode *InVal = Tmp2.Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002227 int InIx = Tmp2.ResNo;
2228 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2229 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattnerf87324e2006-04-11 01:31:51 +00002230
Dan Gohman7f321562007-06-25 16:23:39 +00002231 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002232 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00002233 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002234 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002235 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002236 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002237 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002238 } else if (NumElems == 1) {
2239 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002240 Tmp2 = ScalarizeVectorOp(Tmp2);
2241 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002242 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002243
2244 // FIXME: Returns of gcc generic vectors smaller than a legal type
2245 // should be returned in integer registers!
2246
Chris Lattnerf87324e2006-04-11 01:31:51 +00002247 // The scalarized value type may not be legal, e.g. it might require
2248 // promotion or expansion. Relegalize the return.
2249 Result = LegalizeOp(Result);
2250 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002251 // FIXME: Returns of gcc generic vectors larger than a legal vector
2252 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00002253 SDOperand Lo, Hi;
2254 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002255 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002256 Result = LegalizeOp(Result);
2257 }
2258 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002259 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002260 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002261 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002262 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002263 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002264 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002265 }
2266 break;
2267 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002268 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002269 break;
2270 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002271 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002272 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002273 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002274 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2275 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002276 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002277 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002278 break;
2279 case Expand: {
2280 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00002281 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002282 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002283 ExpandOp(Node->getOperand(i), Lo, Hi);
2284 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002285 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00002286 if (Hi.Val) {
2287 NewValues.push_back(Hi);
2288 NewValues.push_back(Node->getOperand(i+1));
2289 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002290 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002291 }
2292 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002293 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002294 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002295
2296 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002297 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002298 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002299 Result = DAG.getNode(ISD::RET, MVT::Other,
2300 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002301 break;
2302 }
2303 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002304
Chris Lattner6862dbc2006-01-29 21:02:23 +00002305 if (Result.getOpcode() == ISD::RET) {
2306 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2307 default: assert(0 && "This action is not supported yet!");
2308 case TargetLowering::Legal: break;
2309 case TargetLowering::Custom:
2310 Tmp1 = TLI.LowerOperation(Result, DAG);
2311 if (Tmp1.Val) Result = Tmp1;
2312 break;
2313 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002314 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002315 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002316 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002317 StoreSDNode *ST = cast<StoreSDNode>(Node);
2318 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2319 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002320 int SVOffset = ST->getSrcValueOffset();
2321 unsigned Alignment = ST->getAlignment();
2322 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002323
Evan Cheng8b2794a2006-10-13 21:14:26 +00002324 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002325 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2326 // FIXME: We shouldn't do this for TargetConstantFP's.
2327 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2328 // to phase ordering between legalized code and the dag combiner. This
2329 // probably means that we need to integrate dag combiner and legalizer
2330 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002331 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002332 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002333 if (CFP->getValueType(0) == MVT::f32 &&
2334 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002335 Tmp3 = DAG.getConstant(CFP->getValueAPF().
2336 convertToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002337 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002338 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2339 SVOffset, isVolatile, Alignment);
2340 break;
2341 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002342 // If this target supports 64-bit registers, do a single 64-bit store.
2343 if (getTypeAction(MVT::i64) == Legal) {
2344 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002345 zextOrTrunc(64), MVT::i64);
Chris Lattner3cb93512007-10-15 05:46:06 +00002346 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2347 SVOffset, isVolatile, Alignment);
2348 break;
2349 } else if (getTypeAction(MVT::i32) == Legal) {
2350 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2351 // stores. If the target supports neither 32- nor 64-bits, this
2352 // xform is certainly not worth it.
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002353 const APInt &IntVal =CFP->getValueAPF().convertToAPInt();
2354 SDOperand Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2355 SDOperand Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002356 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002357
2358 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2359 SVOffset, isVolatile, Alignment);
2360 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002361 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002362 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002363 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002364
2365 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2366 break;
2367 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002368 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002369 }
2370
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002371 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002372 case Legal: {
2373 Tmp3 = LegalizeOp(ST->getValue());
2374 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2375 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002376
Evan Cheng8b2794a2006-10-13 21:14:26 +00002377 MVT::ValueType VT = Tmp3.getValueType();
2378 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2379 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002380 case TargetLowering::Legal:
2381 // If this is an unaligned store and the target doesn't support it,
2382 // expand it.
2383 if (!TLI.allowsUnalignedMemoryAccesses()) {
2384 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002385 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002386 if (ST->getAlignment() < ABIAlignment)
2387 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2388 TLI);
2389 }
2390 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002391 case TargetLowering::Custom:
2392 Tmp1 = TLI.LowerOperation(Result, DAG);
2393 if (Tmp1.Val) Result = Tmp1;
2394 break;
2395 case TargetLowering::Promote:
2396 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2397 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2398 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2399 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002400 ST->getSrcValue(), SVOffset, isVolatile,
2401 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002402 break;
2403 }
2404 break;
2405 }
2406 case Promote:
2407 // Truncate the value and store the result.
2408 Tmp3 = PromoteOp(ST->getValue());
2409 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002410 SVOffset, ST->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002411 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002412 break;
2413
2414 case Expand:
2415 unsigned IncrementSize = 0;
2416 SDOperand Lo, Hi;
2417
2418 // If this is a vector type, then we have to calculate the increment as
2419 // the product of the element size in bytes, and the number of elements
2420 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00002421 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002422 SDNode *InVal = ST->getValue().Val;
Dale Johannesene5269622007-10-20 00:07:52 +00002423 int InIx = ST->getValue().ResNo;
Chris Lattner0bd48932008-01-17 07:00:52 +00002424 MVT::ValueType InVT = InVal->getValueType(InIx);
2425 unsigned NumElems = MVT::getVectorNumElements(InVT);
2426 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002427
Dan Gohman7f321562007-06-25 16:23:39 +00002428 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002429 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00002430 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002431 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002432 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002433 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002434 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002435 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002436 Result = LegalizeOp(Result);
2437 break;
2438 } else if (NumElems == 1) {
2439 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002440 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002441 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002442 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002443 // The scalarized value type may not be legal, e.g. it might require
2444 // promotion or expansion. Relegalize the scalar store.
2445 Result = LegalizeOp(Result);
2446 break;
2447 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002448 SplitVectorOp(ST->getValue(), Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00002449 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2450 MVT::getSizeInBits(EVT)/8;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002451 }
2452 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002453 ExpandOp(ST->getValue(), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002454 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002455
Duncan Sands0753fc12008-02-11 10:37:04 +00002456 if (TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002457 std::swap(Lo, Hi);
2458 }
2459
2460 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002461 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002462
2463 if (Hi.Val == NULL) {
2464 // Must be int <-> float one-to-one expansion.
2465 Result = Lo;
2466 break;
2467 }
2468
Evan Cheng8b2794a2006-10-13 21:14:26 +00002469 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002470 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002471 assert(isTypeLegal(Tmp2.getValueType()) &&
2472 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002473 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002474 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002475 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002476 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002477 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2478 break;
2479 }
2480 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002481 switch (getTypeAction(ST->getValue().getValueType())) {
2482 case Legal:
2483 Tmp3 = LegalizeOp(ST->getValue());
2484 break;
2485 case Promote:
2486 // We can promote the value, the truncstore will still take care of it.
2487 Tmp3 = PromoteOp(ST->getValue());
2488 break;
2489 case Expand:
2490 // Just store the low part. This may become a non-trunc store, so make
2491 // sure to use getTruncStore, not UpdateNodeOperands below.
2492 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2493 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2494 SVOffset, MVT::i8, isVolatile, Alignment);
2495 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002496
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002497 MVT::ValueType StVT = ST->getMemoryVT();
Duncan Sands7e857202008-01-22 07:17:34 +00002498 unsigned StWidth = MVT::getSizeInBits(StVT);
2499
2500 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2501 // Promote to a byte-sized store with upper bits zero if not
2502 // storing an integral number of bytes. For example, promote
2503 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2504 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2505 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2506 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2507 SVOffset, NVT, isVolatile, Alignment);
2508 } else if (StWidth & (StWidth - 1)) {
2509 // If not storing a power-of-2 number of bits, expand as two stores.
2510 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2511 "Unsupported truncstore!");
2512 unsigned RoundWidth = 1 << Log2_32(StWidth);
2513 assert(RoundWidth < StWidth);
2514 unsigned ExtraWidth = StWidth - RoundWidth;
2515 assert(ExtraWidth < RoundWidth);
2516 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2517 "Store size not an integral number of bytes!");
2518 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2519 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2520 SDOperand Lo, Hi;
2521 unsigned IncrementSize;
2522
2523 if (TLI.isLittleEndian()) {
2524 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2525 // Store the bottom RoundWidth bits.
2526 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2527 SVOffset, RoundVT,
2528 isVolatile, Alignment);
2529
2530 // Store the remaining ExtraWidth bits.
2531 IncrementSize = RoundWidth / 8;
2532 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2533 DAG.getIntPtrConstant(IncrementSize));
2534 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2535 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2536 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2537 SVOffset + IncrementSize, ExtraVT, isVolatile,
2538 MinAlign(Alignment, IncrementSize));
2539 } else {
2540 // Big endian - avoid unaligned stores.
2541 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2542 // Store the top RoundWidth bits.
2543 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2544 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2545 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2546 RoundVT, isVolatile, Alignment);
2547
2548 // Store the remaining ExtraWidth bits.
2549 IncrementSize = RoundWidth / 8;
2550 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2551 DAG.getIntPtrConstant(IncrementSize));
2552 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2553 SVOffset + IncrementSize, ExtraVT, isVolatile,
2554 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002555 }
Duncan Sands7e857202008-01-22 07:17:34 +00002556
2557 // The order of the stores doesn't matter.
2558 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2559 } else {
2560 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2561 Tmp2 != ST->getBasePtr())
2562 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2563 ST->getOffset());
2564
2565 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2566 default: assert(0 && "This action is not supported yet!");
2567 case TargetLowering::Legal:
2568 // If this is an unaligned store and the target doesn't support it,
2569 // expand it.
2570 if (!TLI.allowsUnalignedMemoryAccesses()) {
2571 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002572 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Duncan Sands7e857202008-01-22 07:17:34 +00002573 if (ST->getAlignment() < ABIAlignment)
2574 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2575 TLI);
2576 }
2577 break;
2578 case TargetLowering::Custom:
2579 Result = TLI.LowerOperation(Result, DAG);
2580 break;
2581 case Expand:
2582 // TRUNCSTORE:i16 i32 -> STORE i16
2583 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2584 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2585 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2586 isVolatile, Alignment);
2587 break;
2588 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002589 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002590 }
2591 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002592 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002593 case ISD::PCMARKER:
2594 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002595 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002596 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002597 case ISD::STACKSAVE:
2598 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002599 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2600 Tmp1 = Result.getValue(0);
2601 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002602
Chris Lattner140d53c2006-01-13 02:50:02 +00002603 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2604 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002605 case TargetLowering::Legal: break;
2606 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002607 Tmp3 = TLI.LowerOperation(Result, DAG);
2608 if (Tmp3.Val) {
2609 Tmp1 = LegalizeOp(Tmp3);
2610 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002611 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002612 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002613 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002614 // Expand to CopyFromReg if the target set
2615 // StackPointerRegisterToSaveRestore.
2616 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002617 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002618 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002619 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002620 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002621 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2622 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002623 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002624 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002625 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002626
2627 // Since stacksave produce two values, make sure to remember that we
2628 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002629 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2630 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2631 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002632
Chris Lattner140d53c2006-01-13 02:50:02 +00002633 case ISD::STACKRESTORE:
2634 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2635 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002636 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002637
2638 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2639 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002640 case TargetLowering::Legal: break;
2641 case TargetLowering::Custom:
2642 Tmp1 = TLI.LowerOperation(Result, DAG);
2643 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002644 break;
2645 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002646 // Expand to CopyToReg if the target set
2647 // StackPointerRegisterToSaveRestore.
2648 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2649 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2650 } else {
2651 Result = Tmp1;
2652 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002653 break;
2654 }
2655 break;
2656
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002657 case ISD::READCYCLECOUNTER:
2658 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002659 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002660 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2661 Node->getValueType(0))) {
2662 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002663 case TargetLowering::Legal:
2664 Tmp1 = Result.getValue(0);
2665 Tmp2 = Result.getValue(1);
2666 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002667 case TargetLowering::Custom:
2668 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002669 Tmp1 = LegalizeOp(Result.getValue(0));
2670 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002671 break;
2672 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002673
2674 // Since rdcc produce two values, make sure to remember that we legalized
2675 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002676 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2677 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002678 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002679
Chris Lattner2ee743f2005-01-14 22:08:15 +00002680 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002681 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2682 case Expand: assert(0 && "It's impossible to expand bools");
2683 case Legal:
2684 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2685 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002686 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00002687 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002688 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002689 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002690 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002691 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002692 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002693 break;
2694 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002695 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002696 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002697 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002698
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002699 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002700
Nate Begemanb942a3d2005-08-23 04:29:48 +00002701 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002702 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002703 case TargetLowering::Legal: break;
2704 case TargetLowering::Custom: {
2705 Tmp1 = TLI.LowerOperation(Result, DAG);
2706 if (Tmp1.Val) Result = Tmp1;
2707 break;
2708 }
Nate Begeman9373a812005-08-10 20:51:12 +00002709 case TargetLowering::Expand:
2710 if (Tmp1.getOpcode() == ISD::SETCC) {
2711 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2712 Tmp2, Tmp3,
2713 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2714 } else {
2715 Result = DAG.getSelectCC(Tmp1,
2716 DAG.getConstant(0, Tmp1.getValueType()),
2717 Tmp2, Tmp3, ISD::SETNE);
2718 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002719 break;
2720 case TargetLowering::Promote: {
2721 MVT::ValueType NVT =
2722 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2723 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002724 if (MVT::isVector(Tmp2.getValueType())) {
2725 ExtOp = ISD::BIT_CONVERT;
2726 TruncOp = ISD::BIT_CONVERT;
2727 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002728 ExtOp = ISD::ANY_EXTEND;
2729 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002730 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002731 ExtOp = ISD::FP_EXTEND;
2732 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002733 }
2734 // Promote each of the values to the new type.
2735 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2736 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2737 // Perform the larger operation, then round down.
2738 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002739 if (TruncOp != ISD::FP_ROUND)
2740 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2741 else
2742 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2743 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002744 break;
2745 }
2746 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002747 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002748 case ISD::SELECT_CC: {
2749 Tmp1 = Node->getOperand(0); // LHS
2750 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002751 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2752 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002753 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002754
Nate Begeman750ac1b2006-02-01 07:19:44 +00002755 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2756
2757 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2758 // the LHS is a legal SETCC itself. In this case, we need to compare
2759 // the result against zero to select between true and false values.
2760 if (Tmp2.Val == 0) {
2761 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2762 CC = DAG.getCondCode(ISD::SETNE);
2763 }
2764 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2765
2766 // Everything is legal, see if we should expand this op or something.
2767 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2768 default: assert(0 && "This action is not supported yet!");
2769 case TargetLowering::Legal: break;
2770 case TargetLowering::Custom:
2771 Tmp1 = TLI.LowerOperation(Result, DAG);
2772 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002773 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002774 }
2775 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002776 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002777 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002778 Tmp1 = Node->getOperand(0);
2779 Tmp2 = Node->getOperand(1);
2780 Tmp3 = Node->getOperand(2);
2781 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2782
2783 // If we had to Expand the SetCC operands into a SELECT node, then it may
2784 // not always be possible to return a true LHS & RHS. In this case, just
2785 // return the value we legalized, returned in the LHS
2786 if (Tmp2.Val == 0) {
2787 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002788 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002789 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002790
Chris Lattner73e142f2006-01-30 22:43:50 +00002791 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002792 default: assert(0 && "Cannot handle this action for SETCC yet!");
2793 case TargetLowering::Custom:
2794 isCustom = true;
2795 // FALLTHROUGH.
2796 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002797 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002798 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002799 Tmp4 = TLI.LowerOperation(Result, DAG);
2800 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002801 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002802 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002803 case TargetLowering::Promote: {
2804 // First step, figure out the appropriate operation to use.
2805 // Allow SETCC to not be supported for all legal data types
2806 // Mostly this targets FP
2807 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002808 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002809
2810 // Scan for the appropriate larger type to use.
2811 while (1) {
2812 NewInTy = (MVT::ValueType)(NewInTy+1);
2813
2814 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2815 "Fell off of the edge of the integer world");
2816 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2817 "Fell off of the edge of the floating point world");
2818
2819 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002820 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002821 break;
2822 }
2823 if (MVT::isInteger(NewInTy))
2824 assert(0 && "Cannot promote Legal Integer SETCC yet");
2825 else {
2826 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2827 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2828 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002829 Tmp1 = LegalizeOp(Tmp1);
2830 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002831 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002832 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002833 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002834 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002835 case TargetLowering::Expand:
2836 // Expand a setcc node into a select_cc of the same condition, lhs, and
2837 // rhs that selects between const 1 (true) and const 0 (false).
2838 MVT::ValueType VT = Node->getValueType(0);
2839 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2840 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002841 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002842 break;
2843 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002844 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002845 case ISD::MEMSET:
2846 case ISD::MEMCPY:
2847 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002848 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002849 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2850
2851 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2852 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2853 case Expand: assert(0 && "Cannot expand a byte!");
2854 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002855 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002856 break;
2857 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002858 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002859 break;
2860 }
2861 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002862 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002863 }
Chris Lattner272455b2005-02-02 03:44:41 +00002864
2865 SDOperand Tmp4;
2866 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002867 case Expand: {
2868 // Length is too big, just take the lo-part of the length.
2869 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002870 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002871 break;
2872 }
Chris Lattnere5605212005-01-28 22:29:18 +00002873 case Legal:
2874 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002875 break;
2876 case Promote:
2877 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002878 break;
2879 }
2880
2881 SDOperand Tmp5;
2882 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2883 case Expand: assert(0 && "Cannot expand this yet!");
2884 case Legal:
2885 Tmp5 = LegalizeOp(Node->getOperand(4));
2886 break;
2887 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002888 Tmp5 = PromoteOp(Node->getOperand(4));
2889 break;
2890 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002891
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002892 SDOperand Tmp6;
2893 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2894 case Expand: assert(0 && "Cannot expand this yet!");
2895 case Legal:
2896 Tmp6 = LegalizeOp(Node->getOperand(5));
2897 break;
2898 case Promote:
2899 Tmp6 = PromoteOp(Node->getOperand(5));
2900 break;
2901 }
2902
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002903 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2904 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002905 case TargetLowering::Custom:
2906 isCustom = true;
2907 // FALLTHROUGH
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002908 case TargetLowering::Legal: {
2909 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2910 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner456a93a2006-01-28 07:39:30 +00002911 if (isCustom) {
2912 Tmp1 = TLI.LowerOperation(Result, DAG);
2913 if (Tmp1.Val) Result = Tmp1;
2914 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002915 break;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002916 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002917 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002918 // Otherwise, the target does not support this operation. Lower the
2919 // operation to an explicit libcall as appropriate.
2920 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002921 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002922 TargetLowering::ArgListTy Args;
2923 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002924
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002925 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002926 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002927 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002928 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002929 // Extend the (previously legalized) ubyte argument to be an int value
2930 // for the call.
2931 if (Tmp3.getValueType() > MVT::i32)
2932 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2933 else
2934 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002935 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002936 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002937 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002938 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002939
2940 FnName = "memset";
2941 } else if (Node->getOpcode() == ISD::MEMCPY ||
2942 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002943 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002944 Entry.Node = Tmp2; Args.push_back(Entry);
2945 Entry.Node = Tmp3; Args.push_back(Entry);
2946 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002947 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2948 } else {
2949 assert(0 && "Unknown op!");
2950 }
Chris Lattner45982da2005-05-12 16:53:42 +00002951
Chris Lattnere1bd8222005-01-11 05:57:22 +00002952 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00002953 TLI.LowerCallTo(Tmp1, Type::VoidTy,
2954 false, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002955 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002956 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002957 break;
2958 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002959 }
2960 break;
2961 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002962
Chris Lattner5b359c62005-04-02 04:00:59 +00002963 case ISD::SHL_PARTS:
2964 case ISD::SRA_PARTS:
2965 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002966 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002967 bool Changed = false;
2968 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2969 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2970 Changed |= Ops.back() != Node->getOperand(i);
2971 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002972 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002973 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002974
Evan Cheng05a2d562006-01-09 18:31:59 +00002975 switch (TLI.getOperationAction(Node->getOpcode(),
2976 Node->getValueType(0))) {
2977 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002978 case TargetLowering::Legal: break;
2979 case TargetLowering::Custom:
2980 Tmp1 = TLI.LowerOperation(Result, DAG);
2981 if (Tmp1.Val) {
2982 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002983 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002984 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002985 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2986 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002987 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002988 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002989 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002990 return RetVal;
2991 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002992 break;
2993 }
2994
Chris Lattner2c8086f2005-04-02 05:00:07 +00002995 // Since these produce multiple values, make sure to remember that we
2996 // legalized all of them.
2997 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2998 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2999 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00003000 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003001
3002 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00003003 case ISD::ADD:
3004 case ISD::SUB:
3005 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00003006 case ISD::MULHS:
3007 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003008 case ISD::UDIV:
3009 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003010 case ISD::AND:
3011 case ISD::OR:
3012 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00003013 case ISD::SHL:
3014 case ISD::SRL:
3015 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00003016 case ISD::FADD:
3017 case ISD::FSUB:
3018 case ISD::FMUL:
3019 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00003020 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003021 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00003022 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3023 case Expand: assert(0 && "Not possible");
3024 case Legal:
3025 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3026 break;
3027 case Promote:
3028 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3029 break;
3030 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003031
3032 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003033
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003034 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003035 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003036 case TargetLowering::Legal: break;
3037 case TargetLowering::Custom:
3038 Tmp1 = TLI.LowerOperation(Result, DAG);
3039 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00003040 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003041 case TargetLowering::Expand: {
Dan Gohman525178c2007-10-08 18:33:35 +00003042 MVT::ValueType VT = Op.getValueType();
3043
3044 // See if multiply or divide can be lowered using two-result operations.
3045 SDVTList VTs = DAG.getVTList(VT, VT);
3046 if (Node->getOpcode() == ISD::MUL) {
3047 // We just need the low half of the multiply; try both the signed
3048 // and unsigned forms. If the target supports both SMUL_LOHI and
3049 // UMUL_LOHI, form a preference by checking which forms of plain
3050 // MULH it supports.
3051 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3052 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3053 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3054 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3055 unsigned OpToUse = 0;
3056 if (HasSMUL_LOHI && !HasMULHS) {
3057 OpToUse = ISD::SMUL_LOHI;
3058 } else if (HasUMUL_LOHI && !HasMULHU) {
3059 OpToUse = ISD::UMUL_LOHI;
3060 } else if (HasSMUL_LOHI) {
3061 OpToUse = ISD::SMUL_LOHI;
3062 } else if (HasUMUL_LOHI) {
3063 OpToUse = ISD::UMUL_LOHI;
3064 }
3065 if (OpToUse) {
3066 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
3067 break;
3068 }
3069 }
3070 if (Node->getOpcode() == ISD::MULHS &&
3071 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
3072 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3073 break;
3074 }
3075 if (Node->getOpcode() == ISD::MULHU &&
3076 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
3077 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3078 break;
3079 }
3080 if (Node->getOpcode() == ISD::SDIV &&
3081 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3082 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3083 break;
3084 }
3085 if (Node->getOpcode() == ISD::UDIV &&
3086 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3087 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3088 break;
3089 }
3090
Dan Gohman82669522007-10-11 23:57:53 +00003091 // Check to see if we have a libcall for this operator.
3092 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3093 bool isSigned = false;
3094 switch (Node->getOpcode()) {
3095 case ISD::UDIV:
3096 case ISD::SDIV:
3097 if (VT == MVT::i32) {
3098 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng56966222007-01-12 02:11:51 +00003099 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003100 isSigned = Node->getOpcode() == ISD::SDIV;
3101 }
3102 break;
3103 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003104 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3105 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003106 break;
3107 default: break;
3108 }
3109 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3110 SDOperand Dummy;
3111 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003112 break;
3113 }
3114
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003115 assert(MVT::isVector(Node->getValueType(0)) &&
3116 "Cannot expand this binary operator!");
3117 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003118 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003119 break;
3120 }
Evan Chengcc987612006-04-12 21:20:24 +00003121 case TargetLowering::Promote: {
3122 switch (Node->getOpcode()) {
3123 default: assert(0 && "Do not know how to promote this BinOp!");
3124 case ISD::AND:
3125 case ISD::OR:
3126 case ISD::XOR: {
3127 MVT::ValueType OVT = Node->getValueType(0);
3128 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3129 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3130 // Bit convert each of the values to the new type.
3131 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3132 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3133 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3134 // Bit convert the result back the original type.
3135 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3136 break;
3137 }
3138 }
3139 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003140 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003141 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003142
Dan Gohmane14ea862007-10-05 14:17:22 +00003143 case ISD::SMUL_LOHI:
3144 case ISD::UMUL_LOHI:
3145 case ISD::SDIVREM:
3146 case ISD::UDIVREM:
3147 // These nodes will only be produced by target-specific lowering, so
3148 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003149 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003150 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003151
3152 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3153 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3154 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003155 break;
3156
Chris Lattnera09f8482006-03-05 05:09:38 +00003157 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3158 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3159 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3160 case Expand: assert(0 && "Not possible");
3161 case Legal:
3162 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3163 break;
3164 case Promote:
3165 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3166 break;
3167 }
3168
3169 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3170
3171 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3172 default: assert(0 && "Operation not supported");
3173 case TargetLowering::Custom:
3174 Tmp1 = TLI.LowerOperation(Result, DAG);
3175 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003176 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003177 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003178 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003179 // If this target supports fabs/fneg natively and select is cheap,
3180 // do this efficiently.
3181 if (!TLI.isSelectExpensive() &&
3182 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3183 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003184 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003185 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003186 // Get the sign bit of the RHS.
3187 MVT::ValueType IVT =
3188 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3189 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003190 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003191 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3192 // Get the absolute value of the result.
3193 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3194 // Select between the nabs and abs value based on the sign bit of
3195 // the input.
3196 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3197 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3198 AbsVal),
3199 AbsVal);
3200 Result = LegalizeOp(Result);
3201 break;
3202 }
3203
3204 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00003205 MVT::ValueType NVT =
3206 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3207 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3208 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3209 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003210 break;
3211 }
Evan Cheng912095b2007-01-04 21:56:39 +00003212 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003213 break;
3214
Nate Begeman551bf3f2006-02-17 05:43:56 +00003215 case ISD::ADDC:
3216 case ISD::SUBC:
3217 Tmp1 = LegalizeOp(Node->getOperand(0));
3218 Tmp2 = LegalizeOp(Node->getOperand(1));
3219 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3220 // Since this produces two values, make sure to remember that we legalized
3221 // both of them.
3222 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3223 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3224 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003225
Nate Begeman551bf3f2006-02-17 05:43:56 +00003226 case ISD::ADDE:
3227 case ISD::SUBE:
3228 Tmp1 = LegalizeOp(Node->getOperand(0));
3229 Tmp2 = LegalizeOp(Node->getOperand(1));
3230 Tmp3 = LegalizeOp(Node->getOperand(2));
3231 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3232 // Since this produces two values, make sure to remember that we legalized
3233 // both of them.
3234 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3235 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3236 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003237
Nate Begeman419f8b62005-10-18 00:27:41 +00003238 case ISD::BUILD_PAIR: {
3239 MVT::ValueType PairTy = Node->getValueType(0);
3240 // TODO: handle the case where the Lo and Hi operands are not of legal type
3241 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3242 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3243 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003244 case TargetLowering::Promote:
3245 case TargetLowering::Custom:
3246 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003247 case TargetLowering::Legal:
3248 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3249 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3250 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003251 case TargetLowering::Expand:
3252 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3253 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3254 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3255 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3256 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003257 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003258 break;
3259 }
3260 break;
3261 }
3262
Nate Begemanc105e192005-04-06 00:23:54 +00003263 case ISD::UREM:
3264 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003265 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003266 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3267 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003268
Nate Begemanc105e192005-04-06 00:23:54 +00003269 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003270 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3271 case TargetLowering::Custom:
3272 isCustom = true;
3273 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003274 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003275 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003276 if (isCustom) {
3277 Tmp1 = TLI.LowerOperation(Result, DAG);
3278 if (Tmp1.Val) Result = Tmp1;
3279 }
Nate Begemanc105e192005-04-06 00:23:54 +00003280 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003281 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003282 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003283 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohman525178c2007-10-08 18:33:35 +00003284 MVT::ValueType VT = Node->getValueType(0);
3285
3286 // See if remainder can be lowered using two-result operations.
3287 SDVTList VTs = DAG.getVTList(VT, VT);
3288 if (Node->getOpcode() == ISD::SREM &&
3289 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3290 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3291 break;
3292 }
3293 if (Node->getOpcode() == ISD::UREM &&
3294 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3295 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3296 break;
3297 }
3298
3299 if (MVT::isInteger(VT)) {
3300 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003301 TargetLowering::Legal) {
3302 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003303 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003304 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3305 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman80176312007-11-05 23:35:22 +00003306 } else if (MVT::isVector(VT)) {
3307 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003308 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003309 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003310 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003311 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3312 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003313 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003314 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003315 }
Dan Gohman0d974262007-11-06 22:11:54 +00003316 } else {
3317 assert(MVT::isFloatingPoint(VT) &&
3318 "remainder op must have integer or floating-point type");
Dan Gohman80176312007-11-05 23:35:22 +00003319 if (MVT::isVector(VT)) {
3320 Result = LegalizeOp(UnrollVectorOp(Op));
3321 } else {
3322 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003323 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3324 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman80176312007-11-05 23:35:22 +00003325 SDOperand Dummy;
3326 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3327 false/*sign irrelevant*/, Dummy);
3328 }
Nate Begemanc105e192005-04-06 00:23:54 +00003329 }
3330 break;
3331 }
Dan Gohman525178c2007-10-08 18:33:35 +00003332 }
Nate Begemanc105e192005-04-06 00:23:54 +00003333 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003334 case ISD::VAARG: {
3335 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3336 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3337
Chris Lattner5c62f332006-01-28 07:42:08 +00003338 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003339 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3340 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003341 case TargetLowering::Custom:
3342 isCustom = true;
3343 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003344 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003345 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3346 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003347 Tmp1 = Result.getValue(1);
3348
3349 if (isCustom) {
3350 Tmp2 = TLI.LowerOperation(Result, DAG);
3351 if (Tmp2.Val) {
3352 Result = LegalizeOp(Tmp2);
3353 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3354 }
3355 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003356 break;
3357 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003358 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3359 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003360 // Increment the pointer, VAList, to the next vaarg
3361 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3362 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3363 TLI.getPointerTy()));
3364 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003365 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003366 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003367 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003368 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003369 Result = LegalizeOp(Result);
3370 break;
3371 }
3372 }
3373 // Since VAARG produces two values, make sure to remember that we
3374 // legalized both of them.
3375 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00003376 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3377 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003378 }
3379
3380 case ISD::VACOPY:
3381 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3382 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3383 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3384
3385 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3386 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003387 case TargetLowering::Custom:
3388 isCustom = true;
3389 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003390 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003391 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3392 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003393 if (isCustom) {
3394 Tmp1 = TLI.LowerOperation(Result, DAG);
3395 if (Tmp1.Val) Result = Tmp1;
3396 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003397 break;
3398 case TargetLowering::Expand:
3399 // This defaults to loading a pointer from the input and storing it to the
3400 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003401 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3402 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3403 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VD, 0);
3404 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VS, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003405 break;
3406 }
3407 break;
3408
3409 case ISD::VAEND:
3410 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3411 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3412
3413 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3414 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003415 case TargetLowering::Custom:
3416 isCustom = true;
3417 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003418 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003419 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003420 if (isCustom) {
3421 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3422 if (Tmp1.Val) Result = Tmp1;
3423 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003424 break;
3425 case TargetLowering::Expand:
3426 Result = Tmp1; // Default to a no-op, return the chain
3427 break;
3428 }
3429 break;
3430
3431 case ISD::VASTART:
3432 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3433 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3434
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003435 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3436
Nate Begemanacc398c2006-01-25 18:21:52 +00003437 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3438 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003439 case TargetLowering::Legal: break;
3440 case TargetLowering::Custom:
3441 Tmp1 = TLI.LowerOperation(Result, DAG);
3442 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003443 break;
3444 }
3445 break;
3446
Nate Begeman35ef9132006-01-11 21:21:00 +00003447 case ISD::ROTL:
3448 case ISD::ROTR:
3449 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3450 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003451 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003452 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3453 default:
3454 assert(0 && "ROTL/ROTR legalize operation not supported");
3455 break;
3456 case TargetLowering::Legal:
3457 break;
3458 case TargetLowering::Custom:
3459 Tmp1 = TLI.LowerOperation(Result, DAG);
3460 if (Tmp1.Val) Result = Tmp1;
3461 break;
3462 case TargetLowering::Promote:
3463 assert(0 && "Do not know how to promote ROTL/ROTR");
3464 break;
3465 case TargetLowering::Expand:
3466 assert(0 && "Do not know how to expand ROTL/ROTR");
3467 break;
3468 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003469 break;
3470
Nate Begemand88fc032006-01-14 03:14:10 +00003471 case ISD::BSWAP:
3472 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3473 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003474 case TargetLowering::Custom:
3475 assert(0 && "Cannot custom legalize this yet!");
3476 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003477 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003478 break;
3479 case TargetLowering::Promote: {
3480 MVT::ValueType OVT = Tmp1.getValueType();
3481 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00003482 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00003483
Chris Lattner456a93a2006-01-28 07:39:30 +00003484 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3485 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3486 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3487 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3488 break;
3489 }
3490 case TargetLowering::Expand:
3491 Result = ExpandBSWAP(Tmp1);
3492 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003493 }
3494 break;
3495
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003496 case ISD::CTPOP:
3497 case ISD::CTTZ:
3498 case ISD::CTLZ:
3499 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3500 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003501 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003502 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003503 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003504 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003505 TargetLowering::Custom) {
3506 Tmp1 = TLI.LowerOperation(Result, DAG);
3507 if (Tmp1.Val) {
3508 Result = Tmp1;
3509 }
Scott Michel910b66d2007-07-30 21:00:31 +00003510 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003511 break;
3512 case TargetLowering::Promote: {
3513 MVT::ValueType OVT = Tmp1.getValueType();
3514 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003515
3516 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003517 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3518 // Perform the larger operation, then subtract if needed.
3519 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003520 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003521 case ISD::CTPOP:
3522 Result = Tmp1;
3523 break;
3524 case ISD::CTTZ:
3525 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00003526 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003527 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003528 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003529 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00003530 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003531 break;
3532 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003533 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003534 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003535 DAG.getConstant(MVT::getSizeInBits(NVT) -
3536 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003537 break;
3538 }
3539 break;
3540 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003541 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003542 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003543 break;
3544 }
3545 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003546
Chris Lattner2c8086f2005-04-02 05:00:07 +00003547 // Unary operators
3548 case ISD::FABS:
3549 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003550 case ISD::FSQRT:
3551 case ISD::FSIN:
3552 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003553 Tmp1 = LegalizeOp(Node->getOperand(0));
3554 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003555 case TargetLowering::Promote:
3556 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003557 isCustom = true;
3558 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003559 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003560 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003561 if (isCustom) {
3562 Tmp1 = TLI.LowerOperation(Result, DAG);
3563 if (Tmp1.Val) Result = Tmp1;
3564 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003565 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003566 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003567 switch (Node->getOpcode()) {
3568 default: assert(0 && "Unreachable!");
3569 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003570 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3571 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003572 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003573 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003574 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003575 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3576 MVT::ValueType VT = Node->getValueType(0);
3577 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003578 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00003579 ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003580 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3581 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003582 break;
3583 }
3584 case ISD::FSQRT:
3585 case ISD::FSIN:
3586 case ISD::FCOS: {
3587 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003588
3589 // Expand unsupported unary vector operators by unrolling them.
3590 if (MVT::isVector(VT)) {
3591 Result = LegalizeOp(UnrollVectorOp(Op));
3592 break;
3593 }
3594
Evan Cheng56966222007-01-12 02:11:51 +00003595 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003596 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003597 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003598 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3599 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003600 break;
3601 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003602 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3603 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003604 break;
3605 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003606 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3607 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003608 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003609 default: assert(0 && "Unreachable!");
3610 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00003611 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003612 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3613 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003614 break;
3615 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003616 }
3617 break;
3618 }
3619 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003620 case ISD::FPOWI: {
Dan Gohman82669522007-10-11 23:57:53 +00003621 MVT::ValueType VT = Node->getValueType(0);
3622
3623 // Expand unsupported unary vector operators by unrolling them.
3624 if (MVT::isVector(VT)) {
3625 Result = LegalizeOp(UnrollVectorOp(Op));
3626 break;
3627 }
3628
3629 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003630 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3631 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003632 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003633 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3634 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003635 break;
3636 }
Chris Lattner35481892005-12-23 00:16:34 +00003637 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003638 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003639 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3640 Node->getValueType(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003641 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3642 // The input has to be a vector type, we have to either scalarize it, pack
3643 // it, or convert it based on whether the input vector type is legal.
3644 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesene5269622007-10-20 00:07:52 +00003645 int InIx = Node->getOperand(0).ResNo;
3646 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3647 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohman7f321562007-06-25 16:23:39 +00003648
3649 // Figure out if there is a simple type corresponding to this Vector
3650 // type. If so, convert to the vector type.
3651 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3652 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003653 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003654 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3655 LegalizeOp(Node->getOperand(0)));
3656 break;
3657 } else if (NumElems == 1) {
3658 // Turn this into a bit convert of the scalar input.
3659 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3660 ScalarizeVectorOp(Node->getOperand(0)));
3661 break;
3662 } else {
3663 // FIXME: UNIMP! Store then reload
3664 assert(0 && "Cast from unsupported vector type not implemented yet!");
3665 }
Chris Lattner67993f72006-01-23 07:30:46 +00003666 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003667 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3668 Node->getOperand(0).getValueType())) {
3669 default: assert(0 && "Unknown operation action!");
3670 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003671 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3672 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003673 break;
3674 case TargetLowering::Legal:
3675 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003676 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003677 break;
3678 }
3679 }
3680 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003681
Chris Lattner2c8086f2005-04-02 05:00:07 +00003682 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003683 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003684 case ISD::UINT_TO_FP: {
3685 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003686 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3687 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003688 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003689 Node->getOperand(0).getValueType())) {
3690 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003691 case TargetLowering::Custom:
3692 isCustom = true;
3693 // FALLTHROUGH
3694 case TargetLowering::Legal:
3695 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003696 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003697 if (isCustom) {
3698 Tmp1 = TLI.LowerOperation(Result, DAG);
3699 if (Tmp1.Val) Result = Tmp1;
3700 }
3701 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003702 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003703 Result = ExpandLegalINT_TO_FP(isSigned,
3704 LegalizeOp(Node->getOperand(0)),
3705 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003706 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003707 case TargetLowering::Promote:
3708 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3709 Node->getValueType(0),
3710 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003711 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003712 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003713 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003714 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003715 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3716 Node->getValueType(0), Node->getOperand(0));
3717 break;
3718 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003719 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003720 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003721 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3722 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003723 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003724 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3725 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003726 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003727 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3728 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003729 break;
3730 }
3731 break;
3732 }
3733 case ISD::TRUNCATE:
3734 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3735 case Legal:
3736 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003737 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003738 break;
3739 case Expand:
3740 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3741
3742 // Since the result is legal, we should just be able to truncate the low
3743 // part of the source.
3744 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3745 break;
3746 case Promote:
3747 Result = PromoteOp(Node->getOperand(0));
3748 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3749 break;
3750 }
3751 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003752
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003753 case ISD::FP_TO_SINT:
3754 case ISD::FP_TO_UINT:
3755 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3756 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003757 Tmp1 = LegalizeOp(Node->getOperand(0));
3758
Chris Lattner1618beb2005-07-29 00:11:56 +00003759 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3760 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003761 case TargetLowering::Custom:
3762 isCustom = true;
3763 // FALLTHROUGH
3764 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003765 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003766 if (isCustom) {
3767 Tmp1 = TLI.LowerOperation(Result, DAG);
3768 if (Tmp1.Val) Result = Tmp1;
3769 }
3770 break;
3771 case TargetLowering::Promote:
3772 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3773 Node->getOpcode() == ISD::FP_TO_SINT);
3774 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003775 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003776 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3777 SDOperand True, False;
3778 MVT::ValueType VT = Node->getOperand(0).getValueType();
3779 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00003780 const uint64_t zero[] = {0, 0};
3781 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003782 APInt x = APInt::getSignBit(MVT::getSizeInBits(NVT));
3783 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003784 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003785 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Nate Begemand2558e32005-08-14 01:20:53 +00003786 Node->getOperand(0), Tmp2, ISD::SETLT);
3787 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3788 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003789 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003790 Tmp2));
3791 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003792 DAG.getConstant(x, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003793 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3794 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003795 } else {
3796 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3797 }
3798 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003799 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003800 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003801 case Expand: {
Evan Cheng6af00d52006-12-13 01:57:55 +00003802 MVT::ValueType VT = Op.getValueType();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003803 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003804 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003805 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003806 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3807 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3808 Node->getOperand(0), DAG.getValueType(MVT::f64));
3809 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3810 DAG.getIntPtrConstant(1));
3811 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3812 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003813 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3814 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3815 Tmp2 = DAG.getConstantFP(apf, OVT);
3816 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3817 // FIXME: generated code sucks.
3818 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3819 DAG.getNode(ISD::ADD, MVT::i32,
3820 DAG.getNode(ISD::FP_TO_SINT, VT,
3821 DAG.getNode(ISD::FSUB, OVT,
3822 Node->getOperand(0), Tmp2)),
3823 DAG.getConstant(0x80000000, MVT::i32)),
3824 DAG.getNode(ISD::FP_TO_SINT, VT,
3825 Node->getOperand(0)),
3826 DAG.getCondCode(ISD::SETGE));
3827 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003828 break;
3829 }
Dan Gohmana2e94852008-03-10 23:03:31 +00003830 // Convert f32 / f64 to i32 / i64 / i128.
Evan Cheng56966222007-01-12 02:11:51 +00003831 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003832 switch (Node->getOpcode()) {
Dale Johannesen73328d12007-09-19 23:55:34 +00003833 case ISD::FP_TO_SINT: {
Dan Gohmana2e94852008-03-10 23:03:31 +00003834 if (VT == MVT::i32) {
3835 if (OVT == MVT::f32)
3836 LC = RTLIB::FPTOSINT_F32_I32;
3837 else if (OVT == MVT::f64)
3838 LC = RTLIB::FPTOSINT_F64_I32;
3839 else
3840 assert(0 && "Unexpected i32-to-fp conversion!");
3841 } else if (VT == MVT::i64) {
3842 if (OVT == MVT::f32)
3843 LC = RTLIB::FPTOSINT_F32_I64;
3844 else if (OVT == MVT::f64)
3845 LC = RTLIB::FPTOSINT_F64_I64;
3846 else if (OVT == MVT::f80)
3847 LC = RTLIB::FPTOSINT_F80_I64;
3848 else if (OVT == MVT::ppcf128)
3849 LC = RTLIB::FPTOSINT_PPCF128_I64;
3850 else
3851 assert(0 && "Unexpected i64-to-fp conversion!");
3852 } else if (VT == MVT::i128) {
3853 if (OVT == MVT::f32)
3854 LC = RTLIB::FPTOSINT_F32_I128;
3855 else if (OVT == MVT::f64)
3856 LC = RTLIB::FPTOSINT_F64_I128;
3857 else if (OVT == MVT::f80)
3858 LC = RTLIB::FPTOSINT_F80_I128;
3859 else if (OVT == MVT::ppcf128)
3860 LC = RTLIB::FPTOSINT_PPCF128_I128;
3861 else
3862 assert(0 && "Unexpected i128-to-fp conversion!");
3863 } else {
3864 assert(0 && "Unexpectd int-to-fp conversion!");
Dale Johannesen73328d12007-09-19 23:55:34 +00003865 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003866 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003867 }
3868 case ISD::FP_TO_UINT: {
Dan Gohmana2e94852008-03-10 23:03:31 +00003869 if (VT == MVT::i32) {
3870 if (OVT == MVT::f32)
3871 LC = RTLIB::FPTOUINT_F32_I32;
3872 else if (OVT == MVT::f64)
3873 LC = RTLIB::FPTOUINT_F64_I32;
3874 else if (OVT == MVT::f80)
3875 LC = RTLIB::FPTOUINT_F80_I32;
3876 else
3877 assert(0 && "Unexpected i32-to-fp conversion!");
3878 } else if (VT == MVT::i64) {
3879 if (OVT == MVT::f32)
3880 LC = RTLIB::FPTOUINT_F32_I64;
3881 else if (OVT == MVT::f64)
3882 LC = RTLIB::FPTOUINT_F64_I64;
3883 else if (OVT == MVT::f80)
3884 LC = RTLIB::FPTOUINT_F80_I64;
3885 else if (OVT == MVT::ppcf128)
3886 LC = RTLIB::FPTOUINT_PPCF128_I64;
3887 else
3888 assert(0 && "Unexpected i64-to-fp conversion!");
3889 } else if (VT == MVT::i128) {
3890 if (OVT == MVT::f32)
3891 LC = RTLIB::FPTOUINT_F32_I128;
3892 else if (OVT == MVT::f64)
3893 LC = RTLIB::FPTOUINT_F64_I128;
3894 else if (OVT == MVT::f80)
3895 LC = RTLIB::FPTOUINT_F80_I128;
3896 else if (OVT == MVT::ppcf128)
3897 LC = RTLIB::FPTOUINT_PPCF128_I128;
3898 else
3899 assert(0 && "Unexpected i128-to-fp conversion!");
3900 } else {
3901 assert(0 && "Unexpectd int-to-fp conversion!");
Dale Johannesen73328d12007-09-19 23:55:34 +00003902 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003903 break;
Dale Johannesen73328d12007-09-19 23:55:34 +00003904 }
Evan Cheng6af00d52006-12-13 01:57:55 +00003905 default: assert(0 && "Unreachable!");
3906 }
3907 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003908 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3909 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003910 break;
3911 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003912 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003913 Tmp1 = PromoteOp(Node->getOperand(0));
3914 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3915 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003916 break;
3917 }
3918 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003919
Chris Lattnerf2670a82008-01-16 06:57:07 +00003920 case ISD::FP_EXTEND: {
Chris Lattner0bd48932008-01-17 07:00:52 +00003921 MVT::ValueType DstVT = Op.getValueType();
3922 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3923 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3924 // The only other way we can lower this is to turn it into a STORE,
3925 // LOAD pair, targetting a temporary location (a stack slot).
3926 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3927 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00003928 }
3929 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3930 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3931 case Legal:
3932 Tmp1 = LegalizeOp(Node->getOperand(0));
3933 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3934 break;
3935 case Promote:
3936 Tmp1 = PromoteOp(Node->getOperand(0));
3937 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3938 break;
3939 }
3940 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003941 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003942 case ISD::FP_ROUND: {
Chris Lattner0bd48932008-01-17 07:00:52 +00003943 MVT::ValueType DstVT = Op.getValueType();
3944 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3945 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3946 if (SrcVT == MVT::ppcf128) {
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003947 SDOperand Lo;
3948 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00003949 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003950 if (DstVT!=MVT::f64)
3951 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00003952 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00003953 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003954 // The only other way we can lower this is to turn it into a STORE,
3955 // LOAD pair, targetting a temporary location (a stack slot).
3956 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3957 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00003958 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00003959 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3960 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3961 case Legal:
3962 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003963 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003964 break;
3965 case Promote:
3966 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003967 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3968 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003969 break;
3970 }
3971 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003972 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003973 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003974 case ISD::ZERO_EXTEND:
3975 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003976 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003977 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003978 case Legal:
3979 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel0123b7d2008-02-15 23:05:48 +00003980 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3981 TargetLowering::Custom) {
3982 Tmp2 = TLI.LowerOperation(Result, DAG);
3983 if (Tmp2.Val) {
3984 Tmp1 = Tmp2;
3985 }
3986 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003987 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003988 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003989 case Promote:
3990 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003991 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003992 Tmp1 = PromoteOp(Node->getOperand(0));
3993 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003994 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003995 case ISD::ZERO_EXTEND:
3996 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003997 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003998 Result = DAG.getZeroExtendInReg(Result,
3999 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00004000 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004001 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00004002 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004003 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00004004 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004005 Result,
4006 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00004007 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004008 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004009 }
4010 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00004011 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00004012 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00004013 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004014 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00004015
4016 // If this operation is not supported, convert it to a shl/shr or load/store
4017 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004018 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4019 default: assert(0 && "This action not supported for this op yet!");
4020 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004021 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004022 break;
4023 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00004024 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00004025 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00004026 // NOTE: we could fall back on load/store here too for targets without
4027 // SAR. However, it is doubtful that any exist.
4028 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
4029 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00004030 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00004031 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4032 Node->getOperand(0), ShiftCst);
4033 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4034 Result, ShiftCst);
4035 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00004036 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00004037 // EXTLOAD pair, targetting a temporary location (a stack slot).
4038
4039 // NOTE: there is a choice here between constantly creating new stack
4040 // slots and always reusing the same one. We currently always create
4041 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00004042 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4043 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00004044 } else {
4045 assert(0 && "Unknown op");
4046 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004047 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00004048 }
Chris Lattner0f69b292005-01-15 06:18:18 +00004049 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004050 }
Duncan Sands36397f52007-07-27 12:58:54 +00004051 case ISD::TRAMPOLINE: {
4052 SDOperand Ops[6];
4053 for (unsigned i = 0; i != 6; ++i)
4054 Ops[i] = LegalizeOp(Node->getOperand(i));
4055 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4056 // The only option for this node is to custom lower it.
4057 Result = TLI.LowerOperation(Result, DAG);
4058 assert(Result.Val && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00004059
4060 // Since trampoline produces two values, make sure to remember that we
4061 // legalized both of them.
4062 Tmp1 = LegalizeOp(Result.getValue(1));
4063 Result = LegalizeOp(Result);
4064 AddLegalizedOperand(SDOperand(Node, 0), Result);
4065 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
4066 return Op.ResNo ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00004067 }
Dan Gohman1a024862008-01-31 00:41:03 +00004068 case ISD::FLT_ROUNDS_: {
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004069 MVT::ValueType VT = Node->getValueType(0);
4070 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4071 default: assert(0 && "This action not supported for this op yet!");
4072 case TargetLowering::Custom:
4073 Result = TLI.LowerOperation(Op, DAG);
4074 if (Result.Val) break;
4075 // Fall Thru
4076 case TargetLowering::Legal:
4077 // If this operation is not supported, lower it to constant 1
4078 Result = DAG.getConstant(1, VT);
4079 break;
4080 }
4081 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004082 case ISD::TRAP: {
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004083 MVT::ValueType VT = Node->getValueType(0);
4084 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4085 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004086 case TargetLowering::Legal:
4087 Tmp1 = LegalizeOp(Node->getOperand(0));
4088 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4089 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004090 case TargetLowering::Custom:
4091 Result = TLI.LowerOperation(Op, DAG);
4092 if (Result.Val) break;
4093 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004094 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004095 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004096 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004097 TargetLowering::ArgListTy Args;
4098 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004099 TLI.LowerCallTo(Tmp1, Type::VoidTy,
4100 false, false, false, CallingConv::C, false,
Chris Lattner034f12e2008-01-15 22:09:33 +00004101 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
4102 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004103 Result = CallResult.second;
4104 break;
4105 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004106 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004107 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00004108 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004109
Chris Lattner4ddd2832006-04-08 04:13:17 +00004110 assert(Result.getValueType() == Op.getValueType() &&
4111 "Bad legalization!");
4112
Chris Lattner456a93a2006-01-28 07:39:30 +00004113 // Make sure that the generated code is itself legal.
4114 if (Result != Op)
4115 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004116
Chris Lattner45982da2005-05-12 16:53:42 +00004117 // Note that LegalizeOp may be reentered even from single-use nodes, which
4118 // means that we always must cache transformed nodes.
4119 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004120 return Result;
4121}
4122
Chris Lattner8b6fa222005-01-15 22:16:26 +00004123/// PromoteOp - Given an operation that produces a value in an invalid type,
4124/// promote it to compute the value into a larger type. The produced value will
4125/// have the correct bits for the low portion of the register, but no guarantee
4126/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00004127SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
4128 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004129 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004130 assert(getTypeAction(VT) == Promote &&
4131 "Caller should expand or legalize operands that are not promotable!");
4132 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
4133 "Cannot promote to smaller type!");
4134
Chris Lattner03c85462005-01-15 05:21:40 +00004135 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00004136 SDOperand Result;
4137 SDNode *Node = Op.Val;
4138
Roman Levensteine3263322008-03-26 12:39:26 +00004139 DenseMap<SDOperandImpl, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004140 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004141
Chris Lattner03c85462005-01-15 05:21:40 +00004142 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004143 case ISD::CopyFromReg:
4144 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004145 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004146#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004147 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004148#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004149 assert(0 && "Do not know how to promote this operator!");
4150 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004151 case ISD::UNDEF:
4152 Result = DAG.getNode(ISD::UNDEF, NVT);
4153 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004154 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004155 if (VT != MVT::i1)
4156 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4157 else
4158 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004159 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4160 break;
4161 case ISD::ConstantFP:
4162 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4163 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4164 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004165
Chris Lattner82fbfb62005-01-18 02:59:52 +00004166 case ISD::SETCC:
Scott Michel5b8f82e2008-03-10 15:42:14 +00004167 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman5922f562008-03-14 00:53:31 +00004168 && "SetCC type is not legal??");
Scott Michel5b8f82e2008-03-10 15:42:14 +00004169 Result = DAG.getNode(ISD::SETCC,
Nate Begeman5922f562008-03-14 00:53:31 +00004170 TLI.getSetCCResultType(Node->getOperand(0)),
4171 Node->getOperand(0), Node->getOperand(1),
4172 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004173 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00004174
Chris Lattner03c85462005-01-15 05:21:40 +00004175 case ISD::TRUNCATE:
4176 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4177 case Legal:
4178 Result = LegalizeOp(Node->getOperand(0));
4179 assert(Result.getValueType() >= NVT &&
4180 "This truncation doesn't make sense!");
4181 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4182 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4183 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004184 case Promote:
4185 // The truncation is not required, because we don't guarantee anything
4186 // about high bits anyway.
4187 Result = PromoteOp(Node->getOperand(0));
4188 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004189 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004190 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4191 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004192 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004193 }
4194 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004195 case ISD::SIGN_EXTEND:
4196 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004197 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004198 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4199 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4200 case Legal:
4201 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004202 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004203 break;
4204 case Promote:
4205 // Promote the reg if it's smaller.
4206 Result = PromoteOp(Node->getOperand(0));
4207 // The high bits are not guaranteed to be anything. Insert an extend.
4208 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004209 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004210 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004211 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004212 Result = DAG.getZeroExtendInReg(Result,
4213 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004214 break;
4215 }
4216 break;
Chris Lattner35481892005-12-23 00:16:34 +00004217 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004218 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4219 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004220 Result = PromoteOp(Result);
4221 break;
4222
Chris Lattner8b6fa222005-01-15 22:16:26 +00004223 case ISD::FP_EXTEND:
4224 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4225 case ISD::FP_ROUND:
4226 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4227 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4228 case Promote: assert(0 && "Unreachable with 2 FP types!");
4229 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004230 if (Node->getConstantOperandVal(1) == 0) {
4231 // Input is legal? Do an FP_ROUND_INREG.
4232 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4233 DAG.getValueType(VT));
4234 } else {
4235 // Just remove the truncate, it isn't affecting the value.
4236 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4237 Node->getOperand(1));
4238 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004239 break;
4240 }
4241 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004242 case ISD::SINT_TO_FP:
4243 case ISD::UINT_TO_FP:
4244 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4245 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004246 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004247 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004248 break;
4249
4250 case Promote:
4251 Result = PromoteOp(Node->getOperand(0));
4252 if (Node->getOpcode() == ISD::SINT_TO_FP)
4253 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004254 Result,
4255 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004256 else
Chris Lattner23993562005-04-13 02:38:47 +00004257 Result = DAG.getZeroExtendInReg(Result,
4258 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004259 // No extra round required here.
4260 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004261 break;
4262 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004263 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4264 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004265 // Round if we cannot tolerate excess precision.
4266 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004267 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4268 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004269 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004270 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004271 break;
4272
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004273 case ISD::SIGN_EXTEND_INREG:
4274 Result = PromoteOp(Node->getOperand(0));
4275 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4276 Node->getOperand(1));
4277 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004278 case ISD::FP_TO_SINT:
4279 case ISD::FP_TO_UINT:
4280 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4281 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004282 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004283 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004284 break;
4285 case Promote:
4286 // The input result is prerounded, so we don't have to do anything
4287 // special.
4288 Tmp1 = PromoteOp(Node->getOperand(0));
4289 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004290 }
Nate Begemand2558e32005-08-14 01:20:53 +00004291 // If we're promoting a UINT to a larger size, check to see if the new node
4292 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4293 // we can use that instead. This allows us to generate better code for
4294 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4295 // legal, such as PowerPC.
4296 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004297 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004298 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4299 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004300 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4301 } else {
4302 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4303 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004304 break;
4305
Chris Lattner2c8086f2005-04-02 05:00:07 +00004306 case ISD::FABS:
4307 case ISD::FNEG:
4308 Tmp1 = PromoteOp(Node->getOperand(0));
4309 assert(Tmp1.getValueType() == NVT);
4310 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4311 // NOTE: we do not have to do any extra rounding here for
4312 // NoExcessFPPrecision, because we know the input will have the appropriate
4313 // precision, and these operations don't modify precision at all.
4314 break;
4315
Chris Lattnerda6ba872005-04-28 21:44:33 +00004316 case ISD::FSQRT:
4317 case ISD::FSIN:
4318 case ISD::FCOS:
4319 Tmp1 = PromoteOp(Node->getOperand(0));
4320 assert(Tmp1.getValueType() == NVT);
4321 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004322 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004323 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4324 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004325 break;
4326
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004327 case ISD::FPOWI: {
4328 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4329 // directly as well, which may be better.
4330 Tmp1 = PromoteOp(Node->getOperand(0));
4331 assert(Tmp1.getValueType() == NVT);
4332 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4333 if (NoExcessFPPrecision)
4334 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4335 DAG.getValueType(VT));
4336 break;
4337 }
4338
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004339 case ISD::ATOMIC_LCS: {
4340 Tmp2 = PromoteOp(Node->getOperand(2));
4341 Tmp3 = PromoteOp(Node->getOperand(3));
4342 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4343 Node->getOperand(1), Tmp2, Tmp3,
4344 cast<AtomicSDNode>(Node)->getVT());
4345 // Remember that we legalized the chain.
4346 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4347 break;
4348 }
4349 case ISD::ATOMIC_LAS:
4350 case ISD::ATOMIC_SWAP: {
4351 Tmp2 = PromoteOp(Node->getOperand(2));
4352 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4353 Node->getOperand(1), Tmp2,
4354 cast<AtomicSDNode>(Node)->getVT());
4355 // Remember that we legalized the chain.
4356 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4357 break;
4358 }
4359
Chris Lattner03c85462005-01-15 05:21:40 +00004360 case ISD::AND:
4361 case ISD::OR:
4362 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004363 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004364 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004365 case ISD::MUL:
4366 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004367 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004368 // that too is okay if they are integer operations.
4369 Tmp1 = PromoteOp(Node->getOperand(0));
4370 Tmp2 = PromoteOp(Node->getOperand(1));
4371 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4372 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004373 break;
4374 case ISD::FADD:
4375 case ISD::FSUB:
4376 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004377 Tmp1 = PromoteOp(Node->getOperand(0));
4378 Tmp2 = PromoteOp(Node->getOperand(1));
4379 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4380 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4381
4382 // Floating point operations will give excess precision that we may not be
4383 // able to tolerate. If we DO allow excess precision, just leave it,
4384 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004385 // FIXME: Why would we need to round FP ops more than integer ones?
4386 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004387 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004388 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4389 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004390 break;
4391
Chris Lattner8b6fa222005-01-15 22:16:26 +00004392 case ISD::SDIV:
4393 case ISD::SREM:
4394 // These operators require that their input be sign extended.
4395 Tmp1 = PromoteOp(Node->getOperand(0));
4396 Tmp2 = PromoteOp(Node->getOperand(1));
4397 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004398 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4399 DAG.getValueType(VT));
4400 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4401 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004402 }
4403 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4404
4405 // Perform FP_ROUND: this is probably overly pessimistic.
4406 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004407 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4408 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004409 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004410 case ISD::FDIV:
4411 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004412 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004413 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004414 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004415 case Expand: assert(0 && "not implemented");
4416 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4417 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004418 }
4419 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004420 case Expand: assert(0 && "not implemented");
4421 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4422 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004423 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004424 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4425
4426 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004427 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004428 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4429 DAG.getValueType(VT));
4430 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004431
4432 case ISD::UDIV:
4433 case ISD::UREM:
4434 // These operators require that their input be zero extended.
4435 Tmp1 = PromoteOp(Node->getOperand(0));
4436 Tmp2 = PromoteOp(Node->getOperand(1));
4437 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004438 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4439 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004440 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4441 break;
4442
4443 case ISD::SHL:
4444 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004445 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004446 break;
4447 case ISD::SRA:
4448 // The input value must be properly sign extended.
4449 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004450 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4451 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004452 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004453 break;
4454 case ISD::SRL:
4455 // The input value must be properly zero extended.
4456 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004457 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004458 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004459 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004460
4461 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004462 Tmp1 = Node->getOperand(0); // Get the chain.
4463 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004464 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4465 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4466 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4467 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004468 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4469 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004470 // Increment the pointer, VAList, to the next vaarg
4471 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4472 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4473 TLI.getPointerTy()));
4474 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004475 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004476 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004477 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004478 }
4479 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004480 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004481 break;
4482
Evan Cheng466685d2006-10-09 20:57:25 +00004483 case ISD::LOAD: {
4484 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004485 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4486 ? ISD::EXTLOAD : LD->getExtensionType();
4487 Result = DAG.getExtLoad(ExtType, NVT,
4488 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004489 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004490 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004491 LD->isVolatile(),
4492 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004493 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004494 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004495 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004496 }
Chris Lattner03c85462005-01-15 05:21:40 +00004497 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00004498 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4499 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00004500 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004501 break;
Nate Begeman9373a812005-08-10 20:51:12 +00004502 case ISD::SELECT_CC:
4503 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4504 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4505 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004506 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004507 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004508 case ISD::BSWAP:
4509 Tmp1 = Node->getOperand(0);
4510 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4511 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4512 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004513 DAG.getConstant(MVT::getSizeInBits(NVT) -
4514 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00004515 TLI.getShiftAmountTy()));
4516 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004517 case ISD::CTPOP:
4518 case ISD::CTTZ:
4519 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004520 // Zero extend the argument
4521 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004522 // Perform the larger operation, then subtract if needed.
4523 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004524 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004525 case ISD::CTPOP:
4526 Result = Tmp1;
4527 break;
4528 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004529 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004530 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004531 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4532 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004533 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004534 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004535 break;
4536 case ISD::CTLZ:
4537 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004538 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004539 DAG.getConstant(MVT::getSizeInBits(NVT) -
4540 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004541 break;
4542 }
4543 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004544 case ISD::EXTRACT_SUBVECTOR:
4545 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004546 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004547 case ISD::EXTRACT_VECTOR_ELT:
4548 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4549 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004550 }
4551
4552 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004553
4554 // Make sure the result is itself legal.
4555 Result = LegalizeOp(Result);
4556
4557 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004558 AddPromotedOperand(Op, Result);
4559 return Result;
4560}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004561
Dan Gohman7f321562007-06-25 16:23:39 +00004562/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4563/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4564/// based on the vector type. The return type of this matches the element type
4565/// of the vector, which may not be legal for the target.
4566SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004567 // We know that operand #0 is the Vec vector. If the index is a constant
4568 // or if the invec is a supported hardware type, we can use it. Otherwise,
4569 // lower to a store then an indexed load.
4570 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004571 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004572
Dan Gohmane40c7b02007-09-24 15:54:53 +00004573 MVT::ValueType TVT = Vec.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00004574 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00004575
Dan Gohman7f321562007-06-25 16:23:39 +00004576 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4577 default: assert(0 && "This action is not supported yet!");
4578 case TargetLowering::Custom: {
4579 Vec = LegalizeOp(Vec);
4580 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4581 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4582 if (Tmp3.Val)
4583 return Tmp3;
4584 break;
4585 }
4586 case TargetLowering::Legal:
4587 if (isTypeLegal(TVT)) {
4588 Vec = LegalizeOp(Vec);
4589 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004590 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004591 }
4592 break;
4593 case TargetLowering::Expand:
4594 break;
4595 }
4596
4597 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004598 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004599 Op = ScalarizeVectorOp(Vec);
4600 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00004601 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00004602 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00004603 SDOperand Lo, Hi;
4604 SplitVectorOp(Vec, Lo, Hi);
Nate Begeman55030dc2008-01-29 02:24:00 +00004605 if (CIdx->getValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00004606 Vec = Lo;
4607 } else {
4608 Vec = Hi;
Nate Begeman55030dc2008-01-29 02:24:00 +00004609 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00004610 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004611 }
Dan Gohman7f321562007-06-25 16:23:39 +00004612
Chris Lattner15972212006-03-31 17:55:51 +00004613 // It's now an extract from the appropriate high or low part. Recurse.
4614 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004615 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004616 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004617 // Store the value to a temporary stack slot, then LOAD the scalar
4618 // element back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004619 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohman7f321562007-06-25 16:23:39 +00004620 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4621
4622 // Add the offset to the index.
4623 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4624 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4625 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004626
4627 if (MVT::getSizeInBits(Idx.getValueType()) >
4628 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004629 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004630 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004631 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004632
Dan Gohman7f321562007-06-25 16:23:39 +00004633 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4634
4635 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004636 }
Dan Gohman7f321562007-06-25 16:23:39 +00004637 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004638}
4639
Dan Gohman7f321562007-06-25 16:23:39 +00004640/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004641/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00004642SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004643 // We know that operand #0 is the Vec vector. For now we assume the index
4644 // is a constant and that the extracted result is a supported hardware type.
4645 SDOperand Vec = Op.getOperand(0);
4646 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4647
Dan Gohman7f321562007-06-25 16:23:39 +00004648 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004649
4650 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4651 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004652 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004653 }
4654
4655 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4656 SDOperand Lo, Hi;
4657 SplitVectorOp(Vec, Lo, Hi);
4658 if (CIdx->getValue() < NumElems/2) {
4659 Vec = Lo;
4660 } else {
4661 Vec = Hi;
4662 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4663 }
4664
4665 // It's now an extract from the appropriate high or low part. Recurse.
4666 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004667 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004668}
4669
Nate Begeman750ac1b2006-02-01 07:19:44 +00004670/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4671/// with condition CC on the current target. This usually involves legalizing
4672/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4673/// there may be no choice but to create a new SetCC node to represent the
4674/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4675/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4676void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4677 SDOperand &RHS,
4678 SDOperand &CC) {
Dale Johannesen638ccd52007-10-06 01:24:11 +00004679 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004680
4681 switch (getTypeAction(LHS.getValueType())) {
4682 case Legal:
4683 Tmp1 = LegalizeOp(LHS); // LHS
4684 Tmp2 = LegalizeOp(RHS); // RHS
4685 break;
4686 case Promote:
4687 Tmp1 = PromoteOp(LHS); // LHS
4688 Tmp2 = PromoteOp(RHS); // RHS
4689
4690 // If this is an FP compare, the operands have already been extended.
4691 if (MVT::isInteger(LHS.getValueType())) {
4692 MVT::ValueType VT = LHS.getValueType();
4693 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4694
4695 // Otherwise, we have to insert explicit sign or zero extends. Note
4696 // that we could insert sign extends for ALL conditions, but zero extend
4697 // is cheaper on many machines (an AND instead of two shifts), so prefer
4698 // it.
4699 switch (cast<CondCodeSDNode>(CC)->get()) {
4700 default: assert(0 && "Unknown integer comparison!");
4701 case ISD::SETEQ:
4702 case ISD::SETNE:
4703 case ISD::SETUGE:
4704 case ISD::SETUGT:
4705 case ISD::SETULE:
4706 case ISD::SETULT:
4707 // ALL of these operations will work if we either sign or zero extend
4708 // the operands (including the unsigned comparisons!). Zero extend is
4709 // usually a simpler/cheaper operation, so prefer it.
4710 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4711 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4712 break;
4713 case ISD::SETGE:
4714 case ISD::SETGT:
4715 case ISD::SETLT:
4716 case ISD::SETLE:
4717 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4718 DAG.getValueType(VT));
4719 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4720 DAG.getValueType(VT));
4721 break;
4722 }
4723 }
4724 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004725 case Expand: {
4726 MVT::ValueType VT = LHS.getValueType();
4727 if (VT == MVT::f32 || VT == MVT::f64) {
4728 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00004729 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004730 switch (cast<CondCodeSDNode>(CC)->get()) {
4731 case ISD::SETEQ:
4732 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004733 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004734 break;
4735 case ISD::SETNE:
4736 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004737 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004738 break;
4739 case ISD::SETGE:
4740 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004741 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004742 break;
4743 case ISD::SETLT:
4744 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004745 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004746 break;
4747 case ISD::SETLE:
4748 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004749 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004750 break;
4751 case ISD::SETGT:
4752 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004753 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004754 break;
4755 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004756 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4757 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004758 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004759 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004760 break;
4761 default:
Evan Cheng56966222007-01-12 02:11:51 +00004762 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004763 switch (cast<CondCodeSDNode>(CC)->get()) {
4764 case ISD::SETONE:
4765 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004766 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004767 // Fallthrough
4768 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004769 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004770 break;
4771 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004772 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004773 break;
4774 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004775 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004776 break;
4777 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004778 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004779 break;
Evan Cheng56966222007-01-12 02:11:51 +00004780 case ISD::SETUEQ:
4781 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004782 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004783 default: assert(0 && "Unsupported FP setcc!");
4784 }
4785 }
4786
4787 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004788 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004789 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004790 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004791 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004792 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004793 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00004794 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00004795 CC);
Evan Cheng56966222007-01-12 02:11:51 +00004796 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004797 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004798 false /*sign irrelevant*/, Dummy);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004799 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004800 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004801 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4802 Tmp2 = SDOperand();
4803 }
4804 LHS = Tmp1;
4805 RHS = Tmp2;
4806 return;
4807 }
4808
Nate Begeman750ac1b2006-02-01 07:19:44 +00004809 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4810 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004811 ExpandOp(RHS, RHSLo, RHSHi);
4812 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4813
4814 if (VT==MVT::ppcf128) {
4815 // FIXME: This generated code sucks. We want to generate
4816 // FCMP crN, hi1, hi2
4817 // BNE crN, L:
4818 // FCMP crN, lo1, lo2
4819 // The following can be improved, but not that much.
Scott Michel5b8f82e2008-03-10 15:42:14 +00004820 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
4821 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004822 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004823 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
4824 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004825 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4826 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4827 Tmp2 = SDOperand();
4828 break;
4829 }
4830
4831 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004832 case ISD::SETEQ:
4833 case ISD::SETNE:
4834 if (RHSLo == RHSHi)
4835 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4836 if (RHSCST->isAllOnesValue()) {
4837 // Comparison to -1.
4838 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4839 Tmp2 = RHSLo;
4840 break;
4841 }
4842
4843 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4844 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4845 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4846 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4847 break;
4848 default:
4849 // If this is a comparison of the sign bit, just look at the top part.
4850 // X > -1, x < 0
4851 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4852 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004853 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00004854 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4855 CST->isAllOnesValue())) { // X > -1
4856 Tmp1 = LHSHi;
4857 Tmp2 = RHSHi;
4858 break;
4859 }
4860
4861 // FIXME: This generated code sucks.
4862 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004863 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004864 default: assert(0 && "Unknown integer setcc!");
4865 case ISD::SETLT:
4866 case ISD::SETULT: LowCC = ISD::SETULT; break;
4867 case ISD::SETGT:
4868 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4869 case ISD::SETLE:
4870 case ISD::SETULE: LowCC = ISD::SETULE; break;
4871 case ISD::SETGE:
4872 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4873 }
4874
4875 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4876 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4877 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4878
4879 // NOTE: on targets without efficient SELECT of bools, we can always use
4880 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004881 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004882 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman5922f562008-03-14 00:53:31 +00004883 LowCC, false, DagCombineInfo);
Evan Cheng2e677812007-02-08 22:16:19 +00004884 if (!Tmp1.Val)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004885 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
4886 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00004887 CCCode, false, DagCombineInfo);
4888 if (!Tmp2.Val)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004889 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00004890 RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004891
4892 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4893 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
Dan Gohman002e5d02008-03-13 22:13:53 +00004894 if ((Tmp1C && Tmp1C->isNullValue()) ||
4895 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00004896 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4897 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00004898 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00004899 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4900 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4901 // low part is known false, returns high part.
4902 // For LE / GE, if high part is known false, ignore the low part.
4903 // For LT / GT, if high part is known true, ignore the low part.
4904 Tmp1 = Tmp2;
4905 Tmp2 = SDOperand();
4906 } else {
Scott Michel5b8f82e2008-03-10 15:42:14 +00004907 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00004908 ISD::SETEQ, false, DagCombineInfo);
4909 if (!Result.Val)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004910 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00004911 ISD::SETEQ);
Evan Cheng2e677812007-02-08 22:16:19 +00004912 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4913 Result, Tmp1, Tmp2));
4914 Tmp1 = Result;
4915 Tmp2 = SDOperand();
4916 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004917 }
4918 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004919 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004920 LHS = Tmp1;
4921 RHS = Tmp2;
4922}
4923
Chris Lattner1401d152008-01-16 07:45:30 +00004924/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4925/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4926/// a load from the stack slot to DestVT, extending it if needed.
4927/// The resultant code need not be legal.
4928SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4929 MVT::ValueType SlotVT,
4930 MVT::ValueType DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00004931 // Create the stack frame object.
Chris Lattner1401d152008-01-16 07:45:30 +00004932 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4933
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00004934 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00004935 int SPFI = StackPtrFI->getIndex();
4936
Chris Lattner1401d152008-01-16 07:45:30 +00004937 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4938 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4939 unsigned DestSize = MVT::getSizeInBits(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004940
Chris Lattner1401d152008-01-16 07:45:30 +00004941 // Emit a store to the stack slot. Use a truncstore if the input value is
4942 // later than DestVT.
4943 SDOperand Store;
4944 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00004945 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004946 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00004947 SPFI, SlotVT);
Chris Lattner1401d152008-01-16 07:45:30 +00004948 else {
4949 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00004950 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004951 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00004952 SPFI, SlotVT);
Chris Lattner1401d152008-01-16 07:45:30 +00004953 }
4954
Chris Lattner35481892005-12-23 00:16:34 +00004955 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00004956 if (SlotSize == DestSize)
4957 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4958
4959 assert(SlotSize < DestSize && "Unknown extension!");
4960 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Chris Lattner35481892005-12-23 00:16:34 +00004961}
4962
Chris Lattner4352cc92006-04-04 17:23:26 +00004963SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4964 // Create a vector sized/aligned stack slot, store the value to element #0,
4965 // then load the whole vector back out.
Chris Lattner85dd3be2007-10-15 17:48:57 +00004966 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00004967
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00004968 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00004969 int SPFI = StackPtrFI->getIndex();
4970
Evan Cheng786225a2006-10-05 23:01:46 +00004971 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004972 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohman69de1932008-02-06 22:27:42 +00004973 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman3069b872008-02-07 18:41:25 +00004974 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner4352cc92006-04-04 17:23:26 +00004975}
4976
4977
Chris Lattnerce872152006-03-19 06:31:19 +00004978/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004979/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004980SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4981
4982 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004983 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004984 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004985 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004986 SDOperand SplatValue = Node->getOperand(0);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00004987
4988 // FIXME: it would be far nicer to change this into map<SDOperand,uint64_t>
4989 // and use a bitmask instead of a list of elements.
Evan Cheng033e6812006-03-24 01:17:21 +00004990 std::map<SDOperand, std::vector<unsigned> > Values;
4991 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004992 bool isConstant = true;
4993 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4994 SplatValue.getOpcode() != ISD::UNDEF)
4995 isConstant = false;
4996
Evan Cheng033e6812006-03-24 01:17:21 +00004997 for (unsigned i = 1; i < NumElems; ++i) {
4998 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004999 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005000 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005001 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005002 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00005003 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005004
5005 // If this isn't a constant element or an undef, we can't use a constant
5006 // pool load.
5007 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5008 V.getOpcode() != ISD::UNDEF)
5009 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005010 }
5011
5012 if (isOnlyLowElement) {
5013 // If the low element is an undef too, then this whole things is an undef.
5014 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5015 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5016 // Otherwise, turn this into a scalar_to_vector node.
5017 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5018 Node->getOperand(0));
5019 }
5020
Chris Lattner2eb86532006-03-24 07:29:17 +00005021 // If all elements are constants, create a load from the constant pool.
5022 if (isConstant) {
5023 MVT::ValueType VT = Node->getValueType(0);
5024 const Type *OpNTy =
5025 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
5026 std::vector<Constant*> CV;
5027 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5028 if (ConstantFPSDNode *V =
5029 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesenf04afdb2007-08-30 00:23:21 +00005030 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005031 } else if (ConstantSDNode *V =
5032 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00005033 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005034 } else {
5035 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
5036 CV.push_back(UndefValue::get(OpNTy));
5037 }
5038 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005039 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00005040 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005041 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005042 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005043 }
5044
Chris Lattner87100e02006-03-20 01:52:29 +00005045 if (SplatValue.Val) { // Splat of one value?
5046 // Build the shuffle constant vector: <0, 0, 0, 0>
5047 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00005048 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00005049 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00005050 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005051 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
5052 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00005053
5054 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005055 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00005056 // Get the splatted value into the low element of a vector register.
5057 SDOperand LowValVec =
5058 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5059
5060 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5061 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5062 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5063 SplatMask);
5064 }
5065 }
5066
Evan Cheng033e6812006-03-24 01:17:21 +00005067 // If there are only two unique elements, we may be able to turn this into a
5068 // vector shuffle.
5069 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005070 // Get the two values in deterministic order.
5071 SDOperand Val1 = Node->getOperand(1);
5072 SDOperand Val2;
5073 std::map<SDOperand, std::vector<unsigned> >::iterator MI = Values.begin();
5074 if (MI->first != Val1)
5075 Val2 = MI->first;
5076 else
5077 Val2 = (++MI)->first;
5078
5079 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5080 // vector shuffle has the undef vector on the RHS.
5081 if (Val1.getOpcode() == ISD::UNDEF)
5082 std::swap(Val1, Val2);
5083
Evan Cheng033e6812006-03-24 01:17:21 +00005084 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005085 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5086 MVT::ValueType MaskEltVT = MVT::getVectorElementType(MaskVT);
Evan Cheng033e6812006-03-24 01:17:21 +00005087 std::vector<SDOperand> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005088
5089 // Set elements of the shuffle mask for Val1.
5090 std::vector<unsigned> &Val1Elts = Values[Val1];
5091 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5092 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5093
5094 // Set elements of the shuffle mask for Val2.
5095 std::vector<unsigned> &Val2Elts = Values[Val2];
5096 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5097 if (Val2.getOpcode() != ISD::UNDEF)
5098 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5099 else
5100 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5101
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005102 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
5103 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005104
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005105 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005106 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5107 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005108 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5109 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
5110 SDOperand Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00005111
5112 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005113 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00005114 }
5115 }
Chris Lattnerce872152006-03-19 06:31:19 +00005116
5117 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5118 // aligned object on the stack, store each element into it, then load
5119 // the result as a vector.
5120 MVT::ValueType VT = Node->getValueType(0);
5121 // Create the stack frame object.
Chris Lattner85dd3be2007-10-15 17:48:57 +00005122 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005123
5124 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005125 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00005126 unsigned TypeByteSize =
5127 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005128 // Store (in the right endianness) the elements to memory.
5129 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5130 // Ignore undef elements.
5131 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5132
Chris Lattner841c8822006-03-22 01:46:54 +00005133 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005134
5135 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5136 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5137
Evan Cheng786225a2006-10-05 23:01:46 +00005138 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00005139 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00005140 }
5141
5142 SDOperand StoreChain;
5143 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005144 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5145 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005146 else
5147 StoreChain = DAG.getEntryNode();
5148
5149 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00005150 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005151}
5152
Chris Lattner5b359c62005-04-02 04:00:59 +00005153void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5154 SDOperand Op, SDOperand Amt,
5155 SDOperand &Lo, SDOperand &Hi) {
5156 // Expand the subcomponents.
5157 SDOperand LHSL, LHSH;
5158 ExpandOp(Op, LHSL, LHSH);
5159
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005160 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005161 MVT::ValueType VT = LHSL.getValueType();
5162 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005163 Hi = Lo.getValue(1);
5164}
5165
5166
Chris Lattnere34b3962005-01-19 04:19:40 +00005167/// ExpandShift - Try to find a clever way to expand this shift operation out to
5168/// smaller elements. If we can't find a way that is more efficient than a
5169/// libcall on this target, return false. Otherwise, return true with the
5170/// low-parts expanded into Lo and Hi.
5171bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
5172 SDOperand &Lo, SDOperand &Hi) {
5173 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5174 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005175
Chris Lattnere34b3962005-01-19 04:19:40 +00005176 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005177 SDOperand ShAmt = LegalizeOp(Amt);
5178 MVT::ValueType ShTy = ShAmt.getValueType();
Dan Gohman91dc17b2008-02-20 16:57:27 +00005179 unsigned ShBits = MVT::getSizeInBits(ShTy);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005180 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
5181 unsigned NVTBits = MVT::getSizeInBits(NVT);
5182
Chris Lattner7a3c8552007-10-14 20:35:12 +00005183 // Handle the case when Amt is an immediate.
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005184 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5185 unsigned Cst = CN->getValue();
5186 // Expand the incoming operand to be shifted, so that we have its parts
5187 SDOperand InL, InH;
5188 ExpandOp(Op, InL, InH);
5189 switch(Opc) {
5190 case ISD::SHL:
5191 if (Cst > VTBits) {
5192 Lo = DAG.getConstant(0, NVT);
5193 Hi = DAG.getConstant(0, NVT);
5194 } else if (Cst > NVTBits) {
5195 Lo = DAG.getConstant(0, NVT);
5196 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005197 } else if (Cst == NVTBits) {
5198 Lo = DAG.getConstant(0, NVT);
5199 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005200 } else {
5201 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5202 Hi = DAG.getNode(ISD::OR, NVT,
5203 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5204 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5205 }
5206 return true;
5207 case ISD::SRL:
5208 if (Cst > VTBits) {
5209 Lo = DAG.getConstant(0, NVT);
5210 Hi = DAG.getConstant(0, NVT);
5211 } else if (Cst > NVTBits) {
5212 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5213 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005214 } else if (Cst == NVTBits) {
5215 Lo = InH;
5216 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005217 } else {
5218 Lo = DAG.getNode(ISD::OR, NVT,
5219 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5220 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5221 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5222 }
5223 return true;
5224 case ISD::SRA:
5225 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005226 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005227 DAG.getConstant(NVTBits-1, ShTy));
5228 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005229 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005230 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005231 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005232 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005233 } else if (Cst == NVTBits) {
5234 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005235 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005236 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005237 } else {
5238 Lo = DAG.getNode(ISD::OR, NVT,
5239 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5240 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5241 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5242 }
5243 return true;
5244 }
5245 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005246
5247 // Okay, the shift amount isn't constant. However, if we can tell that it is
5248 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005249 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5250 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005251 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005252
Dan Gohman9e255b72008-02-22 01:12:31 +00005253 // If we know that if any of the high bits of the shift amount are one, then
5254 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005255 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005256 // Mask out the high bit, which we know is set.
5257 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005258 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005259
5260 // Expand the incoming operand to be shifted, so that we have its parts
5261 SDOperand InL, InH;
5262 ExpandOp(Op, InL, InH);
5263 switch(Opc) {
5264 case ISD::SHL:
5265 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5266 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5267 return true;
5268 case ISD::SRL:
5269 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5270 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5271 return true;
5272 case ISD::SRA:
5273 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5274 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5275 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5276 return true;
5277 }
5278 }
5279
Dan Gohman9e255b72008-02-22 01:12:31 +00005280 // If we know that the high bits of the shift amount are all zero, then we can
5281 // do this as a couple of simple shifts.
5282 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005283 // Compute 32-amt.
5284 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5285 DAG.getConstant(NVTBits, Amt.getValueType()),
5286 Amt);
5287
5288 // Expand the incoming operand to be shifted, so that we have its parts
5289 SDOperand InL, InH;
5290 ExpandOp(Op, InL, InH);
5291 switch(Opc) {
5292 case ISD::SHL:
5293 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5294 Hi = DAG.getNode(ISD::OR, NVT,
5295 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5296 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5297 return true;
5298 case ISD::SRL:
5299 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5300 Lo = DAG.getNode(ISD::OR, NVT,
5301 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5302 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5303 return true;
5304 case ISD::SRA:
5305 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5306 Lo = DAG.getNode(ISD::OR, NVT,
5307 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5308 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5309 return true;
5310 }
5311 }
5312
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005313 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005314}
Chris Lattner77e77a62005-01-21 06:05:23 +00005315
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005316
Chris Lattner77e77a62005-01-21 06:05:23 +00005317// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5318// does not fit into a register, return the lo part and set the hi part to the
5319// by-reg argument. If it does fit into a single register, return the result
5320// and leave the Hi part unset.
5321SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00005322 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005323 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5324 // The input chain to this libcall is the entry node of the function.
5325 // Legalizing the call will automatically add the previous call to the
5326 // dependence.
5327 SDOperand InChain = DAG.getEntryNode();
5328
Chris Lattner77e77a62005-01-21 06:05:23 +00005329 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005330 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005331 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5332 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5333 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00005334 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005335 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005336 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005337 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005338 }
5339 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005340
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005341 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00005342 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005343 std::pair<SDOperand,SDOperand> CallInfo =
Duncan Sands00fee652008-02-14 17:28:50 +00005344 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5345 false, Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005346
Chris Lattner6831a812006-02-13 09:18:02 +00005347 // Legalize the call sequence, starting with the chain. This will advance
5348 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5349 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5350 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00005351 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005352 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005353 default: assert(0 && "Unknown thing");
5354 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005355 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005356 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005357 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005358 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005359 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005360 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005361 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005362}
5363
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005364
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005365/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5366///
Chris Lattner77e77a62005-01-21 06:05:23 +00005367SDOperand SelectionDAGLegalize::
5368ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Dan Gohmand91446d2008-03-05 01:08:17 +00005369 MVT::ValueType SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005370 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005371
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005372 if (!isSigned) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005373 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005374 // incoming integer is set. To handle this, we dynamically test to see if
5375 // it is set, and, if so, add a fudge factor.
Dan Gohman034f60e2008-03-11 01:59:03 +00005376 SDOperand Hi;
5377 if (ExpandSource) {
5378 SDOperand Lo;
5379 ExpandOp(Source, Lo, Hi);
5380 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5381 } else {
5382 // The comparison for the sign bit will use the entire operand.
5383 Hi = Source;
5384 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005385
Chris Lattner66de05b2005-05-13 04:45:13 +00005386 // If this is unsigned, and not supported, first perform the conversion to
5387 // signed, then adjust the result if the sign bit is set.
Dan Gohman034f60e2008-03-11 01:59:03 +00005388 SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner66de05b2005-05-13 04:45:13 +00005389
Scott Michel5b8f82e2008-03-10 15:42:14 +00005390 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005391 DAG.getConstant(0, Hi.getValueType()),
5392 ISD::SETLT);
Chris Lattner0bd48932008-01-17 07:00:52 +00005393 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005394 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5395 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005396 uint64_t FF = 0x5f800000ULL;
5397 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00005398 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005399
Chris Lattner5839bf22005-08-26 17:15:30 +00005400 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00005401 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5402 SDOperand FudgeInReg;
5403 if (DestTy == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005404 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005405 PseudoSourceValue::getConstantPool(), 0);
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005406 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005407 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005408 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005409 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005410 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman69de1932008-02-06 22:27:42 +00005411 MVT::f32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005412 else
5413 assert(0 && "Unexpected conversion");
5414
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005415 MVT::ValueType SCVT = SignedConv.getValueType();
5416 if (SCVT != DestTy) {
5417 // Destination type needs to be expanded as well. The FADD now we are
5418 // constructing will be expanded into a libcall.
5419 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
Dan Gohmand91446d2008-03-05 01:08:17 +00005420 assert(MVT::getSizeInBits(SCVT) * 2 == MVT::getSizeInBits(DestTy));
5421 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005422 SignedConv, SignedConv.getValue(1));
5423 }
5424 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5425 }
Chris Lattner473a9902005-09-29 06:44:39 +00005426 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005427 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005428
Chris Lattnera88a2602005-05-14 05:33:54 +00005429 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005430 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005431 default: assert(0 && "This action not implemented for this operation!");
5432 case TargetLowering::Legal:
5433 case TargetLowering::Expand:
5434 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005435 case TargetLowering::Custom: {
5436 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5437 Source), DAG);
5438 if (NV.Val)
5439 return LegalizeOp(NV);
5440 break; // The target decided this was legal after all
5441 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005442 }
5443
Chris Lattner13689e22005-05-12 07:00:44 +00005444 // Expand the source, then glue it back together for the call. We must expand
5445 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00005446 if (ExpandSource) {
5447 SDOperand SrcLo, SrcHi;
5448 ExpandOp(Source, SrcLo, SrcHi);
5449 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5450 }
Chris Lattner13689e22005-05-12 07:00:44 +00005451
Evan Cheng56966222007-01-12 02:11:51 +00005452 RTLIB::Libcall LC;
Evan Cheng110cf482008-04-01 01:50:16 +00005453 if (SourceVT == MVT::i32) {
5454 if (DestTy == MVT::f32)
5455 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
5456 else {
5457 assert(DestTy == MVT::f64 && "Unknown fp value type!");
5458 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
5459 }
5460 } else if (SourceVT == MVT::i64) {
Dan Gohmand91446d2008-03-05 01:08:17 +00005461 if (DestTy == MVT::f32)
5462 LC = RTLIB::SINTTOFP_I64_F32;
Dan Gohman034f60e2008-03-11 01:59:03 +00005463 else if (DestTy == MVT::f64)
Dan Gohmand91446d2008-03-05 01:08:17 +00005464 LC = RTLIB::SINTTOFP_I64_F64;
Dan Gohman034f60e2008-03-11 01:59:03 +00005465 else if (DestTy == MVT::f80)
5466 LC = RTLIB::SINTTOFP_I64_F80;
5467 else {
5468 assert(DestTy == MVT::ppcf128 && "Unknown fp value type!");
5469 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dan Gohmand91446d2008-03-05 01:08:17 +00005470 }
5471 } else if (SourceVT == MVT::i128) {
5472 if (DestTy == MVT::f32)
5473 LC = RTLIB::SINTTOFP_I128_F32;
5474 else if (DestTy == MVT::f64)
5475 LC = RTLIB::SINTTOFP_I128_F64;
5476 else if (DestTy == MVT::f80)
5477 LC = RTLIB::SINTTOFP_I128_F80;
5478 else {
5479 assert(DestTy == MVT::ppcf128 && "Unknown fp value type!");
5480 LC = RTLIB::SINTTOFP_I128_PPCF128;
5481 }
5482 } else {
5483 assert(0 && "Unknown int value type");
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005484 }
Chris Lattner6831a812006-02-13 09:18:02 +00005485
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005486 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00005487 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohmana2e94852008-03-10 23:03:31 +00005488 SDOperand HiPart;
5489 SDOperand Result = ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5490 HiPart);
Evan Cheng110cf482008-04-01 01:50:16 +00005491 if (Result.getValueType() != DestTy && HiPart.Val)
Dan Gohmana2e94852008-03-10 23:03:31 +00005492 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5493 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005494}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005495
Chris Lattner22cde6a2006-01-28 08:25:58 +00005496/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5497/// INT_TO_FP operation of the specified operand when the target requests that
5498/// we expand it. At this point, we know that the result and operand types are
5499/// legal for the target.
5500SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5501 SDOperand Op0,
5502 MVT::ValueType DestVT) {
5503 if (Op0.getValueType() == MVT::i32) {
5504 // simple 32-bit [signed|unsigned] integer to float/double expansion
5505
Chris Lattner23594d42008-01-16 07:03:22 +00005506 // Get the stack frame index of a 8 byte buffer.
5507 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5508
Chris Lattner22cde6a2006-01-28 08:25:58 +00005509 // word offset constant for Hi/Lo address computation
5510 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5511 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00005512 SDOperand Hi = StackSlot;
5513 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5514 if (TLI.isLittleEndian())
5515 std::swap(Hi, Lo);
5516
Chris Lattner22cde6a2006-01-28 08:25:58 +00005517 // if signed map to unsigned space
5518 SDOperand Op0Mapped;
5519 if (isSigned) {
5520 // constant used to invert sign bit (signed to unsigned mapping)
5521 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5522 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5523 } else {
5524 Op0Mapped = Op0;
5525 }
5526 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00005527 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005528 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005529 // initial hi portion of constructed double
5530 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5531 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00005532 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005533 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00005534 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005535 // FP constant to bias correct the final result
5536 SDOperand Bias = DAG.getConstantFP(isSigned ?
5537 BitsToDouble(0x4330000080000000ULL)
5538 : BitsToDouble(0x4330000000000000ULL),
5539 MVT::f64);
5540 // subtract the bias
5541 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5542 // final result
5543 SDOperand Result;
5544 // handle final rounding
5545 if (DestVT == MVT::f64) {
5546 // do nothing
5547 Result = Sub;
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005548 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00005549 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5550 DAG.getIntPtrConstant(0));
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005551 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5552 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005553 }
5554 return Result;
5555 }
5556 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5557 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5558
Scott Michel5b8f82e2008-03-10 15:42:14 +00005559 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005560 DAG.getConstant(0, Op0.getValueType()),
5561 ISD::SETLT);
Chris Lattner0bd48932008-01-17 07:00:52 +00005562 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005563 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5564 SignSet, Four, Zero);
5565
5566 // If the sign bit of the integer is set, the large number will be treated
5567 // as a negative number. To counteract this, the dynamic code adds an
5568 // offset depending on the data type.
5569 uint64_t FF;
5570 switch (Op0.getValueType()) {
5571 default: assert(0 && "Unsupported integer type!");
5572 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5573 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5574 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5575 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5576 }
5577 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005578 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005579
5580 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5581 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5582 SDOperand FudgeInReg;
5583 if (DestVT == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005584 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005585 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005586 else {
Dan Gohman69de1932008-02-06 22:27:42 +00005587 FudgeInReg =
5588 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5589 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005590 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman69de1932008-02-06 22:27:42 +00005591 MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005592 }
5593
5594 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5595}
5596
5597/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5598/// *INT_TO_FP operation of the specified operand when the target requests that
5599/// we promote it. At this point, we know that the result and operand types are
5600/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5601/// operation that takes a larger input.
5602SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5603 MVT::ValueType DestVT,
5604 bool isSigned) {
5605 // First step, figure out the appropriate *INT_TO_FP operation to use.
5606 MVT::ValueType NewInTy = LegalOp.getValueType();
5607
5608 unsigned OpToUse = 0;
5609
5610 // Scan for the appropriate larger type to use.
5611 while (1) {
5612 NewInTy = (MVT::ValueType)(NewInTy+1);
5613 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5614
5615 // If the target supports SINT_TO_FP of this type, use it.
5616 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5617 default: break;
5618 case TargetLowering::Legal:
5619 if (!TLI.isTypeLegal(NewInTy))
5620 break; // Can't use this datatype.
5621 // FALL THROUGH.
5622 case TargetLowering::Custom:
5623 OpToUse = ISD::SINT_TO_FP;
5624 break;
5625 }
5626 if (OpToUse) break;
5627 if (isSigned) continue;
5628
5629 // If the target supports UINT_TO_FP of this type, use it.
5630 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5631 default: break;
5632 case TargetLowering::Legal:
5633 if (!TLI.isTypeLegal(NewInTy))
5634 break; // Can't use this datatype.
5635 // FALL THROUGH.
5636 case TargetLowering::Custom:
5637 OpToUse = ISD::UINT_TO_FP;
5638 break;
5639 }
5640 if (OpToUse) break;
5641
5642 // Otherwise, try a larger type.
5643 }
5644
5645 // Okay, we found the operation and type to use. Zero extend our input to the
5646 // desired type then run the operation on it.
5647 return DAG.getNode(OpToUse, DestVT,
5648 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5649 NewInTy, LegalOp));
5650}
5651
5652/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5653/// FP_TO_*INT operation of the specified operand when the target requests that
5654/// we promote it. At this point, we know that the result and operand types are
5655/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5656/// operation that returns a larger result.
5657SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5658 MVT::ValueType DestVT,
5659 bool isSigned) {
5660 // First step, figure out the appropriate FP_TO*INT operation to use.
5661 MVT::ValueType NewOutTy = DestVT;
5662
5663 unsigned OpToUse = 0;
5664
5665 // Scan for the appropriate larger type to use.
5666 while (1) {
5667 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5668 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5669
5670 // If the target supports FP_TO_SINT returning this type, use it.
5671 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5672 default: break;
5673 case TargetLowering::Legal:
5674 if (!TLI.isTypeLegal(NewOutTy))
5675 break; // Can't use this datatype.
5676 // FALL THROUGH.
5677 case TargetLowering::Custom:
5678 OpToUse = ISD::FP_TO_SINT;
5679 break;
5680 }
5681 if (OpToUse) break;
5682
5683 // If the target supports FP_TO_UINT of this type, use it.
5684 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5685 default: break;
5686 case TargetLowering::Legal:
5687 if (!TLI.isTypeLegal(NewOutTy))
5688 break; // Can't use this datatype.
5689 // FALL THROUGH.
5690 case TargetLowering::Custom:
5691 OpToUse = ISD::FP_TO_UINT;
5692 break;
5693 }
5694 if (OpToUse) break;
5695
5696 // Otherwise, try a larger type.
5697 }
5698
Chris Lattner27a6c732007-11-24 07:07:01 +00005699
5700 // Okay, we found the operation and type to use.
5701 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5702
5703 // If the operation produces an invalid type, it must be custom lowered. Use
5704 // the target lowering hooks to expand it. Just keep the low part of the
5705 // expanded operation, we know that we're truncating anyway.
5706 if (getTypeAction(NewOutTy) == Expand) {
5707 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5708 assert(Operation.Val && "Didn't return anything");
5709 }
5710
5711 // Truncate the result of the extended FP_TO_*INT operation to the desired
5712 // size.
5713 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005714}
5715
5716/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5717///
5718SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5719 MVT::ValueType VT = Op.getValueType();
5720 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5721 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5722 switch (VT) {
5723 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5724 case MVT::i16:
5725 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5726 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5727 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5728 case MVT::i32:
5729 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5730 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5731 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5732 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5733 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5734 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5735 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5736 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5737 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5738 case MVT::i64:
5739 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5740 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5741 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5742 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5743 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5744 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5745 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5746 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5747 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5748 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5749 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5750 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5751 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5752 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5753 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5754 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5755 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5756 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5757 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5758 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5759 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5760 }
5761}
5762
5763/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5764///
5765SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5766 switch (Opc) {
5767 default: assert(0 && "Cannot expand this yet!");
5768 case ISD::CTPOP: {
5769 static const uint64_t mask[6] = {
5770 0x5555555555555555ULL, 0x3333333333333333ULL,
5771 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5772 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5773 };
5774 MVT::ValueType VT = Op.getValueType();
5775 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005776 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005777 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5778 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5779 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5780 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5781 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5782 DAG.getNode(ISD::AND, VT,
5783 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5784 }
5785 return Op;
5786 }
5787 case ISD::CTLZ: {
5788 // for now, we do this:
5789 // x = x | (x >> 1);
5790 // x = x | (x >> 2);
5791 // ...
5792 // x = x | (x >>16);
5793 // x = x | (x >>32); // for 64-bit input
5794 // return popcount(~x);
5795 //
5796 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5797 MVT::ValueType VT = Op.getValueType();
5798 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00005799 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005800 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5801 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5802 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5803 }
5804 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5805 return DAG.getNode(ISD::CTPOP, VT, Op);
5806 }
5807 case ISD::CTTZ: {
5808 // for now, we use: { return popcount(~x & (x - 1)); }
5809 // unless the target has ctlz but not ctpop, in which case we use:
5810 // { return 32 - nlz(~x & (x-1)); }
5811 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5812 MVT::ValueType VT = Op.getValueType();
5813 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5814 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5815 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5816 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5817 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5818 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5819 TLI.isOperationLegal(ISD::CTLZ, VT))
5820 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00005821 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005822 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5823 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5824 }
5825 }
5826}
Chris Lattnere34b3962005-01-19 04:19:40 +00005827
Chris Lattner3e928bb2005-01-07 07:47:09 +00005828/// ExpandOp - Expand the specified SDOperand into its two component pieces
5829/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5830/// LegalizeNodes map is filled in for any results that are not expanded, the
5831/// ExpandedNodes map is filled in for any results that are expanded, and the
5832/// Lo/Hi values are returned.
5833void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5834 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00005835 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005836 SDNode *Node = Op.Val;
5837 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005838 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00005839 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00005840 "Cannot expand to FP value or to larger int value!");
5841
Chris Lattner6fdcb252005-09-02 20:32:45 +00005842 // See if we already expanded it.
Roman Levensteine3263322008-03-26 12:39:26 +00005843 DenseMap<SDOperandImpl, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00005844 = ExpandedNodes.find(Op);
5845 if (I != ExpandedNodes.end()) {
5846 Lo = I->second.first;
5847 Hi = I->second.second;
5848 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005849 }
5850
Chris Lattner3e928bb2005-01-07 07:47:09 +00005851 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005852 case ISD::CopyFromReg:
5853 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005854 case ISD::FP_ROUND_INREG:
5855 if (VT == MVT::ppcf128 &&
5856 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5857 TargetLowering::Custom) {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00005858 SDOperand SrcLo, SrcHi, Src;
5859 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5860 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5861 SDOperand Result = TLI.LowerOperation(
5862 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00005863 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5864 Lo = Result.Val->getOperand(0);
5865 Hi = Result.Val->getOperand(1);
5866 break;
5867 }
5868 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00005869 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005870#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005871 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005872#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00005873 assert(0 && "Do not know how to expand this operator!");
5874 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00005875 case ISD::EXTRACT_ELEMENT:
5876 ExpandOp(Node->getOperand(0), Lo, Hi);
5877 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5878 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00005879 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00005880 case ISD::EXTRACT_VECTOR_ELT:
5881 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5882 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5883 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5884 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005885 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00005886 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005887 Lo = DAG.getNode(ISD::UNDEF, NVT);
5888 Hi = DAG.getNode(ISD::UNDEF, NVT);
5889 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005890 case ISD::Constant: {
Dan Gohman050f5502008-03-03 22:20:46 +00005891 unsigned NVTBits = MVT::getSizeInBits(NVT);
5892 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
5893 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
5894 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005895 break;
5896 }
Evan Cheng00495212006-12-12 21:32:44 +00005897 case ISD::ConstantFP: {
5898 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00005899 if (CFP->getValueType(0) == MVT::ppcf128) {
5900 APInt api = CFP->getValueAPF().convertToAPInt();
5901 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5902 MVT::f64);
5903 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5904 MVT::f64);
5905 break;
5906 }
Evan Cheng279101e2006-12-12 22:19:28 +00005907 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00005908 if (getTypeAction(Lo.getValueType()) == Expand)
5909 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005910 break;
5911 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005912 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005913 // Return the operands.
5914 Lo = Node->getOperand(0);
5915 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005916 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00005917
5918 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00005919 if (Node->getNumValues() == 1) {
5920 ExpandOp(Op.getOperand(0), Lo, Hi);
5921 break;
5922 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005923 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5924 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5925 Op.getValue(1).getValueType() == MVT::Other &&
5926 "unhandled MERGE_VALUES");
5927 ExpandOp(Op.getOperand(0), Lo, Hi);
5928 // Remember that we legalized the chain.
5929 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5930 break;
Chris Lattner58f79632005-12-12 22:27:43 +00005931
5932 case ISD::SIGN_EXTEND_INREG:
5933 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00005934 // sext_inreg the low part if needed.
5935 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5936
5937 // The high part gets the sign extension from the lo-part. This handles
5938 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005939 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5940 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5941 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005942 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005943
Nate Begemand88fc032006-01-14 03:14:10 +00005944 case ISD::BSWAP: {
5945 ExpandOp(Node->getOperand(0), Lo, Hi);
5946 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5947 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5948 Lo = TempLo;
5949 break;
5950 }
5951
Chris Lattneredb1add2005-05-11 04:51:16 +00005952 case ISD::CTPOP:
5953 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005954 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5955 DAG.getNode(ISD::CTPOP, NVT, Lo),
5956 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005957 Hi = DAG.getConstant(0, NVT);
5958 break;
5959
Chris Lattner39a8f332005-05-12 19:05:01 +00005960 case ISD::CTLZ: {
5961 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005962 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005963 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5964 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005965 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005966 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005967 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5968 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5969
5970 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5971 Hi = DAG.getConstant(0, NVT);
5972 break;
5973 }
5974
5975 case ISD::CTTZ: {
5976 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005977 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005978 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5979 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005980 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005981 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005982 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5983 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5984
5985 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5986 Hi = DAG.getConstant(0, NVT);
5987 break;
5988 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005989
Nate Begemanacc398c2006-01-25 18:21:52 +00005990 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005991 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5992 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005993 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5994 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5995
5996 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005997 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005998 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00005999 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006000 std::swap(Lo, Hi);
6001 break;
6002 }
6003
Chris Lattner3e928bb2005-01-07 07:47:09 +00006004 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006005 LoadSDNode *LD = cast<LoadSDNode>(Node);
6006 SDOperand Ch = LD->getChain(); // Legalize the chain.
6007 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
6008 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006009 int SVOffset = LD->getSrcValueOffset();
6010 unsigned Alignment = LD->getAlignment();
6011 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006012
Evan Cheng466685d2006-10-09 20:57:25 +00006013 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006014 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
6015 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006016 if (VT == MVT::f32 || VT == MVT::f64) {
6017 // f32->i32 or f64->i64 one to one expansion.
6018 // Remember that we legalized the chain.
6019 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006020 // Recursively expand the new load.
6021 if (getTypeAction(NVT) == Expand)
6022 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006023 break;
6024 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006025
Evan Cheng466685d2006-10-09 20:57:25 +00006026 // Increment the pointer to the other half.
6027 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
6028 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006029 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006030 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006031 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006032 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
6033 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006034
Evan Cheng466685d2006-10-09 20:57:25 +00006035 // Build a factor node to remember that this load is independent of the
6036 // other one.
6037 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6038 Hi.getValue(1));
6039
6040 // Remember that we legalized the chain.
6041 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006042 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006043 std::swap(Lo, Hi);
6044 } else {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006045 MVT::ValueType EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006046
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006047 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6048 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006049 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
6050 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006051 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006052 // Remember that we legalized the chain.
6053 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
6054 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6055 break;
6056 }
Evan Cheng466685d2006-10-09 20:57:25 +00006057
6058 if (EVT == NVT)
6059 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006060 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006061 else
6062 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006063 SVOffset, EVT, isVolatile,
6064 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006065
6066 // Remember that we legalized the chain.
6067 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
6068
6069 if (ExtType == ISD::SEXTLOAD) {
6070 // The high part is obtained by SRA'ing all but one of the bits of the
6071 // lo part.
6072 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
6073 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6074 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6075 } else if (ExtType == ISD::ZEXTLOAD) {
6076 // The high part is just a zero.
6077 Hi = DAG.getConstant(0, NVT);
6078 } else /* if (ExtType == ISD::EXTLOAD) */ {
6079 // The high part is undefined.
6080 Hi = DAG.getNode(ISD::UNDEF, NVT);
6081 }
6082 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006083 break;
6084 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006085 case ISD::AND:
6086 case ISD::OR:
6087 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
6088 SDOperand LL, LH, RL, RH;
6089 ExpandOp(Node->getOperand(0), LL, LH);
6090 ExpandOp(Node->getOperand(1), RL, RH);
6091 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6092 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6093 break;
6094 }
6095 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00006096 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006097 ExpandOp(Node->getOperand(1), LL, LH);
6098 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006099 if (getTypeAction(NVT) == Expand)
6100 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00006101 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006102 if (VT != MVT::f32)
6103 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006104 break;
6105 }
Nate Begeman9373a812005-08-10 20:51:12 +00006106 case ISD::SELECT_CC: {
6107 SDOperand TL, TH, FL, FH;
6108 ExpandOp(Node->getOperand(2), TL, TH);
6109 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006110 if (getTypeAction(NVT) == Expand)
6111 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00006112 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6113 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006114 if (VT != MVT::f32)
6115 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6116 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006117 break;
6118 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006119 case ISD::ANY_EXTEND:
6120 // The low part is any extension of the input (which degenerates to a copy).
6121 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6122 // The high part is undefined.
6123 Hi = DAG.getNode(ISD::UNDEF, NVT);
6124 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006125 case ISD::SIGN_EXTEND: {
6126 // The low part is just a sign extension of the input (which degenerates to
6127 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006128 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006129
Chris Lattner3e928bb2005-01-07 07:47:09 +00006130 // The high part is obtained by SRA'ing all but one of the bits of the lo
6131 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00006132 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00006133 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6134 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006135 break;
6136 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006137 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006138 // The low part is just a zero extension of the input (which degenerates to
6139 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006140 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006141
Chris Lattner3e928bb2005-01-07 07:47:09 +00006142 // The high part is just a zero.
6143 Hi = DAG.getConstant(0, NVT);
6144 break;
Chris Lattner35481892005-12-23 00:16:34 +00006145
Chris Lattner4c948eb2007-02-13 23:55:16 +00006146 case ISD::TRUNCATE: {
6147 // The input value must be larger than this value. Expand *it*.
6148 SDOperand NewLo;
6149 ExpandOp(Node->getOperand(0), NewLo, Hi);
6150
6151 // The low part is now either the right size, or it is closer. If not the
6152 // right size, make an illegal truncate so we recursively expand it.
6153 if (NewLo.getValueType() != Node->getValueType(0))
6154 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6155 ExpandOp(NewLo, Lo, Hi);
6156 break;
6157 }
6158
Chris Lattner35481892005-12-23 00:16:34 +00006159 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006160 SDOperand Tmp;
6161 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6162 // If the target wants to, allow it to lower this itself.
6163 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6164 case Expand: assert(0 && "cannot expand FP!");
6165 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6166 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6167 }
6168 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6169 }
6170
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006171 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006172 if (VT == MVT::f32 || VT == MVT::f64) {
6173 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006174 if (getTypeAction(NVT) == Expand)
6175 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006176 break;
6177 }
6178
6179 // If source operand will be expanded to the same type as VT, i.e.
6180 // i64 <- f64, i32 <- f32, expand the source operand instead.
6181 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
6182 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6183 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006184 break;
6185 }
6186
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006187 // Turn this into a load/store pair by default.
6188 if (Tmp.Val == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006189 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006190
Chris Lattner35481892005-12-23 00:16:34 +00006191 ExpandOp(Tmp, Lo, Hi);
6192 break;
6193 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006194
Chris Lattner27a6c732007-11-24 07:07:01 +00006195 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006196 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6197 TargetLowering::Custom &&
6198 "Must custom expand ReadCycleCounter");
Chris Lattner27a6c732007-11-24 07:07:01 +00006199 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6200 assert(Tmp.Val && "Node must be custom expanded!");
6201 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner308575b2005-11-20 22:56:56 +00006202 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006203 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006204 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006205 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006206
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006207 case ISD::ATOMIC_LCS: {
6208 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6209 assert(Tmp.Val && "Node must be custom expanded!");
6210 ExpandOp(Tmp.getValue(0), Lo, Hi);
6211 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
6212 LegalizeOp(Tmp.getValue(1)));
6213 break;
6214 }
6215
6216
6217
Chris Lattner4e6c7462005-01-08 19:27:05 +00006218 // These operators cannot be expanded directly, emit them as calls to
6219 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006220 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006221 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00006222 SDOperand Op;
6223 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6224 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006225 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6226 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006227 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006228
Chris Lattnerf20d1832005-07-30 01:40:57 +00006229 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6230
Chris Lattner80a3e942005-07-29 00:33:32 +00006231 // Now that the custom expander is done, expand the result, which is still
6232 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00006233 if (Op.Val) {
6234 ExpandOp(Op, Lo, Hi);
6235 break;
6236 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006237 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006238
Dale Johannesen161e8972007-10-05 20:04:43 +00006239 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmana2e94852008-03-10 23:03:31 +00006240 if (VT == MVT::i64) {
6241 if (Node->getOperand(0).getValueType() == MVT::f32)
6242 LC = RTLIB::FPTOSINT_F32_I64;
6243 else if (Node->getOperand(0).getValueType() == MVT::f64)
6244 LC = RTLIB::FPTOSINT_F64_I64;
6245 else if (Node->getOperand(0).getValueType() == MVT::f80)
6246 LC = RTLIB::FPTOSINT_F80_I64;
6247 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6248 LC = RTLIB::FPTOSINT_PPCF128_I64;
6249 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6250 false/*sign irrelevant*/, Hi);
6251 } else if (VT == MVT::i128) {
6252 if (Node->getOperand(0).getValueType() == MVT::f32)
6253 LC = RTLIB::FPTOSINT_F32_I128;
6254 else if (Node->getOperand(0).getValueType() == MVT::f64)
6255 LC = RTLIB::FPTOSINT_F64_I128;
6256 else if (Node->getOperand(0).getValueType() == MVT::f80)
6257 LC = RTLIB::FPTOSINT_F80_I128;
6258 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6259 LC = RTLIB::FPTOSINT_PPCF128_I128;
6260 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6261 false/*sign irrelevant*/, Hi);
6262 } else {
6263 assert(0 && "Unexpected uint-to-fp conversion!");
6264 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006265 break;
Evan Cheng56966222007-01-12 02:11:51 +00006266 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006267
Evan Cheng56966222007-01-12 02:11:51 +00006268 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006269 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006270 SDOperand Op;
6271 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6272 case Expand: assert(0 && "cannot expand FP!");
6273 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6274 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6275 }
6276
6277 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6278
6279 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00006280 if (Op.Val) {
6281 ExpandOp(Op, Lo, Hi);
6282 break;
6283 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006284 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006285
Evan Chengdaccea182007-10-05 01:09:32 +00006286 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Dan Gohmana2e94852008-03-10 23:03:31 +00006287 if (VT == MVT::i64) {
6288 if (Node->getOperand(0).getValueType() == MVT::f32)
6289 LC = RTLIB::FPTOUINT_F32_I64;
6290 else if (Node->getOperand(0).getValueType() == MVT::f64)
6291 LC = RTLIB::FPTOUINT_F64_I64;
6292 else if (Node->getOperand(0).getValueType() == MVT::f80)
6293 LC = RTLIB::FPTOUINT_F80_I64;
6294 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6295 LC = RTLIB::FPTOUINT_PPCF128_I64;
6296 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6297 false/*sign irrelevant*/, Hi);
6298 } else if (VT == MVT::i128) {
6299 if (Node->getOperand(0).getValueType() == MVT::f32)
6300 LC = RTLIB::FPTOUINT_F32_I128;
6301 else if (Node->getOperand(0).getValueType() == MVT::f64)
6302 LC = RTLIB::FPTOUINT_F64_I128;
6303 else if (Node->getOperand(0).getValueType() == MVT::f80)
6304 LC = RTLIB::FPTOUINT_F80_I128;
6305 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6306 LC = RTLIB::FPTOUINT_PPCF128_I128;
6307 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6308 false/*sign irrelevant*/, Hi);
6309 } else {
6310 assert(0 && "Unexpected uint-to-fp conversion!");
6311 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006312 break;
Evan Cheng56966222007-01-12 02:11:51 +00006313 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006314
Evan Cheng05a2d562006-01-09 18:31:59 +00006315 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006316 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006317 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006318 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006319 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006320 Op = TLI.LowerOperation(Op, DAG);
6321 if (Op.Val) {
6322 // Now that the custom expander is done, expand the result, which is
6323 // still VT.
6324 ExpandOp(Op, Lo, Hi);
6325 break;
6326 }
6327 }
6328
Chris Lattner79980b02006-09-13 03:50:39 +00006329 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6330 // this X << 1 as X+X.
6331 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00006332 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner79980b02006-09-13 03:50:39 +00006333 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6334 SDOperand LoOps[2], HiOps[3];
6335 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6336 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6337 LoOps[1] = LoOps[0];
6338 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6339
6340 HiOps[1] = HiOps[0];
6341 HiOps[2] = Lo.getValue(1);
6342 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6343 break;
6344 }
6345 }
6346
Chris Lattnere34b3962005-01-19 04:19:40 +00006347 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006348 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006349 break;
Chris Lattner47599822005-04-02 03:38:53 +00006350
6351 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006352 TargetLowering::LegalizeAction Action =
6353 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6354 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6355 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006356 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006357 break;
6358 }
6359
Chris Lattnere34b3962005-01-19 04:19:40 +00006360 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006361 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
6362 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006363 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006364 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006365
Evan Cheng05a2d562006-01-09 18:31:59 +00006366 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006367 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006368 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006369 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006370 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006371 Op = TLI.LowerOperation(Op, DAG);
6372 if (Op.Val) {
6373 // Now that the custom expander is done, expand the result, which is
6374 // still VT.
6375 ExpandOp(Op, Lo, Hi);
6376 break;
6377 }
6378 }
6379
Chris Lattnere34b3962005-01-19 04:19:40 +00006380 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006381 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006382 break;
Chris Lattner47599822005-04-02 03:38:53 +00006383
6384 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006385 TargetLowering::LegalizeAction Action =
6386 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6387 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6388 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006389 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006390 break;
6391 }
6392
Chris Lattnere34b3962005-01-19 04:19:40 +00006393 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006394 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
6395 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006396 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006397 }
6398
6399 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006400 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00006401 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006402 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00006403 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006404 Op = TLI.LowerOperation(Op, DAG);
6405 if (Op.Val) {
6406 // Now that the custom expander is done, expand the result, which is
6407 // still VT.
6408 ExpandOp(Op, Lo, Hi);
6409 break;
6410 }
6411 }
6412
Chris Lattnere34b3962005-01-19 04:19:40 +00006413 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006414 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006415 break;
Chris Lattner47599822005-04-02 03:38:53 +00006416
6417 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006418 TargetLowering::LegalizeAction Action =
6419 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6420 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6421 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006422 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006423 break;
6424 }
6425
Chris Lattnere34b3962005-01-19 04:19:40 +00006426 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006427 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
6428 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006429 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006430 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006431
Misha Brukmanedf128a2005-04-21 22:36:52 +00006432 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006433 case ISD::SUB: {
6434 // If the target wants to custom expand this, let them.
6435 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6436 TargetLowering::Custom) {
6437 Op = TLI.LowerOperation(Op, DAG);
6438 if (Op.Val) {
6439 ExpandOp(Op, Lo, Hi);
6440 break;
6441 }
6442 }
6443
6444 // Expand the subcomponents.
6445 SDOperand LHSL, LHSH, RHSL, RHSH;
6446 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6447 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00006448 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6449 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006450 LoOps[0] = LHSL;
6451 LoOps[1] = RHSL;
6452 HiOps[0] = LHSH;
6453 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006454 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00006455 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006456 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006457 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006458 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00006459 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006460 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00006461 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00006462 }
Chris Lattner84f67882005-01-20 18:52:28 +00006463 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006464 }
Chris Lattnerb429f732007-05-17 18:15:41 +00006465
6466 case ISD::ADDC:
6467 case ISD::SUBC: {
6468 // Expand the subcomponents.
6469 SDOperand LHSL, LHSH, RHSL, RHSH;
6470 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6471 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6472 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6473 SDOperand LoOps[2] = { LHSL, RHSL };
6474 SDOperand HiOps[3] = { LHSH, RHSH };
6475
6476 if (Node->getOpcode() == ISD::ADDC) {
6477 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6478 HiOps[2] = Lo.getValue(1);
6479 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6480 } else {
6481 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6482 HiOps[2] = Lo.getValue(1);
6483 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6484 }
6485 // Remember that we legalized the flag.
6486 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6487 break;
6488 }
6489 case ISD::ADDE:
6490 case ISD::SUBE: {
6491 // Expand the subcomponents.
6492 SDOperand LHSL, LHSH, RHSL, RHSH;
6493 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6494 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6495 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6496 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6497 SDOperand HiOps[3] = { LHSH, RHSH };
6498
6499 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6500 HiOps[2] = Lo.getValue(1);
6501 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6502
6503 // Remember that we legalized the flag.
6504 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6505 break;
6506 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006507 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006508 // If the target wants to custom expand this, let them.
6509 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006510 SDOperand New = TLI.LowerOperation(Op, DAG);
6511 if (New.Val) {
6512 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006513 break;
6514 }
6515 }
6516
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006517 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6518 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006519 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6520 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6521 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanc7c16572005-04-11 03:01:51 +00006522 SDOperand LL, LH, RL, RH;
6523 ExpandOp(Node->getOperand(0), LL, LH);
6524 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006525 unsigned OuterBitSize = Op.getValueSizeInBits();
6526 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00006527 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6528 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00006529 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6530 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6531 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00006532 // The inputs are both zero-extended.
6533 if (HasUMUL_LOHI) {
6534 // We can emit a umul_lohi.
6535 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6536 Hi = SDOperand(Lo.Val, 1);
6537 break;
6538 }
6539 if (HasMULHU) {
6540 // We can emit a mulhu+mul.
6541 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6542 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6543 break;
6544 }
Dan Gohman525178c2007-10-08 18:33:35 +00006545 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006546 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00006547 // The input values are both sign-extended.
6548 if (HasSMUL_LOHI) {
6549 // We can emit a smul_lohi.
6550 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6551 Hi = SDOperand(Lo.Val, 1);
6552 break;
6553 }
6554 if (HasMULHS) {
6555 // We can emit a mulhs+mul.
6556 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6557 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6558 break;
6559 }
6560 }
6561 if (HasUMUL_LOHI) {
6562 // Lo,Hi = umul LHS, RHS.
6563 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6564 DAG.getVTList(NVT, NVT), LL, RL);
6565 Lo = UMulLOHI;
6566 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006567 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6568 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6569 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6570 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006571 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006572 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006573 if (HasMULHU) {
6574 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6575 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6576 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6577 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6578 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6579 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6580 break;
6581 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006582 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006583
Dan Gohman525178c2007-10-08 18:33:35 +00006584 // If nothing else, we can make a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00006585 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6586 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006587 break;
6588 }
Evan Cheng56966222007-01-12 02:11:51 +00006589 case ISD::SDIV:
6590 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6591 break;
6592 case ISD::UDIV:
6593 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6594 break;
6595 case ISD::SREM:
6596 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6597 break;
6598 case ISD::UREM:
6599 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6600 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006601
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006602 case ISD::FADD:
Duncan Sands007f9842008-01-10 10:28:30 +00006603 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6604 RTLIB::ADD_F64,
6605 RTLIB::ADD_F80,
6606 RTLIB::ADD_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006607 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006608 break;
6609 case ISD::FSUB:
Duncan Sands007f9842008-01-10 10:28:30 +00006610 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6611 RTLIB::SUB_F64,
6612 RTLIB::SUB_F80,
6613 RTLIB::SUB_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006614 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006615 break;
6616 case ISD::FMUL:
Duncan Sands007f9842008-01-10 10:28:30 +00006617 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6618 RTLIB::MUL_F64,
6619 RTLIB::MUL_F80,
6620 RTLIB::MUL_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006621 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006622 break;
6623 case ISD::FDIV:
Duncan Sands007f9842008-01-10 10:28:30 +00006624 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6625 RTLIB::DIV_F64,
6626 RTLIB::DIV_F80,
6627 RTLIB::DIV_PPCF128)),
Evan Cheng56966222007-01-12 02:11:51 +00006628 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006629 break;
6630 case ISD::FP_EXTEND:
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006631 if (VT == MVT::ppcf128) {
6632 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6633 Node->getOperand(0).getValueType()==MVT::f64);
6634 const uint64_t zero = 0;
6635 if (Node->getOperand(0).getValueType()==MVT::f32)
6636 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6637 else
6638 Hi = Node->getOperand(0);
6639 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6640 break;
6641 }
Evan Cheng56966222007-01-12 02:11:51 +00006642 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006643 break;
6644 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00006645 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006646 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006647 case ISD::FPOWI:
Duncan Sands007f9842008-01-10 10:28:30 +00006648 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6649 RTLIB::POWI_F64,
6650 RTLIB::POWI_F80,
6651 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00006652 Node, false, Hi);
6653 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006654 case ISD::FSQRT:
6655 case ISD::FSIN:
6656 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00006657 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006658 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006659 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00006660 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6661 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006662 break;
6663 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00006664 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6665 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006666 break;
6667 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00006668 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6669 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006670 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006671 default: assert(0 && "Unreachable!");
6672 }
Evan Cheng56966222007-01-12 02:11:51 +00006673 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006674 break;
6675 }
Evan Cheng966bf242006-12-16 00:52:40 +00006676 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006677 if (VT == MVT::ppcf128) {
6678 SDOperand Tmp;
6679 ExpandOp(Node->getOperand(0), Lo, Tmp);
6680 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6681 // lo = hi==fabs(hi) ? lo : -lo;
6682 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6683 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6684 DAG.getCondCode(ISD::SETEQ));
6685 break;
6686 }
Evan Cheng966bf242006-12-16 00:52:40 +00006687 SDOperand Mask = (VT == MVT::f64)
6688 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6689 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6690 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6691 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6692 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6693 if (getTypeAction(NVT) == Expand)
6694 ExpandOp(Lo, Lo, Hi);
6695 break;
6696 }
6697 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006698 if (VT == MVT::ppcf128) {
6699 ExpandOp(Node->getOperand(0), Lo, Hi);
6700 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6701 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6702 break;
6703 }
Evan Cheng966bf242006-12-16 00:52:40 +00006704 SDOperand Mask = (VT == MVT::f64)
6705 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6706 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6707 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6708 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6709 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6710 if (getTypeAction(NVT) == Expand)
6711 ExpandOp(Lo, Lo, Hi);
6712 break;
6713 }
Evan Cheng912095b2007-01-04 21:56:39 +00006714 case ISD::FCOPYSIGN: {
6715 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6716 if (getTypeAction(NVT) == Expand)
6717 ExpandOp(Lo, Lo, Hi);
6718 break;
6719 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006720 case ISD::SINT_TO_FP:
6721 case ISD::UINT_TO_FP: {
6722 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6723 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00006724
6725 // Promote the operand if needed. Do this before checking for
6726 // ppcf128 so conversions of i16 and i8 work.
6727 if (getTypeAction(SrcVT) == Promote) {
6728 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6729 Tmp = isSigned
6730 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6731 DAG.getValueType(SrcVT))
6732 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6733 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6734 SrcVT = Node->getOperand(0).getValueType();
6735 }
6736
Dan Gohmana2e94852008-03-10 23:03:31 +00006737 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006738 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006739 if (isSigned) {
6740 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6741 Node->getOperand(0)));
6742 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6743 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006744 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006745 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6746 Node->getOperand(0)));
6747 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6748 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00006749 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006750 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6751 DAG.getConstant(0, MVT::i32),
6752 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6753 DAG.getConstantFP(
6754 APFloat(APInt(128, 2, TwoE32)),
6755 MVT::ppcf128)),
6756 Hi,
6757 DAG.getCondCode(ISD::SETLT)),
6758 Lo, Hi);
6759 }
6760 break;
6761 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00006762 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6763 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00006764 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00006765 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6766 Lo, Hi);
6767 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6768 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6769 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6770 DAG.getConstant(0, MVT::i64),
6771 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6772 DAG.getConstantFP(
6773 APFloat(APInt(128, 2, TwoE64)),
6774 MVT::ppcf128)),
6775 Hi,
6776 DAG.getCondCode(ISD::SETLT)),
6777 Lo, Hi);
6778 break;
6779 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00006780
Dan Gohmana2e94852008-03-10 23:03:31 +00006781 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6782 Node->getOperand(0));
Evan Cheng110cf482008-04-01 01:50:16 +00006783 if (getTypeAction(Lo.getValueType()) == Expand)
6784 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00006785 break;
6786 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00006787 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006788
Chris Lattner83397362005-12-21 18:02:52 +00006789 // Make sure the resultant values have been legalized themselves, unless this
6790 // is a type that requires multi-step expansion.
6791 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6792 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00006793 if (Hi.Val)
6794 // Don't legalize the high part if it is expanded to a single node.
6795 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00006796 }
Evan Cheng05a2d562006-01-09 18:31:59 +00006797
6798 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00006799 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00006800 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006801}
6802
Dan Gohman7f321562007-06-25 16:23:39 +00006803/// SplitVectorOp - Given an operand of vector type, break it down into
6804/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00006805void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6806 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00006807 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00006808 SDNode *Node = Op.Val;
Dan Gohmane40c7b02007-09-24 15:54:53 +00006809 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattnerc7029802006-03-18 01:44:44 +00006810 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00006811
Dan Gohmane40c7b02007-09-24 15:54:53 +00006812 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006813
6814 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6815 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6816
6817 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6818 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6819
Chris Lattnerc7029802006-03-18 01:44:44 +00006820 // See if we already split it.
6821 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6822 = SplitNodes.find(Op);
6823 if (I != SplitNodes.end()) {
6824 Lo = I->second.first;
6825 Hi = I->second.second;
6826 return;
6827 }
6828
6829 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006830 default:
6831#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006832 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006833#endif
6834 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00006835 case ISD::UNDEF:
6836 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6837 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6838 break;
Dan Gohman7f321562007-06-25 16:23:39 +00006839 case ISD::BUILD_PAIR:
6840 Lo = Node->getOperand(0);
6841 Hi = Node->getOperand(1);
6842 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00006843 case ISD::INSERT_VECTOR_ELT: {
6844 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6845 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6846 SDOperand ScalarOp = Node->getOperand(1);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006847 if (Index < NewNumElts_Lo)
6848 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Nate Begeman5922f562008-03-14 00:53:31 +00006849 DAG.getIntPtrConstant(Index));
Dan Gohman9fe46622007-09-28 23:53:40 +00006850 else
Nate Begeman5db1afb2007-11-15 21:15:26 +00006851 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
Nate Begeman5922f562008-03-14 00:53:31 +00006852 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
Dan Gohman9fe46622007-09-28 23:53:40 +00006853 break;
6854 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00006855 case ISD::VECTOR_SHUFFLE: {
6856 // Build the low part.
6857 SDOperand Mask = Node->getOperand(2);
6858 SmallVector<SDOperand, 8> Ops;
6859 MVT::ValueType PtrVT = TLI.getPointerTy();
6860
6861 // Insert all of the elements from the input that are needed. We use
6862 // buildvector of extractelement here because the input vectors will have
6863 // to be legalized, so this makes the code simpler.
6864 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Nate Begeman5922f562008-03-14 00:53:31 +00006865 SDOperand IdxNode = Mask.getOperand(i);
6866 if (IdxNode.getOpcode() == ISD::UNDEF) {
6867 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6868 continue;
6869 }
6870 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Chris Lattner6c9c6802007-11-19 21:16:54 +00006871 SDOperand InVec = Node->getOperand(0);
6872 if (Idx >= NumElements) {
6873 InVec = Node->getOperand(1);
6874 Idx -= NumElements;
6875 }
6876 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6877 DAG.getConstant(Idx, PtrVT)));
6878 }
6879 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6880 Ops.clear();
6881
6882 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Nate Begeman5922f562008-03-14 00:53:31 +00006883 SDOperand IdxNode = Mask.getOperand(i);
6884 if (IdxNode.getOpcode() == ISD::UNDEF) {
6885 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
6886 continue;
6887 }
6888 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue();
Chris Lattner6c9c6802007-11-19 21:16:54 +00006889 SDOperand InVec = Node->getOperand(0);
6890 if (Idx >= NumElements) {
6891 InVec = Node->getOperand(1);
6892 Idx -= NumElements;
6893 }
6894 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6895 DAG.getConstant(Idx, PtrVT)));
6896 }
6897 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6898 break;
6899 }
Dan Gohman7f321562007-06-25 16:23:39 +00006900 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006901 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00006902 Node->op_begin()+NewNumElts_Lo);
6903 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006904
Nate Begeman5db1afb2007-11-15 21:15:26 +00006905 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00006906 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006907 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00006908 break;
6909 }
Dan Gohman7f321562007-06-25 16:23:39 +00006910 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00006911 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00006912 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6913 if (NewNumSubvectors == 1) {
6914 Lo = Node->getOperand(0);
6915 Hi = Node->getOperand(1);
6916 } else {
6917 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6918 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006919 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00006920
Dan Gohman7f321562007-06-25 16:23:39 +00006921 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6922 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00006923 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00006924 }
Dan Gohman65956352007-06-13 15:12:02 +00006925 break;
6926 }
Dan Gohmanc6230962007-10-17 14:48:28 +00006927 case ISD::SELECT: {
6928 SDOperand Cond = Node->getOperand(0);
6929
6930 SDOperand LL, LH, RL, RH;
6931 SplitVectorOp(Node->getOperand(1), LL, LH);
6932 SplitVectorOp(Node->getOperand(2), RL, RH);
6933
6934 if (MVT::isVector(Cond.getValueType())) {
6935 // Handle a vector merge.
6936 SDOperand CL, CH;
6937 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00006938 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6939 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006940 } else {
6941 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00006942 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6943 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00006944 }
6945 break;
6946 }
Dan Gohman7f321562007-06-25 16:23:39 +00006947 case ISD::ADD:
6948 case ISD::SUB:
6949 case ISD::MUL:
6950 case ISD::FADD:
6951 case ISD::FSUB:
6952 case ISD::FMUL:
6953 case ISD::SDIV:
6954 case ISD::UDIV:
6955 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00006956 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00006957 case ISD::AND:
6958 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00006959 case ISD::XOR:
6960 case ISD::UREM:
6961 case ISD::SREM:
6962 case ISD::FREM: {
Chris Lattnerc7029802006-03-18 01:44:44 +00006963 SDOperand LL, LH, RL, RH;
6964 SplitVectorOp(Node->getOperand(0), LL, LH);
6965 SplitVectorOp(Node->getOperand(1), RL, RH);
6966
Nate Begeman5db1afb2007-11-15 21:15:26 +00006967 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6968 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00006969 break;
6970 }
Dan Gohman82669522007-10-11 23:57:53 +00006971 case ISD::FPOWI: {
6972 SDOperand L, H;
6973 SplitVectorOp(Node->getOperand(0), L, H);
6974
Nate Begeman5db1afb2007-11-15 21:15:26 +00006975 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6976 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00006977 break;
6978 }
6979 case ISD::CTTZ:
6980 case ISD::CTLZ:
6981 case ISD::CTPOP:
6982 case ISD::FNEG:
6983 case ISD::FABS:
6984 case ISD::FSQRT:
6985 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00006986 case ISD::FCOS:
6987 case ISD::FP_TO_SINT:
6988 case ISD::FP_TO_UINT:
6989 case ISD::SINT_TO_FP:
6990 case ISD::UINT_TO_FP: {
Dan Gohman82669522007-10-11 23:57:53 +00006991 SDOperand L, H;
6992 SplitVectorOp(Node->getOperand(0), L, H);
6993
Nate Begeman5db1afb2007-11-15 21:15:26 +00006994 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6995 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00006996 break;
6997 }
Dan Gohman7f321562007-06-25 16:23:39 +00006998 case ISD::LOAD: {
6999 LoadSDNode *LD = cast<LoadSDNode>(Node);
7000 SDOperand Ch = LD->getChain();
7001 SDOperand Ptr = LD->getBasePtr();
7002 const Value *SV = LD->getSrcValue();
7003 int SVOffset = LD->getSrcValueOffset();
7004 unsigned Alignment = LD->getAlignment();
7005 bool isVolatile = LD->isVolatile();
7006
Nate Begeman5db1afb2007-11-15 21:15:26 +00007007 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
7008 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00007009 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007010 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007011 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007012 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007013 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00007014
7015 // Build a factor node to remember that this load is independent of the
7016 // other one.
7017 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
7018 Hi.getValue(1));
7019
7020 // Remember that we legalized the chain.
7021 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007022 break;
7023 }
Dan Gohman7f321562007-06-25 16:23:39 +00007024 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007025 // We know the result is a vector. The input may be either a vector or a
7026 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00007027 SDOperand InOp = Node->getOperand(0);
7028 if (!MVT::isVector(InOp.getValueType()) ||
7029 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
7030 // The input is a scalar or single-element vector.
7031 // Lower to a store/load so that it can be split.
7032 // FIXME: this could be improved probably.
Chris Lattner85dd3be2007-10-15 17:48:57 +00007033 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00007034 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val);
Chris Lattner7692eb42006-03-23 21:16:34 +00007035
Evan Cheng786225a2006-10-05 23:01:46 +00007036 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00007037 InOp, Ptr,
Dan Gohman3069b872008-02-07 18:41:25 +00007038 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00007039 FI->getIndex());
7040 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman3069b872008-02-07 18:41:25 +00007041 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00007042 FI->getIndex());
Chris Lattner7692eb42006-03-23 21:16:34 +00007043 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007044 // Split the vector and convert each of the pieces now.
7045 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007046 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7047 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007048 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007049 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007050 }
7051
7052 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00007053 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007054 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007055 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00007056}
7057
7058
Dan Gohman89b20c02007-06-27 14:06:22 +00007059/// ScalarizeVectorOp - Given an operand of single-element vector type
7060/// (e.g. v1f32), convert it into the equivalent operation that returns a
7061/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00007062SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
7063 assert(MVT::isVector(Op.getValueType()) &&
7064 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00007065 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00007066 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
7067 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00007068
Dan Gohman7f321562007-06-25 16:23:39 +00007069 // See if we already scalarized it.
7070 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
7071 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00007072
7073 SDOperand Result;
7074 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00007075 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007076#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007077 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007078#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007079 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7080 case ISD::ADD:
7081 case ISD::FADD:
7082 case ISD::SUB:
7083 case ISD::FSUB:
7084 case ISD::MUL:
7085 case ISD::FMUL:
7086 case ISD::SDIV:
7087 case ISD::UDIV:
7088 case ISD::FDIV:
7089 case ISD::SREM:
7090 case ISD::UREM:
7091 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007092 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007093 case ISD::AND:
7094 case ISD::OR:
7095 case ISD::XOR:
7096 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00007097 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007098 ScalarizeVectorOp(Node->getOperand(0)),
7099 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007100 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007101 case ISD::FNEG:
7102 case ISD::FABS:
7103 case ISD::FSQRT:
7104 case ISD::FSIN:
7105 case ISD::FCOS:
7106 Result = DAG.getNode(Node->getOpcode(),
7107 NewVT,
7108 ScalarizeVectorOp(Node->getOperand(0)));
7109 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00007110 case ISD::FPOWI:
7111 Result = DAG.getNode(Node->getOpcode(),
7112 NewVT,
7113 ScalarizeVectorOp(Node->getOperand(0)),
7114 Node->getOperand(1));
7115 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007116 case ISD::LOAD: {
7117 LoadSDNode *LD = cast<LoadSDNode>(Node);
7118 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7119 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00007120
Dan Gohman7f321562007-06-25 16:23:39 +00007121 const Value *SV = LD->getSrcValue();
7122 int SVOffset = LD->getSrcValueOffset();
7123 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
7124 LD->isVolatile(), LD->getAlignment());
7125
Chris Lattnerc7029802006-03-18 01:44:44 +00007126 // Remember that we legalized the chain.
7127 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7128 break;
7129 }
Dan Gohman7f321562007-06-25 16:23:39 +00007130 case ISD::BUILD_VECTOR:
7131 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007132 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007133 case ISD::INSERT_VECTOR_ELT:
7134 // Returning the inserted scalar element.
7135 Result = Node->getOperand(1);
7136 break;
7137 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007138 assert(Node->getOperand(0).getValueType() == NewVT &&
7139 "Concat of non-legal vectors not yet supported!");
7140 Result = Node->getOperand(0);
7141 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007142 case ISD::VECTOR_SHUFFLE: {
7143 // Figure out if the scalar is the LHS or RHS and return it.
7144 SDOperand EltNum = Node->getOperand(2).getOperand(0);
7145 if (cast<ConstantSDNode>(EltNum)->getValue())
7146 Result = ScalarizeVectorOp(Node->getOperand(1));
7147 else
7148 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007149 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007150 }
7151 case ISD::EXTRACT_SUBVECTOR:
7152 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00007153 assert(Result.getValueType() == NewVT);
7154 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007155 case ISD::BIT_CONVERT:
7156 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00007157 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007158 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007159 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007160 ScalarizeVectorOp(Op.getOperand(1)),
7161 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007162 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00007163 }
7164
7165 if (TLI.isTypeLegal(NewVT))
7166 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007167 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7168 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00007169 return Result;
7170}
7171
Chris Lattner3e928bb2005-01-07 07:47:09 +00007172
7173// SelectionDAG::Legalize - This is the entry point for the file.
7174//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00007175void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00007176 if (ViewLegalizeDAGs) viewGraph();
7177
Chris Lattner3e928bb2005-01-07 07:47:09 +00007178 /// run - This is the main entry point to this class.
7179 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00007180 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00007181}
7182