blob: e370e4a89719c07535d178b886942776a0b0ee14 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Evan Cheng61bbbab2007-08-16 23:50:06 +000018#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000019#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000020#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000022#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000023#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000024#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000025#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000026#include "llvm/Support/MathExtras.h"
27#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattner79715142007-02-03 01:12:36 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000030#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000031#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000032#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000033using namespace llvm;
34
Chris Lattner5e46a192006-04-02 03:07:27 +000035#ifndef NDEBUG
36static cl::opt<bool>
37ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
38 cl::desc("Pop up a window to show dags before legalize"));
39#else
40static const bool ViewLegalizeDAGs = 0;
41#endif
42
Chris Lattner3e928bb2005-01-07 07:47:09 +000043//===----------------------------------------------------------------------===//
44/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
45/// hacks on it until the target machine can handle it. This involves
46/// eliminating value sizes the machine cannot handle (promoting small sizes to
47/// large sizes or splitting up large values into small values) as well as
48/// eliminating operations the machine cannot handle.
49///
50/// This code also does a small amount of optimization and recognition of idioms
51/// as part of its processing. For example, if a target does not support a
52/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
53/// will attempt merge setcc and brc instructions into brcc's.
54///
55namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000056class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000057 TargetLowering &TLI;
58 SelectionDAG &DAG;
59
Chris Lattner6831a812006-02-13 09:18:02 +000060 // Libcall insertion helpers.
61
62 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
63 /// legalized. We use this to ensure that calls are properly serialized
64 /// against each other, including inserted libcalls.
65 SDOperand LastCALLSEQ_END;
66
67 /// IsLegalizingCall - This member is used *only* for purposes of providing
68 /// helpful assertions that a libcall isn't created while another call is
69 /// being legalized (which could lead to non-serialized call sequences).
70 bool IsLegalizingCall;
71
Chris Lattner3e928bb2005-01-07 07:47:09 +000072 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000073 Legal, // The target natively supports this operation.
74 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000075 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000076 };
Chris Lattner6831a812006-02-13 09:18:02 +000077
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 /// ValueTypeActions - This is a bitvector that contains two bits for each
79 /// value type, where the two bits correspond to the LegalizeAction enum.
80 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000081 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000082
Chris Lattner3e928bb2005-01-07 07:47:09 +000083 /// LegalizedNodes - For nodes that are of legal width, and that have more
84 /// than one use, this map indicates what regularized operand to use. This
85 /// allows us to avoid legalizing the same thing more than once.
Chris Lattner718071c2007-02-04 00:50:02 +000086 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000087
Chris Lattner03c85462005-01-15 05:21:40 +000088 /// PromotedNodes - For nodes that are below legal width, and that have more
89 /// than one use, this map indicates what promoted value to use. This allows
90 /// us to avoid promoting the same thing more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000091 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000092
Chris Lattnerc7029802006-03-18 01:44:44 +000093 /// ExpandedNodes - For nodes that need to be expanded this map indicates
94 /// which which operands are the expanded version of the input. This allows
95 /// us to avoid expanding the same node more than once.
Chris Lattner40030bf2007-02-04 01:17:38 +000096 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000097
Chris Lattnerc7029802006-03-18 01:44:44 +000098 /// SplitNodes - For vector nodes that need to be split, this map indicates
99 /// which which operands are the split version of the input. This allows us
100 /// to avoid splitting the same node more than once.
101 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
102
Dan Gohman7f321562007-06-25 16:23:39 +0000103 /// ScalarizedNodes - For nodes that need to be converted from vector types to
104 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000105 /// processed to the result.
Dan Gohman7f321562007-06-25 16:23:39 +0000106 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000107
Chris Lattner8afc48e2005-01-07 22:28:47 +0000108 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000109 LegalizedNodes.insert(std::make_pair(From, To));
110 // If someone requests legalization of the new node, return itself.
111 if (From != To)
112 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000113 }
Chris Lattner03c85462005-01-15 05:21:40 +0000114 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner40030bf2007-02-04 01:17:38 +0000115 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000116 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000117 // If someone requests legalization of the new node, return itself.
118 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000119 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000120
Chris Lattner3e928bb2005-01-07 07:47:09 +0000121public:
122
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000123 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000124
Chris Lattner3e928bb2005-01-07 07:47:09 +0000125 /// getTypeAction - Return how we should legalize values of this type, either
126 /// it is already legal or we need to expand it into multiple registers of
127 /// smaller integer type, or we need to promote it to a larger type.
128 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000129 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000130 }
131
132 /// isTypeLegal - Return true if this type is legal on this target.
133 ///
134 bool isTypeLegal(MVT::ValueType VT) const {
135 return getTypeAction(VT) == Legal;
136 }
137
Chris Lattner3e928bb2005-01-07 07:47:09 +0000138 void LegalizeDAG();
139
Chris Lattner456a93a2006-01-28 07:39:30 +0000140private:
Dan Gohman7f321562007-06-25 16:23:39 +0000141 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000142 /// appropriate for its type.
143 void HandleOp(SDOperand Op);
144
145 /// LegalizeOp - We know that the specified value has a legal type.
146 /// Recursively ensure that the operands have legal types, then return the
147 /// result.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000148 SDOperand LegalizeOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000149
150 /// PromoteOp - Given an operation that produces a value in an invalid type,
151 /// promote it to compute the value into a larger type. The produced value
152 /// will have the correct bits for the low portion of the register, but no
153 /// guarantee is made about the top bits: it may be zero, sign-extended, or
154 /// garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000155 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000156
Chris Lattnerc7029802006-03-18 01:44:44 +0000157 /// ExpandOp - Expand the specified SDOperand into its two component pieces
158 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
159 /// the LegalizeNodes map is filled in for any results that are not expanded,
160 /// the ExpandedNodes map is filled in for any results that are expanded, and
161 /// the Lo/Hi values are returned. This applies to integer types and Vector
162 /// types.
163 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
164
Dan Gohman7f321562007-06-25 16:23:39 +0000165 /// SplitVectorOp - Given an operand of vector type, break it down into
166 /// two smaller values.
Chris Lattnerc7029802006-03-18 01:44:44 +0000167 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
168
Dan Gohman89b20c02007-06-27 14:06:22 +0000169 /// ScalarizeVectorOp - Given an operand of single-element vector type
170 /// (e.g. v1f32), convert it into the equivalent operation that returns a
171 /// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +0000172 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000173
Chris Lattner4352cc92006-04-04 17:23:26 +0000174 /// isShuffleLegal - Return true if a vector shuffle is legal with the
175 /// specified mask and type. Targets can specify exactly which masks they
176 /// support and the code generator is tasked with not creating illegal masks.
177 ///
178 /// Note that this will also return true for shuffles that are promoted to a
179 /// different type.
180 ///
181 /// If this is a legal shuffle, this method returns the (possibly promoted)
182 /// build_vector Mask. If it's not a legal shuffle, it returns null.
183 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
184
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000185 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000186 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000187
Nate Begeman750ac1b2006-02-01 07:19:44 +0000188 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
189
Chris Lattnerce872152006-03-19 06:31:19 +0000190 SDOperand CreateStackTemporary(MVT::ValueType VT);
191
Reid Spencer47857812006-12-31 05:55:36 +0000192 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattner77e77a62005-01-21 06:05:23 +0000193 SDOperand &Hi);
194 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
195 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000196
Chris Lattner35481892005-12-23 00:16:34 +0000197 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattnerce872152006-03-19 06:31:19 +0000198 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner4352cc92006-04-04 17:23:26 +0000199 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskey6269ed12005-08-17 00:39:29 +0000200 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
201 SDOperand LegalOp,
202 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000203 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
204 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000205 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
206 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000207
Chris Lattner456a93a2006-01-28 07:39:30 +0000208 SDOperand ExpandBSWAP(SDOperand Op);
209 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattnere34b3962005-01-19 04:19:40 +0000210 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
211 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000212 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
213 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000214
Dan Gohman7f321562007-06-25 16:23:39 +0000215 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner4aab2f42006-04-02 05:06:04 +0000216 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner15972212006-03-31 17:55:51 +0000217
Chris Lattner3e928bb2005-01-07 07:47:09 +0000218 SDOperand getIntPtrConstant(uint64_t Val) {
219 return DAG.getConstant(Val, TLI.getPointerTy());
220 }
221};
222}
223
Chris Lattner4352cc92006-04-04 17:23:26 +0000224/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
225/// specified mask and type. Targets can specify exactly which masks they
226/// support and the code generator is tasked with not creating illegal masks.
227///
228/// Note that this will also return true for shuffles that are promoted to a
229/// different type.
230SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
231 SDOperand Mask) const {
232 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
233 default: return 0;
234 case TargetLowering::Legal:
235 case TargetLowering::Custom:
236 break;
237 case TargetLowering::Promote: {
238 // If this is promoted to a different type, convert the shuffle mask and
239 // ask if it is legal in the promoted type!
240 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
241
242 // If we changed # elements, change the shuffle mask.
243 unsigned NumEltsGrowth =
244 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
245 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
246 if (NumEltsGrowth > 1) {
247 // Renumber the elements.
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000248 SmallVector<SDOperand, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000249 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
250 SDOperand InOp = Mask.getOperand(i);
251 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
252 if (InOp.getOpcode() == ISD::UNDEF)
253 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
254 else {
255 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
256 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
257 }
258 }
259 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000260 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000261 }
262 VT = NVT;
263 break;
264 }
265 }
266 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
267}
268
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000269SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
270 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
271 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000272 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000273 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000274}
275
Chris Lattnere10e6f72007-06-18 21:28:10 +0000276/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
277/// contains all of a nodes operands before it contains the node.
278static void ComputeTopDownOrdering(SelectionDAG &DAG,
279 SmallVector<SDNode*, 64> &Order) {
280
281 DenseMap<SDNode*, unsigned> Visited;
282 std::vector<SDNode*> Worklist;
283 Worklist.reserve(128);
Chris Lattner32fca002005-10-06 01:20:27 +0000284
Chris Lattnere10e6f72007-06-18 21:28:10 +0000285 // Compute ordering from all of the leaves in the graphs, those (like the
286 // entry node) that have no operands.
287 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
288 E = DAG.allnodes_end(); I != E; ++I) {
289 if (I->getNumOperands() == 0) {
290 Visited[I] = 0 - 1U;
291 Worklist.push_back(I);
292 }
Chris Lattner32fca002005-10-06 01:20:27 +0000293 }
294
Chris Lattnere10e6f72007-06-18 21:28:10 +0000295 while (!Worklist.empty()) {
296 SDNode *N = Worklist.back();
297 Worklist.pop_back();
298
299 if (++Visited[N] != N->getNumOperands())
300 continue; // Haven't visited all operands yet
301
302 Order.push_back(N);
303
304 // Now that we have N in, add anything that uses it if all of their operands
305 // are now done.
306 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
307 UI != E; ++UI)
308 Worklist.push_back(*UI);
309 }
310
311 assert(Order.size() == Visited.size() &&
312 Order.size() ==
313 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
314 "Error: DAG is cyclic!");
Chris Lattner32fca002005-10-06 01:20:27 +0000315}
316
Chris Lattner1618beb2005-07-29 00:11:56 +0000317
Chris Lattner3e928bb2005-01-07 07:47:09 +0000318void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000319 LastCALLSEQ_END = DAG.getEntryNode();
320 IsLegalizingCall = false;
321
Chris Lattnerab510a72005-10-02 17:49:46 +0000322 // The legalize process is inherently a bottom-up recursive process (users
323 // legalize their uses before themselves). Given infinite stack space, we
324 // could just start legalizing on the root and traverse the whole graph. In
325 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000326 // blocks. To avoid this problem, compute an ordering of the nodes where each
327 // node is only legalized after all of its operands are legalized.
Chris Lattner0ed44172007-02-04 01:20:02 +0000328 SmallVector<SDNode*, 64> Order;
Chris Lattnere10e6f72007-06-18 21:28:10 +0000329 ComputeTopDownOrdering(DAG, Order);
Chris Lattnerab510a72005-10-02 17:49:46 +0000330
Chris Lattnerc7029802006-03-18 01:44:44 +0000331 for (unsigned i = 0, e = Order.size(); i != e; ++i)
332 HandleOp(SDOperand(Order[i], 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000333
334 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000335 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000336 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
337 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000338
339 ExpandedNodes.clear();
340 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000341 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000342 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000343 ScalarizedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000344
345 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000346 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000347}
348
Chris Lattner6831a812006-02-13 09:18:02 +0000349
350/// FindCallEndFromCallStart - Given a chained node that is part of a call
351/// sequence, find the CALLSEQ_END node that terminates the call sequence.
352static SDNode *FindCallEndFromCallStart(SDNode *Node) {
353 if (Node->getOpcode() == ISD::CALLSEQ_END)
354 return Node;
355 if (Node->use_empty())
356 return 0; // No CallSeqEnd
357
358 // The chain is usually at the end.
359 SDOperand TheChain(Node, Node->getNumValues()-1);
360 if (TheChain.getValueType() != MVT::Other) {
361 // Sometimes it's at the beginning.
362 TheChain = SDOperand(Node, 0);
363 if (TheChain.getValueType() != MVT::Other) {
364 // Otherwise, hunt for it.
365 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
366 if (Node->getValueType(i) == MVT::Other) {
367 TheChain = SDOperand(Node, i);
368 break;
369 }
370
371 // Otherwise, we walked into a node without a chain.
372 if (TheChain.getValueType() != MVT::Other)
373 return 0;
374 }
375 }
376
377 for (SDNode::use_iterator UI = Node->use_begin(),
378 E = Node->use_end(); UI != E; ++UI) {
379
380 // Make sure to only follow users of our token chain.
381 SDNode *User = *UI;
382 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
383 if (User->getOperand(i) == TheChain)
384 if (SDNode *Result = FindCallEndFromCallStart(User))
385 return Result;
386 }
387 return 0;
388}
389
390/// FindCallStartFromCallEnd - Given a chained node that is part of a call
391/// sequence, find the CALLSEQ_START node that initiates the call sequence.
392static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
393 assert(Node && "Didn't find callseq_start for a call??");
394 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
395
396 assert(Node->getOperand(0).getValueType() == MVT::Other &&
397 "Node doesn't have a token chain argument!");
398 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
399}
400
401/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
402/// see if any uses can reach Dest. If no dest operands can get to dest,
403/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000404///
405/// Keep track of the nodes we fine that actually do lead to Dest in
406/// NodesLeadingTo. This avoids retraversing them exponential number of times.
407///
408bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000409 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000410 if (N == Dest) return true; // N certainly leads to Dest :)
411
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000412 // If we've already processed this node and it does lead to Dest, there is no
413 // need to reprocess it.
414 if (NodesLeadingTo.count(N)) return true;
415
Chris Lattner6831a812006-02-13 09:18:02 +0000416 // If the first result of this node has been already legalized, then it cannot
417 // reach N.
418 switch (getTypeAction(N->getValueType(0))) {
419 case Legal:
420 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
421 break;
422 case Promote:
423 if (PromotedNodes.count(SDOperand(N, 0))) return false;
424 break;
425 case Expand:
426 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
427 break;
428 }
429
430 // Okay, this node has not already been legalized. Check and legalize all
431 // operands. If none lead to Dest, then we can legalize this node.
432 bool OperandsLeadToDest = false;
433 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
434 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000435 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000436
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000437 if (OperandsLeadToDest) {
438 NodesLeadingTo.insert(N);
439 return true;
440 }
Chris Lattner6831a812006-02-13 09:18:02 +0000441
442 // Okay, this node looks safe, legalize it and return false.
Chris Lattner80edfb32006-04-17 22:10:08 +0000443 HandleOp(SDOperand(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000444 return false;
445}
446
Dan Gohman7f321562007-06-25 16:23:39 +0000447/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000448/// appropriate for its type.
449void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohman7f321562007-06-25 16:23:39 +0000450 MVT::ValueType VT = Op.getValueType();
451 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000452 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000453 case Legal: (void)LegalizeOp(Op); break;
454 case Promote: (void)PromoteOp(Op); break;
Chris Lattnerc7029802006-03-18 01:44:44 +0000455 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +0000456 if (!MVT::isVector(VT)) {
457 // If this is an illegal scalar, expand it into its two component
458 // pieces.
Chris Lattnerc7029802006-03-18 01:44:44 +0000459 SDOperand X, Y;
460 ExpandOp(Op, X, Y);
Dan Gohman7f321562007-06-25 16:23:39 +0000461 } else if (MVT::getVectorNumElements(VT) == 1) {
462 // If this is an illegal single element vector, convert it to a
463 // scalar operation.
464 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000465 } else {
Dan Gohman7f321562007-06-25 16:23:39 +0000466 // Otherwise, this is an illegal multiple element vector.
467 // Split it in half and legalize both parts.
468 SDOperand X, Y;
469 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000470 }
471 break;
472 }
473}
Chris Lattner6831a812006-02-13 09:18:02 +0000474
Evan Cheng9f877882006-12-13 20:57:08 +0000475/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
476/// a load from the constant pool.
477static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000478 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000479 bool Extend = false;
480
481 // If a FP immediate is precise when represented as a float and if the
482 // target can do an extending load from float to double, we put it into
483 // the constant pool as a float, even if it's is statically typed as a
484 // double.
485 MVT::ValueType VT = CFP->getValueType(0);
486 bool isDouble = VT == MVT::f64;
487 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
488 Type::FloatTy, CFP->getValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000489 if (!UseCP) {
Evan Cheng279101e2006-12-12 22:19:28 +0000490 double Val = LLVMC->getValue();
491 return isDouble
492 ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
493 : DAG.getConstant(FloatToBits(Val), MVT::i32);
494 }
495
Evan Cheng00495212006-12-12 21:32:44 +0000496 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
497 // Only do this if the target has a native EXTLOAD instruction from f32.
498 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
499 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
500 VT = MVT::f32;
501 Extend = true;
502 }
503
504 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
505 if (Extend) {
506 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
507 CPIdx, NULL, 0, MVT::f32);
508 } else {
509 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
510 }
511}
512
Chris Lattner6831a812006-02-13 09:18:02 +0000513
Evan Cheng912095b2007-01-04 21:56:39 +0000514/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
515/// operations.
516static
517SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
518 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng068c5f42007-01-05 21:31:51 +0000519 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng912095b2007-01-04 21:56:39 +0000520 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000521 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
522 "fcopysign expansion only supported for f32 and f64");
Evan Cheng912095b2007-01-04 21:56:39 +0000523 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000524
Evan Cheng912095b2007-01-04 21:56:39 +0000525 // First get the sign bit of second operand.
Evan Cheng068c5f42007-01-05 21:31:51 +0000526 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000527 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
528 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000529 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000530 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000531 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000532 // Shift right or sign-extend it if the two operands have different types.
533 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
534 if (SizeDiff > 0) {
535 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
536 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
537 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
538 } else if (SizeDiff < 0)
539 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng068c5f42007-01-05 21:31:51 +0000540
541 // Clear the sign bit of first operand.
542 SDOperand Mask2 = (VT == MVT::f64)
543 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
544 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
545 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng912095b2007-01-04 21:56:39 +0000546 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000547 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
548
549 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000550 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
551 return Result;
552}
553
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000554/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
555static
556SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
557 TargetLowering &TLI) {
558 assert(MVT::isInteger(ST->getStoredVT()) &&
559 "Non integer unaligned stores not implemented.");
560 int SVOffset = ST->getSrcValueOffset();
561 SDOperand Chain = ST->getChain();
562 SDOperand Ptr = ST->getBasePtr();
563 SDOperand Val = ST->getValue();
564 MVT::ValueType VT = Val.getValueType();
565 // Get the half-size VT
566 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
567 int NumBits = MVT::getSizeInBits(NewStoredVT);
568 int Alignment = ST->getAlignment();
569 int IncrementSize = NumBits / 8;
570
571 // Divide the stored value in two parts.
572 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
573 SDOperand Lo = Val;
574 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
575
576 // Store the two parts
577 SDOperand Store1, Store2;
578 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
579 ST->getSrcValue(), SVOffset, NewStoredVT,
580 ST->isVolatile(), Alignment);
581 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
582 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
583 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
584 ST->getSrcValue(), SVOffset + IncrementSize,
585 NewStoredVT, ST->isVolatile(), Alignment);
586
587 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
588}
589
590/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
591static
592SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
593 TargetLowering &TLI) {
594 assert(MVT::isInteger(LD->getLoadedVT()) &&
595 "Non integer unaligned loads not implemented.");
596 int SVOffset = LD->getSrcValueOffset();
597 SDOperand Chain = LD->getChain();
598 SDOperand Ptr = LD->getBasePtr();
599 MVT::ValueType VT = LD->getValueType(0);
600 MVT::ValueType NewLoadedVT = LD->getLoadedVT() - 1;
601 int NumBits = MVT::getSizeInBits(NewLoadedVT);
602 int Alignment = LD->getAlignment();
603 int IncrementSize = NumBits / 8;
604 ISD::LoadExtType HiExtType = LD->getExtensionType();
605
606 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
607 if (HiExtType == ISD::NON_EXTLOAD)
608 HiExtType = ISD::ZEXTLOAD;
609
610 // Load the value in two parts
611 SDOperand Lo, Hi;
612 if (TLI.isLittleEndian()) {
613 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
614 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
615 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
616 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
617 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
618 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
619 Alignment);
620 } else {
621 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
622 NewLoadedVT,LD->isVolatile(), Alignment);
623 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
624 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
625 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
626 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
627 Alignment);
628 }
629
630 // aggregate the two parts
631 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
632 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
633 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
634
635 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
636 Hi.getValue(1));
637
638 SDOperand Ops[] = { Result, TF };
639 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
640}
Evan Cheng912095b2007-01-04 21:56:39 +0000641
Dan Gohmana3466152007-07-13 20:14:11 +0000642/// LegalizeOp - We know that the specified value has a legal type, and
643/// that its operands are legal. Now ensure that the operation itself
644/// is legal, recursively ensuring that the operands' operations remain
645/// legal.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000646SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000647 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000648 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000649 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000650
Chris Lattner3e928bb2005-01-07 07:47:09 +0000651 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000652 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000653 if (Node->getNumValues() > 1) {
654 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000655 if (getTypeAction(Node->getValueType(i)) != Legal) {
656 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000657 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000658 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000659 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000660 }
661 }
662
Chris Lattner45982da2005-05-12 16:53:42 +0000663 // Note that LegalizeOp may be reentered even from single-use nodes, which
664 // means that we always must cache transformed nodes.
Chris Lattner718071c2007-02-04 00:50:02 +0000665 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000666 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000667
Nate Begeman9373a812005-08-10 20:51:12 +0000668 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000669 SDOperand Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000670 bool isCustom = false;
671
Chris Lattner3e928bb2005-01-07 07:47:09 +0000672 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000673 case ISD::FrameIndex:
674 case ISD::EntryToken:
675 case ISD::Register:
676 case ISD::BasicBlock:
677 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000678 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000679 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000680 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000681 case ISD::TargetConstantPool:
682 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000683 case ISD::TargetGlobalTLSAddress:
Chris Lattner948c1b12006-01-28 08:31:04 +0000684 case ISD::TargetExternalSymbol:
685 case ISD::VALUETYPE:
686 case ISD::SRCVALUE:
687 case ISD::STRING:
688 case ISD::CONDCODE:
689 // Primitives must all be legal.
690 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
691 "This must be legal!");
692 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000693 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000694 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
695 // If this is a target node, legalize it by legalizing the operands then
696 // passing it through.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000697 SmallVector<SDOperand, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000698 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000699 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000700
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000701 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000702
703 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
704 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
705 return Result.getValue(Op.ResNo);
706 }
707 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000708#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000709 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000710#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000711 assert(0 && "Do not know how to legalize this operator!");
712 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000713 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000714 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000715 case ISD::GlobalTLSAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000716 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000717 case ISD::ConstantPool:
718 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000719 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
720 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000721 case TargetLowering::Custom:
722 Tmp1 = TLI.LowerOperation(Op, DAG);
723 if (Tmp1.Val) Result = Tmp1;
724 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000725 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000726 break;
727 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000728 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000729 case ISD::FRAMEADDR:
730 case ISD::RETURNADDR:
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000731 case ISD::FRAME_TO_ARGS_OFFSET:
Nate Begemanbcc5f362007-01-29 22:58:52 +0000732 // The only option for these nodes is to custom lower them. If the target
733 // does not custom lower them, then return zero.
734 Tmp1 = TLI.LowerOperation(Op, DAG);
735 if (Tmp1.Val)
736 Result = Tmp1;
737 else
738 Result = DAG.getConstant(0, TLI.getPointerTy());
739 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000740 case ISD::EXCEPTIONADDR: {
741 Tmp1 = LegalizeOp(Node->getOperand(0));
742 MVT::ValueType VT = Node->getValueType(0);
743 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
744 default: assert(0 && "This action is not supported yet!");
745 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000746 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey2bc210d2007-02-22 15:37:19 +0000747 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
748 }
749 break;
750 case TargetLowering::Custom:
751 Result = TLI.LowerOperation(Op, DAG);
752 if (Result.Val) break;
753 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000754 case TargetLowering::Legal: {
755 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
756 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
757 Ops, 2).getValue(Op.ResNo);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000758 break;
759 }
760 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000761 }
Jim Laskey2bc210d2007-02-22 15:37:19 +0000762 break;
Jim Laskey8782d482007-02-28 20:43:58 +0000763 case ISD::EHSELECTION: {
764 Tmp1 = LegalizeOp(Node->getOperand(0));
765 Tmp2 = LegalizeOp(Node->getOperand(1));
766 MVT::ValueType VT = Node->getValueType(0);
767 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
768 default: assert(0 && "This action is not supported yet!");
769 case TargetLowering::Expand: {
770 unsigned Reg = TLI.getExceptionSelectorRegister();
771 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
772 }
773 break;
774 case TargetLowering::Custom:
775 Result = TLI.LowerOperation(Op, DAG);
776 if (Result.Val) break;
777 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000778 case TargetLowering::Legal: {
779 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
780 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
781 Ops, 2).getValue(Op.ResNo);
Jim Laskey8782d482007-02-28 20:43:58 +0000782 break;
783 }
784 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000785 }
Jim Laskey8782d482007-02-28 20:43:58 +0000786 break;
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000787 case ISD::EH_RETURN: {
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000788 MVT::ValueType VT = Node->getValueType(0);
789 // The only "good" option for this node is to custom lower it.
790 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
791 default: assert(0 && "This action is not supported at all!");
792 case TargetLowering::Custom:
793 Result = TLI.LowerOperation(Op, DAG);
794 if (Result.Val) break;
795 // Fall Thru
796 case TargetLowering::Legal:
797 // Target does not know, how to lower this, lower to noop
798 Result = LegalizeOp(Node->getOperand(0));
799 break;
800 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +0000801 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000802 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000803 case ISD::AssertSext:
804 case ISD::AssertZext:
805 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000806 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +0000807 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000808 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000809 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner456a93a2006-01-28 07:39:30 +0000810 Result = Node->getOperand(Op.ResNo);
811 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000812 case ISD::CopyFromReg:
813 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +0000814 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000815 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000816 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +0000817 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000818 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000819 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +0000820 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000821 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
822 } else {
823 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
824 }
Chris Lattner7310fb12005-12-18 15:27:43 +0000825 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
826 }
Chris Lattner13c184d2005-01-28 06:27:38 +0000827 // Since CopyFromReg produces two values, make sure to remember that we
828 // legalized both of them.
829 AddLegalizedOperand(Op.getValue(0), Result);
830 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
831 return Result.getValue(Op.ResNo);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000832 case ISD::UNDEF: {
833 MVT::ValueType VT = Op.getValueType();
834 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000835 default: assert(0 && "This action is not supported yet!");
836 case TargetLowering::Expand:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000837 if (MVT::isInteger(VT))
838 Result = DAG.getConstant(0, VT);
839 else if (MVT::isFloatingPoint(VT))
840 Result = DAG.getConstantFP(0, VT);
841 else
842 assert(0 && "Unknown value type!");
843 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000844 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000845 break;
846 }
847 break;
848 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000849
Chris Lattner48b61a72006-03-28 00:40:33 +0000850 case ISD::INTRINSIC_W_CHAIN:
851 case ISD::INTRINSIC_WO_CHAIN:
852 case ISD::INTRINSIC_VOID: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000853 SmallVector<SDOperand, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000854 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
855 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000856 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +0000857
858 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +0000859 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +0000860 TargetLowering::Custom) {
861 Tmp3 = TLI.LowerOperation(Result, DAG);
862 if (Tmp3.Val) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +0000863 }
864
865 if (Result.Val->getNumValues() == 1) break;
866
867 // Must have return value and chain result.
868 assert(Result.Val->getNumValues() == 2 &&
869 "Cannot return more than two values!");
870
871 // Since loads produce two values, make sure to remember that we
872 // legalized both of them.
873 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
874 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
875 return Result.getValue(Op.ResNo);
Chris Lattnerd1f04d42006-03-24 02:26:29 +0000876 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000877
878 case ISD::LOCATION:
879 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
880 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
881
882 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
883 case TargetLowering::Promote:
884 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000885 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000886 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000887 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskey1ee29252007-01-26 14:34:52 +0000888 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000889
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000890 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000891 const std::string &FName =
892 cast<StringSDNode>(Node->getOperand(3))->getValue();
893 const std::string &DirName =
894 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000895 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000896
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000897 SmallVector<SDOperand, 8> Ops;
Jim Laskeye81aecb2005-12-21 20:51:37 +0000898 Ops.push_back(Tmp1); // chain
Jim Laskeyabf6d172006-01-05 01:25:28 +0000899 SDOperand LineOp = Node->getOperand(1);
900 SDOperand ColOp = Node->getOperand(2);
901
902 if (useDEBUG_LOC) {
903 Ops.push_back(LineOp); // line #
904 Ops.push_back(ColOp); // col #
905 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000906 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000907 } else {
Jim Laskeyd0e58e32006-02-15 19:34:44 +0000908 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
909 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000910 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000911 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskey1ee29252007-01-26 14:34:52 +0000912 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskeyabf6d172006-01-05 01:25:28 +0000913 }
Jim Laskeye81aecb2005-12-21 20:51:37 +0000914 } else {
915 Result = Tmp1; // chain
916 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000917 break;
Chris Lattnere7736732005-12-18 23:54:29 +0000918 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000919 case TargetLowering::Legal:
Chris Lattner9ad17c92005-12-01 18:21:35 +0000920 if (Tmp1 != Node->getOperand(0) ||
921 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000922 SmallVector<SDOperand, 8> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +0000923 Ops.push_back(Tmp1);
Chris Lattner9ad17c92005-12-01 18:21:35 +0000924 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
925 Ops.push_back(Node->getOperand(1)); // line # must be legal.
926 Ops.push_back(Node->getOperand(2)); // col # must be legal.
927 } else {
928 // Otherwise promote them.
929 Ops.push_back(PromoteOp(Node->getOperand(1)));
930 Ops.push_back(PromoteOp(Node->getOperand(2)));
931 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000932 Ops.push_back(Node->getOperand(3)); // filename must be legal.
933 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000934 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +0000935 }
936 break;
937 }
938 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000939
940 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +0000941 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000942 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000943 default: assert(0 && "This action is not supported yet!");
Jim Laskeyabf6d172006-01-05 01:25:28 +0000944 case TargetLowering::Legal:
945 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
946 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
947 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
948 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000949 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000950 break;
951 }
952 break;
953
Jim Laskey1ee29252007-01-26 14:34:52 +0000954 case ISD::LABEL:
955 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
956 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000957 default: assert(0 && "This action is not supported yet!");
958 case TargetLowering::Legal:
959 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
960 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +0000961 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000962 break;
Chris Lattnera9569f12007-03-03 19:21:38 +0000963 case TargetLowering::Expand:
964 Result = LegalizeOp(Node->getOperand(0));
965 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000966 }
Nate Begeman551bf3f2006-02-17 05:43:56 +0000967 break;
Chris Lattner36ce6912005-11-29 06:21:05 +0000968
Scott Michelc1513d22007-08-08 23:23:31 +0000969 case ISD::Constant: {
970 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
971 unsigned opAction =
972 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
973
Chris Lattner3e928bb2005-01-07 07:47:09 +0000974 // We know we don't need to expand constants here, constants only have one
975 // value and we check that it is fine above.
976
Scott Michelc1513d22007-08-08 23:23:31 +0000977 if (opAction == TargetLowering::Custom) {
978 Tmp1 = TLI.LowerOperation(Result, DAG);
979 if (Tmp1.Val)
980 Result = Tmp1;
981 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000982 break;
Scott Michelc1513d22007-08-08 23:23:31 +0000983 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000984 case ISD::ConstantFP: {
985 // Spill FP immediates to the constant pool if the target cannot directly
986 // codegen them. Targets often have some immediate values that can be
987 // efficiently generated into an FP register without a load. We explicitly
988 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000989 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
990
991 // Check to see if this FP immediate is already legal.
992 bool isLegal = false;
993 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
994 E = TLI.legal_fpimm_end(); I != E; ++I)
995 if (CFP->isExactlyValue(*I)) {
996 isLegal = true;
997 break;
998 }
999
Chris Lattner3181a772006-01-29 06:26:56 +00001000 // If this is a legal constant, turn it into a TargetConstantFP node.
1001 if (isLegal) {
1002 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
1003 break;
1004 }
1005
1006 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1007 default: assert(0 && "This action is not supported yet!");
1008 case TargetLowering::Custom:
1009 Tmp3 = TLI.LowerOperation(Result, DAG);
1010 if (Tmp3.Val) {
1011 Result = Tmp3;
1012 break;
1013 }
1014 // FALLTHROUGH
1015 case TargetLowering::Expand:
Evan Cheng279101e2006-12-12 22:19:28 +00001016 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001017 }
1018 break;
1019 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001020 case ISD::TokenFactor:
1021 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001022 Tmp1 = LegalizeOp(Node->getOperand(0));
1023 Tmp2 = LegalizeOp(Node->getOperand(1));
1024 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1025 } else if (Node->getNumOperands() == 3) {
1026 Tmp1 = LegalizeOp(Node->getOperand(0));
1027 Tmp2 = LegalizeOp(Node->getOperand(1));
1028 Tmp3 = LegalizeOp(Node->getOperand(2));
1029 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001030 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001031 SmallVector<SDOperand, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001032 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001033 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1034 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001035 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001036 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001037 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001038
1039 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001040 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001041 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001042 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1043 assert(Tmp3.Val && "Target didn't custom lower this node!");
1044 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1045 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001046
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001047 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001048 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnerb248e162006-05-17 17:55:45 +00001049 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1050 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattnere2e41732006-05-16 05:49:56 +00001051 if (Op.ResNo == i)
1052 Tmp2 = Tmp1;
1053 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1054 }
1055 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001056 case ISD::EXTRACT_SUBREG: {
1057 Tmp1 = LegalizeOp(Node->getOperand(0));
1058 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1059 assert(idx && "Operand must be a constant");
1060 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1061 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1062 }
1063 break;
1064 case ISD::INSERT_SUBREG: {
1065 Tmp1 = LegalizeOp(Node->getOperand(0));
1066 Tmp2 = LegalizeOp(Node->getOperand(1));
1067 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1068 assert(idx && "Operand must be a constant");
1069 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1070 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1071 }
1072 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001073 case ISD::BUILD_VECTOR:
1074 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001075 default: assert(0 && "This action is not supported yet!");
1076 case TargetLowering::Custom:
1077 Tmp3 = TLI.LowerOperation(Result, DAG);
1078 if (Tmp3.Val) {
1079 Result = Tmp3;
1080 break;
1081 }
1082 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001083 case TargetLowering::Expand:
1084 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerb2827b02006-03-19 00:52:58 +00001085 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001086 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001087 break;
1088 case ISD::INSERT_VECTOR_ELT:
1089 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1090 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1091 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1092 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1093
1094 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1095 Node->getValueType(0))) {
1096 default: assert(0 && "This action is not supported yet!");
1097 case TargetLowering::Legal:
1098 break;
1099 case TargetLowering::Custom:
1100 Tmp3 = TLI.LowerOperation(Result, DAG);
1101 if (Tmp3.Val) {
1102 Result = Tmp3;
1103 break;
1104 }
1105 // FALLTHROUGH
1106 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001107 // If the insert index is a constant, codegen this as a scalar_to_vector,
1108 // then a shuffle that inserts it into the right position in the vector.
1109 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1110 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1111 Tmp1.getValueType(), Tmp2);
1112
1113 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1114 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman51eaa862007-06-14 22:58:02 +00001115 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner8d5a8942006-04-17 19:21:01 +00001116
1117 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1118 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1119 // the RHS.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001120 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001121 for (unsigned i = 0; i != NumElts; ++i) {
1122 if (i != InsertPos->getValue())
1123 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1124 else
1125 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1126 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001127 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1128 &ShufOps[0], ShufOps.size());
Chris Lattner8d5a8942006-04-17 19:21:01 +00001129
1130 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1131 Tmp1, ScVec, ShufMask);
1132 Result = LegalizeOp(Result);
1133 break;
1134 }
1135
Chris Lattner2332b9f2006-03-19 01:17:20 +00001136 // If the target doesn't support this, we have to spill the input vector
1137 // to a temporary stack slot, update the element, then reload it. This is
1138 // badness. We could also load the value into a vector register (either
1139 // with a "move to register" or "extload into register" instruction, then
1140 // permute it into place, if the idx is a constant and if the idx is
1141 // supported by the target.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001142 MVT::ValueType VT = Tmp1.getValueType();
1143 MVT::ValueType EltVT = Tmp2.getValueType();
1144 MVT::ValueType IdxVT = Tmp3.getValueType();
1145 MVT::ValueType PtrVT = TLI.getPointerTy();
1146 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Chengeb0b4612006-03-31 01:27:51 +00001147 // Store the vector.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001148 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001149
1150 // Truncate or zero extend offset to target pointer type.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001151 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1152 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Chengeb0b4612006-03-31 01:27:51 +00001153 // Add the offset to the index.
Evan Chengf6bf87f2006-04-08 01:46:37 +00001154 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1155 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1156 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Chengeb0b4612006-03-31 01:27:51 +00001157 // Store the scalar value.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001158 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Chengeb0b4612006-03-31 01:27:51 +00001159 // Load the updated vector.
Evan Cheng466685d2006-10-09 20:57:25 +00001160 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001161 break;
1162 }
1163 }
1164 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001165 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001166 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1167 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1168 break;
1169 }
1170
Chris Lattnerce872152006-03-19 06:31:19 +00001171 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1172 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1173 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1174 Node->getValueType(0))) {
1175 default: assert(0 && "This action is not supported yet!");
1176 case TargetLowering::Legal:
1177 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001178 case TargetLowering::Custom:
1179 Tmp3 = TLI.LowerOperation(Result, DAG);
1180 if (Tmp3.Val) {
1181 Result = Tmp3;
1182 break;
1183 }
1184 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001185 case TargetLowering::Expand:
1186 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001187 break;
1188 }
Chris Lattnerce872152006-03-19 06:31:19 +00001189 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001190 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001191 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1192 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1193 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1194
1195 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001196 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1197 default: assert(0 && "Unknown operation action!");
1198 case TargetLowering::Legal:
1199 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1200 "vector shuffle should not be created if not legal!");
1201 break;
1202 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001203 Tmp3 = TLI.LowerOperation(Result, DAG);
1204 if (Tmp3.Val) {
1205 Result = Tmp3;
1206 break;
1207 }
1208 // FALLTHROUGH
1209 case TargetLowering::Expand: {
1210 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00001211 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001212 MVT::ValueType PtrVT = TLI.getPointerTy();
1213 SDOperand Mask = Node->getOperand(2);
1214 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001215 SmallVector<SDOperand,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001216 for (unsigned i = 0; i != NumElems; ++i) {
1217 SDOperand Arg = Mask.getOperand(i);
1218 if (Arg.getOpcode() == ISD::UNDEF) {
1219 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1220 } else {
1221 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1222 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1223 if (Idx < NumElems)
1224 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1225 DAG.getConstant(Idx, PtrVT)));
1226 else
1227 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1228 DAG.getConstant(Idx - NumElems, PtrVT)));
1229 }
1230 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001231 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001232 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001233 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001234 case TargetLowering::Promote: {
1235 // Change base type to a different vector type.
1236 MVT::ValueType OVT = Node->getValueType(0);
1237 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1238
1239 // Cast the two input vectors.
1240 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1241 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1242
1243 // Convert the shuffle mask to the right # elements.
1244 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1245 assert(Tmp3.Val && "Shuffle not legal?");
1246 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1247 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1248 break;
1249 }
Chris Lattner87100e02006-03-20 01:52:29 +00001250 }
1251 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001252
1253 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001254 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001255 Tmp2 = LegalizeOp(Node->getOperand(1));
1256 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001257 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001258 break;
1259
Dan Gohman7f321562007-06-25 16:23:39 +00001260 case ISD::EXTRACT_SUBVECTOR:
1261 Tmp1 = Node->getOperand(0);
1262 Tmp2 = LegalizeOp(Node->getOperand(1));
1263 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1264 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001265 break;
1266
Chris Lattner6831a812006-02-13 09:18:02 +00001267 case ISD::CALLSEQ_START: {
1268 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1269
1270 // Recursively Legalize all of the inputs of the call end that do not lead
1271 // to this call start. This ensures that any libcalls that need be inserted
1272 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001273 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001274 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001275 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1276 NodesLeadingTo);
1277 }
Chris Lattner6831a812006-02-13 09:18:02 +00001278
1279 // Now that we legalized all of the inputs (which may have inserted
1280 // libcalls) create the new CALLSEQ_START node.
1281 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1282
1283 // Merge in the last call, to ensure that this call start after the last
1284 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001285 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001286 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1287 Tmp1 = LegalizeOp(Tmp1);
1288 }
Chris Lattner6831a812006-02-13 09:18:02 +00001289
1290 // Do not try to legalize the target-specific arguments (#1+).
1291 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001292 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001293 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001294 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001295 }
1296
1297 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001298 AddLegalizedOperand(Op.getValue(0), Result);
1299 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1300 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1301
Chris Lattner6831a812006-02-13 09:18:02 +00001302 // Now that the callseq_start and all of the non-call nodes above this call
1303 // sequence have been legalized, legalize the call itself. During this
1304 // process, no libcalls can/will be inserted, guaranteeing that no calls
1305 // can overlap.
1306 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1307 SDOperand InCallSEQ = LastCALLSEQ_END;
1308 // Note that we are selecting this call!
1309 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1310 IsLegalizingCall = true;
1311
1312 // Legalize the call, starting from the CALLSEQ_END.
1313 LegalizeOp(LastCALLSEQ_END);
1314 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1315 return Result;
1316 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001317 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001318 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1319 // will cause this node to be legalized as well as handling libcalls right.
1320 if (LastCALLSEQ_END.Val != Node) {
1321 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattner718071c2007-02-04 00:50:02 +00001322 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001323 assert(I != LegalizedNodes.end() &&
1324 "Legalizing the call start should have legalized this node!");
1325 return I->second;
1326 }
1327
1328 // Otherwise, the call start has been legalized and everything is going
1329 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001330 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001331 // Do not try to legalize the target-specific arguments (#1+), except for
1332 // an optional flag input.
1333 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1334 if (Tmp1 != Node->getOperand(0)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001335 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001336 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001337 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001338 }
1339 } else {
1340 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1341 if (Tmp1 != Node->getOperand(0) ||
1342 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001343 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001344 Ops[0] = Tmp1;
1345 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001346 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001347 }
Chris Lattner6a542892006-01-24 05:48:21 +00001348 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001349 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001350 // This finishes up call legalization.
1351 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001352
1353 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1354 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1355 if (Node->getNumValues() == 2)
1356 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1357 return Result.getValue(Op.ResNo);
Evan Chenga7dce3c2006-01-11 22:14:47 +00001358 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng61bbbab2007-08-16 23:50:06 +00001359 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001360 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1361 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1362 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001363 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001364
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001365 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001366 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001367 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001368 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001369 case TargetLowering::Expand: {
1370 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1371 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1372 " not tell us which reg is the stack pointer!");
1373 SDOperand Chain = Tmp1.getOperand(0);
1374 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001375 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1376 Chain = SP.getValue(1);
1377 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1378 unsigned StackAlign =
1379 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1380 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001381 SP = DAG.getNode(ISD::AND, VT, SP,
1382 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001383 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
1384 Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001385 Tmp1 = LegalizeOp(Tmp1);
1386 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001387 break;
1388 }
1389 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001390 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1391 if (Tmp3.Val) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001392 Tmp1 = LegalizeOp(Tmp3);
1393 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001394 }
Chris Lattner903d2782006-01-15 08:54:32 +00001395 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001396 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001397 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001398 }
Chris Lattner903d2782006-01-15 08:54:32 +00001399 // Since this op produce two values, make sure to remember that we
1400 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001401 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1402 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001403 return Op.ResNo ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001404 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001405 case ISD::INLINEASM: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001406 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001407 bool Changed = false;
1408 // Legalize all of the operands of the inline asm, in case they are nodes
1409 // that need to be expanded or something. Note we skip the asm string and
1410 // all of the TargetConstant flags.
1411 SDOperand Op = LegalizeOp(Ops[0]);
1412 Changed = Op != Ops[0];
1413 Ops[0] = Op;
1414
1415 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1416 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1417 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1418 for (++i; NumVals; ++i, --NumVals) {
1419 SDOperand Op = LegalizeOp(Ops[i]);
1420 if (Op != Ops[i]) {
1421 Changed = true;
1422 Ops[i] = Op;
1423 }
1424 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001425 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001426
1427 if (HasInFlag) {
1428 Op = LegalizeOp(Ops.back());
1429 Changed |= Op != Ops.back();
1430 Ops.back() = Op;
1431 }
1432
1433 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001434 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001435
1436 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001437 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001438 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1439 return Result.getValue(Op.ResNo);
Chris Lattner25a022c2006-07-11 01:40:09 +00001440 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001441 case ISD::BR:
1442 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001443 // Ensure that libcalls are emitted before a branch.
1444 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1445 Tmp1 = LegalizeOp(Tmp1);
1446 LastCALLSEQ_END = DAG.getEntryNode();
1447
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001448 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001449 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001450 case ISD::BRIND:
1451 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1452 // Ensure that libcalls are emitted before a branch.
1453 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1454 Tmp1 = LegalizeOp(Tmp1);
1455 LastCALLSEQ_END = DAG.getEntryNode();
1456
1457 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1458 default: assert(0 && "Indirect target must be legal type (pointer)!");
1459 case Legal:
1460 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1461 break;
1462 }
1463 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1464 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001465 case ISD::BR_JT:
1466 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1467 // Ensure that libcalls are emitted before a branch.
1468 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1469 Tmp1 = LegalizeOp(Tmp1);
1470 LastCALLSEQ_END = DAG.getEntryNode();
1471
1472 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1473 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1474
1475 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1476 default: assert(0 && "This action is not supported yet!");
1477 case TargetLowering::Legal: break;
1478 case TargetLowering::Custom:
1479 Tmp1 = TLI.LowerOperation(Result, DAG);
1480 if (Tmp1.Val) Result = Tmp1;
1481 break;
1482 case TargetLowering::Expand: {
1483 SDOperand Chain = Result.getOperand(0);
1484 SDOperand Table = Result.getOperand(1);
1485 SDOperand Index = Result.getOperand(2);
1486
1487 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001488 MachineFunction &MF = DAG.getMachineFunction();
1489 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001490 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1491 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001492
1493 SDOperand LD;
1494 switch (EntrySize) {
1495 default: assert(0 && "Size of jump table not supported yet."); break;
1496 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1497 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1498 }
1499
1500 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001501 // For PIC, the sequence is:
1502 // BRIND(load(Jumptable + index) + RelocBase)
1503 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1504 SDOperand Reloc;
1505 if (TLI.usesGlobalOffsetTable())
1506 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1507 else
1508 Reloc = Table;
Evan Chengd0631892006-10-31 02:31:00 +00001509 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001510 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1511 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1512 } else {
1513 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1514 }
1515 }
1516 }
1517 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001518 case ISD::BRCOND:
1519 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001520 // Ensure that libcalls are emitted before a return.
1521 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1522 Tmp1 = LegalizeOp(Tmp1);
1523 LastCALLSEQ_END = DAG.getEntryNode();
1524
Chris Lattner47e92232005-01-18 19:27:06 +00001525 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1526 case Expand: assert(0 && "It's impossible to expand bools");
1527 case Legal:
1528 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1529 break;
1530 case Promote:
1531 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001532
1533 // The top bits of the promoted condition are not necessarily zero, ensure
1534 // that the value is properly zero extended.
Dan Gohmanea859be2007-06-22 14:59:07 +00001535 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerf9908172006-11-27 04:39:56 +00001536 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1537 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001538 break;
1539 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001540
1541 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001542 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001543
1544 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1545 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001546 case TargetLowering::Legal: break;
1547 case TargetLowering::Custom:
1548 Tmp1 = TLI.LowerOperation(Result, DAG);
1549 if (Tmp1.Val) Result = Tmp1;
1550 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001551 case TargetLowering::Expand:
1552 // Expand brcond's setcc into its constituent parts and create a BR_CC
1553 // Node.
1554 if (Tmp2.getOpcode() == ISD::SETCC) {
1555 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1556 Tmp2.getOperand(0), Tmp2.getOperand(1),
1557 Node->getOperand(2));
1558 } else {
1559 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1560 DAG.getCondCode(ISD::SETNE), Tmp2,
1561 DAG.getConstant(0, Tmp2.getValueType()),
1562 Node->getOperand(2));
1563 }
1564 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001565 }
1566 break;
1567 case ISD::BR_CC:
1568 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001569 // Ensure that libcalls are emitted before a branch.
1570 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1571 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00001572 Tmp2 = Node->getOperand(2); // LHS
1573 Tmp3 = Node->getOperand(3); // RHS
1574 Tmp4 = Node->getOperand(1); // CC
1575
1576 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00001577 LastCALLSEQ_END = DAG.getEntryNode();
1578
Nate Begeman750ac1b2006-02-01 07:19:44 +00001579 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1580 // the LHS is a legal SETCC itself. In this case, we need to compare
1581 // the result against zero to select between true and false values.
1582 if (Tmp3.Val == 0) {
1583 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1584 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00001585 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00001586
1587 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1588 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00001589
Chris Lattner181b7a32005-12-17 23:46:46 +00001590 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1591 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001592 case TargetLowering::Legal: break;
1593 case TargetLowering::Custom:
1594 Tmp4 = TLI.LowerOperation(Result, DAG);
1595 if (Tmp4.Val) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00001596 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001597 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001598 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001599 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00001600 LoadSDNode *LD = cast<LoadSDNode>(Node);
1601 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1602 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001603
Evan Cheng466685d2006-10-09 20:57:25 +00001604 ISD::LoadExtType ExtType = LD->getExtensionType();
1605 if (ExtType == ISD::NON_EXTLOAD) {
1606 MVT::ValueType VT = Node->getValueType(0);
1607 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1608 Tmp3 = Result.getValue(0);
1609 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00001610
Evan Cheng466685d2006-10-09 20:57:25 +00001611 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1612 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001613 case TargetLowering::Legal:
1614 // If this is an unaligned load and the target doesn't support it,
1615 // expand it.
1616 if (!TLI.allowsUnalignedMemoryAccesses()) {
1617 unsigned ABIAlignment = TLI.getTargetData()->
1618 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1619 if (LD->getAlignment() < ABIAlignment){
1620 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1621 TLI);
1622 Tmp3 = Result.getOperand(0);
1623 Tmp4 = Result.getOperand(1);
1624 LegalizeOp(Tmp3);
1625 LegalizeOp(Tmp4);
1626 }
1627 }
1628 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001629 case TargetLowering::Custom:
1630 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1631 if (Tmp1.Val) {
1632 Tmp3 = LegalizeOp(Tmp1);
1633 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00001634 }
Evan Cheng466685d2006-10-09 20:57:25 +00001635 break;
1636 case TargetLowering::Promote: {
1637 // Only promote a load of vector type to another.
1638 assert(MVT::isVector(VT) && "Cannot promote this load!");
1639 // Change base type to a different vector type.
1640 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1641
1642 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001643 LD->getSrcValueOffset(),
1644 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001645 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1646 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001647 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00001648 }
Evan Cheng466685d2006-10-09 20:57:25 +00001649 }
1650 // Since loads produce two values, make sure to remember that we
1651 // legalized both of them.
1652 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1653 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1654 return Op.ResNo ? Tmp4 : Tmp3;
1655 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00001656 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001657 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1658 default: assert(0 && "This action is not supported yet!");
1659 case TargetLowering::Promote:
1660 assert(SrcVT == MVT::i1 &&
1661 "Can only promote extending LOAD from i1 -> i8!");
1662 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1663 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001664 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001665 Tmp1 = Result.getValue(0);
1666 Tmp2 = Result.getValue(1);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001667 break;
Evan Cheng466685d2006-10-09 20:57:25 +00001668 case TargetLowering::Custom:
1669 isCustom = true;
1670 // FALLTHROUGH
1671 case TargetLowering::Legal:
1672 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1673 Tmp1 = Result.getValue(0);
1674 Tmp2 = Result.getValue(1);
1675
1676 if (isCustom) {
1677 Tmp3 = TLI.LowerOperation(Result, DAG);
1678 if (Tmp3.Val) {
1679 Tmp1 = LegalizeOp(Tmp3);
1680 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1681 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001682 } else {
1683 // If this is an unaligned load and the target doesn't support it,
1684 // expand it.
1685 if (!TLI.allowsUnalignedMemoryAccesses()) {
1686 unsigned ABIAlignment = TLI.getTargetData()->
1687 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1688 if (LD->getAlignment() < ABIAlignment){
1689 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1690 TLI);
1691 Tmp1 = Result.getOperand(0);
1692 Tmp2 = Result.getOperand(1);
1693 LegalizeOp(Tmp1);
1694 LegalizeOp(Tmp2);
1695 }
1696 }
Evan Cheng466685d2006-10-09 20:57:25 +00001697 }
1698 break;
1699 case TargetLowering::Expand:
1700 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1701 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1702 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001703 LD->getSrcValueOffset(),
1704 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001705 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1706 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1707 Tmp2 = LegalizeOp(Load.getValue(1));
1708 break;
1709 }
Chris Lattnerfea997a2007-02-01 04:55:59 +00001710 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Cheng466685d2006-10-09 20:57:25 +00001711 // Turn the unsupported load into an EXTLOAD followed by an explicit
1712 // zero/sign extend inreg.
1713 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1714 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001715 LD->getSrcValueOffset(), SrcVT,
1716 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00001717 SDOperand ValRes;
1718 if (ExtType == ISD::SEXTLOAD)
1719 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1720 Result, DAG.getValueType(SrcVT));
1721 else
1722 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1723 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1724 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1725 break;
1726 }
1727 // Since loads produce two values, make sure to remember that we legalized
1728 // both of them.
1729 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1730 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1731 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00001732 }
Chris Lattner01ff7212005-04-10 22:54:25 +00001733 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001734 case ISD::EXTRACT_ELEMENT: {
1735 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1736 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001737 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00001738 case Legal:
1739 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1740 // 1 -> Hi
1741 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1742 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1743 TLI.getShiftAmountTy()));
1744 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1745 } else {
1746 // 0 -> Lo
1747 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1748 Node->getOperand(0));
1749 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001750 break;
1751 case Expand:
1752 // Get both the low and high parts.
1753 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1754 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1755 Result = Tmp2; // 1 -> Hi
1756 else
1757 Result = Tmp1; // 0 -> Lo
1758 break;
1759 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001760 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001761 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001762
1763 case ISD::CopyToReg:
1764 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001765
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001766 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001767 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00001768 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001769 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001770 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001771 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001772 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001773 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001774 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001775 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001776 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1777 Tmp3);
1778 } else {
1779 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00001780 }
1781
1782 // Since this produces two values, make sure to remember that we legalized
1783 // both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001784 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001785 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001786 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00001787 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001788 break;
1789
1790 case ISD::RET:
1791 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001792
1793 // Ensure that libcalls are emitted before a return.
1794 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1795 Tmp1 = LegalizeOp(Tmp1);
1796 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00001797
Chris Lattner3e928bb2005-01-07 07:47:09 +00001798 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00001799 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00001800 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001801 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00001802 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001803 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00001804 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001805 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00001806 case Expand:
Dan Gohman7f321562007-06-25 16:23:39 +00001807 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattnerf87324e2006-04-11 01:31:51 +00001808 SDOperand Lo, Hi;
1809 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00001810
1811 // Big endian systems want the hi reg first.
1812 if (!TLI.isLittleEndian())
1813 std::swap(Lo, Hi);
1814
Evan Cheng13acce32006-12-11 19:27:14 +00001815 if (Hi.Val)
1816 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1817 else
1818 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00001819 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001820 } else {
1821 SDNode *InVal = Tmp2.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001822 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1823 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattnerf87324e2006-04-11 01:31:51 +00001824
Dan Gohman7f321562007-06-25 16:23:39 +00001825 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001826 // type. If so, convert to the vector type.
Chris Lattnerf87324e2006-04-11 01:31:51 +00001827 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001828 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001829 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001830 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001831 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001832 } else if (NumElems == 1) {
1833 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00001834 Tmp2 = ScalarizeVectorOp(Tmp2);
1835 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001836 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001837
1838 // FIXME: Returns of gcc generic vectors smaller than a legal type
1839 // should be returned in integer registers!
1840
Chris Lattnerf87324e2006-04-11 01:31:51 +00001841 // The scalarized value type may not be legal, e.g. it might require
1842 // promotion or expansion. Relegalize the return.
1843 Result = LegalizeOp(Result);
1844 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001845 // FIXME: Returns of gcc generic vectors larger than a legal vector
1846 // type should be returned by reference!
Chris Lattnerf87324e2006-04-11 01:31:51 +00001847 SDOperand Lo, Hi;
1848 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00001849 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00001850 Result = LegalizeOp(Result);
1851 }
1852 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001853 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001854 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001855 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001856 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001857 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001858 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001859 }
1860 break;
1861 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001862 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001863 break;
1864 default: { // ret <values>
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001865 SmallVector<SDOperand, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001866 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001867 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00001868 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1869 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001870 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00001871 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001872 break;
1873 case Expand: {
1874 SDOperand Lo, Hi;
Dan Gohman6595cb32007-06-27 16:08:04 +00001875 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00001876 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001877 ExpandOp(Node->getOperand(i), Lo, Hi);
1878 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00001879 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng13acce32006-12-11 19:27:14 +00001880 if (Hi.Val) {
1881 NewValues.push_back(Hi);
1882 NewValues.push_back(Node->getOperand(i+1));
1883 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001884 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001885 }
1886 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001887 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001888 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001889
1890 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001891 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001892 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001893 Result = DAG.getNode(ISD::RET, MVT::Other,
1894 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001895 break;
1896 }
1897 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001898
Chris Lattner6862dbc2006-01-29 21:02:23 +00001899 if (Result.getOpcode() == ISD::RET) {
1900 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1901 default: assert(0 && "This action is not supported yet!");
1902 case TargetLowering::Legal: break;
1903 case TargetLowering::Custom:
1904 Tmp1 = TLI.LowerOperation(Result, DAG);
1905 if (Tmp1.Val) Result = Tmp1;
1906 break;
1907 }
Evan Cheng17c428e2006-01-06 00:41:43 +00001908 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001909 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001910 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001911 StoreSDNode *ST = cast<StoreSDNode>(Node);
1912 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1913 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001914 int SVOffset = ST->getSrcValueOffset();
1915 unsigned Alignment = ST->getAlignment();
1916 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001917
Evan Cheng8b2794a2006-10-13 21:14:26 +00001918 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001919 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1920 // FIXME: We shouldn't do this for TargetConstantFP's.
1921 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1922 // to phase ordering between legalized code and the dag combiner. This
1923 // probably means that we need to integrate dag combiner and legalizer
1924 // together.
1925 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1926 if (CFP->getValueType(0) == MVT::f32) {
1927 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1928 } else {
1929 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1930 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1931 }
1932 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001933 SVOffset, isVolatile, Alignment);
Chris Lattnerd93d46e2006-12-12 04:18:56 +00001934 break;
1935 }
1936
Evan Cheng8b2794a2006-10-13 21:14:26 +00001937 switch (getTypeAction(ST->getStoredVT())) {
1938 case Legal: {
1939 Tmp3 = LegalizeOp(ST->getValue());
1940 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1941 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00001942
Evan Cheng8b2794a2006-10-13 21:14:26 +00001943 MVT::ValueType VT = Tmp3.getValueType();
1944 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1945 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00001946 case TargetLowering::Legal:
1947 // If this is an unaligned store and the target doesn't support it,
1948 // expand it.
1949 if (!TLI.allowsUnalignedMemoryAccesses()) {
1950 unsigned ABIAlignment = TLI.getTargetData()->
1951 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
1952 if (ST->getAlignment() < ABIAlignment)
1953 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
1954 TLI);
1955 }
1956 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00001957 case TargetLowering::Custom:
1958 Tmp1 = TLI.LowerOperation(Result, DAG);
1959 if (Tmp1.Val) Result = Tmp1;
1960 break;
1961 case TargetLowering::Promote:
1962 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1963 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1964 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1965 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001966 ST->getSrcValue(), SVOffset, isVolatile,
1967 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001968 break;
1969 }
1970 break;
1971 }
1972 case Promote:
1973 // Truncate the value and store the result.
1974 Tmp3 = PromoteOp(ST->getValue());
1975 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001976 SVOffset, ST->getStoredVT(),
1977 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001978 break;
1979
1980 case Expand:
1981 unsigned IncrementSize = 0;
1982 SDOperand Lo, Hi;
1983
1984 // If this is a vector type, then we have to calculate the increment as
1985 // the product of the element size in bytes, and the number of elements
1986 // in the high half of the vector.
Dan Gohman7f321562007-06-25 16:23:39 +00001987 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00001988 SDNode *InVal = ST->getValue().Val;
Dan Gohman7f321562007-06-25 16:23:39 +00001989 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1990 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001991
Dan Gohman7f321562007-06-25 16:23:39 +00001992 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00001993 // type. If so, convert to the vector type.
Evan Cheng8b2794a2006-10-13 21:14:26 +00001994 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00001995 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00001996 // Turn this into a normal store of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00001997 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00001998 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00001999 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002000 Result = LegalizeOp(Result);
2001 break;
2002 } else if (NumElems == 1) {
2003 // Turn this into a normal store of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002004 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002005 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002006 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002007 // The scalarized value type may not be legal, e.g. it might require
2008 // promotion or expansion. Relegalize the scalar store.
2009 Result = LegalizeOp(Result);
2010 break;
2011 } else {
2012 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2013 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2014 }
2015 } else {
2016 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002017 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002018
2019 if (!TLI.isLittleEndian())
2020 std::swap(Lo, Hi);
2021 }
2022
2023 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002024 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002025
2026 if (Hi.Val == NULL) {
2027 // Must be int <-> float one-to-one expansion.
2028 Result = Lo;
2029 break;
2030 }
2031
Evan Cheng8b2794a2006-10-13 21:14:26 +00002032 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2033 getIntPtrConstant(IncrementSize));
2034 assert(isTypeLegal(Tmp2.getValueType()) &&
2035 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002036 SVOffset += IncrementSize;
2037 if (Alignment > IncrementSize)
2038 Alignment = IncrementSize;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002039 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002040 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002041 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2042 break;
2043 }
2044 } else {
2045 // Truncating store
2046 assert(isTypeLegal(ST->getValue().getValueType()) &&
2047 "Cannot handle illegal TRUNCSTORE yet!");
2048 Tmp3 = LegalizeOp(ST->getValue());
2049
2050 // The only promote case we handle is TRUNCSTORE:i1 X into
2051 // -> TRUNCSTORE:i8 (and X, 1)
2052 if (ST->getStoredVT() == MVT::i1 &&
2053 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2054 // Promote the bool to a mask then store.
2055 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2056 DAG.getConstant(1, Tmp3.getValueType()));
2057 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002058 SVOffset, MVT::i8,
2059 isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002060 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2061 Tmp2 != ST->getBasePtr()) {
2062 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2063 ST->getOffset());
2064 }
2065
2066 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2067 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002068 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002069 case TargetLowering::Legal:
2070 // If this is an unaligned store and the target doesn't support it,
2071 // expand it.
2072 if (!TLI.allowsUnalignedMemoryAccesses()) {
2073 unsigned ABIAlignment = TLI.getTargetData()->
2074 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2075 if (ST->getAlignment() < ABIAlignment)
2076 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2077 TLI);
2078 }
2079 break;
Chris Lattner456a93a2006-01-28 07:39:30 +00002080 case TargetLowering::Custom:
2081 Tmp1 = TLI.LowerOperation(Result, DAG);
2082 if (Tmp1.Val) Result = Tmp1;
2083 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002084 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002085 }
2086 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002087 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002088 case ISD::PCMARKER:
2089 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002090 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002091 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002092 case ISD::STACKSAVE:
2093 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002094 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2095 Tmp1 = Result.getValue(0);
2096 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002097
Chris Lattner140d53c2006-01-13 02:50:02 +00002098 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2099 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002100 case TargetLowering::Legal: break;
2101 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002102 Tmp3 = TLI.LowerOperation(Result, DAG);
2103 if (Tmp3.Val) {
2104 Tmp1 = LegalizeOp(Tmp3);
2105 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002106 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002107 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002108 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002109 // Expand to CopyFromReg if the target set
2110 // StackPointerRegisterToSaveRestore.
2111 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002112 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002113 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002114 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002115 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002116 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2117 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002118 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002119 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002120 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002121
2122 // Since stacksave produce two values, make sure to remember that we
2123 // legalized both of them.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002124 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2125 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2126 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002127
Chris Lattner140d53c2006-01-13 02:50:02 +00002128 case ISD::STACKRESTORE:
2129 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2130 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002131 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002132
2133 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2134 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002135 case TargetLowering::Legal: break;
2136 case TargetLowering::Custom:
2137 Tmp1 = TLI.LowerOperation(Result, DAG);
2138 if (Tmp1.Val) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002139 break;
2140 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002141 // Expand to CopyToReg if the target set
2142 // StackPointerRegisterToSaveRestore.
2143 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2144 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2145 } else {
2146 Result = Tmp1;
2147 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002148 break;
2149 }
2150 break;
2151
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002152 case ISD::READCYCLECOUNTER:
2153 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002154 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002155 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2156 Node->getValueType(0))) {
2157 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002158 case TargetLowering::Legal:
2159 Tmp1 = Result.getValue(0);
2160 Tmp2 = Result.getValue(1);
2161 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002162 case TargetLowering::Custom:
2163 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002164 Tmp1 = LegalizeOp(Result.getValue(0));
2165 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002166 break;
2167 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002168
2169 // Since rdcc produce two values, make sure to remember that we legalized
2170 // both of them.
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002171 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2172 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002173 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002174
Chris Lattner2ee743f2005-01-14 22:08:15 +00002175 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002176 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2177 case Expand: assert(0 && "It's impossible to expand bools");
2178 case Legal:
2179 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2180 break;
2181 case Promote:
2182 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002183 // Make sure the condition is either zero or one.
Dan Gohmanea859be2007-06-22 14:59:07 +00002184 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattnerb6c80602006-11-28 01:03:30 +00002185 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2186 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002187 break;
2188 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002189 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002190 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002191
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002192 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002193
Nate Begemanb942a3d2005-08-23 04:29:48 +00002194 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002195 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002196 case TargetLowering::Legal: break;
2197 case TargetLowering::Custom: {
2198 Tmp1 = TLI.LowerOperation(Result, DAG);
2199 if (Tmp1.Val) Result = Tmp1;
2200 break;
2201 }
Nate Begeman9373a812005-08-10 20:51:12 +00002202 case TargetLowering::Expand:
2203 if (Tmp1.getOpcode() == ISD::SETCC) {
2204 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2205 Tmp2, Tmp3,
2206 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2207 } else {
2208 Result = DAG.getSelectCC(Tmp1,
2209 DAG.getConstant(0, Tmp1.getValueType()),
2210 Tmp2, Tmp3, ISD::SETNE);
2211 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002212 break;
2213 case TargetLowering::Promote: {
2214 MVT::ValueType NVT =
2215 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2216 unsigned ExtOp, TruncOp;
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002217 if (MVT::isVector(Tmp2.getValueType())) {
2218 ExtOp = ISD::BIT_CONVERT;
2219 TruncOp = ISD::BIT_CONVERT;
2220 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002221 ExtOp = ISD::ANY_EXTEND;
2222 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002223 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002224 ExtOp = ISD::FP_EXTEND;
2225 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002226 }
2227 // Promote each of the values to the new type.
2228 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2229 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2230 // Perform the larger operation, then round down.
2231 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2232 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2233 break;
2234 }
2235 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002236 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002237 case ISD::SELECT_CC: {
2238 Tmp1 = Node->getOperand(0); // LHS
2239 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002240 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2241 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman750ac1b2006-02-01 07:19:44 +00002242 SDOperand CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002243
Nate Begeman750ac1b2006-02-01 07:19:44 +00002244 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2245
2246 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2247 // the LHS is a legal SETCC itself. In this case, we need to compare
2248 // the result against zero to select between true and false values.
2249 if (Tmp2.Val == 0) {
2250 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2251 CC = DAG.getCondCode(ISD::SETNE);
2252 }
2253 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2254
2255 // Everything is legal, see if we should expand this op or something.
2256 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2257 default: assert(0 && "This action is not supported yet!");
2258 case TargetLowering::Legal: break;
2259 case TargetLowering::Custom:
2260 Tmp1 = TLI.LowerOperation(Result, DAG);
2261 if (Tmp1.Val) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002262 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002263 }
2264 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002265 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002266 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002267 Tmp1 = Node->getOperand(0);
2268 Tmp2 = Node->getOperand(1);
2269 Tmp3 = Node->getOperand(2);
2270 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2271
2272 // If we had to Expand the SetCC operands into a SELECT node, then it may
2273 // not always be possible to return a true LHS & RHS. In this case, just
2274 // return the value we legalized, returned in the LHS
2275 if (Tmp2.Val == 0) {
2276 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002277 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002278 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002279
Chris Lattner73e142f2006-01-30 22:43:50 +00002280 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002281 default: assert(0 && "Cannot handle this action for SETCC yet!");
2282 case TargetLowering::Custom:
2283 isCustom = true;
2284 // FALLTHROUGH.
2285 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002286 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002287 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002288 Tmp4 = TLI.LowerOperation(Result, DAG);
2289 if (Tmp4.Val) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002290 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002291 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002292 case TargetLowering::Promote: {
2293 // First step, figure out the appropriate operation to use.
2294 // Allow SETCC to not be supported for all legal data types
2295 // Mostly this targets FP
2296 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattner00755df2007-02-04 00:27:56 +00002297 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002298
2299 // Scan for the appropriate larger type to use.
2300 while (1) {
2301 NewInTy = (MVT::ValueType)(NewInTy+1);
2302
2303 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2304 "Fell off of the edge of the integer world");
2305 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2306 "Fell off of the edge of the floating point world");
2307
2308 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002309 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002310 break;
2311 }
2312 if (MVT::isInteger(NewInTy))
2313 assert(0 && "Cannot promote Legal Integer SETCC yet");
2314 else {
2315 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2316 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2317 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002318 Tmp1 = LegalizeOp(Tmp1);
2319 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002321 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002322 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002323 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002324 case TargetLowering::Expand:
2325 // Expand a setcc node into a select_cc of the same condition, lhs, and
2326 // rhs that selects between const 1 (true) and const 0 (false).
2327 MVT::ValueType VT = Node->getValueType(0);
2328 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2329 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002330 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002331 break;
2332 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002333 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002334 case ISD::MEMSET:
2335 case ISD::MEMCPY:
2336 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002337 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00002338 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2339
2340 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2341 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2342 case Expand: assert(0 && "Cannot expand a byte!");
2343 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002344 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002345 break;
2346 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00002347 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00002348 break;
2349 }
2350 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002351 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00002352 }
Chris Lattner272455b2005-02-02 03:44:41 +00002353
2354 SDOperand Tmp4;
2355 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00002356 case Expand: {
2357 // Length is too big, just take the lo-part of the length.
2358 SDOperand HiPart;
Chris Lattnerfa9aa2b2006-11-07 04:11:44 +00002359 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattner6814f152005-07-13 01:42:45 +00002360 break;
2361 }
Chris Lattnere5605212005-01-28 22:29:18 +00002362 case Legal:
2363 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00002364 break;
2365 case Promote:
2366 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00002367 break;
2368 }
2369
2370 SDOperand Tmp5;
2371 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2372 case Expand: assert(0 && "Cannot expand this yet!");
2373 case Legal:
2374 Tmp5 = LegalizeOp(Node->getOperand(4));
2375 break;
2376 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00002377 Tmp5 = PromoteOp(Node->getOperand(4));
2378 break;
2379 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002380
2381 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2382 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002383 case TargetLowering::Custom:
2384 isCustom = true;
2385 // FALLTHROUGH
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002386 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002387 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner456a93a2006-01-28 07:39:30 +00002388 if (isCustom) {
2389 Tmp1 = TLI.LowerOperation(Result, DAG);
2390 if (Tmp1.Val) Result = Tmp1;
2391 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002392 break;
2393 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00002394 // Otherwise, the target does not support this operation. Lower the
2395 // operation to an explicit libcall as appropriate.
2396 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00002397 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00002398 TargetLowering::ArgListTy Args;
2399 TargetLowering::ArgListEntry Entry;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002400
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00002401 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00002402 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002403 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00002404 Args.push_back(Entry);
Chris Lattnerdca7abe2006-02-20 06:38:35 +00002405 // Extend the (previously legalized) ubyte argument to be an int value
2406 // for the call.
2407 if (Tmp3.getValueType() > MVT::i32)
2408 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2409 else
2410 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002411 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencer47857812006-12-31 05:55:36 +00002412 Args.push_back(Entry);
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00002413 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencer47857812006-12-31 05:55:36 +00002414 Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002415
2416 FnName = "memset";
2417 } else if (Node->getOpcode() == ISD::MEMCPY ||
2418 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikovb25fe822007-02-01 08:39:52 +00002419 Entry.Ty = IntPtrTy;
Reid Spencerb47b25c2007-01-03 04:22:32 +00002420 Entry.Node = Tmp2; Args.push_back(Entry);
2421 Entry.Node = Tmp3; Args.push_back(Entry);
2422 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattnere1bd8222005-01-11 05:57:22 +00002423 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2424 } else {
2425 assert(0 && "Unknown op!");
2426 }
Chris Lattner45982da2005-05-12 16:53:42 +00002427
Chris Lattnere1bd8222005-01-11 05:57:22 +00002428 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00002429 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00002430 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner456a93a2006-01-28 07:39:30 +00002431 Result = CallResult.second;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002432 break;
2433 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00002434 }
2435 break;
2436 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00002437
Chris Lattner5b359c62005-04-02 04:00:59 +00002438 case ISD::SHL_PARTS:
2439 case ISD::SRA_PARTS:
2440 case ISD::SRL_PARTS: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002441 SmallVector<SDOperand, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00002442 bool Changed = false;
2443 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2444 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2445 Changed |= Ops.back() != Node->getOperand(i);
2446 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002447 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002448 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00002449
Evan Cheng05a2d562006-01-09 18:31:59 +00002450 switch (TLI.getOperationAction(Node->getOpcode(),
2451 Node->getValueType(0))) {
2452 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002453 case TargetLowering::Legal: break;
2454 case TargetLowering::Custom:
2455 Tmp1 = TLI.LowerOperation(Result, DAG);
2456 if (Tmp1.Val) {
2457 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00002458 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002459 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng05a2d562006-01-09 18:31:59 +00002460 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2461 if (i == Op.ResNo)
Evan Cheng12f22742006-01-19 04:54:52 +00002462 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00002463 }
Chris Lattner269f8c02006-01-10 19:43:26 +00002464 assert(RetVal.Val && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00002465 return RetVal;
2466 }
Evan Cheng05a2d562006-01-09 18:31:59 +00002467 break;
2468 }
2469
Chris Lattner2c8086f2005-04-02 05:00:07 +00002470 // Since these produce multiple values, make sure to remember that we
2471 // legalized all of them.
2472 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2473 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2474 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00002475 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002476
2477 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00002478 case ISD::ADD:
2479 case ISD::SUB:
2480 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00002481 case ISD::MULHS:
2482 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002483 case ISD::UDIV:
2484 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002485 case ISD::AND:
2486 case ISD::OR:
2487 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00002488 case ISD::SHL:
2489 case ISD::SRL:
2490 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00002491 case ISD::FADD:
2492 case ISD::FSUB:
2493 case ISD::FMUL:
2494 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002495 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00002496 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2497 case Expand: assert(0 && "Not possible");
2498 case Legal:
2499 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2500 break;
2501 case Promote:
2502 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2503 break;
2504 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002505
2506 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002507
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002508 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002509 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00002510 case TargetLowering::Legal: break;
2511 case TargetLowering::Custom:
2512 Tmp1 = TLI.LowerOperation(Result, DAG);
2513 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth57030e32005-12-25 01:07:37 +00002514 break;
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002515 case TargetLowering::Expand: {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002516 if (Node->getValueType(0) == MVT::i32) {
2517 switch (Node->getOpcode()) {
2518 default: assert(0 && "Do not know how to expand this integer BinOp!");
2519 case ISD::UDIV:
2520 case ISD::SDIV:
Evan Cheng56966222007-01-12 02:11:51 +00002521 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2522 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002523 SDOperand Dummy;
Reid Spencer47857812006-12-31 05:55:36 +00002524 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng56966222007-01-12 02:11:51 +00002525 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002526 };
2527 break;
2528 }
2529
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002530 assert(MVT::isVector(Node->getValueType(0)) &&
2531 "Cannot expand this binary operator!");
2532 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002533 SmallVector<SDOperand, 8> Ops;
Dan Gohman51eaa862007-06-14 22:58:02 +00002534 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002535 MVT::ValueType PtrVT = TLI.getPointerTy();
2536 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2537 i != e; ++i) {
2538 SDOperand Idx = DAG.getConstant(i, PtrVT);
2539 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2540 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2541 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2542 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002543 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2544 &Ops[0], Ops.size());
Chris Lattnerbc70cf82006-04-02 03:57:31 +00002545 break;
2546 }
Evan Chengcc987612006-04-12 21:20:24 +00002547 case TargetLowering::Promote: {
2548 switch (Node->getOpcode()) {
2549 default: assert(0 && "Do not know how to promote this BinOp!");
2550 case ISD::AND:
2551 case ISD::OR:
2552 case ISD::XOR: {
2553 MVT::ValueType OVT = Node->getValueType(0);
2554 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2555 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2556 // Bit convert each of the values to the new type.
2557 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2558 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2559 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2560 // Bit convert the result back the original type.
2561 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2562 break;
2563 }
2564 }
2565 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00002566 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002567 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002568
2569 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2570 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2571 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2572 case Expand: assert(0 && "Not possible");
2573 case Legal:
2574 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2575 break;
2576 case Promote:
2577 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2578 break;
2579 }
2580
2581 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2582
2583 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2584 default: assert(0 && "Operation not supported");
2585 case TargetLowering::Custom:
2586 Tmp1 = TLI.LowerOperation(Result, DAG);
2587 if (Tmp1.Val) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00002588 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002589 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00002590 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00002591 // If this target supports fabs/fneg natively and select is cheap,
2592 // do this efficiently.
2593 if (!TLI.isSelectExpensive() &&
2594 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2595 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00002596 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00002597 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00002598 // Get the sign bit of the RHS.
2599 MVT::ValueType IVT =
2600 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2601 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2602 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2603 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2604 // Get the absolute value of the result.
2605 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2606 // Select between the nabs and abs value based on the sign bit of
2607 // the input.
2608 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2609 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2610 AbsVal),
2611 AbsVal);
2612 Result = LegalizeOp(Result);
2613 break;
2614 }
2615
2616 // Otherwise, do bitwise ops!
Evan Cheng912095b2007-01-04 21:56:39 +00002617 MVT::ValueType NVT =
2618 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2619 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2620 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2621 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00002622 break;
2623 }
Evan Cheng912095b2007-01-04 21:56:39 +00002624 }
Chris Lattnera09f8482006-03-05 05:09:38 +00002625 break;
2626
Nate Begeman551bf3f2006-02-17 05:43:56 +00002627 case ISD::ADDC:
2628 case ISD::SUBC:
2629 Tmp1 = LegalizeOp(Node->getOperand(0));
2630 Tmp2 = LegalizeOp(Node->getOperand(1));
2631 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2632 // Since this produces two values, make sure to remember that we legalized
2633 // both of them.
2634 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2635 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2636 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002637
Nate Begeman551bf3f2006-02-17 05:43:56 +00002638 case ISD::ADDE:
2639 case ISD::SUBE:
2640 Tmp1 = LegalizeOp(Node->getOperand(0));
2641 Tmp2 = LegalizeOp(Node->getOperand(1));
2642 Tmp3 = LegalizeOp(Node->getOperand(2));
2643 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2644 // Since this produces two values, make sure to remember that we legalized
2645 // both of them.
2646 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2647 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2648 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00002649
Nate Begeman419f8b62005-10-18 00:27:41 +00002650 case ISD::BUILD_PAIR: {
2651 MVT::ValueType PairTy = Node->getValueType(0);
2652 // TODO: handle the case where the Lo and Hi operands are not of legal type
2653 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2654 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2655 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002656 case TargetLowering::Promote:
2657 case TargetLowering::Custom:
2658 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00002659 case TargetLowering::Legal:
2660 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2661 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2662 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00002663 case TargetLowering::Expand:
2664 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2665 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2666 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2667 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2668 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00002669 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00002670 break;
2671 }
2672 break;
2673 }
2674
Nate Begemanc105e192005-04-06 00:23:54 +00002675 case ISD::UREM:
2676 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002677 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00002678 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2679 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00002680
Nate Begemanc105e192005-04-06 00:23:54 +00002681 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002682 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2683 case TargetLowering::Custom:
2684 isCustom = true;
2685 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00002686 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002687 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00002688 if (isCustom) {
2689 Tmp1 = TLI.LowerOperation(Result, DAG);
2690 if (Tmp1.Val) Result = Tmp1;
2691 }
Nate Begemanc105e192005-04-06 00:23:54 +00002692 break;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002693 case TargetLowering::Expand:
Evan Cheng6b5578f2006-09-18 23:28:33 +00002694 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00002695 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002696 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002697 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2698 TargetLowering::Legal) {
2699 // X % Y -> X-X/Y*Y
2700 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng6b5578f2006-09-18 23:28:33 +00002701 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002702 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2703 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2704 } else {
2705 assert(Node->getValueType(0) == MVT::i32 &&
2706 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00002707 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2708 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002709 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002710 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00002711 }
Chris Lattner4c64dd72005-08-03 20:31:37 +00002712 } else {
2713 // Floating point mod -> fmod libcall.
Evan Cheng56966222007-01-12 02:11:51 +00002714 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2715 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner4c64dd72005-08-03 20:31:37 +00002716 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002717 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2718 false/*sign irrelevant*/, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00002719 }
2720 break;
2721 }
2722 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00002723 case ISD::VAARG: {
2724 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2725 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2726
Chris Lattner5c62f332006-01-28 07:42:08 +00002727 MVT::ValueType VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00002728 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2729 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002730 case TargetLowering::Custom:
2731 isCustom = true;
2732 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002733 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002734 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2735 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002736 Tmp1 = Result.getValue(1);
2737
2738 if (isCustom) {
2739 Tmp2 = TLI.LowerOperation(Result, DAG);
2740 if (Tmp2.Val) {
2741 Result = LegalizeOp(Tmp2);
2742 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2743 }
2744 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002745 break;
2746 case TargetLowering::Expand: {
Evan Cheng466685d2006-10-09 20:57:25 +00002747 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemanacc398c2006-01-25 18:21:52 +00002748 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00002749 SV->getValue(), SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002750 // Increment the pointer, VAList, to the next vaarg
2751 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2752 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2753 TLI.getPointerTy()));
2754 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00002755 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2756 SV->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002757 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00002758 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00002759 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00002760 Result = LegalizeOp(Result);
2761 break;
2762 }
2763 }
2764 // Since VAARG produces two values, make sure to remember that we
2765 // legalized both of them.
2766 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner456a93a2006-01-28 07:39:30 +00002767 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2768 return Op.ResNo ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00002769 }
2770
2771 case ISD::VACOPY:
2772 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2773 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2774 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2775
2776 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2777 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002778 case TargetLowering::Custom:
2779 isCustom = true;
2780 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002781 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002782 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2783 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002784 if (isCustom) {
2785 Tmp1 = TLI.LowerOperation(Result, DAG);
2786 if (Tmp1.Val) Result = Tmp1;
2787 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002788 break;
2789 case TargetLowering::Expand:
2790 // This defaults to loading a pointer from the input and storing it to the
2791 // output, returning the chain.
Evan Cheng466685d2006-10-09 20:57:25 +00002792 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002793 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Cheng466685d2006-10-09 20:57:25 +00002794 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2795 SVD->getOffset());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002796 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2797 SVS->getOffset());
Nate Begemanacc398c2006-01-25 18:21:52 +00002798 break;
2799 }
2800 break;
2801
2802 case ISD::VAEND:
2803 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2804 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2805
2806 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2807 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002808 case TargetLowering::Custom:
2809 isCustom = true;
2810 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00002811 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002812 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00002813 if (isCustom) {
2814 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2815 if (Tmp1.Val) Result = Tmp1;
2816 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002817 break;
2818 case TargetLowering::Expand:
2819 Result = Tmp1; // Default to a no-op, return the chain
2820 break;
2821 }
2822 break;
2823
2824 case ISD::VASTART:
2825 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2826 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2827
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002828 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2829
Nate Begemanacc398c2006-01-25 18:21:52 +00002830 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2831 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002832 case TargetLowering::Legal: break;
2833 case TargetLowering::Custom:
2834 Tmp1 = TLI.LowerOperation(Result, DAG);
2835 if (Tmp1.Val) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00002836 break;
2837 }
2838 break;
2839
Nate Begeman35ef9132006-01-11 21:21:00 +00002840 case ISD::ROTL:
2841 case ISD::ROTR:
2842 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2843 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002844 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00002845 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2846 default:
2847 assert(0 && "ROTL/ROTR legalize operation not supported");
2848 break;
2849 case TargetLowering::Legal:
2850 break;
2851 case TargetLowering::Custom:
2852 Tmp1 = TLI.LowerOperation(Result, DAG);
2853 if (Tmp1.Val) Result = Tmp1;
2854 break;
2855 case TargetLowering::Promote:
2856 assert(0 && "Do not know how to promote ROTL/ROTR");
2857 break;
2858 case TargetLowering::Expand:
2859 assert(0 && "Do not know how to expand ROTL/ROTR");
2860 break;
2861 }
Nate Begeman35ef9132006-01-11 21:21:00 +00002862 break;
2863
Nate Begemand88fc032006-01-14 03:14:10 +00002864 case ISD::BSWAP:
2865 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2866 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002867 case TargetLowering::Custom:
2868 assert(0 && "Cannot custom legalize this yet!");
2869 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002870 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002871 break;
2872 case TargetLowering::Promote: {
2873 MVT::ValueType OVT = Tmp1.getValueType();
2874 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohmanb55757e2007-05-18 17:52:13 +00002875 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begemand88fc032006-01-14 03:14:10 +00002876
Chris Lattner456a93a2006-01-28 07:39:30 +00002877 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2878 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2879 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2880 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2881 break;
2882 }
2883 case TargetLowering::Expand:
2884 Result = ExpandBSWAP(Tmp1);
2885 break;
Nate Begemand88fc032006-01-14 03:14:10 +00002886 }
2887 break;
2888
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002889 case ISD::CTPOP:
2890 case ISD::CTTZ:
2891 case ISD::CTLZ:
2892 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2893 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00002894 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002895 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002896 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00002897 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00002898 TargetLowering::Custom) {
2899 Tmp1 = TLI.LowerOperation(Result, DAG);
2900 if (Tmp1.Val) {
2901 Result = Tmp1;
2902 }
Scott Michel910b66d2007-07-30 21:00:31 +00002903 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002904 break;
2905 case TargetLowering::Promote: {
2906 MVT::ValueType OVT = Tmp1.getValueType();
2907 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00002908
2909 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002910 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2911 // Perform the larger operation, then subtract if needed.
2912 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002913 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002914 case ISD::CTPOP:
2915 Result = Tmp1;
2916 break;
2917 case ISD::CTTZ:
2918 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002919 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002920 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002921 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002922 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel910b66d2007-07-30 21:00:31 +00002923 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002924 break;
2925 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00002926 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002927 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00002928 DAG.getConstant(MVT::getSizeInBits(NVT) -
2929 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002930 break;
2931 }
2932 break;
2933 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002934 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002935 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00002936 break;
2937 }
2938 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00002939
Chris Lattner2c8086f2005-04-02 05:00:07 +00002940 // Unary operators
2941 case ISD::FABS:
2942 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00002943 case ISD::FSQRT:
2944 case ISD::FSIN:
2945 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002946 Tmp1 = LegalizeOp(Node->getOperand(0));
2947 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002948 case TargetLowering::Promote:
2949 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00002950 isCustom = true;
2951 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00002952 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002953 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00002954 if (isCustom) {
2955 Tmp1 = TLI.LowerOperation(Result, DAG);
2956 if (Tmp1.Val) Result = Tmp1;
2957 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002958 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00002959 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00002960 switch (Node->getOpcode()) {
2961 default: assert(0 && "Unreachable!");
2962 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002963 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2964 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00002965 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002966 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002967 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002968 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2969 MVT::ValueType VT = Node->getValueType(0);
2970 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002971 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00002972 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2973 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002974 break;
2975 }
2976 case ISD::FSQRT:
2977 case ISD::FSIN:
2978 case ISD::FCOS: {
2979 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng56966222007-01-12 02:11:51 +00002980 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002981 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00002982 case ISD::FSQRT:
2983 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2984 break;
2985 case ISD::FSIN:
2986 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2987 break;
2988 case ISD::FCOS:
2989 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2990 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002991 default: assert(0 && "Unreachable!");
2992 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00002993 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00002994 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2995 false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00002996 break;
2997 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00002998 }
2999 break;
3000 }
3001 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003002 case ISD::FPOWI: {
3003 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng56966222007-01-12 02:11:51 +00003004 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
3005 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003006 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003007 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3008 false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003009 break;
3010 }
Chris Lattner35481892005-12-23 00:16:34 +00003011 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003012 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner35481892005-12-23 00:16:34 +00003013 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohman7f321562007-06-25 16:23:39 +00003014 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3015 // The input has to be a vector type, we have to either scalarize it, pack
3016 // it, or convert it based on whether the input vector type is legal.
3017 SDNode *InVal = Node->getOperand(0).Val;
3018 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
3019 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
3020
3021 // Figure out if there is a simple type corresponding to this Vector
3022 // type. If so, convert to the vector type.
3023 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3024 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003025 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003026 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3027 LegalizeOp(Node->getOperand(0)));
3028 break;
3029 } else if (NumElems == 1) {
3030 // Turn this into a bit convert of the scalar input.
3031 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3032 ScalarizeVectorOp(Node->getOperand(0)));
3033 break;
3034 } else {
3035 // FIXME: UNIMP! Store then reload
3036 assert(0 && "Cast from unsupported vector type not implemented yet!");
3037 }
Chris Lattner67993f72006-01-23 07:30:46 +00003038 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003039 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3040 Node->getOperand(0).getValueType())) {
3041 default: assert(0 && "Unknown operation action!");
3042 case TargetLowering::Expand:
3043 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3044 break;
3045 case TargetLowering::Legal:
3046 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003047 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003048 break;
3049 }
3050 }
3051 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003052
Chris Lattner2c8086f2005-04-02 05:00:07 +00003053 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003054 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003055 case ISD::UINT_TO_FP: {
3056 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003057 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3058 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003059 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003060 Node->getOperand(0).getValueType())) {
3061 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003062 case TargetLowering::Custom:
3063 isCustom = true;
3064 // FALLTHROUGH
3065 case TargetLowering::Legal:
3066 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003067 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003068 if (isCustom) {
3069 Tmp1 = TLI.LowerOperation(Result, DAG);
3070 if (Tmp1.Val) Result = Tmp1;
3071 }
3072 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003073 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00003074 Result = ExpandLegalINT_TO_FP(isSigned,
3075 LegalizeOp(Node->getOperand(0)),
3076 Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003077 break;
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003078 case TargetLowering::Promote:
3079 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3080 Node->getValueType(0),
3081 isSigned);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003082 break;
Andrew Lenharth5b5b8c22005-11-30 06:43:03 +00003083 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003084 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00003085 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003086 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3087 Node->getValueType(0), Node->getOperand(0));
3088 break;
3089 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003090 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003091 if (isSigned) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003092 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3093 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003094 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003095 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3096 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003097 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003098 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3099 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003100 break;
3101 }
3102 break;
3103 }
3104 case ISD::TRUNCATE:
3105 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3106 case Legal:
3107 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003108 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003109 break;
3110 case Expand:
3111 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3112
3113 // Since the result is legal, we should just be able to truncate the low
3114 // part of the source.
3115 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3116 break;
3117 case Promote:
3118 Result = PromoteOp(Node->getOperand(0));
3119 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3120 break;
3121 }
3122 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003123
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003124 case ISD::FP_TO_SINT:
3125 case ISD::FP_TO_UINT:
3126 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3127 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003128 Tmp1 = LegalizeOp(Node->getOperand(0));
3129
Chris Lattner1618beb2005-07-29 00:11:56 +00003130 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3131 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003132 case TargetLowering::Custom:
3133 isCustom = true;
3134 // FALLTHROUGH
3135 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003136 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003137 if (isCustom) {
3138 Tmp1 = TLI.LowerOperation(Result, DAG);
3139 if (Tmp1.Val) Result = Tmp1;
3140 }
3141 break;
3142 case TargetLowering::Promote:
3143 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3144 Node->getOpcode() == ISD::FP_TO_SINT);
3145 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003146 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003147 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3148 SDOperand True, False;
3149 MVT::ValueType VT = Node->getOperand(0).getValueType();
3150 MVT::ValueType NVT = Node->getValueType(0);
3151 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
3152 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
3153 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3154 Node->getOperand(0), Tmp2, ISD::SETLT);
3155 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3156 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003157 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003158 Tmp2));
3159 False = DAG.getNode(ISD::XOR, NVT, False,
3160 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003161 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3162 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003163 } else {
3164 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3165 }
3166 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003167 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003168 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003169 case Expand: {
3170 // Convert f32 / f64 to i32 / i64.
3171 MVT::ValueType VT = Op.getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00003172 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng6af00d52006-12-13 01:57:55 +00003173 switch (Node->getOpcode()) {
3174 case ISD::FP_TO_SINT:
3175 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003176 LC = (VT == MVT::i32)
3177 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003178 else
Evan Cheng56966222007-01-12 02:11:51 +00003179 LC = (VT == MVT::i32)
3180 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003181 break;
3182 case ISD::FP_TO_UINT:
3183 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00003184 LC = (VT == MVT::i32)
3185 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003186 else
Evan Cheng56966222007-01-12 02:11:51 +00003187 LC = (VT == MVT::i32)
3188 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng6af00d52006-12-13 01:57:55 +00003189 break;
3190 default: assert(0 && "Unreachable!");
3191 }
3192 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00003193 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3194 false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003195 break;
3196 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003197 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003198 Tmp1 = PromoteOp(Node->getOperand(0));
3199 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3200 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003201 break;
3202 }
3203 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003204
Dale Johannesenab081c72007-08-09 17:27:48 +00003205 case ISD::FP_EXTEND:
Dale Johannesen5411a392007-08-09 01:04:01 +00003206 case ISD::FP_ROUND: {
3207 MVT::ValueType newVT = Op.getValueType();
3208 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3209 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesenab081c72007-08-09 17:27:48 +00003210 // The only way we can lower this is to turn it into a STORE,
Dale Johannesen5411a392007-08-09 01:04:01 +00003211 // LOAD pair, targetting a temporary location (a stack slot).
3212
3213 // NOTE: there is a choice here between constantly creating new stack
3214 // slots and always reusing the same one. We currently always create
3215 // new ones, as reuse may inhibit scheduling.
Dale Johannesenab081c72007-08-09 17:27:48 +00003216 MVT::ValueType slotVT =
3217 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3218 const Type *Ty = MVT::getTypeForValueType(slotVT);
Dale Johannesen5411a392007-08-09 01:04:01 +00003219 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3220 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3221 MachineFunction &MF = DAG.getMachineFunction();
3222 int SSFI =
3223 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3224 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesenab081c72007-08-09 17:27:48 +00003225 if (Node->getOpcode() == ISD::FP_EXTEND) {
3226 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3227 StackSlot, NULL, 0);
3228 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3229 Result, StackSlot, NULL, 0, oldVT);
3230 } else {
3231 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3232 StackSlot, NULL, 0, newVT);
3233 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0, newVT);
3234 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003235 break;
3236 }
Dale Johannesen849f2142007-07-03 00:53:03 +00003237 }
3238 // FALL THROUGH
Chris Lattner13c78e22005-09-02 00:18:10 +00003239 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003240 case ISD::ZERO_EXTEND:
3241 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003242 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003243 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003244 case Legal:
3245 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003246 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003247 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003248 case Promote:
3249 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00003250 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003251 Tmp1 = PromoteOp(Node->getOperand(0));
3252 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00003253 break;
Chris Lattner1713e732005-01-16 00:38:00 +00003254 case ISD::ZERO_EXTEND:
3255 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003256 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00003257 Result = DAG.getZeroExtendInReg(Result,
3258 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00003259 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003260 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003261 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00003262 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00003263 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003264 Result,
3265 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00003266 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003267 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00003268 Result = PromoteOp(Node->getOperand(0));
3269 if (Result.getValueType() != Op.getValueType())
3270 // Dynamically dead while we have only 2 FP types.
3271 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3272 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003273 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00003274 Result = PromoteOp(Node->getOperand(0));
3275 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3276 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003277 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003278 }
3279 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00003280 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00003281 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00003282 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003283 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00003284
3285 // If this operation is not supported, convert it to a shl/shr or load/store
3286 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003287 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3288 default: assert(0 && "This action not supported for this op yet!");
3289 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003290 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003291 break;
3292 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00003293 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00003294 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00003295 // NOTE: we could fall back on load/store here too for targets without
3296 // SAR. However, it is doubtful that any exist.
3297 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3298 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00003299 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00003300 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3301 Node->getOperand(0), ShiftCst);
3302 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3303 Result, ShiftCst);
3304 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00003305 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00003306 // EXTLOAD pair, targetting a temporary location (a stack slot).
3307
3308 // NOTE: there is a choice here between constantly creating new stack
3309 // slots and always reusing the same one. We currently always create
3310 // new ones, as reuse may inhibit scheduling.
3311 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner44b2c502007-04-28 06:42:38 +00003312 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00003313 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003314 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00003315 int SSFI =
Chris Lattner44b2c502007-04-28 06:42:38 +00003316 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003317 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003318 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3319 StackSlot, NULL, 0, ExtraVT);
Chris Lattner5f056bf2005-07-10 01:55:33 +00003320 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Cheng466685d2006-10-09 20:57:25 +00003321 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00003322 } else {
3323 assert(0 && "Unknown op");
3324 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003325 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00003326 }
Chris Lattner0f69b292005-01-15 06:18:18 +00003327 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003328 }
Duncan Sands36397f52007-07-27 12:58:54 +00003329 case ISD::ADJUST_TRAMP: {
3330 Tmp1 = LegalizeOp(Node->getOperand(0));
3331 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3332 default: assert(0 && "This action is not supported yet!");
3333 case TargetLowering::Custom:
3334 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3335 Result = TLI.LowerOperation(Result, DAG);
3336 if (Result.Val) break;
3337 // FALL THROUGH
3338 case TargetLowering::Expand:
3339 Result = Tmp1;
3340 break;
3341 }
3342 break;
3343 }
3344 case ISD::TRAMPOLINE: {
3345 SDOperand Ops[6];
3346 for (unsigned i = 0; i != 6; ++i)
3347 Ops[i] = LegalizeOp(Node->getOperand(i));
3348 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3349 // The only option for this node is to custom lower it.
3350 Result = TLI.LowerOperation(Result, DAG);
3351 assert(Result.Val && "Should always custom lower!");
3352 break;
3353 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00003354 }
Chris Lattner456a93a2006-01-28 07:39:30 +00003355
Chris Lattner4ddd2832006-04-08 04:13:17 +00003356 assert(Result.getValueType() == Op.getValueType() &&
3357 "Bad legalization!");
3358
Chris Lattner456a93a2006-01-28 07:39:30 +00003359 // Make sure that the generated code is itself legal.
3360 if (Result != Op)
3361 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003362
Chris Lattner45982da2005-05-12 16:53:42 +00003363 // Note that LegalizeOp may be reentered even from single-use nodes, which
3364 // means that we always must cache transformed nodes.
3365 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003366 return Result;
3367}
3368
Chris Lattner8b6fa222005-01-15 22:16:26 +00003369/// PromoteOp - Given an operation that produces a value in an invalid type,
3370/// promote it to compute the value into a larger type. The produced value will
3371/// have the correct bits for the low portion of the register, but no guarantee
3372/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00003373SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3374 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003375 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00003376 assert(getTypeAction(VT) == Promote &&
3377 "Caller should expand or legalize operands that are not promotable!");
3378 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3379 "Cannot promote to smaller type!");
3380
Chris Lattner03c85462005-01-15 05:21:40 +00003381 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner03c85462005-01-15 05:21:40 +00003382 SDOperand Result;
3383 SDNode *Node = Op.Val;
3384
Chris Lattner40030bf2007-02-04 01:17:38 +00003385 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00003386 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00003387
Chris Lattner03c85462005-01-15 05:21:40 +00003388 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003389 case ISD::CopyFromReg:
3390 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00003391 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003392#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00003393 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00003394#endif
Chris Lattner03c85462005-01-15 05:21:40 +00003395 assert(0 && "Do not know how to promote this operator!");
3396 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003397 case ISD::UNDEF:
3398 Result = DAG.getNode(ISD::UNDEF, NVT);
3399 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003400 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00003401 if (VT != MVT::i1)
3402 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3403 else
3404 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00003405 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3406 break;
3407 case ISD::ConstantFP:
3408 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3409 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3410 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00003411
Chris Lattner82fbfb62005-01-18 02:59:52 +00003412 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003413 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003414 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3415 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00003416 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00003417
Chris Lattner03c85462005-01-15 05:21:40 +00003418 case ISD::TRUNCATE:
3419 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3420 case Legal:
3421 Result = LegalizeOp(Node->getOperand(0));
3422 assert(Result.getValueType() >= NVT &&
3423 "This truncation doesn't make sense!");
3424 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3425 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3426 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00003427 case Promote:
3428 // The truncation is not required, because we don't guarantee anything
3429 // about high bits anyway.
3430 Result = PromoteOp(Node->getOperand(0));
3431 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003432 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00003433 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3434 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00003435 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00003436 }
3437 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003438 case ISD::SIGN_EXTEND:
3439 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00003440 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003441 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3442 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3443 case Legal:
3444 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00003445 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003446 break;
3447 case Promote:
3448 // Promote the reg if it's smaller.
3449 Result = PromoteOp(Node->getOperand(0));
3450 // The high bits are not guaranteed to be anything. Insert an extend.
3451 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00003452 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00003453 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00003454 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00003455 Result = DAG.getZeroExtendInReg(Result,
3456 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00003457 break;
3458 }
3459 break;
Chris Lattner35481892005-12-23 00:16:34 +00003460 case ISD::BIT_CONVERT:
3461 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3462 Result = PromoteOp(Result);
3463 break;
3464
Chris Lattner8b6fa222005-01-15 22:16:26 +00003465 case ISD::FP_EXTEND:
3466 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3467 case ISD::FP_ROUND:
3468 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3469 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3470 case Promote: assert(0 && "Unreachable with 2 FP types!");
3471 case Legal:
3472 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner456a93a2006-01-28 07:39:30 +00003473 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner15e4b012005-07-10 00:07:11 +00003474 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003475 break;
3476 }
3477 break;
3478
3479 case ISD::SINT_TO_FP:
3480 case ISD::UINT_TO_FP:
3481 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3482 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00003483 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00003484 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003485 break;
3486
3487 case Promote:
3488 Result = PromoteOp(Node->getOperand(0));
3489 if (Node->getOpcode() == ISD::SINT_TO_FP)
3490 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00003491 Result,
3492 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003493 else
Chris Lattner23993562005-04-13 02:38:47 +00003494 Result = DAG.getZeroExtendInReg(Result,
3495 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00003496 // No extra round required here.
3497 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003498 break;
3499 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00003500 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3501 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00003502 // Round if we cannot tolerate excess precision.
3503 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003504 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3505 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00003506 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003507 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003508 break;
3509
Chris Lattner5e3c5b42005-12-09 17:32:47 +00003510 case ISD::SIGN_EXTEND_INREG:
3511 Result = PromoteOp(Node->getOperand(0));
3512 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3513 Node->getOperand(1));
3514 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003515 case ISD::FP_TO_SINT:
3516 case ISD::FP_TO_UINT:
3517 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3518 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00003519 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003520 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003521 break;
3522 case Promote:
3523 // The input result is prerounded, so we don't have to do anything
3524 // special.
3525 Tmp1 = PromoteOp(Node->getOperand(0));
3526 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003527 }
Nate Begemand2558e32005-08-14 01:20:53 +00003528 // If we're promoting a UINT to a larger size, check to see if the new node
3529 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3530 // we can use that instead. This allows us to generate better code for
3531 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3532 // legal, such as PowerPC.
3533 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003534 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00003535 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3536 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00003537 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3538 } else {
3539 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3540 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00003541 break;
3542
Chris Lattner2c8086f2005-04-02 05:00:07 +00003543 case ISD::FABS:
3544 case ISD::FNEG:
3545 Tmp1 = PromoteOp(Node->getOperand(0));
3546 assert(Tmp1.getValueType() == NVT);
3547 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3548 // NOTE: we do not have to do any extra rounding here for
3549 // NoExcessFPPrecision, because we know the input will have the appropriate
3550 // precision, and these operations don't modify precision at all.
3551 break;
3552
Chris Lattnerda6ba872005-04-28 21:44:33 +00003553 case ISD::FSQRT:
3554 case ISD::FSIN:
3555 case ISD::FCOS:
3556 Tmp1 = PromoteOp(Node->getOperand(0));
3557 assert(Tmp1.getValueType() == NVT);
3558 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003559 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003560 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3561 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00003562 break;
3563
Chris Lattner8b2d42c2007-03-03 23:43:21 +00003564 case ISD::FPOWI: {
3565 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3566 // directly as well, which may be better.
3567 Tmp1 = PromoteOp(Node->getOperand(0));
3568 assert(Tmp1.getValueType() == NVT);
3569 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3570 if (NoExcessFPPrecision)
3571 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3572 DAG.getValueType(VT));
3573 break;
3574 }
3575
Chris Lattner03c85462005-01-15 05:21:40 +00003576 case ISD::AND:
3577 case ISD::OR:
3578 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00003579 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00003580 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00003581 case ISD::MUL:
3582 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00003583 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00003584 // that too is okay if they are integer operations.
3585 Tmp1 = PromoteOp(Node->getOperand(0));
3586 Tmp2 = PromoteOp(Node->getOperand(1));
3587 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3588 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00003589 break;
3590 case ISD::FADD:
3591 case ISD::FSUB:
3592 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00003593 Tmp1 = PromoteOp(Node->getOperand(0));
3594 Tmp2 = PromoteOp(Node->getOperand(1));
3595 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3596 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3597
3598 // Floating point operations will give excess precision that we may not be
3599 // able to tolerate. If we DO allow excess precision, just leave it,
3600 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00003601 // FIXME: Why would we need to round FP ops more than integer ones?
3602 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00003603 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003604 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3605 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00003606 break;
3607
Chris Lattner8b6fa222005-01-15 22:16:26 +00003608 case ISD::SDIV:
3609 case ISD::SREM:
3610 // These operators require that their input be sign extended.
3611 Tmp1 = PromoteOp(Node->getOperand(0));
3612 Tmp2 = PromoteOp(Node->getOperand(1));
3613 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00003614 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3615 DAG.getValueType(VT));
3616 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3617 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003618 }
3619 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3620
3621 // Perform FP_ROUND: this is probably overly pessimistic.
3622 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00003623 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3624 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003625 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003626 case ISD::FDIV:
3627 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00003628 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00003629 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00003630 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3631 case Legal:
3632 Tmp1 = LegalizeOp(Node->getOperand(0));
3633 break;
3634 case Promote:
3635 Tmp1 = PromoteOp(Node->getOperand(0));
3636 break;
3637 case Expand:
3638 assert(0 && "not implemented");
3639 }
3640 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3641 case Legal:
3642 Tmp2 = LegalizeOp(Node->getOperand(1));
3643 break;
3644 case Promote:
3645 Tmp2 = PromoteOp(Node->getOperand(1));
3646 break;
3647 case Expand:
3648 assert(0 && "not implemented");
3649 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003650 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3651
3652 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00003653 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00003654 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3655 DAG.getValueType(VT));
3656 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00003657
3658 case ISD::UDIV:
3659 case ISD::UREM:
3660 // These operators require that their input be zero extended.
3661 Tmp1 = PromoteOp(Node->getOperand(0));
3662 Tmp2 = PromoteOp(Node->getOperand(1));
3663 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00003664 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3665 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00003666 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3667 break;
3668
3669 case ISD::SHL:
3670 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003671 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003672 break;
3673 case ISD::SRA:
3674 // The input value must be properly sign extended.
3675 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00003676 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3677 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003678 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003679 break;
3680 case ISD::SRL:
3681 // The input value must be properly zero extended.
3682 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00003683 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00003684 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00003685 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00003686
3687 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00003688 Tmp1 = Node->getOperand(0); // Get the chain.
3689 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00003690 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3691 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3692 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3693 } else {
Evan Cheng466685d2006-10-09 20:57:25 +00003694 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman0aed7842006-01-28 03:14:31 +00003695 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Cheng466685d2006-10-09 20:57:25 +00003696 SV->getValue(), SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003697 // Increment the pointer, VAList, to the next vaarg
3698 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3699 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3700 TLI.getPointerTy()));
3701 // Store the incremented VAList to the legalized pointer
Evan Cheng8b2794a2006-10-13 21:14:26 +00003702 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3703 SV->getOffset());
Nate Begeman0aed7842006-01-28 03:14:31 +00003704 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003705 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00003706 }
3707 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003708 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00003709 break;
3710
Evan Cheng466685d2006-10-09 20:57:25 +00003711 case ISD::LOAD: {
3712 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00003713 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3714 ? ISD::EXTLOAD : LD->getExtensionType();
3715 Result = DAG.getExtLoad(ExtType, NVT,
3716 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00003717 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003718 LD->getLoadedVT(),
3719 LD->isVolatile(),
3720 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00003721 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00003722 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00003723 break;
Evan Cheng466685d2006-10-09 20:57:25 +00003724 }
Chris Lattner03c85462005-01-15 05:21:40 +00003725 case ISD::SELECT:
Chris Lattner03c85462005-01-15 05:21:40 +00003726 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3727 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner456a93a2006-01-28 07:39:30 +00003728 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00003729 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003730 case ISD::SELECT_CC:
3731 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3732 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3733 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00003734 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00003735 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003736 case ISD::BSWAP:
3737 Tmp1 = Node->getOperand(0);
3738 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3739 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3740 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003741 DAG.getConstant(MVT::getSizeInBits(NVT) -
3742 MVT::getSizeInBits(VT),
Nate Begemand88fc032006-01-14 03:14:10 +00003743 TLI.getShiftAmountTy()));
3744 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003745 case ISD::CTPOP:
3746 case ISD::CTTZ:
3747 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003748 // Zero extend the argument
3749 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003750 // Perform the larger operation, then subtract if needed.
3751 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003752 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003753 case ISD::CTPOP:
3754 Result = Tmp1;
3755 break;
3756 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003757 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00003758 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003759 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3760 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003761 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003762 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003763 break;
3764 case ISD::CTLZ:
3765 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003766 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohmanb55757e2007-05-18 17:52:13 +00003767 DAG.getConstant(MVT::getSizeInBits(NVT) -
3768 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00003769 break;
3770 }
3771 break;
Dan Gohman7f321562007-06-25 16:23:39 +00003772 case ISD::EXTRACT_SUBVECTOR:
3773 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00003774 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00003775 case ISD::EXTRACT_VECTOR_ELT:
3776 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3777 break;
Chris Lattner03c85462005-01-15 05:21:40 +00003778 }
3779
3780 assert(Result.Val && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003781
3782 // Make sure the result is itself legal.
3783 Result = LegalizeOp(Result);
3784
3785 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00003786 AddPromotedOperand(Op, Result);
3787 return Result;
3788}
Chris Lattner3e928bb2005-01-07 07:47:09 +00003789
Dan Gohman7f321562007-06-25 16:23:39 +00003790/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3791/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3792/// based on the vector type. The return type of this matches the element type
3793/// of the vector, which may not be legal for the target.
3794SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner15972212006-03-31 17:55:51 +00003795 // We know that operand #0 is the Vec vector. If the index is a constant
3796 // or if the invec is a supported hardware type, we can use it. Otherwise,
3797 // lower to a store then an indexed load.
3798 SDOperand Vec = Op.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00003799 SDOperand Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00003800
3801 SDNode *InVal = Vec.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00003802 MVT::ValueType TVT = InVal->getValueType(0);
3803 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner15972212006-03-31 17:55:51 +00003804
Dan Gohman7f321562007-06-25 16:23:39 +00003805 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3806 default: assert(0 && "This action is not supported yet!");
3807 case TargetLowering::Custom: {
3808 Vec = LegalizeOp(Vec);
3809 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3810 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3811 if (Tmp3.Val)
3812 return Tmp3;
3813 break;
3814 }
3815 case TargetLowering::Legal:
3816 if (isTypeLegal(TVT)) {
3817 Vec = LegalizeOp(Vec);
3818 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00003819 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00003820 }
3821 break;
3822 case TargetLowering::Expand:
3823 break;
3824 }
3825
3826 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00003827 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003828 Op = ScalarizeVectorOp(Vec);
3829 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3830 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner15972212006-03-31 17:55:51 +00003831 SDOperand Lo, Hi;
3832 SplitVectorOp(Vec, Lo, Hi);
3833 if (CIdx->getValue() < NumElems/2) {
3834 Vec = Lo;
3835 } else {
3836 Vec = Hi;
Dan Gohman7f321562007-06-25 16:23:39 +00003837 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3838 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00003839 }
Dan Gohman7f321562007-06-25 16:23:39 +00003840
Chris Lattner15972212006-03-31 17:55:51 +00003841 // It's now an extract from the appropriate high or low part. Recurse.
3842 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003843 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00003844 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00003845 // Store the value to a temporary stack slot, then LOAD the scalar
3846 // element back out.
3847 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3848 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3849
3850 // Add the offset to the index.
3851 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3852 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3853 DAG.getConstant(EltSize, Idx.getValueType()));
3854 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3855
3856 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00003857 }
Dan Gohman7f321562007-06-25 16:23:39 +00003858 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00003859}
3860
Dan Gohman7f321562007-06-25 16:23:39 +00003861/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00003862/// we assume the operation can be split if it is not already legal.
Dan Gohman7f321562007-06-25 16:23:39 +00003863SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman65956352007-06-13 15:12:02 +00003864 // We know that operand #0 is the Vec vector. For now we assume the index
3865 // is a constant and that the extracted result is a supported hardware type.
3866 SDOperand Vec = Op.getOperand(0);
3867 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3868
Dan Gohman7f321562007-06-25 16:23:39 +00003869 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00003870
3871 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3872 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00003873 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00003874 }
3875
3876 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3877 SDOperand Lo, Hi;
3878 SplitVectorOp(Vec, Lo, Hi);
3879 if (CIdx->getValue() < NumElems/2) {
3880 Vec = Lo;
3881 } else {
3882 Vec = Hi;
3883 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3884 }
3885
3886 // It's now an extract from the appropriate high or low part. Recurse.
3887 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00003888 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00003889}
3890
Nate Begeman750ac1b2006-02-01 07:19:44 +00003891/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3892/// with condition CC on the current target. This usually involves legalizing
3893/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3894/// there may be no choice but to create a new SetCC node to represent the
3895/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3896/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3897void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3898 SDOperand &RHS,
3899 SDOperand &CC) {
3900 SDOperand Tmp1, Tmp2, Result;
3901
3902 switch (getTypeAction(LHS.getValueType())) {
3903 case Legal:
3904 Tmp1 = LegalizeOp(LHS); // LHS
3905 Tmp2 = LegalizeOp(RHS); // RHS
3906 break;
3907 case Promote:
3908 Tmp1 = PromoteOp(LHS); // LHS
3909 Tmp2 = PromoteOp(RHS); // RHS
3910
3911 // If this is an FP compare, the operands have already been extended.
3912 if (MVT::isInteger(LHS.getValueType())) {
3913 MVT::ValueType VT = LHS.getValueType();
3914 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3915
3916 // Otherwise, we have to insert explicit sign or zero extends. Note
3917 // that we could insert sign extends for ALL conditions, but zero extend
3918 // is cheaper on many machines (an AND instead of two shifts), so prefer
3919 // it.
3920 switch (cast<CondCodeSDNode>(CC)->get()) {
3921 default: assert(0 && "Unknown integer comparison!");
3922 case ISD::SETEQ:
3923 case ISD::SETNE:
3924 case ISD::SETUGE:
3925 case ISD::SETUGT:
3926 case ISD::SETULE:
3927 case ISD::SETULT:
3928 // ALL of these operations will work if we either sign or zero extend
3929 // the operands (including the unsigned comparisons!). Zero extend is
3930 // usually a simpler/cheaper operation, so prefer it.
3931 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3932 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3933 break;
3934 case ISD::SETGE:
3935 case ISD::SETGT:
3936 case ISD::SETLT:
3937 case ISD::SETLE:
3938 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3939 DAG.getValueType(VT));
3940 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3941 DAG.getValueType(VT));
3942 break;
3943 }
3944 }
3945 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003946 case Expand: {
3947 MVT::ValueType VT = LHS.getValueType();
3948 if (VT == MVT::f32 || VT == MVT::f64) {
3949 // Expand into one or more soft-fp libcall(s).
Evan Cheng56966222007-01-12 02:11:51 +00003950 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00003951 switch (cast<CondCodeSDNode>(CC)->get()) {
3952 case ISD::SETEQ:
3953 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00003954 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003955 break;
3956 case ISD::SETNE:
3957 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00003958 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003959 break;
3960 case ISD::SETGE:
3961 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00003962 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003963 break;
3964 case ISD::SETLT:
3965 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00003966 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003967 break;
3968 case ISD::SETLE:
3969 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00003970 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003971 break;
3972 case ISD::SETGT:
3973 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00003974 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003975 break;
3976 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00003977 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3978 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00003979 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00003980 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003981 break;
3982 default:
Evan Cheng56966222007-01-12 02:11:51 +00003983 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003984 switch (cast<CondCodeSDNode>(CC)->get()) {
3985 case ISD::SETONE:
3986 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00003987 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003988 // Fallthrough
3989 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00003990 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003991 break;
3992 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00003993 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003994 break;
3995 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00003996 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00003997 break;
3998 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00003999 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004000 break;
Evan Cheng56966222007-01-12 02:11:51 +00004001 case ISD::SETUEQ:
4002 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004003 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004004 default: assert(0 && "Unsupported FP setcc!");
4005 }
4006 }
4007
4008 SDOperand Dummy;
Evan Cheng56966222007-01-12 02:11:51 +00004009 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencer47857812006-12-31 05:55:36 +00004010 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004011 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004012 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004013 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004014 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng2b49c502006-12-15 02:59:56 +00004015 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng56966222007-01-12 02:11:51 +00004016 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencer47857812006-12-31 05:55:36 +00004017 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencerb47b25c2007-01-03 04:22:32 +00004018 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004019 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004020 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004021 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4022 Tmp2 = SDOperand();
4023 }
4024 LHS = Tmp1;
4025 RHS = Tmp2;
4026 return;
4027 }
4028
Nate Begeman750ac1b2006-02-01 07:19:44 +00004029 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4030 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng2b49c502006-12-15 02:59:56 +00004031 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004032 switch (cast<CondCodeSDNode>(CC)->get()) {
4033 case ISD::SETEQ:
4034 case ISD::SETNE:
4035 if (RHSLo == RHSHi)
4036 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4037 if (RHSCST->isAllOnesValue()) {
4038 // Comparison to -1.
4039 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4040 Tmp2 = RHSLo;
4041 break;
4042 }
4043
4044 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4045 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4046 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4047 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4048 break;
4049 default:
4050 // If this is a comparison of the sign bit, just look at the top part.
4051 // X > -1, x < 0
4052 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4053 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4054 CST->getValue() == 0) || // X < 0
4055 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4056 CST->isAllOnesValue())) { // X > -1
4057 Tmp1 = LHSHi;
4058 Tmp2 = RHSHi;
4059 break;
4060 }
4061
4062 // FIXME: This generated code sucks.
4063 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004064 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4065 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004066 default: assert(0 && "Unknown integer setcc!");
4067 case ISD::SETLT:
4068 case ISD::SETULT: LowCC = ISD::SETULT; break;
4069 case ISD::SETGT:
4070 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4071 case ISD::SETLE:
4072 case ISD::SETULE: LowCC = ISD::SETULE; break;
4073 case ISD::SETGE:
4074 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4075 }
4076
4077 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4078 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4079 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4080
4081 // NOTE: on targets without efficient SELECT of bools, we can always use
4082 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004083 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4084 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4085 false, DagCombineInfo);
4086 if (!Tmp1.Val)
4087 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4088 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4089 CCCode, false, DagCombineInfo);
4090 if (!Tmp2.Val)
4091 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
4092
4093 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4094 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4095 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4096 (Tmp2C && Tmp2C->getValue() == 0 &&
4097 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4098 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4099 (Tmp2C && Tmp2C->getValue() == 1 &&
4100 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4101 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4102 // low part is known false, returns high part.
4103 // For LE / GE, if high part is known false, ignore the low part.
4104 // For LT / GT, if high part is known true, ignore the low part.
4105 Tmp1 = Tmp2;
4106 Tmp2 = SDOperand();
4107 } else {
4108 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4109 ISD::SETEQ, false, DagCombineInfo);
4110 if (!Result.Val)
4111 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4112 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4113 Result, Tmp1, Tmp2));
4114 Tmp1 = Result;
4115 Tmp2 = SDOperand();
4116 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004117 }
4118 }
Evan Cheng2b49c502006-12-15 02:59:56 +00004119 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00004120 LHS = Tmp1;
4121 RHS = Tmp2;
4122}
4123
Chris Lattner35481892005-12-23 00:16:34 +00004124/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner232348d2005-12-23 00:52:30 +00004125/// The resultant code need not be legal. Note that SrcOp is the input operand
4126/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner35481892005-12-23 00:16:34 +00004127SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4128 SDOperand SrcOp) {
4129 // Create the stack frame object.
Chris Lattnerce872152006-03-19 06:31:19 +00004130 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner35481892005-12-23 00:16:34 +00004131
4132 // Emit a store to the stack slot.
Evan Cheng8b2794a2006-10-13 21:14:26 +00004133 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004134 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004135 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner35481892005-12-23 00:16:34 +00004136}
4137
Chris Lattner4352cc92006-04-04 17:23:26 +00004138SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4139 // Create a vector sized/aligned stack slot, store the value to element #0,
4140 // then load the whole vector back out.
4141 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Cheng786225a2006-10-05 23:01:46 +00004142 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004143 NULL, 0);
Evan Cheng466685d2006-10-09 20:57:25 +00004144 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00004145}
4146
4147
Chris Lattnerce872152006-03-19 06:31:19 +00004148/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00004149/// support the operation, but do support the resultant vector type.
Chris Lattnerce872152006-03-19 06:31:19 +00004150SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4151
4152 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00004153 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00004154 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00004155 bool isOnlyLowElement = true;
Chris Lattner87100e02006-03-20 01:52:29 +00004156 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng033e6812006-03-24 01:17:21 +00004157 std::map<SDOperand, std::vector<unsigned> > Values;
4158 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004159 bool isConstant = true;
4160 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4161 SplatValue.getOpcode() != ISD::UNDEF)
4162 isConstant = false;
4163
Evan Cheng033e6812006-03-24 01:17:21 +00004164 for (unsigned i = 1; i < NumElems; ++i) {
4165 SDOperand V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00004166 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00004167 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00004168 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00004169 if (SplatValue != V)
Chris Lattner87100e02006-03-20 01:52:29 +00004170 SplatValue = SDOperand(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004171
4172 // If this isn't a constant element or an undef, we can't use a constant
4173 // pool load.
4174 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4175 V.getOpcode() != ISD::UNDEF)
4176 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00004177 }
4178
4179 if (isOnlyLowElement) {
4180 // If the low element is an undef too, then this whole things is an undef.
4181 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4182 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4183 // Otherwise, turn this into a scalar_to_vector node.
4184 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4185 Node->getOperand(0));
4186 }
4187
Chris Lattner2eb86532006-03-24 07:29:17 +00004188 // If all elements are constants, create a load from the constant pool.
4189 if (isConstant) {
4190 MVT::ValueType VT = Node->getValueType(0);
4191 const Type *OpNTy =
4192 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4193 std::vector<Constant*> CV;
4194 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4195 if (ConstantFPSDNode *V =
4196 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
4197 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
4198 } else if (ConstantSDNode *V =
4199 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencerb83eb642006-10-20 07:07:24 +00004200 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00004201 } else {
4202 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4203 CV.push_back(UndefValue::get(OpNTy));
4204 }
4205 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00004206 Constant *CP = ConstantVector::get(CV);
Chris Lattner2eb86532006-03-24 07:29:17 +00004207 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00004208 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner2eb86532006-03-24 07:29:17 +00004209 }
4210
Chris Lattner87100e02006-03-20 01:52:29 +00004211 if (SplatValue.Val) { // Splat of one value?
4212 // Build the shuffle constant vector: <0, 0, 0, 0>
4213 MVT::ValueType MaskVT =
Evan Cheng033e6812006-03-24 01:17:21 +00004214 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00004215 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004216 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004217 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4218 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00004219
4220 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004221 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00004222 // Get the splatted value into the low element of a vector register.
4223 SDOperand LowValVec =
4224 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4225
4226 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4227 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4228 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4229 SplatMask);
4230 }
4231 }
4232
Evan Cheng033e6812006-03-24 01:17:21 +00004233 // If there are only two unique elements, we may be able to turn this into a
4234 // vector shuffle.
4235 if (Values.size() == 2) {
4236 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4237 MVT::ValueType MaskVT =
4238 MVT::getIntVectorWithNumElements(NumElems);
4239 std::vector<SDOperand> MaskVec(NumElems);
4240 unsigned i = 0;
4241 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4242 E = Values.end(); I != E; ++I) {
4243 for (std::vector<unsigned>::iterator II = I->second.begin(),
4244 EE = I->second.end(); II != EE; ++II)
Dan Gohman51eaa862007-06-14 22:58:02 +00004245 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng033e6812006-03-24 01:17:21 +00004246 i += NumElems;
4247 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004248 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4249 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004250
4251 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00004252 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4253 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004254 SmallVector<SDOperand, 8> Ops;
Evan Cheng033e6812006-03-24 01:17:21 +00004255 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4256 E = Values.end(); I != E; ++I) {
4257 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4258 I->first);
4259 Ops.push_back(Op);
4260 }
4261 Ops.push_back(ShuffleMask);
4262
4263 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004264 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4265 &Ops[0], Ops.size());
Evan Cheng033e6812006-03-24 01:17:21 +00004266 }
4267 }
Chris Lattnerce872152006-03-19 06:31:19 +00004268
4269 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4270 // aligned object on the stack, store each element into it, then load
4271 // the result as a vector.
4272 MVT::ValueType VT = Node->getValueType(0);
4273 // Create the stack frame object.
4274 SDOperand FIPtr = CreateStackTemporary(VT);
4275
4276 // Emit a store of each element to the stack slot.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004277 SmallVector<SDOperand, 8> Stores;
Chris Lattnerce872152006-03-19 06:31:19 +00004278 unsigned TypeByteSize =
4279 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattnerce872152006-03-19 06:31:19 +00004280 // Store (in the right endianness) the elements to memory.
4281 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4282 // Ignore undef elements.
4283 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4284
Chris Lattner841c8822006-03-22 01:46:54 +00004285 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00004286
4287 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4288 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4289
Evan Cheng786225a2006-10-05 23:01:46 +00004290 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004291 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00004292 }
4293
4294 SDOperand StoreChain;
4295 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004296 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4297 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00004298 else
4299 StoreChain = DAG.getEntryNode();
4300
4301 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00004302 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00004303}
4304
4305/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4306/// specified value type.
4307SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4308 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4309 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner58092e32007-01-20 22:35:55 +00004310 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004311 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner58092e32007-01-20 22:35:55 +00004312 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattnerce872152006-03-19 06:31:19 +00004313 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4314}
4315
Chris Lattner5b359c62005-04-02 04:00:59 +00004316void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4317 SDOperand Op, SDOperand Amt,
4318 SDOperand &Lo, SDOperand &Hi) {
4319 // Expand the subcomponents.
4320 SDOperand LHSL, LHSH;
4321 ExpandOp(Op, LHSL, LHSH);
4322
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004323 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00004324 MVT::ValueType VT = LHSL.getValueType();
4325 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00004326 Hi = Lo.getValue(1);
4327}
4328
4329
Chris Lattnere34b3962005-01-19 04:19:40 +00004330/// ExpandShift - Try to find a clever way to expand this shift operation out to
4331/// smaller elements. If we can't find a way that is more efficient than a
4332/// libcall on this target, return false. Otherwise, return true with the
4333/// low-parts expanded into Lo and Hi.
4334bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4335 SDOperand &Lo, SDOperand &Hi) {
4336 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4337 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004338
Chris Lattnere34b3962005-01-19 04:19:40 +00004339 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004340 SDOperand ShAmt = LegalizeOp(Amt);
4341 MVT::ValueType ShTy = ShAmt.getValueType();
4342 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4343 unsigned NVTBits = MVT::getSizeInBits(NVT);
4344
4345 // Handle the case when Amt is an immediate. Other cases are currently broken
4346 // and are disabled.
4347 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4348 unsigned Cst = CN->getValue();
4349 // Expand the incoming operand to be shifted, so that we have its parts
4350 SDOperand InL, InH;
4351 ExpandOp(Op, InL, InH);
4352 switch(Opc) {
4353 case ISD::SHL:
4354 if (Cst > VTBits) {
4355 Lo = DAG.getConstant(0, NVT);
4356 Hi = DAG.getConstant(0, NVT);
4357 } else if (Cst > NVTBits) {
4358 Lo = DAG.getConstant(0, NVT);
4359 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004360 } else if (Cst == NVTBits) {
4361 Lo = DAG.getConstant(0, NVT);
4362 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004363 } else {
4364 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4365 Hi = DAG.getNode(ISD::OR, NVT,
4366 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4367 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4368 }
4369 return true;
4370 case ISD::SRL:
4371 if (Cst > VTBits) {
4372 Lo = DAG.getConstant(0, NVT);
4373 Hi = DAG.getConstant(0, NVT);
4374 } else if (Cst > NVTBits) {
4375 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4376 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00004377 } else if (Cst == NVTBits) {
4378 Lo = InH;
4379 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004380 } else {
4381 Lo = DAG.getNode(ISD::OR, NVT,
4382 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4383 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4384 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4385 }
4386 return true;
4387 case ISD::SRA:
4388 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004389 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004390 DAG.getConstant(NVTBits-1, ShTy));
4391 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00004392 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004393 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00004394 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004395 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00004396 } else if (Cst == NVTBits) {
4397 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00004398 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00004399 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004400 } else {
4401 Lo = DAG.getNode(ISD::OR, NVT,
4402 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4403 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4404 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4405 }
4406 return true;
4407 }
4408 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004409
4410 // Okay, the shift amount isn't constant. However, if we can tell that it is
4411 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4412 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00004413 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00004414
4415 // If we know that the high bit of the shift amount is one, then we can do
4416 // this as a couple of simple shifts.
4417 if (KnownOne & Mask) {
4418 // Mask out the high bit, which we know is set.
4419 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4420 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4421
4422 // Expand the incoming operand to be shifted, so that we have its parts
4423 SDOperand InL, InH;
4424 ExpandOp(Op, InL, InH);
4425 switch(Opc) {
4426 case ISD::SHL:
4427 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4428 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4429 return true;
4430 case ISD::SRL:
4431 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4432 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4433 return true;
4434 case ISD::SRA:
4435 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4436 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4437 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4438 return true;
4439 }
4440 }
4441
4442 // If we know that the high bit of the shift amount is zero, then we can do
4443 // this as a couple of simple shifts.
4444 if (KnownZero & Mask) {
4445 // Compute 32-amt.
4446 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4447 DAG.getConstant(NVTBits, Amt.getValueType()),
4448 Amt);
4449
4450 // Expand the incoming operand to be shifted, so that we have its parts
4451 SDOperand InL, InH;
4452 ExpandOp(Op, InL, InH);
4453 switch(Opc) {
4454 case ISD::SHL:
4455 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4456 Hi = DAG.getNode(ISD::OR, NVT,
4457 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4458 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4459 return true;
4460 case ISD::SRL:
4461 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4462 Lo = DAG.getNode(ISD::OR, NVT,
4463 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4464 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4465 return true;
4466 case ISD::SRA:
4467 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4468 Lo = DAG.getNode(ISD::OR, NVT,
4469 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4470 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4471 return true;
4472 }
4473 }
4474
Nate Begemanf1fe32e2005-04-06 21:13:14 +00004475 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00004476}
Chris Lattner77e77a62005-01-21 06:05:23 +00004477
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004478
Chris Lattner77e77a62005-01-21 06:05:23 +00004479// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4480// does not fit into a register, return the lo part and set the hi part to the
4481// by-reg argument. If it does fit into a single register, return the result
4482// and leave the Hi part unset.
4483SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencer47857812006-12-31 05:55:36 +00004484 bool isSigned, SDOperand &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00004485 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4486 // The input chain to this libcall is the entry node of the function.
4487 // Legalizing the call will automatically add the previous call to the
4488 // dependence.
4489 SDOperand InChain = DAG.getEntryNode();
4490
Chris Lattner77e77a62005-01-21 06:05:23 +00004491 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00004492 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00004493 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4494 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4495 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencer47857812006-12-31 05:55:36 +00004496 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00004497 Entry.isSExt = isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00004498 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00004499 }
4500 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00004501
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004502 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00004503 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004504 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencer47857812006-12-31 05:55:36 +00004505 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattneradf6a962005-05-13 18:50:42 +00004506 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00004507
Chris Lattner6831a812006-02-13 09:18:02 +00004508 // Legalize the call sequence, starting with the chain. This will advance
4509 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4510 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4511 LegalizeOp(CallInfo.second);
Chris Lattner99c25b82005-09-02 20:26:58 +00004512 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004513 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004514 default: assert(0 && "Unknown thing");
4515 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00004516 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00004517 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004518 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00004519 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00004520 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00004521 }
Chris Lattner99c25b82005-09-02 20:26:58 +00004522 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00004523}
4524
Chris Lattner9c32d3b2005-01-23 04:42:50 +00004525
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004526/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4527///
Chris Lattner77e77a62005-01-21 06:05:23 +00004528SDOperand SelectionDAGLegalize::
4529ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattner77e77a62005-01-21 06:05:23 +00004530 assert(getTypeAction(Source.getValueType()) == Expand &&
4531 "This is not an expansion!");
4532 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4533
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004534 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00004535 assert(Source.getValueType() == MVT::i64 &&
4536 "This only works for 64-bit -> FP");
4537 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4538 // incoming integer is set. To handle this, we dynamically test to see if
4539 // it is set, and, if so, add a fudge factor.
4540 SDOperand Lo, Hi;
4541 ExpandOp(Source, Lo, Hi);
4542
Chris Lattner66de05b2005-05-13 04:45:13 +00004543 // If this is unsigned, and not supported, first perform the conversion to
4544 // signed, then adjust the result if the sign bit is set.
4545 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4546 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4547
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004548 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4549 DAG.getConstant(0, Hi.getValueType()),
4550 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004551 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4552 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4553 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00004554 uint64_t FF = 0x5f800000ULL;
4555 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004556 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004557
Chris Lattner5839bf22005-08-26 17:15:30 +00004558 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00004559 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4560 SDOperand FudgeInReg;
4561 if (DestTy == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004562 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004563 else {
4564 assert(DestTy == MVT::f64 && "Unexpected conversion");
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004565 // FIXME: Avoid the extend by construction the right constantpool?
Chris Lattner5f056bf2005-07-10 01:55:33 +00004566 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Cheng466685d2006-10-09 20:57:25 +00004567 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00004568 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004569 MVT::ValueType SCVT = SignedConv.getValueType();
4570 if (SCVT != DestTy) {
4571 // Destination type needs to be expanded as well. The FADD now we are
4572 // constructing will be expanded into a libcall.
4573 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4574 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4575 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4576 SignedConv, SignedConv.getValue(1));
4577 }
4578 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4579 }
Chris Lattner473a9902005-09-29 06:44:39 +00004580 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00004581 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004582
Chris Lattnera88a2602005-05-14 05:33:54 +00004583 // Check to see if the target has a custom way to lower this. If so, use it.
4584 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4585 default: assert(0 && "This action not implemented for this operation!");
4586 case TargetLowering::Legal:
4587 case TargetLowering::Expand:
4588 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00004589 case TargetLowering::Custom: {
4590 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4591 Source), DAG);
4592 if (NV.Val)
4593 return LegalizeOp(NV);
4594 break; // The target decided this was legal after all
4595 }
Chris Lattnera88a2602005-05-14 05:33:54 +00004596 }
4597
Chris Lattner13689e22005-05-12 07:00:44 +00004598 // Expand the source, then glue it back together for the call. We must expand
4599 // the source in case it is shared (this pass of legalize must traverse it).
4600 SDOperand SrcLo, SrcHi;
4601 ExpandOp(Source, SrcLo, SrcHi);
4602 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4603
Evan Cheng56966222007-01-12 02:11:51 +00004604 RTLIB::Libcall LC;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004605 if (DestTy == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00004606 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004607 else {
4608 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng56966222007-01-12 02:11:51 +00004609 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00004610 }
Chris Lattner6831a812006-02-13 09:18:02 +00004611
Evan Cheng4c6cfad2007-04-27 07:33:31 +00004612 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner6831a812006-02-13 09:18:02 +00004613 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4614 SDOperand UnusedHiPart;
Evan Cheng56966222007-01-12 02:11:51 +00004615 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4616 UnusedHiPart);
Chris Lattner77e77a62005-01-21 06:05:23 +00004617}
Misha Brukmanedf128a2005-04-21 22:36:52 +00004618
Chris Lattner22cde6a2006-01-28 08:25:58 +00004619/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4620/// INT_TO_FP operation of the specified operand when the target requests that
4621/// we expand it. At this point, we know that the result and operand types are
4622/// legal for the target.
4623SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4624 SDOperand Op0,
4625 MVT::ValueType DestVT) {
4626 if (Op0.getValueType() == MVT::i32) {
4627 // simple 32-bit [signed|unsigned] integer to float/double expansion
4628
Chris Lattner58092e32007-01-20 22:35:55 +00004629 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner22cde6a2006-01-28 08:25:58 +00004630 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner58092e32007-01-20 22:35:55 +00004631 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4632 unsigned StackAlign =
Chris Lattnerd2b7cec2007-02-14 05:52:17 +00004633 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner58092e32007-01-20 22:35:55 +00004634 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004635 // get address of 8 byte buffer
4636 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4637 // word offset constant for Hi/Lo address computation
4638 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4639 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner408c4282006-03-23 05:29:04 +00004640 SDOperand Hi = StackSlot;
4641 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4642 if (TLI.isLittleEndian())
4643 std::swap(Hi, Lo);
4644
Chris Lattner22cde6a2006-01-28 08:25:58 +00004645 // if signed map to unsigned space
4646 SDOperand Op0Mapped;
4647 if (isSigned) {
4648 // constant used to invert sign bit (signed to unsigned mapping)
4649 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4650 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4651 } else {
4652 Op0Mapped = Op0;
4653 }
4654 // store the lo of the constructed double - based on integer input
Evan Cheng786225a2006-10-05 23:01:46 +00004655 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004656 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004657 // initial hi portion of constructed double
4658 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4659 // store the hi of the constructed double - biased exponent
Evan Cheng8b2794a2006-10-13 21:14:26 +00004660 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004661 // load the constructed double
Evan Cheng466685d2006-10-09 20:57:25 +00004662 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004663 // FP constant to bias correct the final result
4664 SDOperand Bias = DAG.getConstantFP(isSigned ?
4665 BitsToDouble(0x4330000080000000ULL)
4666 : BitsToDouble(0x4330000000000000ULL),
4667 MVT::f64);
4668 // subtract the bias
4669 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4670 // final result
4671 SDOperand Result;
4672 // handle final rounding
4673 if (DestVT == MVT::f64) {
4674 // do nothing
4675 Result = Sub;
4676 } else {
4677 // if f32 then cast to f32
4678 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4679 }
4680 return Result;
4681 }
4682 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4683 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4684
4685 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4686 DAG.getConstant(0, Op0.getValueType()),
4687 ISD::SETLT);
4688 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4689 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4690 SignSet, Four, Zero);
4691
4692 // If the sign bit of the integer is set, the large number will be treated
4693 // as a negative number. To counteract this, the dynamic code adds an
4694 // offset depending on the data type.
4695 uint64_t FF;
4696 switch (Op0.getValueType()) {
4697 default: assert(0 && "Unsupported integer type!");
4698 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4699 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4700 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4701 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4702 }
4703 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00004704 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004705
4706 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4707 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4708 SDOperand FudgeInReg;
4709 if (DestVT == MVT::f32)
Evan Cheng466685d2006-10-09 20:57:25 +00004710 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004711 else {
4712 assert(DestVT == MVT::f64 && "Unexpected conversion");
4713 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4714 DAG.getEntryNode(), CPIdx,
Evan Cheng466685d2006-10-09 20:57:25 +00004715 NULL, 0, MVT::f32));
Chris Lattner22cde6a2006-01-28 08:25:58 +00004716 }
4717
4718 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4719}
4720
4721/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4722/// *INT_TO_FP operation of the specified operand when the target requests that
4723/// we promote it. At this point, we know that the result and operand types are
4724/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4725/// operation that takes a larger input.
4726SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4727 MVT::ValueType DestVT,
4728 bool isSigned) {
4729 // First step, figure out the appropriate *INT_TO_FP operation to use.
4730 MVT::ValueType NewInTy = LegalOp.getValueType();
4731
4732 unsigned OpToUse = 0;
4733
4734 // Scan for the appropriate larger type to use.
4735 while (1) {
4736 NewInTy = (MVT::ValueType)(NewInTy+1);
4737 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4738
4739 // If the target supports SINT_TO_FP of this type, use it.
4740 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4741 default: break;
4742 case TargetLowering::Legal:
4743 if (!TLI.isTypeLegal(NewInTy))
4744 break; // Can't use this datatype.
4745 // FALL THROUGH.
4746 case TargetLowering::Custom:
4747 OpToUse = ISD::SINT_TO_FP;
4748 break;
4749 }
4750 if (OpToUse) break;
4751 if (isSigned) continue;
4752
4753 // If the target supports UINT_TO_FP of this type, use it.
4754 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4755 default: break;
4756 case TargetLowering::Legal:
4757 if (!TLI.isTypeLegal(NewInTy))
4758 break; // Can't use this datatype.
4759 // FALL THROUGH.
4760 case TargetLowering::Custom:
4761 OpToUse = ISD::UINT_TO_FP;
4762 break;
4763 }
4764 if (OpToUse) break;
4765
4766 // Otherwise, try a larger type.
4767 }
4768
4769 // Okay, we found the operation and type to use. Zero extend our input to the
4770 // desired type then run the operation on it.
4771 return DAG.getNode(OpToUse, DestVT,
4772 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4773 NewInTy, LegalOp));
4774}
4775
4776/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4777/// FP_TO_*INT operation of the specified operand when the target requests that
4778/// we promote it. At this point, we know that the result and operand types are
4779/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4780/// operation that returns a larger result.
4781SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4782 MVT::ValueType DestVT,
4783 bool isSigned) {
4784 // First step, figure out the appropriate FP_TO*INT operation to use.
4785 MVT::ValueType NewOutTy = DestVT;
4786
4787 unsigned OpToUse = 0;
4788
4789 // Scan for the appropriate larger type to use.
4790 while (1) {
4791 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4792 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4793
4794 // If the target supports FP_TO_SINT returning this type, use it.
4795 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4796 default: break;
4797 case TargetLowering::Legal:
4798 if (!TLI.isTypeLegal(NewOutTy))
4799 break; // Can't use this datatype.
4800 // FALL THROUGH.
4801 case TargetLowering::Custom:
4802 OpToUse = ISD::FP_TO_SINT;
4803 break;
4804 }
4805 if (OpToUse) break;
4806
4807 // If the target supports FP_TO_UINT of this type, use it.
4808 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4809 default: break;
4810 case TargetLowering::Legal:
4811 if (!TLI.isTypeLegal(NewOutTy))
4812 break; // Can't use this datatype.
4813 // FALL THROUGH.
4814 case TargetLowering::Custom:
4815 OpToUse = ISD::FP_TO_UINT;
4816 break;
4817 }
4818 if (OpToUse) break;
4819
4820 // Otherwise, try a larger type.
4821 }
4822
4823 // Okay, we found the operation and type to use. Truncate the result of the
4824 // extended FP_TO_*INT operation to the desired size.
4825 return DAG.getNode(ISD::TRUNCATE, DestVT,
4826 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4827}
4828
4829/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4830///
4831SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4832 MVT::ValueType VT = Op.getValueType();
4833 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4834 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4835 switch (VT) {
4836 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4837 case MVT::i16:
4838 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4839 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4840 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4841 case MVT::i32:
4842 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4843 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4844 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4845 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4846 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4847 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4848 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4849 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4850 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4851 case MVT::i64:
4852 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4853 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4854 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4855 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4856 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4857 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4858 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4859 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4860 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4861 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4862 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4863 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4864 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4865 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4866 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4867 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4868 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4869 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4870 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4871 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4872 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4873 }
4874}
4875
4876/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4877///
4878SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4879 switch (Opc) {
4880 default: assert(0 && "Cannot expand this yet!");
4881 case ISD::CTPOP: {
4882 static const uint64_t mask[6] = {
4883 0x5555555555555555ULL, 0x3333333333333333ULL,
4884 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4885 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4886 };
4887 MVT::ValueType VT = Op.getValueType();
4888 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004889 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004890 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4891 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4892 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4893 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4894 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4895 DAG.getNode(ISD::AND, VT,
4896 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4897 }
4898 return Op;
4899 }
4900 case ISD::CTLZ: {
4901 // for now, we do this:
4902 // x = x | (x >> 1);
4903 // x = x | (x >> 2);
4904 // ...
4905 // x = x | (x >>16);
4906 // x = x | (x >>32); // for 64-bit input
4907 // return popcount(~x);
4908 //
4909 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4910 MVT::ValueType VT = Op.getValueType();
4911 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohmanb55757e2007-05-18 17:52:13 +00004912 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00004913 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4914 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4915 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4916 }
4917 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4918 return DAG.getNode(ISD::CTPOP, VT, Op);
4919 }
4920 case ISD::CTTZ: {
4921 // for now, we use: { return popcount(~x & (x - 1)); }
4922 // unless the target has ctlz but not ctpop, in which case we use:
4923 // { return 32 - nlz(~x & (x-1)); }
4924 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4925 MVT::ValueType VT = Op.getValueType();
4926 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4927 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4928 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4929 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4930 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4931 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4932 TLI.isOperationLegal(ISD::CTLZ, VT))
4933 return DAG.getNode(ISD::SUB, VT,
Dan Gohmanb55757e2007-05-18 17:52:13 +00004934 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00004935 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4936 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4937 }
4938 }
4939}
Chris Lattnere34b3962005-01-19 04:19:40 +00004940
Chris Lattner3e928bb2005-01-07 07:47:09 +00004941/// ExpandOp - Expand the specified SDOperand into its two component pieces
4942/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4943/// LegalizeNodes map is filled in for any results that are not expanded, the
4944/// ExpandedNodes map is filled in for any results that are expanded, and the
4945/// Lo/Hi values are returned.
4946void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4947 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00004948 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004949 SDNode *Node = Op.Val;
4950 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00004951 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohman7f321562007-06-25 16:23:39 +00004952 MVT::isVector(VT)) &&
Chris Lattner3e928bb2005-01-07 07:47:09 +00004953 "Cannot expand to FP value or to larger int value!");
4954
Chris Lattner6fdcb252005-09-02 20:32:45 +00004955 // See if we already expanded it.
Chris Lattner40030bf2007-02-04 01:17:38 +00004956 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00004957 = ExpandedNodes.find(Op);
4958 if (I != ExpandedNodes.end()) {
4959 Lo = I->second.first;
4960 Hi = I->second.second;
4961 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004962 }
4963
Chris Lattner3e928bb2005-01-07 07:47:09 +00004964 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00004965 case ISD::CopyFromReg:
4966 assert(0 && "CopyFromReg must be legal!");
4967 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004968#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004969 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004970#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00004971 assert(0 && "Do not know how to expand this operator!");
4972 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004973 case ISD::UNDEF:
Evan Chengaa975c12006-12-16 02:20:50 +00004974 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004975 Lo = DAG.getNode(ISD::UNDEF, NVT);
4976 Hi = DAG.getNode(ISD::UNDEF, NVT);
4977 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004978 case ISD::Constant: {
4979 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4980 Lo = DAG.getConstant(Cst, NVT);
4981 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4982 break;
4983 }
Evan Cheng00495212006-12-12 21:32:44 +00004984 case ISD::ConstantFP: {
4985 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng279101e2006-12-12 22:19:28 +00004986 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00004987 if (getTypeAction(Lo.getValueType()) == Expand)
4988 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00004989 break;
4990 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004991 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00004992 // Return the operands.
4993 Lo = Node->getOperand(0);
4994 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00004995 break;
Chris Lattner58f79632005-12-12 22:27:43 +00004996
4997 case ISD::SIGN_EXTEND_INREG:
4998 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00004999 // sext_inreg the low part if needed.
5000 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5001
5002 // The high part gets the sign extension from the lo-part. This handles
5003 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00005004 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5005 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5006 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00005007 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00005008
Nate Begemand88fc032006-01-14 03:14:10 +00005009 case ISD::BSWAP: {
5010 ExpandOp(Node->getOperand(0), Lo, Hi);
5011 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5012 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5013 Lo = TempLo;
5014 break;
5015 }
5016
Chris Lattneredb1add2005-05-11 04:51:16 +00005017 case ISD::CTPOP:
5018 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00005019 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5020 DAG.getNode(ISD::CTPOP, NVT, Lo),
5021 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00005022 Hi = DAG.getConstant(0, NVT);
5023 break;
5024
Chris Lattner39a8f332005-05-12 19:05:01 +00005025 case ISD::CTLZ: {
5026 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005027 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005028 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5029 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005030 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5031 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005032 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5033 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5034
5035 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5036 Hi = DAG.getConstant(0, NVT);
5037 break;
5038 }
5039
5040 case ISD::CTTZ: {
5041 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00005042 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00005043 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5044 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005045 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5046 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00005047 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5048 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5049
5050 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5051 Hi = DAG.getConstant(0, NVT);
5052 break;
5053 }
Chris Lattneredb1add2005-05-11 04:51:16 +00005054
Nate Begemanacc398c2006-01-25 18:21:52 +00005055 case ISD::VAARG: {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005056 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5057 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00005058 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5059 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5060
5061 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005062 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00005063 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5064 if (!TLI.isLittleEndian())
5065 std::swap(Lo, Hi);
5066 break;
5067 }
5068
Chris Lattner3e928bb2005-01-07 07:47:09 +00005069 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00005070 LoadSDNode *LD = cast<LoadSDNode>(Node);
5071 SDOperand Ch = LD->getChain(); // Legalize the chain.
5072 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5073 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005074 int SVOffset = LD->getSrcValueOffset();
5075 unsigned Alignment = LD->getAlignment();
5076 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005077
Evan Cheng466685d2006-10-09 20:57:25 +00005078 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005079 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5080 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00005081 if (VT == MVT::f32 || VT == MVT::f64) {
5082 // f32->i32 or f64->i64 one to one expansion.
5083 // Remember that we legalized the chain.
5084 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00005085 // Recursively expand the new load.
5086 if (getTypeAction(NVT) == Expand)
5087 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00005088 break;
5089 }
Chris Lattnerec39a452005-01-19 18:02:17 +00005090
Evan Cheng466685d2006-10-09 20:57:25 +00005091 // Increment the pointer to the other half.
5092 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5093 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5094 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005095 SVOffset += IncrementSize;
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005096 if (Alignment > IncrementSize)
5097 Alignment = IncrementSize;
5098 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5099 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00005100
Evan Cheng466685d2006-10-09 20:57:25 +00005101 // Build a factor node to remember that this load is independent of the
5102 // other one.
5103 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5104 Hi.getValue(1));
5105
5106 // Remember that we legalized the chain.
5107 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5108 if (!TLI.isLittleEndian())
5109 std::swap(Lo, Hi);
5110 } else {
Evan Cheng2e49f092006-10-11 07:10:22 +00005111 MVT::ValueType EVT = LD->getLoadedVT();
Evan Cheng548f6112006-12-13 03:19:57 +00005112
5113 if (VT == MVT::f64 && EVT == MVT::f32) {
5114 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5115 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005116 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00005117 // Remember that we legalized the chain.
5118 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5119 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5120 break;
5121 }
Evan Cheng466685d2006-10-09 20:57:25 +00005122
5123 if (EVT == NVT)
5124 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005125 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005126 else
5127 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00005128 SVOffset, EVT, isVolatile,
5129 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00005130
5131 // Remember that we legalized the chain.
5132 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5133
5134 if (ExtType == ISD::SEXTLOAD) {
5135 // The high part is obtained by SRA'ing all but one of the bits of the
5136 // lo part.
5137 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5138 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5139 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5140 } else if (ExtType == ISD::ZEXTLOAD) {
5141 // The high part is just a zero.
5142 Hi = DAG.getConstant(0, NVT);
5143 } else /* if (ExtType == ISD::EXTLOAD) */ {
5144 // The high part is undefined.
5145 Hi = DAG.getNode(ISD::UNDEF, NVT);
5146 }
5147 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005148 break;
5149 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005150 case ISD::AND:
5151 case ISD::OR:
5152 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5153 SDOperand LL, LH, RL, RH;
5154 ExpandOp(Node->getOperand(0), LL, LH);
5155 ExpandOp(Node->getOperand(1), RL, RH);
5156 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5157 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5158 break;
5159 }
5160 case ISD::SELECT: {
Chris Lattner456a93a2006-01-28 07:39:30 +00005161 SDOperand LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005162 ExpandOp(Node->getOperand(1), LL, LH);
5163 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00005164 if (getTypeAction(NVT) == Expand)
5165 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00005166 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00005167 if (VT != MVT::f32)
5168 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00005169 break;
5170 }
Nate Begeman9373a812005-08-10 20:51:12 +00005171 case ISD::SELECT_CC: {
5172 SDOperand TL, TH, FL, FH;
5173 ExpandOp(Node->getOperand(2), TL, TH);
5174 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00005175 if (getTypeAction(NVT) == Expand)
5176 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00005177 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5178 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00005179 if (VT != MVT::f32)
5180 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5181 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00005182 break;
5183 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005184 case ISD::ANY_EXTEND:
5185 // The low part is any extension of the input (which degenerates to a copy).
5186 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5187 // The high part is undefined.
5188 Hi = DAG.getNode(ISD::UNDEF, NVT);
5189 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00005190 case ISD::SIGN_EXTEND: {
5191 // The low part is just a sign extension of the input (which degenerates to
5192 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005193 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005194
Chris Lattner3e928bb2005-01-07 07:47:09 +00005195 // The high part is obtained by SRA'ing all but one of the bits of the lo
5196 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00005197 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner8137c9e2006-01-28 05:07:51 +00005198 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5199 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00005200 break;
5201 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00005202 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00005203 // The low part is just a zero extension of the input (which degenerates to
5204 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00005205 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005206
Chris Lattner3e928bb2005-01-07 07:47:09 +00005207 // The high part is just a zero.
5208 Hi = DAG.getConstant(0, NVT);
5209 break;
Chris Lattner35481892005-12-23 00:16:34 +00005210
Chris Lattner4c948eb2007-02-13 23:55:16 +00005211 case ISD::TRUNCATE: {
5212 // The input value must be larger than this value. Expand *it*.
5213 SDOperand NewLo;
5214 ExpandOp(Node->getOperand(0), NewLo, Hi);
5215
5216 // The low part is now either the right size, or it is closer. If not the
5217 // right size, make an illegal truncate so we recursively expand it.
5218 if (NewLo.getValueType() != Node->getValueType(0))
5219 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5220 ExpandOp(NewLo, Lo, Hi);
5221 break;
5222 }
5223
Chris Lattner35481892005-12-23 00:16:34 +00005224 case ISD::BIT_CONVERT: {
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005225 SDOperand Tmp;
5226 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5227 // If the target wants to, allow it to lower this itself.
5228 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5229 case Expand: assert(0 && "cannot expand FP!");
5230 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5231 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5232 }
5233 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5234 }
5235
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005236 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00005237 if (VT == MVT::f32 || VT == MVT::f64) {
5238 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00005239 if (getTypeAction(NVT) == Expand)
5240 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00005241 break;
5242 }
5243
5244 // If source operand will be expanded to the same type as VT, i.e.
5245 // i64 <- f64, i32 <- f32, expand the source operand instead.
5246 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5247 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5248 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005249 break;
5250 }
5251
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005252 // Turn this into a load/store pair by default.
5253 if (Tmp.Val == 0)
Evan Cheng13acce32006-12-11 19:27:14 +00005254 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnerf3f333d2006-09-09 00:20:27 +00005255
Chris Lattner35481892005-12-23 00:16:34 +00005256 ExpandOp(Tmp, Lo, Hi);
5257 break;
5258 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005259
Chris Lattner8137c9e2006-01-28 05:07:51 +00005260 case ISD::READCYCLECOUNTER:
Chris Lattner308575b2005-11-20 22:56:56 +00005261 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5262 TargetLowering::Custom &&
5263 "Must custom expand ReadCycleCounter");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005264 Lo = TLI.LowerOperation(Op, DAG);
5265 assert(Lo.Val && "Node must be custom expanded!");
5266 Hi = Lo.getValue(1);
Chris Lattner308575b2005-11-20 22:56:56 +00005267 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00005268 LegalizeOp(Lo.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00005269 break;
5270
Chris Lattner4e6c7462005-01-08 19:27:05 +00005271 // These operators cannot be expanded directly, emit them as calls to
5272 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00005273 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005274 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00005275 SDOperand Op;
5276 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5277 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00005278 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5279 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00005280 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005281
Chris Lattnerf20d1832005-07-30 01:40:57 +00005282 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5283
Chris Lattner80a3e942005-07-29 00:33:32 +00005284 // Now that the custom expander is done, expand the result, which is still
5285 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00005286 if (Op.Val) {
5287 ExpandOp(Op, Lo, Hi);
5288 break;
5289 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005290 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005291
Evan Cheng56966222007-01-12 02:11:51 +00005292 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005293 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005294 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005295 else
Evan Cheng56966222007-01-12 02:11:51 +00005296 LC = RTLIB::FPTOSINT_F64_I64;
5297 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5298 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005299 break;
Evan Cheng56966222007-01-12 02:11:51 +00005300 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005301
Evan Cheng56966222007-01-12 02:11:51 +00005302 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00005303 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005304 SDOperand Op;
5305 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5306 case Expand: assert(0 && "cannot expand FP!");
5307 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5308 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5309 }
5310
5311 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5312
5313 // Now that the custom expander is done, expand the result.
Chris Lattner07dffd62005-08-26 00:14:16 +00005314 if (Op.Val) {
5315 ExpandOp(Op, Lo, Hi);
5316 break;
5317 }
Chris Lattner80a3e942005-07-29 00:33:32 +00005318 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00005319
Evan Cheng56966222007-01-12 02:11:51 +00005320 RTLIB::Libcall LC;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005321 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005322 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner4e6c7462005-01-08 19:27:05 +00005323 else
Evan Cheng56966222007-01-12 02:11:51 +00005324 LC = RTLIB::FPTOUINT_F64_I64;
5325 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5326 false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00005327 break;
Evan Cheng56966222007-01-12 02:11:51 +00005328 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00005329
Evan Cheng05a2d562006-01-09 18:31:59 +00005330 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005331 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005332 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005333 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005334 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005335 Op = TLI.LowerOperation(Op, DAG);
5336 if (Op.Val) {
5337 // Now that the custom expander is done, expand the result, which is
5338 // still VT.
5339 ExpandOp(Op, Lo, Hi);
5340 break;
5341 }
5342 }
5343
Chris Lattner79980b02006-09-13 03:50:39 +00005344 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5345 // this X << 1 as X+X.
5346 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5347 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5348 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5349 SDOperand LoOps[2], HiOps[3];
5350 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5351 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5352 LoOps[1] = LoOps[0];
5353 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5354
5355 HiOps[1] = HiOps[0];
5356 HiOps[2] = Lo.getValue(1);
5357 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5358 break;
5359 }
5360 }
5361
Chris Lattnere34b3962005-01-19 04:19:40 +00005362 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005363 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005364 break;
Chris Lattner47599822005-04-02 03:38:53 +00005365
5366 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005367 TargetLowering::LegalizeAction Action =
5368 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5369 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5370 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005371 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005372 break;
5373 }
5374
Chris Lattnere34b3962005-01-19 04:19:40 +00005375 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005376 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5377 false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005378 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005379 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005380
Evan Cheng05a2d562006-01-09 18:31:59 +00005381 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005382 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005383 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005384 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005385 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005386 Op = TLI.LowerOperation(Op, DAG);
5387 if (Op.Val) {
5388 // Now that the custom expander is done, expand the result, which is
5389 // still VT.
5390 ExpandOp(Op, Lo, Hi);
5391 break;
5392 }
5393 }
5394
Chris Lattnere34b3962005-01-19 04:19:40 +00005395 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005396 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005397 break;
Chris Lattner47599822005-04-02 03:38:53 +00005398
5399 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005400 TargetLowering::LegalizeAction Action =
5401 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5402 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5403 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005404 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005405 break;
5406 }
5407
Chris Lattnere34b3962005-01-19 04:19:40 +00005408 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005409 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5410 true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005411 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005412 }
5413
5414 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00005415 // If the target wants custom lowering, do so.
Chris Lattner348e93c2006-01-21 04:27:00 +00005416 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00005417 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattner8137c9e2006-01-28 05:07:51 +00005418 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00005419 Op = TLI.LowerOperation(Op, DAG);
5420 if (Op.Val) {
5421 // Now that the custom expander is done, expand the result, which is
5422 // still VT.
5423 ExpandOp(Op, Lo, Hi);
5424 break;
5425 }
5426 }
5427
Chris Lattnere34b3962005-01-19 04:19:40 +00005428 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00005429 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00005430 break;
Chris Lattner47599822005-04-02 03:38:53 +00005431
5432 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00005433 TargetLowering::LegalizeAction Action =
5434 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5435 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5436 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00005437 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00005438 break;
5439 }
5440
Chris Lattnere34b3962005-01-19 04:19:40 +00005441 // Otherwise, emit a libcall.
Evan Cheng56966222007-01-12 02:11:51 +00005442 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5443 false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00005444 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00005445 }
Chris Lattnere34b3962005-01-19 04:19:40 +00005446
Misha Brukmanedf128a2005-04-21 22:36:52 +00005447 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00005448 case ISD::SUB: {
5449 // If the target wants to custom expand this, let them.
5450 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5451 TargetLowering::Custom) {
5452 Op = TLI.LowerOperation(Op, DAG);
5453 if (Op.Val) {
5454 ExpandOp(Op, Lo, Hi);
5455 break;
5456 }
5457 }
5458
5459 // Expand the subcomponents.
5460 SDOperand LHSL, LHSH, RHSL, RHSH;
5461 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5462 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00005463 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5464 SDOperand LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005465 LoOps[0] = LHSL;
5466 LoOps[1] = RHSL;
5467 HiOps[0] = LHSH;
5468 HiOps[1] = RHSH;
Nate Begeman551bf3f2006-02-17 05:43:56 +00005469 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner79980b02006-09-13 03:50:39 +00005470 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005471 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005472 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005473 } else {
Chris Lattner79980b02006-09-13 03:50:39 +00005474 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005475 HiOps[2] = Lo.getValue(1);
Chris Lattner79980b02006-09-13 03:50:39 +00005476 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman551bf3f2006-02-17 05:43:56 +00005477 }
Chris Lattner84f67882005-01-20 18:52:28 +00005478 break;
Chris Lattner8137c9e2006-01-28 05:07:51 +00005479 }
Chris Lattnerb429f732007-05-17 18:15:41 +00005480
5481 case ISD::ADDC:
5482 case ISD::SUBC: {
5483 // Expand the subcomponents.
5484 SDOperand LHSL, LHSH, RHSL, RHSH;
5485 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5486 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5487 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5488 SDOperand LoOps[2] = { LHSL, RHSL };
5489 SDOperand HiOps[3] = { LHSH, RHSH };
5490
5491 if (Node->getOpcode() == ISD::ADDC) {
5492 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5493 HiOps[2] = Lo.getValue(1);
5494 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5495 } else {
5496 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5497 HiOps[2] = Lo.getValue(1);
5498 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5499 }
5500 // Remember that we legalized the flag.
5501 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5502 break;
5503 }
5504 case ISD::ADDE:
5505 case ISD::SUBE: {
5506 // Expand the subcomponents.
5507 SDOperand LHSL, LHSH, RHSL, RHSH;
5508 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5509 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5510 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5511 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5512 SDOperand HiOps[3] = { LHSH, RHSH };
5513
5514 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5515 HiOps[2] = Lo.getValue(1);
5516 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5517
5518 // Remember that we legalized the flag.
5519 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5520 break;
5521 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005522 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005523 // If the target wants to custom expand this, let them.
5524 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattner8829dc82006-09-16 05:08:34 +00005525 SDOperand New = TLI.LowerOperation(Op, DAG);
5526 if (New.Val) {
5527 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00005528 break;
5529 }
5530 }
5531
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005532 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5533 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005534 if (HasMULHS || HasMULHU) {
Nate Begemanc7c16572005-04-11 03:01:51 +00005535 SDOperand LL, LH, RL, RH;
5536 ExpandOp(Node->getOperand(0), LL, LH);
5537 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00005538 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman2cbba892006-12-11 02:23:46 +00005539 // FIXME: Move this to the dag combiner.
Nate Begeman56eb8682005-08-30 02:44:00 +00005540 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5541 // extended the sign bit of the low half through the upper half, and if so
5542 // emit a MULHS instead of the alternate sequence that is valid for any
5543 // i64 x i64 multiply.
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005544 if (HasMULHS &&
Nate Begeman56eb8682005-08-30 02:44:00 +00005545 // is RH an extension of the sign bit of RL?
5546 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5547 RH.getOperand(1).getOpcode() == ISD::Constant &&
5548 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5549 // is LH an extension of the sign bit of LL?
5550 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5551 LH.getOperand(1).getOpcode() == ISD::Constant &&
5552 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005553 // Low part:
5554 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5555 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005556 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattnera89654b2006-09-16 00:21:44 +00005557 break;
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005558 } else if (HasMULHU) {
Chris Lattnera89654b2006-09-16 00:21:44 +00005559 // Low part:
5560 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5561
5562 // High part:
Nate Begeman56eb8682005-08-30 02:44:00 +00005563 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5564 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5565 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5566 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5567 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00005568 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00005569 }
Nate Begemanc7c16572005-04-11 03:01:51 +00005570 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00005571
Evan Cheng56966222007-01-12 02:11:51 +00005572 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5573 false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00005574 break;
5575 }
Evan Cheng56966222007-01-12 02:11:51 +00005576 case ISD::SDIV:
5577 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5578 break;
5579 case ISD::UDIV:
5580 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5581 break;
5582 case ISD::SREM:
5583 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5584 break;
5585 case ISD::UREM:
5586 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5587 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00005588
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005589 case ISD::FADD:
Evan Cheng56966222007-01-12 02:11:51 +00005590 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5591 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5592 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005593 break;
5594 case ISD::FSUB:
Evan Cheng56966222007-01-12 02:11:51 +00005595 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5596 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5597 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005598 break;
5599 case ISD::FMUL:
Evan Cheng56966222007-01-12 02:11:51 +00005600 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5601 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5602 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005603 break;
5604 case ISD::FDIV:
Evan Cheng56966222007-01-12 02:11:51 +00005605 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5606 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5607 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005608 break;
5609 case ISD::FP_EXTEND:
Evan Cheng56966222007-01-12 02:11:51 +00005610 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005611 break;
5612 case ISD::FP_ROUND:
Evan Cheng56966222007-01-12 02:11:51 +00005613 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00005614 break;
Lauro Ramos Venancioc90f0892007-08-15 22:13:27 +00005615 case ISD::FPOWI:
5616 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5617 ? RTLIB::POWI_F32 : RTLIB::POWI_F64),
5618 Node, false, Hi);
5619 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005620 case ISD::FSQRT:
5621 case ISD::FSIN:
5622 case ISD::FCOS: {
Evan Cheng56966222007-01-12 02:11:51 +00005623 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005624 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00005625 case ISD::FSQRT:
5626 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5627 break;
5628 case ISD::FSIN:
5629 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5630 break;
5631 case ISD::FCOS:
5632 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5633 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00005634 default: assert(0 && "Unreachable!");
5635 }
Evan Cheng56966222007-01-12 02:11:51 +00005636 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00005637 break;
5638 }
Evan Cheng966bf242006-12-16 00:52:40 +00005639 case ISD::FABS: {
5640 SDOperand Mask = (VT == MVT::f64)
5641 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5642 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5643 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5644 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5645 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5646 if (getTypeAction(NVT) == Expand)
5647 ExpandOp(Lo, Lo, Hi);
5648 break;
5649 }
5650 case ISD::FNEG: {
5651 SDOperand Mask = (VT == MVT::f64)
5652 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5653 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5654 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5655 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5656 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5657 if (getTypeAction(NVT) == Expand)
5658 ExpandOp(Lo, Lo, Hi);
5659 break;
5660 }
Evan Cheng912095b2007-01-04 21:56:39 +00005661 case ISD::FCOPYSIGN: {
5662 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5663 if (getTypeAction(NVT) == Expand)
5664 ExpandOp(Lo, Lo, Hi);
5665 break;
5666 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005667 case ISD::SINT_TO_FP:
5668 case ISD::UINT_TO_FP: {
5669 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5670 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng56966222007-01-12 02:11:51 +00005671 RTLIB::Libcall LC;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005672 if (Node->getOperand(0).getValueType() == MVT::i64) {
5673 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005674 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005675 else
Evan Cheng56966222007-01-12 02:11:51 +00005676 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005677 } else {
5678 if (VT == MVT::f32)
Evan Cheng56966222007-01-12 02:11:51 +00005679 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005680 else
Evan Cheng56966222007-01-12 02:11:51 +00005681 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng7df28dc2006-12-19 01:44:04 +00005682 }
5683
5684 // Promote the operand if needed.
5685 if (getTypeAction(SrcVT) == Promote) {
5686 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5687 Tmp = isSigned
5688 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5689 DAG.getValueType(SrcVT))
5690 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5691 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5692 }
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005693
5694 const char *LibCall = TLI.getLibcallName(LC);
5695 if (LibCall)
5696 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5697 else {
5698 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5699 Node->getOperand(0));
5700 if (getTypeAction(Lo.getValueType()) == Expand)
5701 ExpandOp(Lo, Lo, Hi);
5702 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00005703 break;
5704 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00005705 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00005706
Chris Lattner83397362005-12-21 18:02:52 +00005707 // Make sure the resultant values have been legalized themselves, unless this
5708 // is a type that requires multi-step expansion.
5709 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5710 Lo = LegalizeOp(Lo);
Evan Cheng13acce32006-12-11 19:27:14 +00005711 if (Hi.Val)
5712 // Don't legalize the high part if it is expanded to a single node.
5713 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00005714 }
Evan Cheng05a2d562006-01-09 18:31:59 +00005715
5716 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005717 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng05a2d562006-01-09 18:31:59 +00005718 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00005719}
5720
Dan Gohman7f321562007-06-25 16:23:39 +00005721/// SplitVectorOp - Given an operand of vector type, break it down into
5722/// two smaller values, still of vector type.
Chris Lattnerc7029802006-03-18 01:44:44 +00005723void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5724 SDOperand &Hi) {
Dan Gohman7f321562007-06-25 16:23:39 +00005725 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005726 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005727 unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0));
Chris Lattnerc7029802006-03-18 01:44:44 +00005728 assert(NumElements > 1 && "Cannot split a single element vector!");
5729 unsigned NewNumElts = NumElements/2;
Dan Gohman7f321562007-06-25 16:23:39 +00005730 MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0));
5731 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattnerc7029802006-03-18 01:44:44 +00005732
5733 // See if we already split it.
5734 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5735 = SplitNodes.find(Op);
5736 if (I != SplitNodes.end()) {
5737 Lo = I->second.first;
5738 Hi = I->second.second;
5739 return;
5740 }
5741
5742 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005743 default:
5744#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005745 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005746#endif
5747 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohman7f321562007-06-25 16:23:39 +00005748 case ISD::BUILD_PAIR:
5749 Lo = Node->getOperand(0);
5750 Hi = Node->getOperand(1);
5751 break;
5752 case ISD::BUILD_VECTOR: {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005753 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5754 Node->op_begin()+NewNumElts);
Dan Gohman7f321562007-06-25 16:23:39 +00005755 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005756
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005757 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohman7f321562007-06-25 16:23:39 +00005758 Node->op_end());
5759 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00005760 break;
5761 }
Dan Gohman7f321562007-06-25 16:23:39 +00005762 case ISD::CONCAT_VECTORS: {
5763 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5764 if (NewNumSubvectors == 1) {
5765 Lo = Node->getOperand(0);
5766 Hi = Node->getOperand(1);
5767 } else {
5768 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5769 Node->op_begin()+NewNumSubvectors);
5770 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00005771
Dan Gohman7f321562007-06-25 16:23:39 +00005772 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5773 Node->op_end());
5774 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5775 }
Dan Gohman65956352007-06-13 15:12:02 +00005776 break;
5777 }
Dan Gohman7f321562007-06-25 16:23:39 +00005778 case ISD::ADD:
5779 case ISD::SUB:
5780 case ISD::MUL:
5781 case ISD::FADD:
5782 case ISD::FSUB:
5783 case ISD::FMUL:
5784 case ISD::SDIV:
5785 case ISD::UDIV:
5786 case ISD::FDIV:
5787 case ISD::AND:
5788 case ISD::OR:
5789 case ISD::XOR: {
Chris Lattnerc7029802006-03-18 01:44:44 +00005790 SDOperand LL, LH, RL, RH;
5791 SplitVectorOp(Node->getOperand(0), LL, LH);
5792 SplitVectorOp(Node->getOperand(1), RL, RH);
5793
Dan Gohman7f321562007-06-25 16:23:39 +00005794 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5795 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00005796 break;
5797 }
Dan Gohman7f321562007-06-25 16:23:39 +00005798 case ISD::LOAD: {
5799 LoadSDNode *LD = cast<LoadSDNode>(Node);
5800 SDOperand Ch = LD->getChain();
5801 SDOperand Ptr = LD->getBasePtr();
5802 const Value *SV = LD->getSrcValue();
5803 int SVOffset = LD->getSrcValueOffset();
5804 unsigned Alignment = LD->getAlignment();
5805 bool isVolatile = LD->isVolatile();
5806
5807 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5808 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00005809 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5810 getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00005811 SVOffset += IncrementSize;
5812 if (Alignment > IncrementSize)
5813 Alignment = IncrementSize;
5814 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00005815
5816 // Build a factor node to remember that this load is independent of the
5817 // other one.
5818 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5819 Hi.getValue(1));
5820
5821 // Remember that we legalized the chain.
5822 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00005823 break;
5824 }
Dan Gohman7f321562007-06-25 16:23:39 +00005825 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00005826 // We know the result is a vector. The input may be either a vector or a
5827 // scalar value.
Dan Gohman10a7aa62007-06-29 00:09:08 +00005828 SDOperand InOp = Node->getOperand(0);
5829 if (!MVT::isVector(InOp.getValueType()) ||
5830 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
5831 // The input is a scalar or single-element vector.
5832 // Lower to a store/load so that it can be split.
5833 // FIXME: this could be improved probably.
5834 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattner7692eb42006-03-23 21:16:34 +00005835
Evan Cheng786225a2006-10-05 23:01:46 +00005836 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman10a7aa62007-06-29 00:09:08 +00005837 InOp, Ptr, NULL, 0);
5838 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00005839 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00005840 // Split the vector and convert each of the pieces now.
5841 SplitVectorOp(InOp, Lo, Hi);
5842 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5843 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
5844 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00005845 }
Chris Lattnerc7029802006-03-18 01:44:44 +00005846 }
5847
5848 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00005849 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00005850 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00005851 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005852}
5853
5854
Dan Gohman89b20c02007-06-27 14:06:22 +00005855/// ScalarizeVectorOp - Given an operand of single-element vector type
5856/// (e.g. v1f32), convert it into the equivalent operation that returns a
5857/// scalar (e.g. f32) value.
Dan Gohman7f321562007-06-25 16:23:39 +00005858SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5859 assert(MVT::isVector(Op.getValueType()) &&
5860 "Bad ScalarizeVectorOp invocation!");
Chris Lattnerc7029802006-03-18 01:44:44 +00005861 SDNode *Node = Op.Val;
Dan Gohman7f321562007-06-25 16:23:39 +00005862 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5863 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00005864
Dan Gohman7f321562007-06-25 16:23:39 +00005865 // See if we already scalarized it.
5866 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5867 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00005868
5869 SDOperand Result;
5870 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00005871 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005872#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00005873 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00005874#endif
Dan Gohman7f321562007-06-25 16:23:39 +00005875 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5876 case ISD::ADD:
5877 case ISD::FADD:
5878 case ISD::SUB:
5879 case ISD::FSUB:
5880 case ISD::MUL:
5881 case ISD::FMUL:
5882 case ISD::SDIV:
5883 case ISD::UDIV:
5884 case ISD::FDIV:
5885 case ISD::SREM:
5886 case ISD::UREM:
5887 case ISD::FREM:
5888 case ISD::AND:
5889 case ISD::OR:
5890 case ISD::XOR:
5891 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00005892 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00005893 ScalarizeVectorOp(Node->getOperand(0)),
5894 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00005895 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005896 case ISD::FNEG:
5897 case ISD::FABS:
5898 case ISD::FSQRT:
5899 case ISD::FSIN:
5900 case ISD::FCOS:
5901 Result = DAG.getNode(Node->getOpcode(),
5902 NewVT,
5903 ScalarizeVectorOp(Node->getOperand(0)));
5904 break;
5905 case ISD::LOAD: {
5906 LoadSDNode *LD = cast<LoadSDNode>(Node);
5907 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5908 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattnerc7029802006-03-18 01:44:44 +00005909
Dan Gohman7f321562007-06-25 16:23:39 +00005910 const Value *SV = LD->getSrcValue();
5911 int SVOffset = LD->getSrcValueOffset();
5912 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5913 LD->isVolatile(), LD->getAlignment());
5914
Chris Lattnerc7029802006-03-18 01:44:44 +00005915 // Remember that we legalized the chain.
5916 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5917 break;
5918 }
Dan Gohman7f321562007-06-25 16:23:39 +00005919 case ISD::BUILD_VECTOR:
5920 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00005921 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005922 case ISD::INSERT_VECTOR_ELT:
5923 // Returning the inserted scalar element.
5924 Result = Node->getOperand(1);
5925 break;
5926 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00005927 assert(Node->getOperand(0).getValueType() == NewVT &&
5928 "Concat of non-legal vectors not yet supported!");
5929 Result = Node->getOperand(0);
5930 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005931 case ISD::VECTOR_SHUFFLE: {
5932 // Figure out if the scalar is the LHS or RHS and return it.
5933 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5934 if (cast<ConstantSDNode>(EltNum)->getValue())
5935 Result = ScalarizeVectorOp(Node->getOperand(1));
5936 else
5937 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00005938 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005939 }
5940 case ISD::EXTRACT_SUBVECTOR:
5941 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00005942 assert(Result.getValueType() == NewVT);
5943 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005944 case ISD::BIT_CONVERT:
5945 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattner5b2316e2006-03-28 20:24:43 +00005946 break;
Dan Gohman7f321562007-06-25 16:23:39 +00005947 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005948 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00005949 ScalarizeVectorOp(Op.getOperand(1)),
5950 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005951 break;
Chris Lattnerc7029802006-03-18 01:44:44 +00005952 }
5953
5954 if (TLI.isTypeLegal(NewVT))
5955 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00005956 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
5957 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00005958 return Result;
5959}
5960
Chris Lattner3e928bb2005-01-07 07:47:09 +00005961
5962// SelectionDAG::Legalize - This is the entry point for the file.
5963//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005964void SelectionDAG::Legalize() {
Chris Lattner5e46a192006-04-02 03:07:27 +00005965 if (ViewLegalizeDAGs) viewGraph();
5966
Chris Lattner3e928bb2005-01-07 07:47:09 +00005967 /// run - This is the main entry point to this class.
5968 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00005969 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00005970}
5971