blob: da55eafdb28a2b72d6e91089680a131e961e50ad [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey70323a82006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Cheng631ccc62007-08-16 23:50:06 +000018#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000019#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000020#include "llvm/Target/TargetData.h"
Evan Cheng84a28d42006-10-30 08:00:44 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000022#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000023#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000024#include "llvm/Constants.h"
Reid Spencera94d3942007-01-19 21:13:56 +000025#include "llvm/DerivedTypes.h"
Chris Lattneref598052006-04-02 03:07:27 +000026#include "llvm/Support/MathExtras.h"
27#include "llvm/Support/CommandLine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattnere83030b2007-02-03 01:12:36 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattner97af9d52006-08-08 01:09:31 +000030#include "llvm/ADT/SmallVector.h"
Chris Lattnerebeb48d2007-02-04 00:27:56 +000031#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng1d2e9952006-03-24 01:17:21 +000032#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000033using namespace llvm;
34
Chris Lattneref598052006-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 Lattnerdc750592005-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 Lattner54a34cd2006-06-28 21:58:30 +000056class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattnerdc750592005-01-07 07:47:09 +000057 TargetLowering &TLI;
58 SelectionDAG &DAG;
59
Chris Lattner462505f2006-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 Lattnerdc750592005-01-07 07:47:09 +000072 enum LegalizeAction {
Chris Lattner2c748af2006-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 Lattneraa2372562006-05-24 17:04:05 +000075 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000076 };
Chris Lattner462505f2006-02-13 09:18:02 +000077
Chris Lattnerdc750592005-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 Lattner2c748af2006-01-29 08:42:06 +000081 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000082
Chris Lattnerdc750592005-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 Lattnered39c862007-02-04 00:50:02 +000086 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000087
Chris Lattner1f2c9d82005-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 Lattner4b0ddb22007-02-04 01:17:38 +000091 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner1f2c9d82005-01-15 05:21:40 +000092
Chris Lattner32206f52006-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 Lattner4b0ddb22007-02-04 01:17:38 +000096 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000097
Chris Lattner32206f52006-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 Gohmana8665142007-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 Lattner32206f52006-03-18 01:44:44 +0000105 /// processed to the result.
Dan Gohmana8665142007-06-25 16:23:39 +0000106 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattner32206f52006-03-18 01:44:44 +0000107
Chris Lattnerea4ca942005-01-07 22:28:47 +0000108 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-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 Lattnerea4ca942005-01-07 22:28:47 +0000113 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000114 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner4b0ddb22007-02-04 01:17:38 +0000115 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000116 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-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 Lattner1f2c9d82005-01-15 05:21:40 +0000119 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000120
Chris Lattnerdc750592005-01-07 07:47:09 +0000121public:
122
Chris Lattner4add7e32005-01-23 04:42:50 +0000123 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000124
Chris Lattnerdc750592005-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 Lattner2c748af2006-01-29 08:42:06 +0000129 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-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 Lattnerdc750592005-01-07 07:47:09 +0000138 void LegalizeDAG();
139
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000140private:
Dan Gohmana8665142007-06-25 16:23:39 +0000141 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-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 Lattnerdc750592005-01-07 07:47:09 +0000148 SDOperand LegalizeOp(SDOperand O);
Chris Lattner32206f52006-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 Lattner1f2c9d82005-01-15 05:21:40 +0000155 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000156
Chris Lattner32206f52006-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 Gohmana8665142007-06-25 16:23:39 +0000165 /// SplitVectorOp - Given an operand of vector type, break it down into
166 /// two smaller values.
Chris Lattner32206f52006-03-18 01:44:44 +0000167 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
168
Dan Gohmanf4e86da2007-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 Gohmana8665142007-06-25 16:23:39 +0000172 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000173
Chris Lattner6be79822006-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 Lattner4488f0c2006-07-26 23:55:56 +0000185 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000186 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000187
Nate Begeman7e7f4392006-02-01 07:19:44 +0000188 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
189
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000190 SDOperand CreateStackTemporary(MVT::ValueType VT);
191
Reid Spencere63b6512006-12-31 05:55:36 +0000192 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattneraac464e2005-01-21 06:05:23 +0000193 SDOperand &Hi);
194 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
195 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000196
Chris Lattner36e663d2005-12-23 00:16:34 +0000197 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000198 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner6be79822006-04-04 17:23:26 +0000199 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000200 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
201 SDOperand LegalOp,
202 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000203 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
204 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000205 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
206 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000207
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000208 SDOperand ExpandBSWAP(SDOperand Op);
209 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000210 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
211 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000212 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
213 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000214
Dan Gohmana8665142007-06-25 16:23:39 +0000215 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner42a5fca2006-04-02 05:06:04 +0000216 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner6f423252006-03-31 17:55:51 +0000217
Chris Lattnerdc750592005-01-07 07:47:09 +0000218 SDOperand getIntPtrConstant(uint64_t Val) {
219 return DAG.getConstant(Val, TLI.getPointerTy());
220 }
221};
222}
223
Chris Lattner6be79822006-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 Lattnerc24a1d32006-08-08 02:23:42 +0000248 SmallVector<SDOperand, 8> Ops;
Chris Lattner6be79822006-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 Lattnerc24a1d32006-08-08 02:23:42 +0000260 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-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 Lattner4add7e32005-01-23 04:42:50 +0000269SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
270 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
271 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000272 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000273 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000274}
275
Chris Lattnere31adc82007-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 Lattner4bbbb9e2005-10-06 01:20:27 +0000284
Chris Lattnere31adc82007-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 Lattner4bbbb9e2005-10-06 01:20:27 +0000293 }
294
Chris Lattnere31adc82007-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 Lattner4bbbb9e2005-10-06 01:20:27 +0000315}
316
Chris Lattner44fe26f2005-07-29 00:11:56 +0000317
Chris Lattnerdc750592005-01-07 07:47:09 +0000318void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000319 LastCALLSEQ_END = DAG.getEntryNode();
320 IsLegalizingCall = false;
321
Chris Lattner9cfccfb2005-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 Lattner4bbbb9e2005-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 Lattner94c44c92007-02-04 01:20:02 +0000328 SmallVector<SDNode*, 64> Order;
Chris Lattnere31adc82007-06-18 21:28:10 +0000329 ComputeTopDownOrdering(DAG, Order);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000330
Chris Lattner32206f52006-03-18 01:44:44 +0000331 for (unsigned i = 0, e = Order.size(); i != e; ++i)
332 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000333
334 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000335 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000336 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
337 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000338
339 ExpandedNodes.clear();
340 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000341 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000342 SplitNodes.clear();
Dan Gohmana8665142007-06-25 16:23:39 +0000343 ScalarizedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000344
345 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000346 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000347}
348
Chris Lattner462505f2006-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 Lattner4488f0c2006-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 Lattnerebeb48d2007-02-04 00:27:56 +0000409 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000410 if (N == Dest) return true; // N certainly leads to Dest :)
411
Chris Lattner4488f0c2006-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 Lattner462505f2006-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 Lattner4488f0c2006-07-26 23:55:56 +0000435 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000436
Chris Lattner4488f0c2006-07-26 23:55:56 +0000437 if (OperandsLeadToDest) {
438 NodesLeadingTo.insert(N);
439 return true;
440 }
Chris Lattner462505f2006-02-13 09:18:02 +0000441
442 // Okay, this node looks safe, legalize it and return false.
Chris Lattner916ae072006-04-17 22:10:08 +0000443 HandleOp(SDOperand(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000444 return false;
445}
446
Dan Gohmana8665142007-06-25 16:23:39 +0000447/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000448/// appropriate for its type.
449void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohmana8665142007-06-25 16:23:39 +0000450 MVT::ValueType VT = Op.getValueType();
451 switch (getTypeAction(VT)) {
Chris Lattner32206f52006-03-18 01:44:44 +0000452 default: assert(0 && "Bad type action!");
Dan Gohmana8665142007-06-25 16:23:39 +0000453 case Legal: (void)LegalizeOp(Op); break;
454 case Promote: (void)PromoteOp(Op); break;
Chris Lattner32206f52006-03-18 01:44:44 +0000455 case Expand:
Dan Gohmana8665142007-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 Lattner32206f52006-03-18 01:44:44 +0000459 SDOperand X, Y;
Chris Lattner2ed652f2007-08-25 01:00:22 +0000460 if (Op.getOpcode() == ISD::TargetConstant)
461 break; // Allow illegal target nodes.
Chris Lattner32206f52006-03-18 01:44:44 +0000462 ExpandOp(Op, X, Y);
Dan Gohmana8665142007-06-25 16:23:39 +0000463 } else if (MVT::getVectorNumElements(VT) == 1) {
464 // If this is an illegal single element vector, convert it to a
465 // scalar operation.
466 (void)ScalarizeVectorOp(Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000467 } else {
Dan Gohmana8665142007-06-25 16:23:39 +0000468 // Otherwise, this is an illegal multiple element vector.
469 // Split it in half and legalize both parts.
470 SDOperand X, Y;
471 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000472 }
473 break;
474 }
475}
Chris Lattner462505f2006-02-13 09:18:02 +0000476
Evan Cheng22cf8992006-12-13 20:57:08 +0000477/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
478/// a load from the constant pool.
479static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000480 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000481 bool Extend = false;
482
483 // If a FP immediate is precise when represented as a float and if the
484 // target can do an extending load from float to double, we put it into
485 // the constant pool as a float, even if it's is statically typed as a
486 // double.
487 MVT::ValueType VT = CFP->getValueType(0);
488 bool isDouble = VT == MVT::f64;
489 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
Dale Johannesend246b2c2007-08-30 00:23:21 +0000490 Type::FloatTy, CFP->getValueAPF());
Evan Cheng22cf8992006-12-13 20:57:08 +0000491 if (!UseCP) {
Dale Johannesen028084e2007-09-12 03:30:33 +0000492 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
493 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000494 }
495
Dale Johannesend246b2c2007-08-30 00:23:21 +0000496 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng47833a12006-12-12 21:32:44 +0000497 // 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 Lattner462505f2006-02-13 09:18:02 +0000513
Evan Cheng003feb02007-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 Cheng3b841dd2007-01-05 21:31:51 +0000519 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000520 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000521 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
522 "fcopysign expansion only supported for f32 and f64");
Evan Cheng003feb02007-01-04 21:56:39 +0000523 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000524
Evan Cheng003feb02007-01-04 21:56:39 +0000525 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000526 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000527 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
528 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000529 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000530 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000531 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-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 Cheng3b841dd2007-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 Cheng003feb02007-01-04 21:56:39 +0000546 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000547 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
548
549 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000550 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
551 return Result;
552}
553
Lauro Ramos Venancio0db44182007-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) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000558 SDOperand Chain = ST->getChain();
559 SDOperand Ptr = ST->getBasePtr();
560 SDOperand Val = ST->getValue();
561 MVT::ValueType VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000562 int Alignment = ST->getAlignment();
563 int SVOffset = ST->getSrcValueOffset();
564 if (MVT::isFloatingPoint(ST->getStoredVT())) {
565 // Expand to a bitconvert of the value to the integer type of the
566 // same size, then a (misaligned) int store.
567 MVT::ValueType intVT;
568 if (VT==MVT::f64)
569 intVT = MVT::i64;
570 else if (VT==MVT::f32)
571 intVT = MVT::i32;
572 else
573 assert(0 && "Unaligned load of unsupported floating point type");
574
575 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
576 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
577 SVOffset, ST->isVolatile(), Alignment);
578 }
579 assert(MVT::isInteger(ST->getStoredVT()) &&
580 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000581 // Get the half-size VT
582 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
583 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000584 int IncrementSize = NumBits / 8;
585
586 // Divide the stored value in two parts.
587 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
588 SDOperand Lo = Val;
589 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
590
591 // Store the two parts
592 SDOperand Store1, Store2;
593 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
594 ST->getSrcValue(), SVOffset, NewStoredVT,
595 ST->isVolatile(), Alignment);
596 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
597 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
598 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
599 ST->getSrcValue(), SVOffset + IncrementSize,
600 NewStoredVT, ST->isVolatile(), Alignment);
601
602 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
603}
604
605/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
606static
607SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
608 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000609 int SVOffset = LD->getSrcValueOffset();
610 SDOperand Chain = LD->getChain();
611 SDOperand Ptr = LD->getBasePtr();
612 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000613 MVT::ValueType LoadedVT = LD->getLoadedVT();
614 if (MVT::isFloatingPoint(VT)) {
615 // Expand to a (misaligned) integer load of the same size,
616 // then bitconvert to floating point.
617 MVT::ValueType intVT;
618 if (LoadedVT==MVT::f64)
619 intVT = MVT::i64;
620 else if (LoadedVT==MVT::f32)
621 intVT = MVT::i32;
622 else
623 assert(0 && "Unaligned load of unsupported floating point type");
624
625 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
626 SVOffset, LD->isVolatile(),
627 LD->getAlignment());
628 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
629 if (LoadedVT != VT)
630 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
631
632 SDOperand Ops[] = { Result, Chain };
633 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
634 Ops, 2);
635 }
636 assert(MVT::isInteger(LoadedVT) && "Unaligned load of unsupported type.");
637 MVT::ValueType NewLoadedVT = LoadedVT - 1;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000638 int NumBits = MVT::getSizeInBits(NewLoadedVT);
639 int Alignment = LD->getAlignment();
640 int IncrementSize = NumBits / 8;
641 ISD::LoadExtType HiExtType = LD->getExtensionType();
642
643 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
644 if (HiExtType == ISD::NON_EXTLOAD)
645 HiExtType = ISD::ZEXTLOAD;
646
647 // Load the value in two parts
648 SDOperand Lo, Hi;
649 if (TLI.isLittleEndian()) {
650 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
651 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
652 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
653 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
654 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
655 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
656 Alignment);
657 } else {
658 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
659 NewLoadedVT,LD->isVolatile(), Alignment);
660 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
661 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
662 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
663 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
664 Alignment);
665 }
666
667 // aggregate the two parts
668 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
669 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
670 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
671
672 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
673 Hi.getValue(1));
674
675 SDOperand Ops[] = { Result, TF };
676 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
677}
Evan Cheng003feb02007-01-04 21:56:39 +0000678
Dan Gohmanff727882007-07-13 20:14:11 +0000679/// LegalizeOp - We know that the specified value has a legal type, and
680/// that its operands are legal. Now ensure that the operation itself
681/// is legal, recursively ensuring that the operands' operations remain
682/// legal.
Chris Lattnerdc750592005-01-07 07:47:09 +0000683SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner2ed652f2007-08-25 01:00:22 +0000684 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
685 return Op;
686
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000687 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000688 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000689 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000690
Chris Lattnerdc750592005-01-07 07:47:09 +0000691 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000692 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000693 if (Node->getNumValues() > 1) {
694 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000695 if (getTypeAction(Node->getValueType(i)) != Legal) {
696 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000697 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000698 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000699 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000700 }
701 }
702
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000703 // Note that LegalizeOp may be reentered even from single-use nodes, which
704 // means that we always must cache transformed nodes.
Chris Lattnered39c862007-02-04 00:50:02 +0000705 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000706 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000707
Nate Begemane5b86d72005-08-10 20:51:12 +0000708 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000709 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000710 bool isCustom = false;
711
Chris Lattnerdc750592005-01-07 07:47:09 +0000712 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000713 case ISD::FrameIndex:
714 case ISD::EntryToken:
715 case ISD::Register:
716 case ISD::BasicBlock:
717 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000718 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000719 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000720 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000721 case ISD::TargetConstantPool:
722 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000723 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000724 case ISD::TargetExternalSymbol:
725 case ISD::VALUETYPE:
726 case ISD::SRCVALUE:
727 case ISD::STRING:
728 case ISD::CONDCODE:
729 // Primitives must all be legal.
730 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
731 "This must be legal!");
732 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000733 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000734 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
735 // If this is a target node, legalize it by legalizing the operands then
736 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000737 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000738 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000739 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000740
Chris Lattner97af9d52006-08-08 01:09:31 +0000741 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000742
743 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
744 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
745 return Result.getValue(Op.ResNo);
746 }
747 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000748#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000749 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000750#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000751 assert(0 && "Do not know how to legalize this operator!");
752 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000753 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000754 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000755 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000756 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000757 case ISD::ConstantPool:
758 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000759 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
760 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000761 case TargetLowering::Custom:
762 Tmp1 = TLI.LowerOperation(Op, DAG);
763 if (Tmp1.Val) Result = Tmp1;
764 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000765 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000766 break;
767 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000768 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000769 case ISD::FRAMEADDR:
770 case ISD::RETURNADDR:
771 // The only option for these nodes is to custom lower them. If the target
772 // does not custom lower them, then return zero.
773 Tmp1 = TLI.LowerOperation(Op, DAG);
774 if (Tmp1.Val)
775 Result = Tmp1;
776 else
777 Result = DAG.getConstant(0, TLI.getPointerTy());
778 break;
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000779 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000780 MVT::ValueType VT = Node->getValueType(0);
781 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
782 default: assert(0 && "This action is not supported yet!");
783 case TargetLowering::Custom:
784 Result = TLI.LowerOperation(Op, DAG);
785 if (Result.Val) break;
786 // Fall Thru
787 case TargetLowering::Legal:
788 Result = DAG.getConstant(0, VT);
789 break;
790 }
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000791 }
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000792 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000793 case ISD::EXCEPTIONADDR: {
794 Tmp1 = LegalizeOp(Node->getOperand(0));
795 MVT::ValueType VT = Node->getValueType(0);
796 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
797 default: assert(0 && "This action is not supported yet!");
798 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000799 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey7f5872c2007-02-22 15:37:19 +0000800 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
801 }
802 break;
803 case TargetLowering::Custom:
804 Result = TLI.LowerOperation(Op, DAG);
805 if (Result.Val) break;
806 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000807 case TargetLowering::Legal: {
808 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
809 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
810 Ops, 2).getValue(Op.ResNo);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000811 break;
812 }
813 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000814 }
Jim Laskey7f5872c2007-02-22 15:37:19 +0000815 break;
Jim Laskey644af6b2007-02-28 20:43:58 +0000816 case ISD::EHSELECTION: {
817 Tmp1 = LegalizeOp(Node->getOperand(0));
818 Tmp2 = LegalizeOp(Node->getOperand(1));
819 MVT::ValueType VT = Node->getValueType(0);
820 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
821 default: assert(0 && "This action is not supported yet!");
822 case TargetLowering::Expand: {
823 unsigned Reg = TLI.getExceptionSelectorRegister();
824 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
825 }
826 break;
827 case TargetLowering::Custom:
828 Result = TLI.LowerOperation(Op, DAG);
829 if (Result.Val) break;
830 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000831 case TargetLowering::Legal: {
832 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
833 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
834 Ops, 2).getValue(Op.ResNo);
Jim Laskey644af6b2007-02-28 20:43:58 +0000835 break;
836 }
837 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000838 }
Jim Laskey644af6b2007-02-28 20:43:58 +0000839 break;
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000840 case ISD::EH_RETURN: {
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000841 MVT::ValueType VT = Node->getValueType(0);
842 // The only "good" option for this node is to custom lower it.
843 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
844 default: assert(0 && "This action is not supported at all!");
845 case TargetLowering::Custom:
846 Result = TLI.LowerOperation(Op, DAG);
847 if (Result.Val) break;
848 // Fall Thru
849 case TargetLowering::Legal:
850 // Target does not know, how to lower this, lower to noop
851 Result = LegalizeOp(Node->getOperand(0));
852 break;
853 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000854 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000855 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000856 case ISD::AssertSext:
857 case ISD::AssertZext:
858 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000859 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000860 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000861 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000862 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000863 Result = Node->getOperand(Op.ResNo);
864 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000865 case ISD::CopyFromReg:
866 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000867 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000868 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000869 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000870 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000871 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000872 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000873 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000874 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
875 } else {
876 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
877 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000878 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
879 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000880 // Since CopyFromReg produces two values, make sure to remember that we
881 // legalized both of them.
882 AddLegalizedOperand(Op.getValue(0), Result);
883 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
884 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000885 case ISD::UNDEF: {
886 MVT::ValueType VT = Op.getValueType();
887 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000888 default: assert(0 && "This action is not supported yet!");
889 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000890 if (MVT::isInteger(VT))
891 Result = DAG.getConstant(0, VT);
892 else if (MVT::isFloatingPoint(VT))
893 Result = DAG.getConstantFP(0, VT);
894 else
895 assert(0 && "Unknown value type!");
896 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000897 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000898 break;
899 }
900 break;
901 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000902
Chris Lattnere55d1712006-03-28 00:40:33 +0000903 case ISD::INTRINSIC_W_CHAIN:
904 case ISD::INTRINSIC_WO_CHAIN:
905 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +0000906 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +0000907 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
908 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000909 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +0000910
911 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000912 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000913 TargetLowering::Custom) {
914 Tmp3 = TLI.LowerOperation(Result, DAG);
915 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000916 }
917
918 if (Result.Val->getNumValues() == 1) break;
919
920 // Must have return value and chain result.
921 assert(Result.Val->getNumValues() == 2 &&
922 "Cannot return more than two values!");
923
924 // Since loads produce two values, make sure to remember that we
925 // legalized both of them.
926 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
927 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
928 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000929 }
Chris Lattner435b4022005-11-29 06:21:05 +0000930
931 case ISD::LOCATION:
932 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
933 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
934
935 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
936 case TargetLowering::Promote:
937 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000938 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +0000939 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000940 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +0000941 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000942
Jim Laskeyc56315c2007-01-26 21:22:28 +0000943 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000944 const std::string &FName =
945 cast<StringSDNode>(Node->getOperand(3))->getValue();
946 const std::string &DirName =
947 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000948 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000949
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000950 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +0000951 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000952 SDOperand LineOp = Node->getOperand(1);
953 SDOperand ColOp = Node->getOperand(2);
954
955 if (useDEBUG_LOC) {
956 Ops.push_back(LineOp); // line #
957 Ops.push_back(ColOp); // col #
958 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000959 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000960 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000961 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
962 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000963 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000964 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskeyf9e54452007-01-26 14:34:52 +0000965 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000966 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000967 } else {
968 Result = Tmp1; // chain
969 }
Chris Lattner435b4022005-11-29 06:21:05 +0000970 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000971 }
Chris Lattner435b4022005-11-29 06:21:05 +0000972 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000973 if (Tmp1 != Node->getOperand(0) ||
974 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +0000975 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +0000976 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000977 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
978 Ops.push_back(Node->getOperand(1)); // line # must be legal.
979 Ops.push_back(Node->getOperand(2)); // col # must be legal.
980 } else {
981 // Otherwise promote them.
982 Ops.push_back(PromoteOp(Node->getOperand(1)));
983 Ops.push_back(PromoteOp(Node->getOperand(2)));
984 }
Chris Lattner435b4022005-11-29 06:21:05 +0000985 Ops.push_back(Node->getOperand(3)); // filename must be legal.
986 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +0000987 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +0000988 }
989 break;
990 }
991 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000992
993 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000994 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000995 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000996 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000997 case TargetLowering::Legal:
998 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
999 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1000 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1001 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001002 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001003 break;
1004 }
1005 break;
1006
Jim Laskeyf9e54452007-01-26 14:34:52 +00001007 case ISD::LABEL:
1008 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1009 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001010 default: assert(0 && "This action is not supported yet!");
1011 case TargetLowering::Legal:
1012 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1013 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001014 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +00001015 break;
Chris Lattner567b9252007-03-03 19:21:38 +00001016 case TargetLowering::Expand:
1017 Result = LegalizeOp(Node->getOperand(0));
1018 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001019 }
Nate Begeman5965bd12006-02-17 05:43:56 +00001020 break;
Chris Lattner435b4022005-11-29 06:21:05 +00001021
Scott Michel9d09c5c2007-08-08 23:23:31 +00001022 case ISD::Constant: {
1023 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1024 unsigned opAction =
1025 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1026
Chris Lattnerdc750592005-01-07 07:47:09 +00001027 // We know we don't need to expand constants here, constants only have one
1028 // value and we check that it is fine above.
1029
Scott Michel9d09c5c2007-08-08 23:23:31 +00001030 if (opAction == TargetLowering::Custom) {
1031 Tmp1 = TLI.LowerOperation(Result, DAG);
1032 if (Tmp1.Val)
1033 Result = Tmp1;
1034 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001035 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +00001036 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001037 case ISD::ConstantFP: {
1038 // Spill FP immediates to the constant pool if the target cannot directly
1039 // codegen them. Targets often have some immediate values that can be
1040 // efficiently generated into an FP register without a load. We explicitly
1041 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +00001042 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1043
1044 // Check to see if this FP immediate is already legal.
1045 bool isLegal = false;
1046 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1047 E = TLI.legal_fpimm_end(); I != E; ++I)
1048 if (CFP->isExactlyValue(*I)) {
1049 isLegal = true;
1050 break;
1051 }
1052
Chris Lattner758b0ac2006-01-29 06:26:56 +00001053 // If this is a legal constant, turn it into a TargetConstantFP node.
1054 if (isLegal) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00001055 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1056 CFP->getValueType(0));
Chris Lattner758b0ac2006-01-29 06:26:56 +00001057 break;
1058 }
1059
1060 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1061 default: assert(0 && "This action is not supported yet!");
1062 case TargetLowering::Custom:
1063 Tmp3 = TLI.LowerOperation(Result, DAG);
1064 if (Tmp3.Val) {
1065 Result = Tmp3;
1066 break;
1067 }
1068 // FALLTHROUGH
1069 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +00001070 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001071 }
1072 break;
1073 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001074 case ISD::TokenFactor:
1075 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001076 Tmp1 = LegalizeOp(Node->getOperand(0));
1077 Tmp2 = LegalizeOp(Node->getOperand(1));
1078 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1079 } else if (Node->getNumOperands() == 3) {
1080 Tmp1 = LegalizeOp(Node->getOperand(0));
1081 Tmp2 = LegalizeOp(Node->getOperand(1));
1082 Tmp3 = LegalizeOp(Node->getOperand(2));
1083 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001084 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +00001085 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001086 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001087 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1088 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001089 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001090 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001091 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001092
1093 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001094 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001095 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001096 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1097 assert(Tmp3.Val && "Target didn't custom lower this node!");
1098 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1099 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001100
Chris Lattneraaa23d92006-05-16 22:53:20 +00001101 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001102 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +00001103 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1104 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001105 if (Op.ResNo == i)
1106 Tmp2 = Tmp1;
1107 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1108 }
1109 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001110 case ISD::EXTRACT_SUBREG: {
1111 Tmp1 = LegalizeOp(Node->getOperand(0));
1112 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1113 assert(idx && "Operand must be a constant");
1114 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1115 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1116 }
1117 break;
1118 case ISD::INSERT_SUBREG: {
1119 Tmp1 = LegalizeOp(Node->getOperand(0));
1120 Tmp2 = LegalizeOp(Node->getOperand(1));
1121 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1122 assert(idx && "Operand must be a constant");
1123 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1124 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1125 }
1126 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001127 case ISD::BUILD_VECTOR:
1128 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001129 default: assert(0 && "This action is not supported yet!");
1130 case TargetLowering::Custom:
1131 Tmp3 = TLI.LowerOperation(Result, DAG);
1132 if (Tmp3.Val) {
1133 Result = Tmp3;
1134 break;
1135 }
1136 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001137 case TargetLowering::Expand:
1138 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001139 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001140 }
Chris Lattner29b23012006-03-19 01:17:20 +00001141 break;
1142 case ISD::INSERT_VECTOR_ELT:
1143 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1144 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1145 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1146 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1147
1148 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1149 Node->getValueType(0))) {
1150 default: assert(0 && "This action is not supported yet!");
1151 case TargetLowering::Legal:
1152 break;
1153 case TargetLowering::Custom:
1154 Tmp3 = TLI.LowerOperation(Result, DAG);
1155 if (Tmp3.Val) {
1156 Result = Tmp3;
1157 break;
1158 }
1159 // FALLTHROUGH
1160 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001161 // If the insert index is a constant, codegen this as a scalar_to_vector,
1162 // then a shuffle that inserts it into the right position in the vector.
1163 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1164 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1165 Tmp1.getValueType(), Tmp2);
1166
1167 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1168 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman5c441312007-06-14 22:58:02 +00001169 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner326870b2006-04-17 19:21:01 +00001170
1171 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1172 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1173 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001174 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +00001175 for (unsigned i = 0; i != NumElts; ++i) {
1176 if (i != InsertPos->getValue())
1177 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1178 else
1179 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1180 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001181 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1182 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +00001183
1184 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1185 Tmp1, ScVec, ShufMask);
1186 Result = LegalizeOp(Result);
1187 break;
1188 }
1189
Chris Lattner29b23012006-03-19 01:17:20 +00001190 // If the target doesn't support this, we have to spill the input vector
1191 // to a temporary stack slot, update the element, then reload it. This is
1192 // badness. We could also load the value into a vector register (either
1193 // with a "move to register" or "extload into register" instruction, then
1194 // permute it into place, if the idx is a constant and if the idx is
1195 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001196 MVT::ValueType VT = Tmp1.getValueType();
1197 MVT::ValueType EltVT = Tmp2.getValueType();
1198 MVT::ValueType IdxVT = Tmp3.getValueType();
1199 MVT::ValueType PtrVT = TLI.getPointerTy();
1200 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001201 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +00001202 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001203
1204 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001205 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1206 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001207 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001208 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1209 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1210 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001211 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +00001212 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001213 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +00001214 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +00001215 break;
1216 }
1217 }
1218 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001219 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001220 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1221 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1222 break;
1223 }
1224
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001225 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1226 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1227 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1228 Node->getValueType(0))) {
1229 default: assert(0 && "This action is not supported yet!");
1230 case TargetLowering::Legal:
1231 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001232 case TargetLowering::Custom:
1233 Tmp3 = TLI.LowerOperation(Result, DAG);
1234 if (Tmp3.Val) {
1235 Result = Tmp3;
1236 break;
1237 }
1238 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001239 case TargetLowering::Expand:
1240 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001241 break;
1242 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001243 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001244 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001245 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1246 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1247 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1248
1249 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001250 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1251 default: assert(0 && "Unknown operation action!");
1252 case TargetLowering::Legal:
1253 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1254 "vector shuffle should not be created if not legal!");
1255 break;
1256 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001257 Tmp3 = TLI.LowerOperation(Result, DAG);
1258 if (Tmp3.Val) {
1259 Result = Tmp3;
1260 break;
1261 }
1262 // FALLTHROUGH
1263 case TargetLowering::Expand: {
1264 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00001265 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng9fa89592006-04-05 06:07:11 +00001266 MVT::ValueType PtrVT = TLI.getPointerTy();
1267 SDOperand Mask = Node->getOperand(2);
1268 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001269 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001270 for (unsigned i = 0; i != NumElems; ++i) {
1271 SDOperand Arg = Mask.getOperand(i);
1272 if (Arg.getOpcode() == ISD::UNDEF) {
1273 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1274 } else {
1275 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1276 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1277 if (Idx < NumElems)
1278 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1279 DAG.getConstant(Idx, PtrVT)));
1280 else
1281 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1282 DAG.getConstant(Idx - NumElems, PtrVT)));
1283 }
1284 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001285 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001286 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001287 }
Chris Lattner6be79822006-04-04 17:23:26 +00001288 case TargetLowering::Promote: {
1289 // Change base type to a different vector type.
1290 MVT::ValueType OVT = Node->getValueType(0);
1291 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1292
1293 // Cast the two input vectors.
1294 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1295 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1296
1297 // Convert the shuffle mask to the right # elements.
1298 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1299 assert(Tmp3.Val && "Shuffle not legal?");
1300 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1301 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1302 break;
1303 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001304 }
1305 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001306
1307 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001308 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001309 Tmp2 = LegalizeOp(Node->getOperand(1));
1310 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001311 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001312 break;
1313
Dan Gohmana8665142007-06-25 16:23:39 +00001314 case ISD::EXTRACT_SUBVECTOR:
1315 Tmp1 = Node->getOperand(0);
1316 Tmp2 = LegalizeOp(Node->getOperand(1));
1317 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1318 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001319 break;
1320
Chris Lattner462505f2006-02-13 09:18:02 +00001321 case ISD::CALLSEQ_START: {
1322 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1323
1324 // Recursively Legalize all of the inputs of the call end that do not lead
1325 // to this call start. This ensures that any libcalls that need be inserted
1326 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001327 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001328 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001329 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1330 NodesLeadingTo);
1331 }
Chris Lattner462505f2006-02-13 09:18:02 +00001332
1333 // Now that we legalized all of the inputs (which may have inserted
1334 // libcalls) create the new CALLSEQ_START node.
1335 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1336
1337 // Merge in the last call, to ensure that this call start after the last
1338 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001339 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001340 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1341 Tmp1 = LegalizeOp(Tmp1);
1342 }
Chris Lattner462505f2006-02-13 09:18:02 +00001343
1344 // Do not try to legalize the target-specific arguments (#1+).
1345 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001346 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001347 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001348 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001349 }
1350
1351 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001352 AddLegalizedOperand(Op.getValue(0), Result);
1353 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1354 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1355
Chris Lattner462505f2006-02-13 09:18:02 +00001356 // Now that the callseq_start and all of the non-call nodes above this call
1357 // sequence have been legalized, legalize the call itself. During this
1358 // process, no libcalls can/will be inserted, guaranteeing that no calls
1359 // can overlap.
1360 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1361 SDOperand InCallSEQ = LastCALLSEQ_END;
1362 // Note that we are selecting this call!
1363 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1364 IsLegalizingCall = true;
1365
1366 // Legalize the call, starting from the CALLSEQ_END.
1367 LegalizeOp(LastCALLSEQ_END);
1368 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1369 return Result;
1370 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001371 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001372 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1373 // will cause this node to be legalized as well as handling libcalls right.
1374 if (LastCALLSEQ_END.Val != Node) {
1375 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001376 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001377 assert(I != LegalizedNodes.end() &&
1378 "Legalizing the call start should have legalized this node!");
1379 return I->second;
1380 }
1381
1382 // Otherwise, the call start has been legalized and everything is going
1383 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001384 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001385 // Do not try to legalize the target-specific arguments (#1+), except for
1386 // an optional flag input.
1387 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1388 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001389 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001390 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001391 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001392 }
1393 } else {
1394 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1395 if (Tmp1 != Node->getOperand(0) ||
1396 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001397 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001398 Ops[0] = Tmp1;
1399 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001400 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001401 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001402 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001403 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001404 // This finishes up call legalization.
1405 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001406
1407 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1408 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1409 if (Node->getNumValues() == 2)
1410 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1411 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001412 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng631ccc62007-08-16 23:50:06 +00001413 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerec26b482005-01-09 19:03:49 +00001414 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1415 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1416 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001417 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001418
Chris Lattnerd02b0542006-01-28 10:58:55 +00001419 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001420 Tmp2 = Result.getValue(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001421 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Cheng7f4ec822006-01-11 22:14:47 +00001422 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001423 case TargetLowering::Expand: {
1424 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1425 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1426 " not tell us which reg is the stack pointer!");
1427 SDOperand Chain = Tmp1.getOperand(0);
1428 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001429 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1430 Chain = SP.getValue(1);
1431 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1432 unsigned StackAlign =
1433 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1434 if (Align > StackAlign)
Evan Chengcb6d65e2007-08-17 18:02:22 +00001435 SP = DAG.getNode(ISD::AND, VT, SP,
1436 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng631ccc62007-08-16 23:50:06 +00001437 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
1438 Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001439 Tmp1 = LegalizeOp(Tmp1);
1440 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001441 break;
1442 }
1443 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001444 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1445 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001446 Tmp1 = LegalizeOp(Tmp3);
1447 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001448 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001449 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001450 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001451 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001452 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001453 // Since this op produce two values, make sure to remember that we
1454 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001455 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1456 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001457 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001458 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001459 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001460 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001461 bool Changed = false;
1462 // Legalize all of the operands of the inline asm, in case they are nodes
1463 // that need to be expanded or something. Note we skip the asm string and
1464 // all of the TargetConstant flags.
1465 SDOperand Op = LegalizeOp(Ops[0]);
1466 Changed = Op != Ops[0];
1467 Ops[0] = Op;
1468
1469 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1470 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1471 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1472 for (++i; NumVals; ++i, --NumVals) {
1473 SDOperand Op = LegalizeOp(Ops[i]);
1474 if (Op != Ops[i]) {
1475 Changed = true;
1476 Ops[i] = Op;
1477 }
1478 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001479 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001480
1481 if (HasInFlag) {
1482 Op = LegalizeOp(Ops.back());
1483 Changed |= Op != Ops.back();
1484 Ops.back() = Op;
1485 }
1486
1487 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001488 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001489
1490 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001491 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001492 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1493 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001494 }
Chris Lattner68a12142005-01-07 22:12:08 +00001495 case ISD::BR:
1496 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001497 // Ensure that libcalls are emitted before a branch.
1498 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1499 Tmp1 = LegalizeOp(Tmp1);
1500 LastCALLSEQ_END = DAG.getEntryNode();
1501
Chris Lattnerd02b0542006-01-28 10:58:55 +00001502 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001503 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001504 case ISD::BRIND:
1505 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1506 // Ensure that libcalls are emitted before a branch.
1507 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1508 Tmp1 = LegalizeOp(Tmp1);
1509 LastCALLSEQ_END = DAG.getEntryNode();
1510
1511 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1512 default: assert(0 && "Indirect target must be legal type (pointer)!");
1513 case Legal:
1514 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1515 break;
1516 }
1517 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1518 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001519 case ISD::BR_JT:
1520 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1521 // Ensure that libcalls are emitted before a branch.
1522 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1523 Tmp1 = LegalizeOp(Tmp1);
1524 LastCALLSEQ_END = DAG.getEntryNode();
1525
1526 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1527 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1528
1529 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1530 default: assert(0 && "This action is not supported yet!");
1531 case TargetLowering::Legal: break;
1532 case TargetLowering::Custom:
1533 Tmp1 = TLI.LowerOperation(Result, DAG);
1534 if (Tmp1.Val) Result = Tmp1;
1535 break;
1536 case TargetLowering::Expand: {
1537 SDOperand Chain = Result.getOperand(0);
1538 SDOperand Table = Result.getOperand(1);
1539 SDOperand Index = Result.getOperand(2);
1540
1541 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001542 MachineFunction &MF = DAG.getMachineFunction();
1543 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001544 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1545 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001546
1547 SDOperand LD;
1548 switch (EntrySize) {
1549 default: assert(0 && "Size of jump table not supported yet."); break;
1550 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1551 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1552 }
1553
1554 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001555 // For PIC, the sequence is:
1556 // BRIND(load(Jumptable + index) + RelocBase)
1557 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1558 SDOperand Reloc;
1559 if (TLI.usesGlobalOffsetTable())
1560 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1561 else
1562 Reloc = Table;
Evan Chenge6d58472006-10-31 02:31:00 +00001563 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng84a28d42006-10-30 08:00:44 +00001564 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1565 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1566 } else {
1567 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1568 }
1569 }
1570 }
1571 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001572 case ISD::BRCOND:
1573 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001574 // Ensure that libcalls are emitted before a return.
1575 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1576 Tmp1 = LegalizeOp(Tmp1);
1577 LastCALLSEQ_END = DAG.getEntryNode();
1578
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001579 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1580 case Expand: assert(0 && "It's impossible to expand bools");
1581 case Legal:
1582 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1583 break;
1584 case Promote:
1585 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001586
1587 // The top bits of the promoted condition are not necessarily zero, ensure
1588 // that the value is properly zero extended.
Dan Gohman309d3d52007-06-22 14:59:07 +00001589 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerdb189382006-11-27 04:39:56 +00001590 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1591 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001592 break;
1593 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001594
1595 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001596 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001597
1598 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1599 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001600 case TargetLowering::Legal: break;
1601 case TargetLowering::Custom:
1602 Tmp1 = TLI.LowerOperation(Result, DAG);
1603 if (Tmp1.Val) Result = Tmp1;
1604 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001605 case TargetLowering::Expand:
1606 // Expand brcond's setcc into its constituent parts and create a BR_CC
1607 // Node.
1608 if (Tmp2.getOpcode() == ISD::SETCC) {
1609 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1610 Tmp2.getOperand(0), Tmp2.getOperand(1),
1611 Node->getOperand(2));
1612 } else {
1613 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1614 DAG.getCondCode(ISD::SETNE), Tmp2,
1615 DAG.getConstant(0, Tmp2.getValueType()),
1616 Node->getOperand(2));
1617 }
1618 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001619 }
1620 break;
1621 case ISD::BR_CC:
1622 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001623 // Ensure that libcalls are emitted before a branch.
1624 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1625 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001626 Tmp2 = Node->getOperand(2); // LHS
1627 Tmp3 = Node->getOperand(3); // RHS
1628 Tmp4 = Node->getOperand(1); // CC
1629
1630 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001631 LastCALLSEQ_END = DAG.getEntryNode();
1632
Nate Begeman7e7f4392006-02-01 07:19:44 +00001633 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1634 // the LHS is a legal SETCC itself. In this case, we need to compare
1635 // the result against zero to select between true and false values.
1636 if (Tmp3.Val == 0) {
1637 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1638 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001639 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001640
1641 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1642 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001643
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001644 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1645 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001646 case TargetLowering::Legal: break;
1647 case TargetLowering::Custom:
1648 Tmp4 = TLI.LowerOperation(Result, DAG);
1649 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001650 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001651 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001652 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001653 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001654 LoadSDNode *LD = cast<LoadSDNode>(Node);
1655 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1656 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001657
Evan Chenge71fe34d2006-10-09 20:57:25 +00001658 ISD::LoadExtType ExtType = LD->getExtensionType();
1659 if (ExtType == ISD::NON_EXTLOAD) {
1660 MVT::ValueType VT = Node->getValueType(0);
1661 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1662 Tmp3 = Result.getValue(0);
1663 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001664
Evan Chenge71fe34d2006-10-09 20:57:25 +00001665 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1666 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001667 case TargetLowering::Legal:
1668 // If this is an unaligned load and the target doesn't support it,
1669 // expand it.
1670 if (!TLI.allowsUnalignedMemoryAccesses()) {
1671 unsigned ABIAlignment = TLI.getTargetData()->
1672 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1673 if (LD->getAlignment() < ABIAlignment){
1674 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1675 TLI);
1676 Tmp3 = Result.getOperand(0);
1677 Tmp4 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001678 Tmp3 = LegalizeOp(Tmp3);
1679 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001680 }
1681 }
1682 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001683 case TargetLowering::Custom:
1684 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1685 if (Tmp1.Val) {
1686 Tmp3 = LegalizeOp(Tmp1);
1687 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001688 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001689 break;
1690 case TargetLowering::Promote: {
1691 // Only promote a load of vector type to another.
1692 assert(MVT::isVector(VT) && "Cannot promote this load!");
1693 // Change base type to a different vector type.
1694 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1695
1696 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001697 LD->getSrcValueOffset(),
1698 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001699 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1700 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001701 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001702 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001703 }
1704 // Since loads produce two values, make sure to remember that we
1705 // legalized both of them.
1706 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1707 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1708 return Op.ResNo ? Tmp4 : Tmp3;
1709 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001710 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001711 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1712 default: assert(0 && "This action is not supported yet!");
1713 case TargetLowering::Promote:
1714 assert(SrcVT == MVT::i1 &&
1715 "Can only promote extending LOAD from i1 -> i8!");
1716 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1717 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00001718 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001719 Tmp1 = Result.getValue(0);
1720 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001721 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001722 case TargetLowering::Custom:
1723 isCustom = true;
1724 // FALLTHROUGH
1725 case TargetLowering::Legal:
1726 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1727 Tmp1 = Result.getValue(0);
1728 Tmp2 = Result.getValue(1);
1729
1730 if (isCustom) {
1731 Tmp3 = TLI.LowerOperation(Result, DAG);
1732 if (Tmp3.Val) {
1733 Tmp1 = LegalizeOp(Tmp3);
1734 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1735 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001736 } else {
1737 // If this is an unaligned load and the target doesn't support it,
1738 // expand it.
1739 if (!TLI.allowsUnalignedMemoryAccesses()) {
1740 unsigned ABIAlignment = TLI.getTargetData()->
1741 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1742 if (LD->getAlignment() < ABIAlignment){
1743 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1744 TLI);
1745 Tmp1 = Result.getOperand(0);
1746 Tmp2 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001747 Tmp1 = LegalizeOp(Tmp1);
1748 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001749 }
1750 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001751 }
1752 break;
1753 case TargetLowering::Expand:
1754 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1755 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1756 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001757 LD->getSrcValueOffset(),
1758 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001759 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1760 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1761 Tmp2 = LegalizeOp(Load.getValue(1));
1762 break;
1763 }
Chris Lattner296a83c2007-02-01 04:55:59 +00001764 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001765 // Turn the unsupported load into an EXTLOAD followed by an explicit
1766 // zero/sign extend inreg.
1767 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1768 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001769 LD->getSrcValueOffset(), SrcVT,
1770 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001771 SDOperand ValRes;
1772 if (ExtType == ISD::SEXTLOAD)
1773 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1774 Result, DAG.getValueType(SrcVT));
1775 else
1776 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1777 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1778 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1779 break;
1780 }
1781 // Since loads produce two values, make sure to remember that we legalized
1782 // both of them.
1783 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1784 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1785 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001786 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001787 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001788 case ISD::EXTRACT_ELEMENT: {
1789 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1790 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001791 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001792 case Legal:
1793 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1794 // 1 -> Hi
1795 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1796 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1797 TLI.getShiftAmountTy()));
1798 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1799 } else {
1800 // 0 -> Lo
1801 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1802 Node->getOperand(0));
1803 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001804 break;
1805 case Expand:
1806 // Get both the low and high parts.
1807 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1808 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1809 Result = Tmp2; // 1 -> Hi
1810 else
1811 Result = Tmp1; // 0 -> Lo
1812 break;
1813 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001814 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001815 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001816
1817 case ISD::CopyToReg:
1818 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001819
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001820 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001821 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001822 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001823 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001824 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001825 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001826 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001827 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001828 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001829 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001830 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1831 Tmp3);
1832 } else {
1833 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001834 }
1835
1836 // Since this produces two values, make sure to remember that we legalized
1837 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001838 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001839 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001840 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001841 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001842 break;
1843
1844 case ISD::RET:
1845 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001846
1847 // Ensure that libcalls are emitted before a return.
1848 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1849 Tmp1 = LegalizeOp(Tmp1);
1850 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001851
Chris Lattnerdc750592005-01-07 07:47:09 +00001852 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001853 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001854 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001855 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001856 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001857 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001858 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001859 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001860 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +00001861 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001862 SDOperand Lo, Hi;
1863 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00001864
1865 // Big endian systems want the hi reg first.
1866 if (!TLI.isLittleEndian())
1867 std::swap(Lo, Hi);
1868
Evan Cheng3432ab92006-12-11 19:27:14 +00001869 if (Hi.Val)
1870 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1871 else
1872 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001873 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001874 } else {
1875 SDNode *InVal = Tmp2.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00001876 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1877 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001878
Dan Gohmana8665142007-06-25 16:23:39 +00001879 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001880 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001881 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00001882 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001883 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00001884 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001885 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001886 } else if (NumElems == 1) {
1887 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00001888 Tmp2 = ScalarizeVectorOp(Tmp2);
1889 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001890 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001891
1892 // FIXME: Returns of gcc generic vectors smaller than a legal type
1893 // should be returned in integer registers!
1894
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001895 // The scalarized value type may not be legal, e.g. it might require
1896 // promotion or expansion. Relegalize the return.
1897 Result = LegalizeOp(Result);
1898 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001899 // FIXME: Returns of gcc generic vectors larger than a legal vector
1900 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001901 SDOperand Lo, Hi;
1902 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00001903 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001904 Result = LegalizeOp(Result);
1905 }
1906 }
Misha Brukman835702a2005-04-21 22:36:52 +00001907 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001908 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001909 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00001910 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001911 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001912 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001913 }
1914 break;
1915 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001916 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001917 break;
1918 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00001919 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00001920 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00001921 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00001922 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1923 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001924 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00001925 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001926 break;
1927 case Expand: {
1928 SDOperand Lo, Hi;
Dan Gohman3b62d722007-06-27 16:08:04 +00001929 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001930 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001931 ExpandOp(Node->getOperand(i), Lo, Hi);
1932 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00001933 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00001934 if (Hi.Val) {
1935 NewValues.push_back(Hi);
1936 NewValues.push_back(Node->getOperand(i+1));
1937 }
Misha Brukman835702a2005-04-21 22:36:52 +00001938 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001939 }
1940 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001941 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001942 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001943
1944 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00001945 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00001946 else
Chris Lattner97af9d52006-08-08 01:09:31 +00001947 Result = DAG.getNode(ISD::RET, MVT::Other,
1948 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00001949 break;
1950 }
1951 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001952
Chris Lattner4d1ea712006-01-29 21:02:23 +00001953 if (Result.getOpcode() == ISD::RET) {
1954 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1955 default: assert(0 && "This action is not supported yet!");
1956 case TargetLowering::Legal: break;
1957 case TargetLowering::Custom:
1958 Tmp1 = TLI.LowerOperation(Result, DAG);
1959 if (Tmp1.Val) Result = Tmp1;
1960 break;
1961 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001962 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001963 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001964 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00001965 StoreSDNode *ST = cast<StoreSDNode>(Node);
1966 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1967 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00001968 int SVOffset = ST->getSrcValueOffset();
1969 unsigned Alignment = ST->getAlignment();
1970 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00001971
Evan Chengab51cf22006-10-13 21:14:26 +00001972 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001973 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1974 // FIXME: We shouldn't do this for TargetConstantFP's.
1975 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1976 // to phase ordering between legalized code and the dag combiner. This
1977 // probably means that we need to integrate dag combiner and legalizer
1978 // together.
1979 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1980 if (CFP->getValueType(0) == MVT::f32) {
Dale Johannesen028084e2007-09-12 03:30:33 +00001981 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
1982 convertToAPInt().getZExtValue(),
Dale Johannesen245dceb2007-09-11 18:32:33 +00001983 MVT::i32);
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001984 } else {
1985 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Dale Johannesen028084e2007-09-12 03:30:33 +00001986 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
1987 getZExtValue(), MVT::i64);
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001988 }
1989 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001990 SVOffset, isVolatile, Alignment);
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001991 break;
1992 }
1993
Evan Chengab51cf22006-10-13 21:14:26 +00001994 switch (getTypeAction(ST->getStoredVT())) {
1995 case Legal: {
1996 Tmp3 = LegalizeOp(ST->getValue());
1997 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1998 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00001999
Evan Chengab51cf22006-10-13 21:14:26 +00002000 MVT::ValueType VT = Tmp3.getValueType();
2001 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2002 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002003 case TargetLowering::Legal:
2004 // If this is an unaligned store and the target doesn't support it,
2005 // expand it.
2006 if (!TLI.allowsUnalignedMemoryAccesses()) {
2007 unsigned ABIAlignment = TLI.getTargetData()->
2008 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2009 if (ST->getAlignment() < ABIAlignment)
2010 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2011 TLI);
2012 }
2013 break;
Evan Chengab51cf22006-10-13 21:14:26 +00002014 case TargetLowering::Custom:
2015 Tmp1 = TLI.LowerOperation(Result, DAG);
2016 if (Tmp1.Val) Result = Tmp1;
2017 break;
2018 case TargetLowering::Promote:
2019 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2020 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2021 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2022 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00002023 ST->getSrcValue(), SVOffset, isVolatile,
2024 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002025 break;
2026 }
2027 break;
2028 }
2029 case Promote:
2030 // Truncate the value and store the result.
2031 Tmp3 = PromoteOp(ST->getValue());
2032 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002033 SVOffset, ST->getStoredVT(),
2034 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002035 break;
2036
2037 case Expand:
2038 unsigned IncrementSize = 0;
2039 SDOperand Lo, Hi;
2040
2041 // If this is a vector type, then we have to calculate the increment as
2042 // the product of the element size in bytes, and the number of elements
2043 // in the high half of the vector.
Dan Gohmana8665142007-06-25 16:23:39 +00002044 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002045 SDNode *InVal = ST->getValue().Val;
Dan Gohmana8665142007-06-25 16:23:39 +00002046 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
2047 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Chengab51cf22006-10-13 21:14:26 +00002048
Dan Gohmana8665142007-06-25 16:23:39 +00002049 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002050 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00002051 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002052 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002053 // Turn this into a normal store of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002054 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00002055 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002056 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002057 Result = LegalizeOp(Result);
2058 break;
2059 } else if (NumElems == 1) {
2060 // Turn this into a normal store of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002061 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00002062 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002063 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002064 // The scalarized value type may not be legal, e.g. it might require
2065 // promotion or expansion. Relegalize the scalar store.
2066 Result = LegalizeOp(Result);
2067 break;
2068 } else {
2069 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2070 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2071 }
2072 } else {
2073 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00002074 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002075
2076 if (!TLI.isLittleEndian())
2077 std::swap(Lo, Hi);
2078 }
2079
2080 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002081 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002082
2083 if (Hi.Val == NULL) {
2084 // Must be int <-> float one-to-one expansion.
2085 Result = Lo;
2086 break;
2087 }
2088
Evan Chengab51cf22006-10-13 21:14:26 +00002089 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2090 getIntPtrConstant(IncrementSize));
2091 assert(isTypeLegal(Tmp2.getValueType()) &&
2092 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002093 SVOffset += IncrementSize;
2094 if (Alignment > IncrementSize)
2095 Alignment = IncrementSize;
Evan Chengab51cf22006-10-13 21:14:26 +00002096 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002097 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002098 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2099 break;
2100 }
2101 } else {
2102 // Truncating store
2103 assert(isTypeLegal(ST->getValue().getValueType()) &&
2104 "Cannot handle illegal TRUNCSTORE yet!");
2105 Tmp3 = LegalizeOp(ST->getValue());
2106
2107 // The only promote case we handle is TRUNCSTORE:i1 X into
2108 // -> TRUNCSTORE:i8 (and X, 1)
2109 if (ST->getStoredVT() == MVT::i1 &&
2110 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2111 // Promote the bool to a mask then store.
2112 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2113 DAG.getConstant(1, Tmp3.getValueType()));
2114 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002115 SVOffset, MVT::i8,
2116 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002117 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2118 Tmp2 != ST->getBasePtr()) {
2119 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2120 ST->getOffset());
2121 }
2122
2123 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2124 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002125 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002126 case TargetLowering::Legal:
2127 // If this is an unaligned store and the target doesn't support it,
2128 // expand it.
2129 if (!TLI.allowsUnalignedMemoryAccesses()) {
2130 unsigned ABIAlignment = TLI.getTargetData()->
2131 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2132 if (ST->getAlignment() < ABIAlignment)
2133 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2134 TLI);
2135 }
2136 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002137 case TargetLowering::Custom:
2138 Tmp1 = TLI.LowerOperation(Result, DAG);
2139 if (Tmp1.Val) Result = Tmp1;
2140 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002141 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002142 }
2143 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002144 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002145 case ISD::PCMARKER:
2146 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002147 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002148 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002149 case ISD::STACKSAVE:
2150 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002151 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2152 Tmp1 = Result.getValue(0);
2153 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002154
Chris Lattnerb3266452006-01-13 02:50:02 +00002155 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2156 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002157 case TargetLowering::Legal: break;
2158 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002159 Tmp3 = TLI.LowerOperation(Result, DAG);
2160 if (Tmp3.Val) {
2161 Tmp1 = LegalizeOp(Tmp3);
2162 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002163 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002164 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002165 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002166 // Expand to CopyFromReg if the target set
2167 // StackPointerRegisterToSaveRestore.
2168 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002169 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002170 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002171 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002172 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002173 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2174 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002175 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002176 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002177 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002178
2179 // Since stacksave produce two values, make sure to remember that we
2180 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002181 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2182 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2183 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002184
Chris Lattnerb3266452006-01-13 02:50:02 +00002185 case ISD::STACKRESTORE:
2186 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2187 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002188 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002189
2190 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2191 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002192 case TargetLowering::Legal: break;
2193 case TargetLowering::Custom:
2194 Tmp1 = TLI.LowerOperation(Result, DAG);
2195 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002196 break;
2197 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002198 // Expand to CopyToReg if the target set
2199 // StackPointerRegisterToSaveRestore.
2200 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2201 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2202 } else {
2203 Result = Tmp1;
2204 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002205 break;
2206 }
2207 break;
2208
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002209 case ISD::READCYCLECOUNTER:
2210 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002211 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002212 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2213 Node->getValueType(0))) {
2214 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002215 case TargetLowering::Legal:
2216 Tmp1 = Result.getValue(0);
2217 Tmp2 = Result.getValue(1);
2218 break;
Evan Cheng69739932006-11-29 08:26:18 +00002219 case TargetLowering::Custom:
2220 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002221 Tmp1 = LegalizeOp(Result.getValue(0));
2222 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002223 break;
2224 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002225
2226 // Since rdcc produce two values, make sure to remember that we legalized
2227 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002228 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2229 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002230 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002231
Chris Lattner39c67442005-01-14 22:08:15 +00002232 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002233 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2234 case Expand: assert(0 && "It's impossible to expand bools");
2235 case Legal:
2236 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2237 break;
2238 case Promote:
2239 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002240 // Make sure the condition is either zero or one.
Dan Gohman309d3d52007-06-22 14:59:07 +00002241 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattner3abb6362006-11-28 01:03:30 +00002242 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2243 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002244 break;
2245 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002246 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002247 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002248
Chris Lattnerd02b0542006-01-28 10:58:55 +00002249 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002250
Nate Begeman987121a2005-08-23 04:29:48 +00002251 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002252 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002253 case TargetLowering::Legal: break;
2254 case TargetLowering::Custom: {
2255 Tmp1 = TLI.LowerOperation(Result, DAG);
2256 if (Tmp1.Val) Result = Tmp1;
2257 break;
2258 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002259 case TargetLowering::Expand:
2260 if (Tmp1.getOpcode() == ISD::SETCC) {
2261 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2262 Tmp2, Tmp3,
2263 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2264 } else {
2265 Result = DAG.getSelectCC(Tmp1,
2266 DAG.getConstant(0, Tmp1.getValueType()),
2267 Tmp2, Tmp3, ISD::SETNE);
2268 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002269 break;
2270 case TargetLowering::Promote: {
2271 MVT::ValueType NVT =
2272 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2273 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002274 if (MVT::isVector(Tmp2.getValueType())) {
2275 ExtOp = ISD::BIT_CONVERT;
2276 TruncOp = ISD::BIT_CONVERT;
2277 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002278 ExtOp = ISD::ANY_EXTEND;
2279 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002280 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002281 ExtOp = ISD::FP_EXTEND;
2282 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002283 }
2284 // Promote each of the values to the new type.
2285 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2286 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2287 // Perform the larger operation, then round down.
2288 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2289 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2290 break;
2291 }
2292 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002293 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002294 case ISD::SELECT_CC: {
2295 Tmp1 = Node->getOperand(0); // LHS
2296 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002297 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2298 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002299 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002300
Nate Begeman7e7f4392006-02-01 07:19:44 +00002301 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2302
2303 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2304 // the LHS is a legal SETCC itself. In this case, we need to compare
2305 // the result against zero to select between true and false values.
2306 if (Tmp2.Val == 0) {
2307 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2308 CC = DAG.getCondCode(ISD::SETNE);
2309 }
2310 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2311
2312 // Everything is legal, see if we should expand this op or something.
2313 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2314 default: assert(0 && "This action is not supported yet!");
2315 case TargetLowering::Legal: break;
2316 case TargetLowering::Custom:
2317 Tmp1 = TLI.LowerOperation(Result, DAG);
2318 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002319 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002320 }
2321 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002322 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002323 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002324 Tmp1 = Node->getOperand(0);
2325 Tmp2 = Node->getOperand(1);
2326 Tmp3 = Node->getOperand(2);
2327 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2328
2329 // If we had to Expand the SetCC operands into a SELECT node, then it may
2330 // not always be possible to return a true LHS & RHS. In this case, just
2331 // return the value we legalized, returned in the LHS
2332 if (Tmp2.Val == 0) {
2333 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002334 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002335 }
Nate Begeman987121a2005-08-23 04:29:48 +00002336
Chris Lattnerf263a232006-01-30 22:43:50 +00002337 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002338 default: assert(0 && "Cannot handle this action for SETCC yet!");
2339 case TargetLowering::Custom:
2340 isCustom = true;
2341 // FALLTHROUGH.
2342 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002343 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002344 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002345 Tmp4 = TLI.LowerOperation(Result, DAG);
2346 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002347 }
Nate Begeman987121a2005-08-23 04:29:48 +00002348 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002349 case TargetLowering::Promote: {
2350 // First step, figure out the appropriate operation to use.
2351 // Allow SETCC to not be supported for all legal data types
2352 // Mostly this targets FP
2353 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002354 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002355
2356 // Scan for the appropriate larger type to use.
2357 while (1) {
2358 NewInTy = (MVT::ValueType)(NewInTy+1);
2359
2360 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2361 "Fell off of the edge of the integer world");
2362 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2363 "Fell off of the edge of the floating point world");
2364
2365 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002366 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002367 break;
2368 }
2369 if (MVT::isInteger(NewInTy))
2370 assert(0 && "Cannot promote Legal Integer SETCC yet");
2371 else {
2372 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2373 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2374 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002375 Tmp1 = LegalizeOp(Tmp1);
2376 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002377 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002378 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002379 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002380 }
Nate Begeman987121a2005-08-23 04:29:48 +00002381 case TargetLowering::Expand:
2382 // Expand a setcc node into a select_cc of the same condition, lhs, and
2383 // rhs that selects between const 1 (true) and const 0 (false).
2384 MVT::ValueType VT = Node->getValueType(0);
2385 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2386 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002387 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002388 break;
2389 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002390 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002391 case ISD::MEMSET:
2392 case ISD::MEMCPY:
2393 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002394 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002395 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2396
2397 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2398 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2399 case Expand: assert(0 && "Cannot expand a byte!");
2400 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002401 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002402 break;
2403 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002404 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002405 break;
2406 }
2407 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002408 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002409 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002410
2411 SDOperand Tmp4;
2412 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002413 case Expand: {
2414 // Length is too big, just take the lo-part of the length.
2415 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002416 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002417 break;
2418 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002419 case Legal:
2420 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002421 break;
2422 case Promote:
2423 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002424 break;
2425 }
2426
2427 SDOperand Tmp5;
2428 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2429 case Expand: assert(0 && "Cannot expand this yet!");
2430 case Legal:
2431 Tmp5 = LegalizeOp(Node->getOperand(4));
2432 break;
2433 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002434 Tmp5 = PromoteOp(Node->getOperand(4));
2435 break;
2436 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002437
2438 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2439 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002440 case TargetLowering::Custom:
2441 isCustom = true;
2442 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00002443 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002444 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002445 if (isCustom) {
2446 Tmp1 = TLI.LowerOperation(Result, DAG);
2447 if (Tmp1.Val) Result = Tmp1;
2448 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002449 break;
2450 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002451 // Otherwise, the target does not support this operation. Lower the
2452 // operation to an explicit libcall as appropriate.
2453 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002454 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002455 TargetLowering::ArgListTy Args;
2456 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002457
Reid Spencer6dced922005-01-12 14:53:45 +00002458 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002459 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002460 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00002461 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002462 // Extend the (previously legalized) ubyte argument to be an int value
2463 // for the call.
2464 if (Tmp3.getValueType() > MVT::i32)
2465 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2466 else
2467 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002468 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencere63b6512006-12-31 05:55:36 +00002469 Args.push_back(Entry);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002470 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002471 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002472
2473 FnName = "memset";
2474 } else if (Node->getOpcode() == ISD::MEMCPY ||
2475 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002476 Entry.Ty = IntPtrTy;
Reid Spencer791864c2007-01-03 04:22:32 +00002477 Entry.Node = Tmp2; Args.push_back(Entry);
2478 Entry.Node = Tmp3; Args.push_back(Entry);
2479 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002480 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2481 } else {
2482 assert(0 && "Unknown op!");
2483 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002484
Chris Lattner85d70c62005-01-11 05:57:22 +00002485 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002486 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002487 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002488 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002489 break;
2490 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002491 }
2492 break;
2493 }
Chris Lattner5385db52005-05-09 20:23:03 +00002494
Chris Lattner4157c412005-04-02 04:00:59 +00002495 case ISD::SHL_PARTS:
2496 case ISD::SRA_PARTS:
2497 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002498 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002499 bool Changed = false;
2500 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2501 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2502 Changed |= Ops.back() != Node->getOperand(i);
2503 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002504 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002505 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002506
Evan Cheng870e4f82006-01-09 18:31:59 +00002507 switch (TLI.getOperationAction(Node->getOpcode(),
2508 Node->getValueType(0))) {
2509 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002510 case TargetLowering::Legal: break;
2511 case TargetLowering::Custom:
2512 Tmp1 = TLI.LowerOperation(Result, DAG);
2513 if (Tmp1.Val) {
2514 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002515 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002516 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002517 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2518 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002519 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002520 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002521 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002522 return RetVal;
2523 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002524 break;
2525 }
2526
Chris Lattner13fe99c2005-04-02 05:00:07 +00002527 // Since these produce multiple values, make sure to remember that we
2528 // legalized all of them.
2529 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2530 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2531 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002532 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002533
2534 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002535 case ISD::ADD:
2536 case ISD::SUB:
2537 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002538 case ISD::MULHS:
2539 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002540 case ISD::UDIV:
2541 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002542 case ISD::AND:
2543 case ISD::OR:
2544 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002545 case ISD::SHL:
2546 case ISD::SRL:
2547 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002548 case ISD::FADD:
2549 case ISD::FSUB:
2550 case ISD::FMUL:
2551 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002552 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002553 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2554 case Expand: assert(0 && "Not possible");
2555 case Legal:
2556 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2557 break;
2558 case Promote:
2559 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2560 break;
2561 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002562
2563 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002564
Andrew Lenharth72594262005-12-24 23:42:32 +00002565 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002566 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002567 case TargetLowering::Legal: break;
2568 case TargetLowering::Custom:
2569 Tmp1 = TLI.LowerOperation(Result, DAG);
2570 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002571 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002572 case TargetLowering::Expand: {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002573 if (Node->getValueType(0) == MVT::i32) {
2574 switch (Node->getOpcode()) {
2575 default: assert(0 && "Do not know how to expand this integer BinOp!");
2576 case ISD::UDIV:
2577 case ISD::SDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00002578 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2579 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002580 SDOperand Dummy;
Reid Spencere63b6512006-12-31 05:55:36 +00002581 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002582 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002583 };
2584 break;
2585 }
2586
Chris Lattner87f08092006-04-02 03:57:31 +00002587 assert(MVT::isVector(Node->getValueType(0)) &&
2588 "Cannot expand this binary operator!");
2589 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002590 SmallVector<SDOperand, 8> Ops;
Dan Gohman5c441312007-06-14 22:58:02 +00002591 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattner87f08092006-04-02 03:57:31 +00002592 MVT::ValueType PtrVT = TLI.getPointerTy();
2593 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2594 i != e; ++i) {
2595 SDOperand Idx = DAG.getConstant(i, PtrVT);
2596 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2597 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2598 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2599 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002600 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2601 &Ops[0], Ops.size());
Chris Lattner87f08092006-04-02 03:57:31 +00002602 break;
2603 }
Evan Cheng119266e2006-04-12 21:20:24 +00002604 case TargetLowering::Promote: {
2605 switch (Node->getOpcode()) {
2606 default: assert(0 && "Do not know how to promote this BinOp!");
2607 case ISD::AND:
2608 case ISD::OR:
2609 case ISD::XOR: {
2610 MVT::ValueType OVT = Node->getValueType(0);
2611 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2612 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2613 // Bit convert each of the values to the new type.
2614 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2615 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2616 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2617 // Bit convert the result back the original type.
2618 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2619 break;
2620 }
2621 }
2622 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002623 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002624 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002625
2626 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2627 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2628 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2629 case Expand: assert(0 && "Not possible");
2630 case Legal:
2631 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2632 break;
2633 case Promote:
2634 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2635 break;
2636 }
2637
2638 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2639
2640 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2641 default: assert(0 && "Operation not supported");
2642 case TargetLowering::Custom:
2643 Tmp1 = TLI.LowerOperation(Result, DAG);
2644 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002645 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002646 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002647 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00002648 // If this target supports fabs/fneg natively and select is cheap,
2649 // do this efficiently.
2650 if (!TLI.isSelectExpensive() &&
2651 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2652 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00002653 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00002654 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002655 // Get the sign bit of the RHS.
2656 MVT::ValueType IVT =
2657 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2658 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2659 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2660 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2661 // Get the absolute value of the result.
2662 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2663 // Select between the nabs and abs value based on the sign bit of
2664 // the input.
2665 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2666 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2667 AbsVal),
2668 AbsVal);
2669 Result = LegalizeOp(Result);
2670 break;
2671 }
2672
2673 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002674 MVT::ValueType NVT =
2675 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2676 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2677 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2678 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002679 break;
2680 }
Evan Cheng003feb02007-01-04 21:56:39 +00002681 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002682 break;
2683
Nate Begeman5965bd12006-02-17 05:43:56 +00002684 case ISD::ADDC:
2685 case ISD::SUBC:
2686 Tmp1 = LegalizeOp(Node->getOperand(0));
2687 Tmp2 = LegalizeOp(Node->getOperand(1));
2688 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2689 // Since this produces two values, make sure to remember that we legalized
2690 // both of them.
2691 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2692 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2693 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002694
Nate Begeman5965bd12006-02-17 05:43:56 +00002695 case ISD::ADDE:
2696 case ISD::SUBE:
2697 Tmp1 = LegalizeOp(Node->getOperand(0));
2698 Tmp2 = LegalizeOp(Node->getOperand(1));
2699 Tmp3 = LegalizeOp(Node->getOperand(2));
2700 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2701 // Since this produces two values, make sure to remember that we legalized
2702 // both of them.
2703 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2704 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2705 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002706
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002707 case ISD::BUILD_PAIR: {
2708 MVT::ValueType PairTy = Node->getValueType(0);
2709 // TODO: handle the case where the Lo and Hi operands are not of legal type
2710 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2711 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2712 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002713 case TargetLowering::Promote:
2714 case TargetLowering::Custom:
2715 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002716 case TargetLowering::Legal:
2717 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2718 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2719 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002720 case TargetLowering::Expand:
2721 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2722 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2723 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2724 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2725 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002726 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002727 break;
2728 }
2729 break;
2730 }
2731
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002732 case ISD::UREM:
2733 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002734 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002735 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2736 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002737
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002738 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002739 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2740 case TargetLowering::Custom:
2741 isCustom = true;
2742 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002743 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002744 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002745 if (isCustom) {
2746 Tmp1 = TLI.LowerOperation(Result, DAG);
2747 if (Tmp1.Val) Result = Tmp1;
2748 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002749 break;
Chris Lattner81914422005-08-03 20:31:37 +00002750 case TargetLowering::Expand:
Evan Cheng1fc7c362006-09-18 23:28:33 +00002751 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002752 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002753 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002754 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2755 TargetLowering::Legal) {
2756 // X % Y -> X-X/Y*Y
2757 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng1fc7c362006-09-18 23:28:33 +00002758 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002759 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2760 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2761 } else {
2762 assert(Node->getValueType(0) == MVT::i32 &&
2763 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00002764 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2765 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002766 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002767 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002768 }
Chris Lattner81914422005-08-03 20:31:37 +00002769 } else {
2770 // Floating point mod -> fmod libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002771 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2772 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner81914422005-08-03 20:31:37 +00002773 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002774 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2775 false/*sign irrelevant*/, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002776 }
2777 break;
2778 }
2779 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002780 case ISD::VAARG: {
2781 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2782 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2783
Chris Lattner364b89a2006-01-28 07:42:08 +00002784 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002785 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2786 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002787 case TargetLowering::Custom:
2788 isCustom = true;
2789 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002790 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002791 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2792 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002793 Tmp1 = Result.getValue(1);
2794
2795 if (isCustom) {
2796 Tmp2 = TLI.LowerOperation(Result, DAG);
2797 if (Tmp2.Val) {
2798 Result = LegalizeOp(Tmp2);
2799 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2800 }
2801 }
Nate Begemane74795c2006-01-25 18:21:52 +00002802 break;
2803 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002804 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00002805 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00002806 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002807 // Increment the pointer, VAList, to the next vaarg
2808 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2809 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2810 TLI.getPointerTy()));
2811 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00002812 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2813 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002814 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00002815 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002816 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002817 Result = LegalizeOp(Result);
2818 break;
2819 }
2820 }
2821 // Since VAARG produces two values, make sure to remember that we
2822 // legalized both of them.
2823 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002824 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2825 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002826 }
2827
2828 case ISD::VACOPY:
2829 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2830 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2831 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2832
2833 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2834 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002835 case TargetLowering::Custom:
2836 isCustom = true;
2837 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002838 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002839 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2840 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002841 if (isCustom) {
2842 Tmp1 = TLI.LowerOperation(Result, DAG);
2843 if (Tmp1.Val) Result = Tmp1;
2844 }
Nate Begemane74795c2006-01-25 18:21:52 +00002845 break;
2846 case TargetLowering::Expand:
2847 // This defaults to loading a pointer from the input and storing it to the
2848 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002849 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00002850 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00002851 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2852 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00002853 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2854 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002855 break;
2856 }
2857 break;
2858
2859 case ISD::VAEND:
2860 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2861 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2862
2863 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2864 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002865 case TargetLowering::Custom:
2866 isCustom = true;
2867 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002868 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002869 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002870 if (isCustom) {
2871 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2872 if (Tmp1.Val) Result = Tmp1;
2873 }
Nate Begemane74795c2006-01-25 18:21:52 +00002874 break;
2875 case TargetLowering::Expand:
2876 Result = Tmp1; // Default to a no-op, return the chain
2877 break;
2878 }
2879 break;
2880
2881 case ISD::VASTART:
2882 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2883 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2884
Chris Lattnerd02b0542006-01-28 10:58:55 +00002885 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2886
Nate Begemane74795c2006-01-25 18:21:52 +00002887 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2888 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002889 case TargetLowering::Legal: break;
2890 case TargetLowering::Custom:
2891 Tmp1 = TLI.LowerOperation(Result, DAG);
2892 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002893 break;
2894 }
2895 break;
2896
Nate Begeman1b8121b2006-01-11 21:21:00 +00002897 case ISD::ROTL:
2898 case ISD::ROTR:
2899 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2900 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00002901 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00002902 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2903 default:
2904 assert(0 && "ROTL/ROTR legalize operation not supported");
2905 break;
2906 case TargetLowering::Legal:
2907 break;
2908 case TargetLowering::Custom:
2909 Tmp1 = TLI.LowerOperation(Result, DAG);
2910 if (Tmp1.Val) Result = Tmp1;
2911 break;
2912 case TargetLowering::Promote:
2913 assert(0 && "Do not know how to promote ROTL/ROTR");
2914 break;
2915 case TargetLowering::Expand:
2916 assert(0 && "Do not know how to expand ROTL/ROTR");
2917 break;
2918 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00002919 break;
2920
Nate Begeman2fba8a32006-01-14 03:14:10 +00002921 case ISD::BSWAP:
2922 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2923 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002924 case TargetLowering::Custom:
2925 assert(0 && "Cannot custom legalize this yet!");
2926 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002927 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002928 break;
2929 case TargetLowering::Promote: {
2930 MVT::ValueType OVT = Tmp1.getValueType();
2931 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohman1796f1f2007-05-18 17:52:13 +00002932 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002933
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002934 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2935 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2936 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2937 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2938 break;
2939 }
2940 case TargetLowering::Expand:
2941 Result = ExpandBSWAP(Tmp1);
2942 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002943 }
2944 break;
2945
Andrew Lenharth5e177822005-05-03 17:19:30 +00002946 case ISD::CTPOP:
2947 case ISD::CTTZ:
2948 case ISD::CTLZ:
2949 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2950 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00002951 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00002952 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002953 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00002954 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00002955 TargetLowering::Custom) {
2956 Tmp1 = TLI.LowerOperation(Result, DAG);
2957 if (Tmp1.Val) {
2958 Result = Tmp1;
2959 }
Scott Michel34e2d222007-07-30 21:00:31 +00002960 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002961 break;
2962 case TargetLowering::Promote: {
2963 MVT::ValueType OVT = Tmp1.getValueType();
2964 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002965
2966 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002967 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2968 // Perform the larger operation, then subtract if needed.
2969 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002970 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002971 case ISD::CTPOP:
2972 Result = Tmp1;
2973 break;
2974 case ISD::CTTZ:
2975 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002976 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00002977 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00002978 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002979 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel34e2d222007-07-30 21:00:31 +00002980 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002981 break;
2982 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002983 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002984 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00002985 DAG.getConstant(MVT::getSizeInBits(NVT) -
2986 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00002987 break;
2988 }
2989 break;
2990 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002991 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002992 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002993 break;
2994 }
2995 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002996
Chris Lattner13fe99c2005-04-02 05:00:07 +00002997 // Unary operators
2998 case ISD::FABS:
2999 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00003000 case ISD::FSQRT:
3001 case ISD::FSIN:
3002 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003003 Tmp1 = LegalizeOp(Node->getOperand(0));
3004 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003005 case TargetLowering::Promote:
3006 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00003007 isCustom = true;
3008 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00003009 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003010 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00003011 if (isCustom) {
3012 Tmp1 = TLI.LowerOperation(Result, DAG);
3013 if (Tmp1.Val) Result = Tmp1;
3014 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003015 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00003016 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003017 switch (Node->getOpcode()) {
3018 default: assert(0 && "Unreachable!");
3019 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003020 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3021 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003022 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00003023 break;
Chris Lattner80026402005-04-30 04:43:14 +00003024 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003025 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3026 MVT::ValueType VT = Node->getValueType(0);
3027 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003028 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003029 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3030 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00003031 break;
3032 }
3033 case ISD::FSQRT:
3034 case ISD::FSIN:
3035 case ISD::FCOS: {
3036 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003037 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00003038 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00003039 case ISD::FSQRT:
3040 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
3041 break;
3042 case ISD::FSIN:
3043 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3044 break;
3045 case ISD::FCOS:
3046 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3047 break;
Chris Lattner80026402005-04-30 04:43:14 +00003048 default: assert(0 && "Unreachable!");
3049 }
Nate Begeman77558da2005-08-04 21:43:28 +00003050 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003051 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3052 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00003053 break;
3054 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003055 }
3056 break;
3057 }
3058 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003059 case ISD::FPOWI: {
3060 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng31cbddf2007-01-12 02:11:51 +00003061 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
3062 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003063 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003064 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3065 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003066 break;
3067 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003068 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003069 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00003070 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohmana8665142007-06-25 16:23:39 +00003071 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3072 // The input has to be a vector type, we have to either scalarize it, pack
3073 // it, or convert it based on whether the input vector type is legal.
3074 SDNode *InVal = Node->getOperand(0).Val;
3075 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
3076 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
3077
3078 // Figure out if there is a simple type corresponding to this Vector
3079 // type. If so, convert to the vector type.
3080 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3081 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003082 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003083 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3084 LegalizeOp(Node->getOperand(0)));
3085 break;
3086 } else if (NumElems == 1) {
3087 // Turn this into a bit convert of the scalar input.
3088 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3089 ScalarizeVectorOp(Node->getOperand(0)));
3090 break;
3091 } else {
3092 // FIXME: UNIMP! Store then reload
3093 assert(0 && "Cast from unsupported vector type not implemented yet!");
3094 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003095 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003096 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3097 Node->getOperand(0).getValueType())) {
3098 default: assert(0 && "Unknown operation action!");
3099 case TargetLowering::Expand:
3100 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3101 break;
3102 case TargetLowering::Legal:
3103 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003104 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003105 break;
3106 }
3107 }
3108 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00003109
Chris Lattner13fe99c2005-04-02 05:00:07 +00003110 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00003111 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003112 case ISD::UINT_TO_FP: {
3113 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00003114 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3115 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00003116 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003117 Node->getOperand(0).getValueType())) {
3118 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003119 case TargetLowering::Custom:
3120 isCustom = true;
3121 // FALLTHROUGH
3122 case TargetLowering::Legal:
3123 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003124 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003125 if (isCustom) {
3126 Tmp1 = TLI.LowerOperation(Result, DAG);
3127 if (Tmp1.Val) Result = Tmp1;
3128 }
3129 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003130 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00003131 Result = ExpandLegalINT_TO_FP(isSigned,
3132 LegalizeOp(Node->getOperand(0)),
3133 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003134 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003135 case TargetLowering::Promote:
3136 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3137 Node->getValueType(0),
3138 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003139 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00003140 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003141 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00003142 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003143 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3144 Node->getValueType(0), Node->getOperand(0));
3145 break;
3146 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003147 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003148 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003149 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3150 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003151 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003152 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3153 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003154 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003155 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3156 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003157 break;
3158 }
3159 break;
3160 }
3161 case ISD::TRUNCATE:
3162 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3163 case Legal:
3164 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003165 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003166 break;
3167 case Expand:
3168 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3169
3170 // Since the result is legal, we should just be able to truncate the low
3171 // part of the source.
3172 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3173 break;
3174 case Promote:
3175 Result = PromoteOp(Node->getOperand(0));
3176 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3177 break;
3178 }
3179 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003180
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003181 case ISD::FP_TO_SINT:
3182 case ISD::FP_TO_UINT:
3183 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3184 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00003185 Tmp1 = LegalizeOp(Node->getOperand(0));
3186
Chris Lattner44fe26f2005-07-29 00:11:56 +00003187 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3188 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003189 case TargetLowering::Custom:
3190 isCustom = true;
3191 // FALLTHROUGH
3192 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003193 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003194 if (isCustom) {
3195 Tmp1 = TLI.LowerOperation(Result, DAG);
3196 if (Tmp1.Val) Result = Tmp1;
3197 }
3198 break;
3199 case TargetLowering::Promote:
3200 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3201 Node->getOpcode() == ISD::FP_TO_SINT);
3202 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00003203 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00003204 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3205 SDOperand True, False;
3206 MVT::ValueType VT = Node->getOperand(0).getValueType();
3207 MVT::ValueType NVT = Node->getValueType(0);
3208 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
3209 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
3210 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3211 Node->getOperand(0), Tmp2, ISD::SETLT);
3212 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3213 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00003214 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00003215 Tmp2));
3216 False = DAG.getNode(ISD::XOR, NVT, False,
3217 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003218 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3219 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00003220 } else {
3221 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3222 }
3223 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003224 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003225 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003226 case Expand: {
3227 // Convert f32 / f64 to i32 / i64.
3228 MVT::ValueType VT = Op.getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00003229 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003230 switch (Node->getOpcode()) {
3231 case ISD::FP_TO_SINT:
3232 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003233 LC = (VT == MVT::i32)
3234 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003235 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00003236 LC = (VT == MVT::i32)
3237 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003238 break;
3239 case ISD::FP_TO_UINT:
3240 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003241 LC = (VT == MVT::i32)
3242 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003243 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00003244 LC = (VT == MVT::i32)
3245 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003246 break;
3247 default: assert(0 && "Unreachable!");
3248 }
3249 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003250 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3251 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003252 break;
3253 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003254 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003255 Tmp1 = PromoteOp(Node->getOperand(0));
3256 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3257 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003258 break;
3259 }
3260 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003261
Dale Johannesenc339e452007-08-09 17:27:48 +00003262 case ISD::FP_EXTEND:
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003263 case ISD::FP_ROUND: {
3264 MVT::ValueType newVT = Op.getValueType();
3265 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3266 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesenc339e452007-08-09 17:27:48 +00003267 // The only way we can lower this is to turn it into a STORE,
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003268 // LOAD pair, targetting a temporary location (a stack slot).
3269
3270 // NOTE: there is a choice here between constantly creating new stack
3271 // slots and always reusing the same one. We currently always create
3272 // new ones, as reuse may inhibit scheduling.
Dale Johannesenc339e452007-08-09 17:27:48 +00003273 MVT::ValueType slotVT =
3274 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3275 const Type *Ty = MVT::getTypeForValueType(slotVT);
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003276 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3277 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3278 MachineFunction &MF = DAG.getMachineFunction();
3279 int SSFI =
3280 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3281 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesenc339e452007-08-09 17:27:48 +00003282 if (Node->getOpcode() == ISD::FP_EXTEND) {
3283 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3284 StackSlot, NULL, 0);
3285 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3286 Result, StackSlot, NULL, 0, oldVT);
3287 } else {
3288 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3289 StackSlot, NULL, 0, newVT);
3290 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0, newVT);
3291 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003292 break;
3293 }
Dale Johannesena2b3c172007-07-03 00:53:03 +00003294 }
3295 // FALL THROUGH
Chris Lattner7753f172005-09-02 00:18:10 +00003296 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003297 case ISD::ZERO_EXTEND:
3298 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003299 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003300 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003301 case Legal:
3302 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003303 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003304 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003305 case Promote:
3306 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003307 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003308 Tmp1 = PromoteOp(Node->getOperand(0));
3309 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003310 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003311 case ISD::ZERO_EXTEND:
3312 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003313 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003314 Result = DAG.getZeroExtendInReg(Result,
3315 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003316 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003317 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003318 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003319 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003320 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003321 Result,
3322 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003323 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003324 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003325 Result = PromoteOp(Node->getOperand(0));
3326 if (Result.getValueType() != Op.getValueType())
3327 // Dynamically dead while we have only 2 FP types.
3328 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3329 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003330 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00003331 Result = PromoteOp(Node->getOperand(0));
3332 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3333 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003334 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003335 }
3336 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003337 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003338 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003339 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003340 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003341
3342 // If this operation is not supported, convert it to a shl/shr or load/store
3343 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003344 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3345 default: assert(0 && "This action not supported for this op yet!");
3346 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003347 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003348 break;
3349 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003350 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003351 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003352 // NOTE: we could fall back on load/store here too for targets without
3353 // SAR. However, it is doubtful that any exist.
3354 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3355 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003356 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003357 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3358 Node->getOperand(0), ShiftCst);
3359 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3360 Result, ShiftCst);
3361 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003362 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003363 // EXTLOAD pair, targetting a temporary location (a stack slot).
3364
3365 // NOTE: there is a choice here between constantly creating new stack
3366 // slots and always reusing the same one. We currently always create
3367 // new ones, as reuse may inhibit scheduling.
3368 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner1deacd62007-04-28 06:42:38 +00003369 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattner945e4372007-02-14 05:52:17 +00003370 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner99222f72005-01-15 07:15:18 +00003371 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00003372 int SSFI =
Chris Lattner1deacd62007-04-28 06:42:38 +00003373 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner99222f72005-01-15 07:15:18 +00003374 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003375 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3376 StackSlot, NULL, 0, ExtraVT);
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003377 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003378 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00003379 } else {
3380 assert(0 && "Unknown op");
3381 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003382 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003383 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003384 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003385 }
Duncan Sands644f9172007-07-27 12:58:54 +00003386 case ISD::TRAMPOLINE: {
3387 SDOperand Ops[6];
3388 for (unsigned i = 0; i != 6; ++i)
3389 Ops[i] = LegalizeOp(Node->getOperand(i));
3390 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3391 // The only option for this node is to custom lower it.
3392 Result = TLI.LowerOperation(Result, DAG);
3393 assert(Result.Val && "Should always custom lower!");
Duncan Sands86e01192007-09-11 14:10:23 +00003394
3395 // Since trampoline produces two values, make sure to remember that we
3396 // legalized both of them.
3397 Tmp1 = LegalizeOp(Result.getValue(1));
3398 Result = LegalizeOp(Result);
3399 AddLegalizedOperand(SDOperand(Node, 0), Result);
3400 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3401 return Op.ResNo ? Tmp1 : Result;
Duncan Sands644f9172007-07-27 12:58:54 +00003402 }
Chris Lattner99222f72005-01-15 07:15:18 +00003403 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003404
Chris Lattner101ea662006-04-08 04:13:17 +00003405 assert(Result.getValueType() == Op.getValueType() &&
3406 "Bad legalization!");
3407
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003408 // Make sure that the generated code is itself legal.
3409 if (Result != Op)
3410 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003411
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003412 // Note that LegalizeOp may be reentered even from single-use nodes, which
3413 // means that we always must cache transformed nodes.
3414 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003415 return Result;
3416}
3417
Chris Lattner4d978642005-01-15 22:16:26 +00003418/// PromoteOp - Given an operation that produces a value in an invalid type,
3419/// promote it to compute the value into a larger type. The produced value will
3420/// have the correct bits for the low portion of the register, but no guarantee
3421/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003422SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3423 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003424 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003425 assert(getTypeAction(VT) == Promote &&
3426 "Caller should expand or legalize operands that are not promotable!");
3427 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3428 "Cannot promote to smaller type!");
3429
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003430 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003431 SDOperand Result;
3432 SDNode *Node = Op.Val;
3433
Chris Lattner4b0ddb22007-02-04 01:17:38 +00003434 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00003435 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003436
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003437 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003438 case ISD::CopyFromReg:
3439 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003440 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003441#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00003442 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003443#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003444 assert(0 && "Do not know how to promote this operator!");
3445 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003446 case ISD::UNDEF:
3447 Result = DAG.getNode(ISD::UNDEF, NVT);
3448 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003449 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003450 if (VT != MVT::i1)
3451 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3452 else
3453 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003454 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3455 break;
3456 case ISD::ConstantFP:
3457 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3458 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3459 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003460
Chris Lattner2cb338d2005-01-18 02:59:52 +00003461 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003462 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003463 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3464 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003465 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003466
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003467 case ISD::TRUNCATE:
3468 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3469 case Legal:
3470 Result = LegalizeOp(Node->getOperand(0));
3471 assert(Result.getValueType() >= NVT &&
3472 "This truncation doesn't make sense!");
3473 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3474 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3475 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003476 case Promote:
3477 // The truncation is not required, because we don't guarantee anything
3478 // about high bits anyway.
3479 Result = PromoteOp(Node->getOperand(0));
3480 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003481 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003482 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3483 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003484 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003485 }
3486 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003487 case ISD::SIGN_EXTEND:
3488 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003489 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003490 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3491 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3492 case Legal:
3493 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003494 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003495 break;
3496 case Promote:
3497 // Promote the reg if it's smaller.
3498 Result = PromoteOp(Node->getOperand(0));
3499 // The high bits are not guaranteed to be anything. Insert an extend.
3500 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003501 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003502 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003503 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003504 Result = DAG.getZeroExtendInReg(Result,
3505 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003506 break;
3507 }
3508 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003509 case ISD::BIT_CONVERT:
3510 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3511 Result = PromoteOp(Result);
3512 break;
3513
Chris Lattner4d978642005-01-15 22:16:26 +00003514 case ISD::FP_EXTEND:
3515 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3516 case ISD::FP_ROUND:
3517 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3518 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3519 case Promote: assert(0 && "Unreachable with 2 FP types!");
3520 case Legal:
3521 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003522 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003523 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003524 break;
3525 }
3526 break;
3527
3528 case ISD::SINT_TO_FP:
3529 case ISD::UINT_TO_FP:
3530 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3531 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003532 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003533 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003534 break;
3535
3536 case Promote:
3537 Result = PromoteOp(Node->getOperand(0));
3538 if (Node->getOpcode() == ISD::SINT_TO_FP)
3539 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003540 Result,
3541 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003542 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003543 Result = DAG.getZeroExtendInReg(Result,
3544 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003545 // No extra round required here.
3546 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003547 break;
3548 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003549 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3550 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003551 // Round if we cannot tolerate excess precision.
3552 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003553 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3554 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003555 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003556 }
Chris Lattner4d978642005-01-15 22:16:26 +00003557 break;
3558
Chris Lattner268d4572005-12-09 17:32:47 +00003559 case ISD::SIGN_EXTEND_INREG:
3560 Result = PromoteOp(Node->getOperand(0));
3561 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3562 Node->getOperand(1));
3563 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003564 case ISD::FP_TO_SINT:
3565 case ISD::FP_TO_UINT:
3566 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3567 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003568 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003569 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003570 break;
3571 case Promote:
3572 // The input result is prerounded, so we don't have to do anything
3573 // special.
3574 Tmp1 = PromoteOp(Node->getOperand(0));
3575 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003576 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003577 // If we're promoting a UINT to a larger size, check to see if the new node
3578 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3579 // we can use that instead. This allows us to generate better code for
3580 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3581 // legal, such as PowerPC.
3582 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003583 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003584 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3585 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003586 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3587 } else {
3588 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3589 }
Chris Lattner4d978642005-01-15 22:16:26 +00003590 break;
3591
Chris Lattner13fe99c2005-04-02 05:00:07 +00003592 case ISD::FABS:
3593 case ISD::FNEG:
3594 Tmp1 = PromoteOp(Node->getOperand(0));
3595 assert(Tmp1.getValueType() == NVT);
3596 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3597 // NOTE: we do not have to do any extra rounding here for
3598 // NoExcessFPPrecision, because we know the input will have the appropriate
3599 // precision, and these operations don't modify precision at all.
3600 break;
3601
Chris Lattner9d6fa982005-04-28 21:44:33 +00003602 case ISD::FSQRT:
3603 case ISD::FSIN:
3604 case ISD::FCOS:
3605 Tmp1 = PromoteOp(Node->getOperand(0));
3606 assert(Tmp1.getValueType() == NVT);
3607 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003608 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003609 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3610 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003611 break;
3612
Chris Lattnerca401aa2007-03-03 23:43:21 +00003613 case ISD::FPOWI: {
3614 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3615 // directly as well, which may be better.
3616 Tmp1 = PromoteOp(Node->getOperand(0));
3617 assert(Tmp1.getValueType() == NVT);
3618 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3619 if (NoExcessFPPrecision)
3620 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3621 DAG.getValueType(VT));
3622 break;
3623 }
3624
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003625 case ISD::AND:
3626 case ISD::OR:
3627 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003628 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003629 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003630 case ISD::MUL:
3631 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003632 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003633 // that too is okay if they are integer operations.
3634 Tmp1 = PromoteOp(Node->getOperand(0));
3635 Tmp2 = PromoteOp(Node->getOperand(1));
3636 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3637 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003638 break;
3639 case ISD::FADD:
3640 case ISD::FSUB:
3641 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003642 Tmp1 = PromoteOp(Node->getOperand(0));
3643 Tmp2 = PromoteOp(Node->getOperand(1));
3644 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3645 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3646
3647 // Floating point operations will give excess precision that we may not be
3648 // able to tolerate. If we DO allow excess precision, just leave it,
3649 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003650 // FIXME: Why would we need to round FP ops more than integer ones?
3651 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003652 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003653 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3654 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003655 break;
3656
Chris Lattner4d978642005-01-15 22:16:26 +00003657 case ISD::SDIV:
3658 case ISD::SREM:
3659 // These operators require that their input be sign extended.
3660 Tmp1 = PromoteOp(Node->getOperand(0));
3661 Tmp2 = PromoteOp(Node->getOperand(1));
3662 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003663 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3664 DAG.getValueType(VT));
3665 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3666 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003667 }
3668 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3669
3670 // Perform FP_ROUND: this is probably overly pessimistic.
3671 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003672 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3673 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003674 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003675 case ISD::FDIV:
3676 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003677 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003678 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00003679 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3680 case Legal:
3681 Tmp1 = LegalizeOp(Node->getOperand(0));
3682 break;
3683 case Promote:
3684 Tmp1 = PromoteOp(Node->getOperand(0));
3685 break;
3686 case Expand:
3687 assert(0 && "not implemented");
3688 }
3689 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3690 case Legal:
3691 Tmp2 = LegalizeOp(Node->getOperand(1));
3692 break;
3693 case Promote:
3694 Tmp2 = PromoteOp(Node->getOperand(1));
3695 break;
3696 case Expand:
3697 assert(0 && "not implemented");
3698 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00003699 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3700
3701 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003702 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003703 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3704 DAG.getValueType(VT));
3705 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003706
3707 case ISD::UDIV:
3708 case ISD::UREM:
3709 // These operators require that their input be zero extended.
3710 Tmp1 = PromoteOp(Node->getOperand(0));
3711 Tmp2 = PromoteOp(Node->getOperand(1));
3712 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003713 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3714 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003715 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3716 break;
3717
3718 case ISD::SHL:
3719 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003720 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003721 break;
3722 case ISD::SRA:
3723 // The input value must be properly sign extended.
3724 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003725 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3726 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003727 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003728 break;
3729 case ISD::SRL:
3730 // The input value must be properly zero extended.
3731 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003732 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003733 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003734 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003735
3736 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003737 Tmp1 = Node->getOperand(0); // Get the chain.
3738 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003739 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3740 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3741 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3742 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00003743 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00003744 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00003745 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003746 // Increment the pointer, VAList, to the next vaarg
3747 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3748 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3749 TLI.getPointerTy()));
3750 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00003751 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3752 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003753 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003754 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00003755 }
3756 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003757 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00003758 break;
3759
Evan Chenge71fe34d2006-10-09 20:57:25 +00003760 case ISD::LOAD: {
3761 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00003762 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3763 ? ISD::EXTLOAD : LD->getExtensionType();
3764 Result = DAG.getExtLoad(ExtType, NVT,
3765 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00003766 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00003767 LD->getLoadedVT(),
3768 LD->isVolatile(),
3769 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003770 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003771 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003772 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00003773 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003774 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003775 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3776 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003777 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003778 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003779 case ISD::SELECT_CC:
3780 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3781 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3782 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003783 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00003784 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003785 case ISD::BSWAP:
3786 Tmp1 = Node->getOperand(0);
3787 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3788 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3789 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003790 DAG.getConstant(MVT::getSizeInBits(NVT) -
3791 MVT::getSizeInBits(VT),
Nate Begeman2fba8a32006-01-14 03:14:10 +00003792 TLI.getShiftAmountTy()));
3793 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003794 case ISD::CTPOP:
3795 case ISD::CTTZ:
3796 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003797 // Zero extend the argument
3798 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003799 // Perform the larger operation, then subtract if needed.
3800 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003801 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003802 case ISD::CTPOP:
3803 Result = Tmp1;
3804 break;
3805 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003806 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003807 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003808 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3809 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003810 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003811 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003812 break;
3813 case ISD::CTLZ:
3814 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003815 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003816 DAG.getConstant(MVT::getSizeInBits(NVT) -
3817 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003818 break;
3819 }
3820 break;
Dan Gohmana8665142007-06-25 16:23:39 +00003821 case ISD::EXTRACT_SUBVECTOR:
3822 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00003823 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00003824 case ISD::EXTRACT_VECTOR_ELT:
3825 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3826 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003827 }
3828
3829 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003830
3831 // Make sure the result is itself legal.
3832 Result = LegalizeOp(Result);
3833
3834 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003835 AddPromotedOperand(Op, Result);
3836 return Result;
3837}
Chris Lattnerdc750592005-01-07 07:47:09 +00003838
Dan Gohmana8665142007-06-25 16:23:39 +00003839/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3840/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3841/// based on the vector type. The return type of this matches the element type
3842/// of the vector, which may not be legal for the target.
3843SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00003844 // We know that operand #0 is the Vec vector. If the index is a constant
3845 // or if the invec is a supported hardware type, we can use it. Otherwise,
3846 // lower to a store then an indexed load.
3847 SDOperand Vec = Op.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00003848 SDOperand Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00003849
3850 SDNode *InVal = Vec.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00003851 MVT::ValueType TVT = InVal->getValueType(0);
3852 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner6f423252006-03-31 17:55:51 +00003853
Dan Gohmana8665142007-06-25 16:23:39 +00003854 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3855 default: assert(0 && "This action is not supported yet!");
3856 case TargetLowering::Custom: {
3857 Vec = LegalizeOp(Vec);
3858 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3859 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3860 if (Tmp3.Val)
3861 return Tmp3;
3862 break;
3863 }
3864 case TargetLowering::Legal:
3865 if (isTypeLegal(TVT)) {
3866 Vec = LegalizeOp(Vec);
3867 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00003868 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00003869 }
3870 break;
3871 case TargetLowering::Expand:
3872 break;
3873 }
3874
3875 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00003876 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00003877 Op = ScalarizeVectorOp(Vec);
3878 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3879 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner6f423252006-03-31 17:55:51 +00003880 SDOperand Lo, Hi;
3881 SplitVectorOp(Vec, Lo, Hi);
3882 if (CIdx->getValue() < NumElems/2) {
3883 Vec = Lo;
3884 } else {
3885 Vec = Hi;
Dan Gohmana8665142007-06-25 16:23:39 +00003886 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3887 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00003888 }
Dan Gohmana8665142007-06-25 16:23:39 +00003889
Chris Lattner6f423252006-03-31 17:55:51 +00003890 // It's now an extract from the appropriate high or low part. Recurse.
3891 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00003892 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00003893 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00003894 // Store the value to a temporary stack slot, then LOAD the scalar
3895 // element back out.
3896 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3897 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3898
3899 // Add the offset to the index.
3900 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3901 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3902 DAG.getConstant(EltSize, Idx.getValueType()));
3903 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3904
3905 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00003906 }
Dan Gohmana8665142007-06-25 16:23:39 +00003907 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00003908}
3909
Dan Gohmana8665142007-06-25 16:23:39 +00003910/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00003911/// we assume the operation can be split if it is not already legal.
Dan Gohmana8665142007-06-25 16:23:39 +00003912SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00003913 // We know that operand #0 is the Vec vector. For now we assume the index
3914 // is a constant and that the extracted result is a supported hardware type.
3915 SDOperand Vec = Op.getOperand(0);
3916 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3917
Dan Gohmana8665142007-06-25 16:23:39 +00003918 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00003919
3920 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3921 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00003922 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00003923 }
3924
3925 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3926 SDOperand Lo, Hi;
3927 SplitVectorOp(Vec, Lo, Hi);
3928 if (CIdx->getValue() < NumElems/2) {
3929 Vec = Lo;
3930 } else {
3931 Vec = Hi;
3932 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3933 }
3934
3935 // It's now an extract from the appropriate high or low part. Recurse.
3936 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00003937 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00003938}
3939
Nate Begeman7e7f4392006-02-01 07:19:44 +00003940/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3941/// with condition CC on the current target. This usually involves legalizing
3942/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3943/// there may be no choice but to create a new SetCC node to represent the
3944/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3945/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3946void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3947 SDOperand &RHS,
3948 SDOperand &CC) {
3949 SDOperand Tmp1, Tmp2, Result;
3950
3951 switch (getTypeAction(LHS.getValueType())) {
3952 case Legal:
3953 Tmp1 = LegalizeOp(LHS); // LHS
3954 Tmp2 = LegalizeOp(RHS); // RHS
3955 break;
3956 case Promote:
3957 Tmp1 = PromoteOp(LHS); // LHS
3958 Tmp2 = PromoteOp(RHS); // RHS
3959
3960 // If this is an FP compare, the operands have already been extended.
3961 if (MVT::isInteger(LHS.getValueType())) {
3962 MVT::ValueType VT = LHS.getValueType();
3963 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3964
3965 // Otherwise, we have to insert explicit sign or zero extends. Note
3966 // that we could insert sign extends for ALL conditions, but zero extend
3967 // is cheaper on many machines (an AND instead of two shifts), so prefer
3968 // it.
3969 switch (cast<CondCodeSDNode>(CC)->get()) {
3970 default: assert(0 && "Unknown integer comparison!");
3971 case ISD::SETEQ:
3972 case ISD::SETNE:
3973 case ISD::SETUGE:
3974 case ISD::SETUGT:
3975 case ISD::SETULE:
3976 case ISD::SETULT:
3977 // ALL of these operations will work if we either sign or zero extend
3978 // the operands (including the unsigned comparisons!). Zero extend is
3979 // usually a simpler/cheaper operation, so prefer it.
3980 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3981 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3982 break;
3983 case ISD::SETGE:
3984 case ISD::SETGT:
3985 case ISD::SETLT:
3986 case ISD::SETLE:
3987 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3988 DAG.getValueType(VT));
3989 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3990 DAG.getValueType(VT));
3991 break;
3992 }
3993 }
3994 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003995 case Expand: {
3996 MVT::ValueType VT = LHS.getValueType();
3997 if (VT == MVT::f32 || VT == MVT::f64) {
3998 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00003999 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004000 switch (cast<CondCodeSDNode>(CC)->get()) {
4001 case ISD::SETEQ:
4002 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004003 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004004 break;
4005 case ISD::SETNE:
4006 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004007 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004008 break;
4009 case ISD::SETGE:
4010 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004011 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004012 break;
4013 case ISD::SETLT:
4014 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004015 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004016 break;
4017 case ISD::SETLE:
4018 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004019 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004020 break;
4021 case ISD::SETGT:
4022 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004023 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004024 break;
4025 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00004026 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4027 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004028 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00004029 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004030 break;
4031 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004032 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004033 switch (cast<CondCodeSDNode>(CC)->get()) {
4034 case ISD::SETONE:
4035 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00004036 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004037 // Fallthrough
4038 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004039 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004040 break;
4041 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004042 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004043 break;
4044 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004045 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004046 break;
4047 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004048 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004049 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004050 case ISD::SETUEQ:
4051 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004052 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004053 default: assert(0 && "Unsupported FP setcc!");
4054 }
4055 }
4056
4057 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004058 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00004059 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004060 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004061 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00004062 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00004063 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004064 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00004065 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00004066 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004067 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004068 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00004069 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004070 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4071 Tmp2 = SDOperand();
4072 }
4073 LHS = Tmp1;
4074 RHS = Tmp2;
4075 return;
4076 }
4077
Nate Begeman7e7f4392006-02-01 07:19:44 +00004078 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4079 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004080 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman7e7f4392006-02-01 07:19:44 +00004081 switch (cast<CondCodeSDNode>(CC)->get()) {
4082 case ISD::SETEQ:
4083 case ISD::SETNE:
4084 if (RHSLo == RHSHi)
4085 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4086 if (RHSCST->isAllOnesValue()) {
4087 // Comparison to -1.
4088 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4089 Tmp2 = RHSLo;
4090 break;
4091 }
4092
4093 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4094 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4095 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4096 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4097 break;
4098 default:
4099 // If this is a comparison of the sign bit, just look at the top part.
4100 // X > -1, x < 0
4101 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4102 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4103 CST->getValue() == 0) || // X < 0
4104 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4105 CST->isAllOnesValue())) { // X > -1
4106 Tmp1 = LHSHi;
4107 Tmp2 = RHSHi;
4108 break;
4109 }
4110
4111 // FIXME: This generated code sucks.
4112 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00004113 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4114 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004115 default: assert(0 && "Unknown integer setcc!");
4116 case ISD::SETLT:
4117 case ISD::SETULT: LowCC = ISD::SETULT; break;
4118 case ISD::SETGT:
4119 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4120 case ISD::SETLE:
4121 case ISD::SETULE: LowCC = ISD::SETULE; break;
4122 case ISD::SETGE:
4123 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4124 }
4125
4126 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4127 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4128 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4129
4130 // NOTE: on targets without efficient SELECT of bools, we can always use
4131 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00004132 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4133 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4134 false, DagCombineInfo);
4135 if (!Tmp1.Val)
4136 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4137 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4138 CCCode, false, DagCombineInfo);
4139 if (!Tmp2.Val)
4140 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
4141
4142 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4143 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4144 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4145 (Tmp2C && Tmp2C->getValue() == 0 &&
4146 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4147 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4148 (Tmp2C && Tmp2C->getValue() == 1 &&
4149 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4150 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4151 // low part is known false, returns high part.
4152 // For LE / GE, if high part is known false, ignore the low part.
4153 // For LT / GT, if high part is known true, ignore the low part.
4154 Tmp1 = Tmp2;
4155 Tmp2 = SDOperand();
4156 } else {
4157 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4158 ISD::SETEQ, false, DagCombineInfo);
4159 if (!Result.Val)
4160 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4161 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4162 Result, Tmp1, Tmp2));
4163 Tmp1 = Result;
4164 Tmp2 = SDOperand();
4165 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004166 }
4167 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004168 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004169 LHS = Tmp1;
4170 RHS = Tmp2;
4171}
4172
Chris Lattner36e663d2005-12-23 00:16:34 +00004173/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00004174/// The resultant code need not be legal. Note that SrcOp is the input operand
4175/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00004176SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4177 SDOperand SrcOp) {
4178 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004179 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004180
4181 // Emit a store to the stack slot.
Evan Chengab51cf22006-10-13 21:14:26 +00004182 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004183 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004184 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004185}
4186
Chris Lattner6be79822006-04-04 17:23:26 +00004187SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4188 // Create a vector sized/aligned stack slot, store the value to element #0,
4189 // then load the whole vector back out.
4190 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00004191 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00004192 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004193 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00004194}
4195
4196
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004197/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00004198/// support the operation, but do support the resultant vector type.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004199SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4200
4201 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00004202 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004203 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004204 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00004205 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004206 std::map<SDOperand, std::vector<unsigned> > Values;
4207 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004208 bool isConstant = true;
4209 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4210 SplatValue.getOpcode() != ISD::UNDEF)
4211 isConstant = false;
4212
Evan Cheng1d2e9952006-03-24 01:17:21 +00004213 for (unsigned i = 1; i < NumElems; ++i) {
4214 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00004215 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004216 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004217 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004218 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00004219 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004220
4221 // If this isn't a constant element or an undef, we can't use a constant
4222 // pool load.
4223 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4224 V.getOpcode() != ISD::UNDEF)
4225 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004226 }
4227
4228 if (isOnlyLowElement) {
4229 // If the low element is an undef too, then this whole things is an undef.
4230 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4231 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4232 // Otherwise, turn this into a scalar_to_vector node.
4233 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4234 Node->getOperand(0));
4235 }
4236
Chris Lattner77e271c2006-03-24 07:29:17 +00004237 // If all elements are constants, create a load from the constant pool.
4238 if (isConstant) {
4239 MVT::ValueType VT = Node->getValueType(0);
4240 const Type *OpNTy =
4241 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4242 std::vector<Constant*> CV;
4243 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4244 if (ConstantFPSDNode *V =
4245 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00004246 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004247 } else if (ConstantSDNode *V =
4248 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00004249 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004250 } else {
4251 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4252 CV.push_back(UndefValue::get(OpNTy));
4253 }
4254 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00004255 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00004256 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00004257 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004258 }
4259
Chris Lattner21e68c82006-03-20 01:52:29 +00004260 if (SplatValue.Val) { // Splat of one value?
4261 // Build the shuffle constant vector: <0, 0, 0, 0>
4262 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00004263 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00004264 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004265 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004266 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4267 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00004268
4269 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004270 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00004271 // Get the splatted value into the low element of a vector register.
4272 SDOperand LowValVec =
4273 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4274
4275 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4276 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4277 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4278 SplatMask);
4279 }
4280 }
4281
Evan Cheng1d2e9952006-03-24 01:17:21 +00004282 // If there are only two unique elements, we may be able to turn this into a
4283 // vector shuffle.
4284 if (Values.size() == 2) {
4285 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4286 MVT::ValueType MaskVT =
4287 MVT::getIntVectorWithNumElements(NumElems);
4288 std::vector<SDOperand> MaskVec(NumElems);
4289 unsigned i = 0;
4290 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4291 E = Values.end(); I != E; ++I) {
4292 for (std::vector<unsigned>::iterator II = I->second.begin(),
4293 EE = I->second.end(); II != EE; ++II)
Dan Gohman5c441312007-06-14 22:58:02 +00004294 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004295 i += NumElems;
4296 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004297 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4298 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004299
4300 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004301 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4302 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004303 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004304 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4305 E = Values.end(); I != E; ++I) {
4306 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4307 I->first);
4308 Ops.push_back(Op);
4309 }
4310 Ops.push_back(ShuffleMask);
4311
4312 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004313 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4314 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004315 }
4316 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004317
4318 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4319 // aligned object on the stack, store each element into it, then load
4320 // the result as a vector.
4321 MVT::ValueType VT = Node->getValueType(0);
4322 // Create the stack frame object.
4323 SDOperand FIPtr = CreateStackTemporary(VT);
4324
4325 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004326 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004327 unsigned TypeByteSize =
4328 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004329 // Store (in the right endianness) the elements to memory.
4330 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4331 // Ignore undef elements.
4332 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4333
Chris Lattner8fa445a2006-03-22 01:46:54 +00004334 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004335
4336 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4337 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4338
Evan Chengdf9ac472006-10-05 23:01:46 +00004339 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00004340 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004341 }
4342
4343 SDOperand StoreChain;
4344 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004345 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4346 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004347 else
4348 StoreChain = DAG.getEntryNode();
4349
4350 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004351 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004352}
4353
4354/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4355/// specified value type.
4356SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4357 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4358 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner50ee0e42007-01-20 22:35:55 +00004359 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattner945e4372007-02-14 05:52:17 +00004360 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004361 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004362 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4363}
4364
Chris Lattner4157c412005-04-02 04:00:59 +00004365void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4366 SDOperand Op, SDOperand Amt,
4367 SDOperand &Lo, SDOperand &Hi) {
4368 // Expand the subcomponents.
4369 SDOperand LHSL, LHSH;
4370 ExpandOp(Op, LHSL, LHSH);
4371
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004372 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00004373 MVT::ValueType VT = LHSL.getValueType();
4374 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00004375 Hi = Lo.getValue(1);
4376}
4377
4378
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004379/// ExpandShift - Try to find a clever way to expand this shift operation out to
4380/// smaller elements. If we can't find a way that is more efficient than a
4381/// libcall on this target, return false. Otherwise, return true with the
4382/// low-parts expanded into Lo and Hi.
4383bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4384 SDOperand &Lo, SDOperand &Hi) {
4385 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4386 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00004387
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004388 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00004389 SDOperand ShAmt = LegalizeOp(Amt);
4390 MVT::ValueType ShTy = ShAmt.getValueType();
4391 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4392 unsigned NVTBits = MVT::getSizeInBits(NVT);
4393
4394 // Handle the case when Amt is an immediate. Other cases are currently broken
4395 // and are disabled.
4396 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4397 unsigned Cst = CN->getValue();
4398 // Expand the incoming operand to be shifted, so that we have its parts
4399 SDOperand InL, InH;
4400 ExpandOp(Op, InL, InH);
4401 switch(Opc) {
4402 case ISD::SHL:
4403 if (Cst > VTBits) {
4404 Lo = DAG.getConstant(0, NVT);
4405 Hi = DAG.getConstant(0, NVT);
4406 } else if (Cst > NVTBits) {
4407 Lo = DAG.getConstant(0, NVT);
4408 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004409 } else if (Cst == NVTBits) {
4410 Lo = DAG.getConstant(0, NVT);
4411 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00004412 } else {
4413 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4414 Hi = DAG.getNode(ISD::OR, NVT,
4415 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4416 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4417 }
4418 return true;
4419 case ISD::SRL:
4420 if (Cst > VTBits) {
4421 Lo = DAG.getConstant(0, NVT);
4422 Hi = DAG.getConstant(0, NVT);
4423 } else if (Cst > NVTBits) {
4424 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4425 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00004426 } else if (Cst == NVTBits) {
4427 Lo = InH;
4428 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00004429 } else {
4430 Lo = DAG.getNode(ISD::OR, NVT,
4431 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4432 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4433 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4434 }
4435 return true;
4436 case ISD::SRA:
4437 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004438 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004439 DAG.getConstant(NVTBits-1, ShTy));
4440 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004441 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004442 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004443 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004444 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004445 } else if (Cst == NVTBits) {
4446 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004447 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004448 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004449 } else {
4450 Lo = DAG.getNode(ISD::OR, NVT,
4451 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4452 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4453 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4454 }
4455 return true;
4456 }
4457 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004458
4459 // Okay, the shift amount isn't constant. However, if we can tell that it is
4460 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4461 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00004462 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00004463
4464 // If we know that the high bit of the shift amount is one, then we can do
4465 // this as a couple of simple shifts.
4466 if (KnownOne & Mask) {
4467 // Mask out the high bit, which we know is set.
4468 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4469 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4470
4471 // Expand the incoming operand to be shifted, so that we have its parts
4472 SDOperand InL, InH;
4473 ExpandOp(Op, InL, InH);
4474 switch(Opc) {
4475 case ISD::SHL:
4476 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4477 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4478 return true;
4479 case ISD::SRL:
4480 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4481 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4482 return true;
4483 case ISD::SRA:
4484 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4485 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4486 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4487 return true;
4488 }
4489 }
4490
4491 // If we know that the high bit of the shift amount is zero, then we can do
4492 // this as a couple of simple shifts.
4493 if (KnownZero & Mask) {
4494 // Compute 32-amt.
4495 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4496 DAG.getConstant(NVTBits, Amt.getValueType()),
4497 Amt);
4498
4499 // Expand the incoming operand to be shifted, so that we have its parts
4500 SDOperand InL, InH;
4501 ExpandOp(Op, InL, InH);
4502 switch(Opc) {
4503 case ISD::SHL:
4504 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4505 Hi = DAG.getNode(ISD::OR, NVT,
4506 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4507 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4508 return true;
4509 case ISD::SRL:
4510 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4511 Lo = DAG.getNode(ISD::OR, NVT,
4512 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4513 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4514 return true;
4515 case ISD::SRA:
4516 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4517 Lo = DAG.getNode(ISD::OR, NVT,
4518 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4519 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4520 return true;
4521 }
4522 }
4523
Nate Begemanb0674922005-04-06 21:13:14 +00004524 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004525}
Chris Lattneraac464e2005-01-21 06:05:23 +00004526
Chris Lattner4add7e32005-01-23 04:42:50 +00004527
Chris Lattneraac464e2005-01-21 06:05:23 +00004528// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4529// does not fit into a register, return the lo part and set the hi part to the
4530// by-reg argument. If it does fit into a single register, return the result
4531// and leave the Hi part unset.
4532SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004533 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004534 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4535 // The input chain to this libcall is the entry node of the function.
4536 // Legalizing the call will automatically add the previous call to the
4537 // dependence.
4538 SDOperand InChain = DAG.getEntryNode();
4539
Chris Lattneraac464e2005-01-21 06:05:23 +00004540 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004541 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004542 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4543 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4544 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004545 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00004546 Entry.isSExt = isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00004547 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004548 }
4549 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004550
Chris Lattner06bbeb62005-05-11 19:02:11 +00004551 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004552 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004553 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004554 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004555 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004556
Chris Lattner462505f2006-02-13 09:18:02 +00004557 // Legalize the call sequence, starting with the chain. This will advance
4558 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4559 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4560 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004561 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004562 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004563 default: assert(0 && "Unknown thing");
4564 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004565 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004566 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004567 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004568 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004569 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004570 }
Chris Lattner63022662005-09-02 20:26:58 +00004571 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004572}
4573
Chris Lattner4add7e32005-01-23 04:42:50 +00004574
Evan Chengbf535fc2007-04-27 07:33:31 +00004575/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4576///
Chris Lattneraac464e2005-01-21 06:05:23 +00004577SDOperand SelectionDAGLegalize::
4578ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004579 assert(getTypeAction(Source.getValueType()) == Expand &&
4580 "This is not an expansion!");
4581 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4582
Chris Lattner06bbeb62005-05-11 19:02:11 +00004583 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004584 assert(Source.getValueType() == MVT::i64 &&
4585 "This only works for 64-bit -> FP");
4586 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4587 // incoming integer is set. To handle this, we dynamically test to see if
4588 // it is set, and, if so, add a fudge factor.
4589 SDOperand Lo, Hi;
4590 ExpandOp(Source, Lo, Hi);
4591
Chris Lattner2a4f7312005-05-13 04:45:13 +00004592 // If this is unsigned, and not supported, first perform the conversion to
4593 // signed, then adjust the result if the sign bit is set.
4594 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4595 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4596
Chris Lattnerd47675e2005-08-09 20:20:18 +00004597 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4598 DAG.getConstant(0, Hi.getValueType()),
4599 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004600 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4601 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4602 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004603 uint64_t FF = 0x5f800000ULL;
4604 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004605 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004606
Chris Lattnerc30405e2005-08-26 17:15:30 +00004607 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004608 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4609 SDOperand FudgeInReg;
4610 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004611 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004612 else {
4613 assert(DestTy == MVT::f64 && "Unexpected conversion");
Evan Chengbf535fc2007-04-27 07:33:31 +00004614 // FIXME: Avoid the extend by construction the right constantpool?
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004615 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004616 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004617 }
Evan Chengbf535fc2007-04-27 07:33:31 +00004618 MVT::ValueType SCVT = SignedConv.getValueType();
4619 if (SCVT != DestTy) {
4620 // Destination type needs to be expanded as well. The FADD now we are
4621 // constructing will be expanded into a libcall.
4622 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4623 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4624 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4625 SignedConv, SignedConv.getValue(1));
4626 }
4627 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4628 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00004629 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00004630 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00004631
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004632 // Check to see if the target has a custom way to lower this. If so, use it.
4633 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4634 default: assert(0 && "This action not implemented for this operation!");
4635 case TargetLowering::Legal:
4636 case TargetLowering::Expand:
4637 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004638 case TargetLowering::Custom: {
4639 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4640 Source), DAG);
4641 if (NV.Val)
4642 return LegalizeOp(NV);
4643 break; // The target decided this was legal after all
4644 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004645 }
4646
Chris Lattner153587e2005-05-12 07:00:44 +00004647 // Expand the source, then glue it back together for the call. We must expand
4648 // the source in case it is shared (this pass of legalize must traverse it).
4649 SDOperand SrcLo, SrcHi;
4650 ExpandOp(Source, SrcLo, SrcHi);
4651 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4652
Evan Cheng31cbddf2007-01-12 02:11:51 +00004653 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004654 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004655 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004656 else {
4657 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00004658 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004659 }
Chris Lattner462505f2006-02-13 09:18:02 +00004660
Evan Chengbf535fc2007-04-27 07:33:31 +00004661 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner462505f2006-02-13 09:18:02 +00004662 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4663 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004664 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4665 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00004666}
Misha Brukman835702a2005-04-21 22:36:52 +00004667
Chris Lattner689bdcc2006-01-28 08:25:58 +00004668/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4669/// INT_TO_FP operation of the specified operand when the target requests that
4670/// we expand it. At this point, we know that the result and operand types are
4671/// legal for the target.
4672SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4673 SDOperand Op0,
4674 MVT::ValueType DestVT) {
4675 if (Op0.getValueType() == MVT::i32) {
4676 // simple 32-bit [signed|unsigned] integer to float/double expansion
4677
Chris Lattner50ee0e42007-01-20 22:35:55 +00004678 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner689bdcc2006-01-28 08:25:58 +00004679 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner50ee0e42007-01-20 22:35:55 +00004680 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4681 unsigned StackAlign =
Chris Lattner945e4372007-02-14 05:52:17 +00004682 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004683 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004684 // get address of 8 byte buffer
4685 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4686 // word offset constant for Hi/Lo address computation
4687 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4688 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00004689 SDOperand Hi = StackSlot;
4690 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4691 if (TLI.isLittleEndian())
4692 std::swap(Hi, Lo);
4693
Chris Lattner689bdcc2006-01-28 08:25:58 +00004694 // if signed map to unsigned space
4695 SDOperand Op0Mapped;
4696 if (isSigned) {
4697 // constant used to invert sign bit (signed to unsigned mapping)
4698 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4699 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4700 } else {
4701 Op0Mapped = Op0;
4702 }
4703 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00004704 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00004705 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004706 // initial hi portion of constructed double
4707 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4708 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00004709 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004710 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00004711 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004712 // FP constant to bias correct the final result
4713 SDOperand Bias = DAG.getConstantFP(isSigned ?
4714 BitsToDouble(0x4330000080000000ULL)
4715 : BitsToDouble(0x4330000000000000ULL),
4716 MVT::f64);
4717 // subtract the bias
4718 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4719 // final result
4720 SDOperand Result;
4721 // handle final rounding
4722 if (DestVT == MVT::f64) {
4723 // do nothing
4724 Result = Sub;
4725 } else {
4726 // if f32 then cast to f32
4727 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4728 }
4729 return Result;
4730 }
4731 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4732 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4733
4734 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4735 DAG.getConstant(0, Op0.getValueType()),
4736 ISD::SETLT);
4737 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4738 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4739 SignSet, Four, Zero);
4740
4741 // If the sign bit of the integer is set, the large number will be treated
4742 // as a negative number. To counteract this, the dynamic code adds an
4743 // offset depending on the data type.
4744 uint64_t FF;
4745 switch (Op0.getValueType()) {
4746 default: assert(0 && "Unsupported integer type!");
4747 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4748 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4749 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4750 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4751 }
4752 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004753 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004754
4755 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4756 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4757 SDOperand FudgeInReg;
4758 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004759 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004760 else {
4761 assert(DestVT == MVT::f64 && "Unexpected conversion");
4762 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4763 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00004764 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00004765 }
4766
4767 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4768}
4769
4770/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4771/// *INT_TO_FP operation of the specified operand when the target requests that
4772/// we promote it. At this point, we know that the result and operand types are
4773/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4774/// operation that takes a larger input.
4775SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4776 MVT::ValueType DestVT,
4777 bool isSigned) {
4778 // First step, figure out the appropriate *INT_TO_FP operation to use.
4779 MVT::ValueType NewInTy = LegalOp.getValueType();
4780
4781 unsigned OpToUse = 0;
4782
4783 // Scan for the appropriate larger type to use.
4784 while (1) {
4785 NewInTy = (MVT::ValueType)(NewInTy+1);
4786 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4787
4788 // If the target supports SINT_TO_FP of this type, use it.
4789 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4790 default: break;
4791 case TargetLowering::Legal:
4792 if (!TLI.isTypeLegal(NewInTy))
4793 break; // Can't use this datatype.
4794 // FALL THROUGH.
4795 case TargetLowering::Custom:
4796 OpToUse = ISD::SINT_TO_FP;
4797 break;
4798 }
4799 if (OpToUse) break;
4800 if (isSigned) continue;
4801
4802 // If the target supports UINT_TO_FP of this type, use it.
4803 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4804 default: break;
4805 case TargetLowering::Legal:
4806 if (!TLI.isTypeLegal(NewInTy))
4807 break; // Can't use this datatype.
4808 // FALL THROUGH.
4809 case TargetLowering::Custom:
4810 OpToUse = ISD::UINT_TO_FP;
4811 break;
4812 }
4813 if (OpToUse) break;
4814
4815 // Otherwise, try a larger type.
4816 }
4817
4818 // Okay, we found the operation and type to use. Zero extend our input to the
4819 // desired type then run the operation on it.
4820 return DAG.getNode(OpToUse, DestVT,
4821 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4822 NewInTy, LegalOp));
4823}
4824
4825/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4826/// FP_TO_*INT operation of the specified operand when the target requests that
4827/// we promote it. At this point, we know that the result and operand types are
4828/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4829/// operation that returns a larger result.
4830SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4831 MVT::ValueType DestVT,
4832 bool isSigned) {
4833 // First step, figure out the appropriate FP_TO*INT operation to use.
4834 MVT::ValueType NewOutTy = DestVT;
4835
4836 unsigned OpToUse = 0;
4837
4838 // Scan for the appropriate larger type to use.
4839 while (1) {
4840 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4841 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4842
4843 // If the target supports FP_TO_SINT returning this type, use it.
4844 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4845 default: break;
4846 case TargetLowering::Legal:
4847 if (!TLI.isTypeLegal(NewOutTy))
4848 break; // Can't use this datatype.
4849 // FALL THROUGH.
4850 case TargetLowering::Custom:
4851 OpToUse = ISD::FP_TO_SINT;
4852 break;
4853 }
4854 if (OpToUse) break;
4855
4856 // If the target supports FP_TO_UINT of this type, use it.
4857 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4858 default: break;
4859 case TargetLowering::Legal:
4860 if (!TLI.isTypeLegal(NewOutTy))
4861 break; // Can't use this datatype.
4862 // FALL THROUGH.
4863 case TargetLowering::Custom:
4864 OpToUse = ISD::FP_TO_UINT;
4865 break;
4866 }
4867 if (OpToUse) break;
4868
4869 // Otherwise, try a larger type.
4870 }
4871
4872 // Okay, we found the operation and type to use. Truncate the result of the
4873 // extended FP_TO_*INT operation to the desired size.
4874 return DAG.getNode(ISD::TRUNCATE, DestVT,
4875 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4876}
4877
4878/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4879///
4880SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4881 MVT::ValueType VT = Op.getValueType();
4882 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4883 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4884 switch (VT) {
4885 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4886 case MVT::i16:
4887 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4888 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4889 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4890 case MVT::i32:
4891 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4892 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4893 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4894 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4895 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4896 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4897 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4898 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4899 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4900 case MVT::i64:
4901 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4902 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4903 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4904 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4905 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4906 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4907 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4908 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4909 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4910 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4911 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4912 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4913 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4914 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4915 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4916 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4917 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4918 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4919 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4920 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4921 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4922 }
4923}
4924
4925/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4926///
4927SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4928 switch (Opc) {
4929 default: assert(0 && "Cannot expand this yet!");
4930 case ISD::CTPOP: {
4931 static const uint64_t mask[6] = {
4932 0x5555555555555555ULL, 0x3333333333333333ULL,
4933 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4934 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4935 };
4936 MVT::ValueType VT = Op.getValueType();
4937 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00004938 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004939 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4940 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4941 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4942 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4943 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4944 DAG.getNode(ISD::AND, VT,
4945 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4946 }
4947 return Op;
4948 }
4949 case ISD::CTLZ: {
4950 // for now, we do this:
4951 // x = x | (x >> 1);
4952 // x = x | (x >> 2);
4953 // ...
4954 // x = x | (x >>16);
4955 // x = x | (x >>32); // for 64-bit input
4956 // return popcount(~x);
4957 //
4958 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4959 MVT::ValueType VT = Op.getValueType();
4960 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00004961 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004962 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4963 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4964 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4965 }
4966 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4967 return DAG.getNode(ISD::CTPOP, VT, Op);
4968 }
4969 case ISD::CTTZ: {
4970 // for now, we use: { return popcount(~x & (x - 1)); }
4971 // unless the target has ctlz but not ctpop, in which case we use:
4972 // { return 32 - nlz(~x & (x-1)); }
4973 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4974 MVT::ValueType VT = Op.getValueType();
4975 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4976 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4977 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4978 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4979 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4980 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4981 TLI.isOperationLegal(ISD::CTLZ, VT))
4982 return DAG.getNode(ISD::SUB, VT,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004983 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00004984 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4985 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4986 }
4987 }
4988}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004989
Chris Lattnerdc750592005-01-07 07:47:09 +00004990/// ExpandOp - Expand the specified SDOperand into its two component pieces
4991/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4992/// LegalizeNodes map is filled in for any results that are not expanded, the
4993/// ExpandedNodes map is filled in for any results that are expanded, and the
4994/// Lo/Hi values are returned.
4995void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4996 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00004997 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00004998 SDNode *Node = Op.Val;
4999 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00005000 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohmana8665142007-06-25 16:23:39 +00005001 MVT::isVector(VT)) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00005002 "Cannot expand to FP value or to larger int value!");
5003
Chris Lattner1a570f12005-09-02 20:32:45 +00005004 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005005 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00005006 = ExpandedNodes.find(Op);
5007 if (I != ExpandedNodes.end()) {
5008 Lo = I->second.first;
5009 Hi = I->second.second;
5010 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00005011 }
5012
Chris Lattnerdc750592005-01-07 07:47:09 +00005013 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00005014 case ISD::CopyFromReg:
5015 assert(0 && "CopyFromReg must be legal!");
5016 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005017#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005018 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005019#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00005020 assert(0 && "Do not know how to expand this operator!");
5021 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00005022 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00005023 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00005024 Lo = DAG.getNode(ISD::UNDEF, NVT);
5025 Hi = DAG.getNode(ISD::UNDEF, NVT);
5026 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005027 case ISD::Constant: {
5028 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5029 Lo = DAG.getConstant(Cst, NVT);
5030 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5031 break;
5032 }
Evan Cheng47833a12006-12-12 21:32:44 +00005033 case ISD::ConstantFP: {
5034 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng3766fc602006-12-12 22:19:28 +00005035 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00005036 if (getTypeAction(Lo.getValueType()) == Expand)
5037 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005038 break;
5039 }
Chris Lattner32e08b72005-03-28 22:03:13 +00005040 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005041 // Return the operands.
5042 Lo = Node->getOperand(0);
5043 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00005044 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005045
5046 case ISD::SIGN_EXTEND_INREG:
5047 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00005048 // sext_inreg the low part if needed.
5049 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5050
5051 // The high part gets the sign extension from the lo-part. This handles
5052 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005053 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5054 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5055 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005056 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00005057
Nate Begeman2fba8a32006-01-14 03:14:10 +00005058 case ISD::BSWAP: {
5059 ExpandOp(Node->getOperand(0), Lo, Hi);
5060 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5061 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5062 Lo = TempLo;
5063 break;
5064 }
5065
Chris Lattner55e9cde2005-05-11 04:51:16 +00005066 case ISD::CTPOP:
5067 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00005068 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5069 DAG.getNode(ISD::CTPOP, NVT, Lo),
5070 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00005071 Hi = DAG.getConstant(0, NVT);
5072 break;
5073
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005074 case ISD::CTLZ: {
5075 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005076 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005077 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5078 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005079 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5080 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005081 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5082 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5083
5084 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5085 Hi = DAG.getConstant(0, NVT);
5086 break;
5087 }
5088
5089 case ISD::CTTZ: {
5090 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005091 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005092 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5093 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005094 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5095 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005096 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5097 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5098
5099 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5100 Hi = DAG.getConstant(0, NVT);
5101 break;
5102 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00005103
Nate Begemane74795c2006-01-25 18:21:52 +00005104 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005105 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5106 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00005107 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5108 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5109
5110 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005111 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00005112 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5113 if (!TLI.isLittleEndian())
5114 std::swap(Lo, Hi);
5115 break;
5116 }
5117
Chris Lattnerdc750592005-01-07 07:47:09 +00005118 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005119 LoadSDNode *LD = cast<LoadSDNode>(Node);
5120 SDOperand Ch = LD->getChain(); // Legalize the chain.
5121 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5122 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman2af30632007-07-09 22:18:38 +00005123 int SVOffset = LD->getSrcValueOffset();
5124 unsigned Alignment = LD->getAlignment();
5125 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00005126
Evan Chenge71fe34d2006-10-09 20:57:25 +00005127 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman2af30632007-07-09 22:18:38 +00005128 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5129 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00005130 if (VT == MVT::f32 || VT == MVT::f64) {
5131 // f32->i32 or f64->i64 one to one expansion.
5132 // Remember that we legalized the chain.
5133 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00005134 // Recursively expand the new load.
5135 if (getTypeAction(NVT) == Expand)
5136 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005137 break;
5138 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00005139
Evan Chenge71fe34d2006-10-09 20:57:25 +00005140 // Increment the pointer to the other half.
5141 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5142 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5143 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005144 SVOffset += IncrementSize;
Dan Gohman2af30632007-07-09 22:18:38 +00005145 if (Alignment > IncrementSize)
5146 Alignment = IncrementSize;
5147 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5148 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00005149
Evan Chenge71fe34d2006-10-09 20:57:25 +00005150 // Build a factor node to remember that this load is independent of the
5151 // other one.
5152 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5153 Hi.getValue(1));
5154
5155 // Remember that we legalized the chain.
5156 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5157 if (!TLI.isLittleEndian())
5158 std::swap(Lo, Hi);
5159 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00005160 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00005161
5162 if (VT == MVT::f64 && EVT == MVT::f32) {
5163 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5164 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005165 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00005166 // Remember that we legalized the chain.
5167 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5168 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5169 break;
5170 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00005171
5172 if (EVT == NVT)
5173 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005174 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005175 else
5176 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005177 SVOffset, EVT, isVolatile,
5178 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005179
5180 // Remember that we legalized the chain.
5181 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5182
5183 if (ExtType == ISD::SEXTLOAD) {
5184 // The high part is obtained by SRA'ing all but one of the bits of the
5185 // lo part.
5186 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5187 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5188 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5189 } else if (ExtType == ISD::ZEXTLOAD) {
5190 // The high part is just a zero.
5191 Hi = DAG.getConstant(0, NVT);
5192 } else /* if (ExtType == ISD::EXTLOAD) */ {
5193 // The high part is undefined.
5194 Hi = DAG.getNode(ISD::UNDEF, NVT);
5195 }
5196 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005197 break;
5198 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005199 case ISD::AND:
5200 case ISD::OR:
5201 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5202 SDOperand LL, LH, RL, RH;
5203 ExpandOp(Node->getOperand(0), LL, LH);
5204 ExpandOp(Node->getOperand(1), RL, RH);
5205 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5206 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5207 break;
5208 }
5209 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005210 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00005211 ExpandOp(Node->getOperand(1), LL, LH);
5212 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00005213 if (getTypeAction(NVT) == Expand)
5214 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005215 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00005216 if (VT != MVT::f32)
5217 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00005218 break;
5219 }
Nate Begemane5b86d72005-08-10 20:51:12 +00005220 case ISD::SELECT_CC: {
5221 SDOperand TL, TH, FL, FH;
5222 ExpandOp(Node->getOperand(2), TL, TH);
5223 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00005224 if (getTypeAction(NVT) == Expand)
5225 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00005226 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5227 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00005228 if (VT != MVT::f32)
5229 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5230 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00005231 break;
5232 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005233 case ISD::ANY_EXTEND:
5234 // The low part is any extension of the input (which degenerates to a copy).
5235 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5236 // The high part is undefined.
5237 Hi = DAG.getNode(ISD::UNDEF, NVT);
5238 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005239 case ISD::SIGN_EXTEND: {
5240 // The low part is just a sign extension of the input (which degenerates to
5241 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005242 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005243
Chris Lattnerdc750592005-01-07 07:47:09 +00005244 // The high part is obtained by SRA'ing all but one of the bits of the lo
5245 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00005246 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005247 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5248 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00005249 break;
5250 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005251 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00005252 // The low part is just a zero extension of the input (which degenerates to
5253 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005254 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005255
Chris Lattnerdc750592005-01-07 07:47:09 +00005256 // The high part is just a zero.
5257 Hi = DAG.getConstant(0, NVT);
5258 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00005259
Chris Lattner59b27fa372007-02-13 23:55:16 +00005260 case ISD::TRUNCATE: {
5261 // The input value must be larger than this value. Expand *it*.
5262 SDOperand NewLo;
5263 ExpandOp(Node->getOperand(0), NewLo, Hi);
5264
5265 // The low part is now either the right size, or it is closer. If not the
5266 // right size, make an illegal truncate so we recursively expand it.
5267 if (NewLo.getValueType() != Node->getValueType(0))
5268 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5269 ExpandOp(NewLo, Lo, Hi);
5270 break;
5271 }
5272
Chris Lattner36e663d2005-12-23 00:16:34 +00005273 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005274 SDOperand Tmp;
5275 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5276 // If the target wants to, allow it to lower this itself.
5277 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5278 case Expand: assert(0 && "cannot expand FP!");
5279 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5280 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5281 }
5282 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5283 }
5284
Evan Cheng4eee7242006-12-09 02:42:38 +00005285 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00005286 if (VT == MVT::f32 || VT == MVT::f64) {
5287 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00005288 if (getTypeAction(NVT) == Expand)
5289 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00005290 break;
5291 }
5292
5293 // If source operand will be expanded to the same type as VT, i.e.
5294 // i64 <- f64, i32 <- f32, expand the source operand instead.
5295 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5296 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5297 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005298 break;
5299 }
5300
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005301 // Turn this into a load/store pair by default.
5302 if (Tmp.Val == 0)
Evan Cheng3432ab92006-12-11 19:27:14 +00005303 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005304
Chris Lattner36e663d2005-12-23 00:16:34 +00005305 ExpandOp(Tmp, Lo, Hi);
5306 break;
5307 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005308
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005309 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00005310 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5311 TargetLowering::Custom &&
5312 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005313 Lo = TLI.LowerOperation(Op, DAG);
5314 assert(Lo.Val && "Node must be custom expanded!");
5315 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00005316 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005317 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005318 break;
5319
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005320 // These operators cannot be expanded directly, emit them as calls to
5321 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005322 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005323 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00005324 SDOperand Op;
5325 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5326 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005327 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5328 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00005329 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005330
Chris Lattner941d84a2005-07-30 01:40:57 +00005331 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5332
Chris Lattnerfe68d752005-07-29 00:33:32 +00005333 // Now that the custom expander is done, expand the result, which is still
5334 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005335 if (Op.Val) {
5336 ExpandOp(Op, Lo, Hi);
5337 break;
5338 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005339 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005340
Evan Cheng31cbddf2007-01-12 02:11:51 +00005341 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005342 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005343 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005344 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005345 LC = RTLIB::FPTOSINT_F64_I64;
5346 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5347 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005348 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005349 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005350
Evan Cheng31cbddf2007-01-12 02:11:51 +00005351 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005352 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005353 SDOperand Op;
5354 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5355 case Expand: assert(0 && "cannot expand FP!");
5356 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5357 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5358 }
5359
5360 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5361
5362 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005363 if (Op.Val) {
5364 ExpandOp(Op, Lo, Hi);
5365 break;
5366 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005367 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005368
Evan Cheng31cbddf2007-01-12 02:11:51 +00005369 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005370 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005371 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005372 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005373 LC = RTLIB::FPTOUINT_F64_I64;
5374 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5375 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005376 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005377 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005378
Evan Cheng870e4f82006-01-09 18:31:59 +00005379 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005380 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005381 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005382 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005383 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005384 Op = TLI.LowerOperation(Op, DAG);
5385 if (Op.Val) {
5386 // Now that the custom expander is done, expand the result, which is
5387 // still VT.
5388 ExpandOp(Op, Lo, Hi);
5389 break;
5390 }
5391 }
5392
Chris Lattner72b503b2006-09-13 03:50:39 +00005393 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5394 // this X << 1 as X+X.
5395 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5396 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5397 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5398 SDOperand LoOps[2], HiOps[3];
5399 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5400 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5401 LoOps[1] = LoOps[0];
5402 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5403
5404 HiOps[1] = HiOps[0];
5405 HiOps[2] = Lo.getValue(1);
5406 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5407 break;
5408 }
5409 }
5410
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005411 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005412 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005413 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005414
5415 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005416 TargetLowering::LegalizeAction Action =
5417 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5418 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5419 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005420 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005421 break;
5422 }
5423
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005424 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005425 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5426 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005427 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005428 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005429
Evan Cheng870e4f82006-01-09 18:31:59 +00005430 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005431 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005432 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005433 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005434 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005435 Op = TLI.LowerOperation(Op, DAG);
5436 if (Op.Val) {
5437 // Now that the custom expander is done, expand the result, which is
5438 // still VT.
5439 ExpandOp(Op, Lo, Hi);
5440 break;
5441 }
5442 }
5443
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005444 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005445 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005446 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005447
5448 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005449 TargetLowering::LegalizeAction Action =
5450 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5451 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5452 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005453 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005454 break;
5455 }
5456
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005457 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005458 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5459 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005460 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005461 }
5462
5463 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005464 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005465 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005466 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005467 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005468 Op = TLI.LowerOperation(Op, DAG);
5469 if (Op.Val) {
5470 // Now that the custom expander is done, expand the result, which is
5471 // still VT.
5472 ExpandOp(Op, Lo, Hi);
5473 break;
5474 }
5475 }
5476
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005477 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005478 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005479 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005480
5481 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005482 TargetLowering::LegalizeAction Action =
5483 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5484 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5485 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005486 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005487 break;
5488 }
5489
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005490 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005491 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5492 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005493 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005494 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005495
Misha Brukman835702a2005-04-21 22:36:52 +00005496 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005497 case ISD::SUB: {
5498 // If the target wants to custom expand this, let them.
5499 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5500 TargetLowering::Custom) {
5501 Op = TLI.LowerOperation(Op, DAG);
5502 if (Op.Val) {
5503 ExpandOp(Op, Lo, Hi);
5504 break;
5505 }
5506 }
5507
5508 // Expand the subcomponents.
5509 SDOperand LHSL, LHSH, RHSL, RHSH;
5510 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5511 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005512 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5513 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005514 LoOps[0] = LHSL;
5515 LoOps[1] = RHSL;
5516 HiOps[0] = LHSH;
5517 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005518 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005519 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005520 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005521 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005522 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005523 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005524 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005525 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005526 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005527 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005528 }
Chris Lattner2135bc02007-05-17 18:15:41 +00005529
5530 case ISD::ADDC:
5531 case ISD::SUBC: {
5532 // Expand the subcomponents.
5533 SDOperand LHSL, LHSH, RHSL, RHSH;
5534 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5535 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5536 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5537 SDOperand LoOps[2] = { LHSL, RHSL };
5538 SDOperand HiOps[3] = { LHSH, RHSH };
5539
5540 if (Node->getOpcode() == ISD::ADDC) {
5541 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5542 HiOps[2] = Lo.getValue(1);
5543 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5544 } else {
5545 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5546 HiOps[2] = Lo.getValue(1);
5547 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5548 }
5549 // Remember that we legalized the flag.
5550 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5551 break;
5552 }
5553 case ISD::ADDE:
5554 case ISD::SUBE: {
5555 // Expand the subcomponents.
5556 SDOperand LHSL, LHSH, RHSL, RHSH;
5557 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5558 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5559 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5560 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5561 SDOperand HiOps[3] = { LHSH, RHSH };
5562
5563 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5564 HiOps[2] = Lo.getValue(1);
5565 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5566
5567 // Remember that we legalized the flag.
5568 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5569 break;
5570 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005571 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005572 // If the target wants to custom expand this, let them.
5573 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00005574 SDOperand New = TLI.LowerOperation(Op, DAG);
5575 if (New.Val) {
5576 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005577 break;
5578 }
5579 }
5580
Evan Chenge93762d2006-09-01 18:17:58 +00005581 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5582 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Chenge93762d2006-09-01 18:17:58 +00005583 if (HasMULHS || HasMULHU) {
Nate Begemanadd0c632005-04-11 03:01:51 +00005584 SDOperand LL, LH, RL, RH;
5585 ExpandOp(Node->getOperand(0), LL, LH);
5586 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00005587 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman8e20c762006-12-11 02:23:46 +00005588 // FIXME: Move this to the dag combiner.
Nate Begeman43144a22005-08-30 02:44:00 +00005589 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5590 // extended the sign bit of the low half through the upper half, and if so
5591 // emit a MULHS instead of the alternate sequence that is valid for any
5592 // i64 x i64 multiply.
Evan Chenge93762d2006-09-01 18:17:58 +00005593 if (HasMULHS &&
Nate Begeman43144a22005-08-30 02:44:00 +00005594 // is RH an extension of the sign bit of RL?
5595 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5596 RH.getOperand(1).getOpcode() == ISD::Constant &&
5597 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5598 // is LH an extension of the sign bit of LL?
5599 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5600 LH.getOperand(1).getOpcode() == ISD::Constant &&
5601 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattner1b633912006-09-16 00:21:44 +00005602 // Low part:
5603 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5604 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005605 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattner1b633912006-09-16 00:21:44 +00005606 break;
Evan Chenge93762d2006-09-01 18:17:58 +00005607 } else if (HasMULHU) {
Chris Lattner1b633912006-09-16 00:21:44 +00005608 // Low part:
5609 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5610
5611 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005612 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5613 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5614 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5615 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5616 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00005617 break;
Nate Begeman43144a22005-08-30 02:44:00 +00005618 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005619 }
Evan Chenge93762d2006-09-01 18:17:58 +00005620
Evan Cheng31cbddf2007-01-12 02:11:51 +00005621 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5622 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00005623 break;
5624 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005625 case ISD::SDIV:
5626 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5627 break;
5628 case ISD::UDIV:
5629 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5630 break;
5631 case ISD::SREM:
5632 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5633 break;
5634 case ISD::UREM:
5635 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5636 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00005637
Evan Cheng4eee7242006-12-09 02:42:38 +00005638 case ISD::FADD:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005639 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5640 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5641 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005642 break;
5643 case ISD::FSUB:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005644 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5645 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5646 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005647 break;
5648 case ISD::FMUL:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005649 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5650 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5651 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005652 break;
5653 case ISD::FDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005654 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5655 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5656 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005657 break;
5658 case ISD::FP_EXTEND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005659 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005660 break;
5661 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005662 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005663 break;
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00005664 case ISD::FPOWI:
5665 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5666 ? RTLIB::POWI_F32 : RTLIB::POWI_F64),
5667 Node, false, Hi);
5668 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005669 case ISD::FSQRT:
5670 case ISD::FSIN:
5671 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005672 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00005673 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005674 case ISD::FSQRT:
5675 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5676 break;
5677 case ISD::FSIN:
5678 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5679 break;
5680 case ISD::FCOS:
5681 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5682 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005683 default: assert(0 && "Unreachable!");
5684 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005685 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00005686 break;
5687 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00005688 case ISD::FABS: {
5689 SDOperand Mask = (VT == MVT::f64)
5690 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5691 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5692 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5693 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5694 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5695 if (getTypeAction(NVT) == Expand)
5696 ExpandOp(Lo, Lo, Hi);
5697 break;
5698 }
5699 case ISD::FNEG: {
5700 SDOperand Mask = (VT == MVT::f64)
5701 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5702 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5703 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5704 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5705 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5706 if (getTypeAction(NVT) == Expand)
5707 ExpandOp(Lo, Lo, Hi);
5708 break;
5709 }
Evan Cheng003feb02007-01-04 21:56:39 +00005710 case ISD::FCOPYSIGN: {
5711 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5712 if (getTypeAction(NVT) == Expand)
5713 ExpandOp(Lo, Lo, Hi);
5714 break;
5715 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005716 case ISD::SINT_TO_FP:
5717 case ISD::UINT_TO_FP: {
5718 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5719 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00005720 RTLIB::Libcall LC;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005721 if (Node->getOperand(0).getValueType() == MVT::i64) {
5722 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005723 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005724 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005725 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005726 } else {
5727 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005728 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005729 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005730 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005731 }
5732
5733 // Promote the operand if needed.
5734 if (getTypeAction(SrcVT) == Promote) {
5735 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5736 Tmp = isSigned
5737 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5738 DAG.getValueType(SrcVT))
5739 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5740 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5741 }
Evan Chengbf535fc2007-04-27 07:33:31 +00005742
5743 const char *LibCall = TLI.getLibcallName(LC);
5744 if (LibCall)
5745 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5746 else {
5747 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5748 Node->getOperand(0));
5749 if (getTypeAction(Lo.getValueType()) == Expand)
5750 ExpandOp(Lo, Lo, Hi);
5751 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005752 break;
5753 }
Evan Chengf3a80c62006-12-13 02:38:13 +00005754 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005755
Chris Lattnerac12f682005-12-21 18:02:52 +00005756 // Make sure the resultant values have been legalized themselves, unless this
5757 // is a type that requires multi-step expansion.
5758 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5759 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00005760 if (Hi.Val)
5761 // Don't legalize the high part if it is expanded to a single node.
5762 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00005763 }
Evan Cheng870e4f82006-01-09 18:31:59 +00005764
5765 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005766 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00005767 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00005768}
5769
Dan Gohmana8665142007-06-25 16:23:39 +00005770/// SplitVectorOp - Given an operand of vector type, break it down into
5771/// two smaller values, still of vector type.
Chris Lattner32206f52006-03-18 01:44:44 +00005772void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5773 SDOperand &Hi) {
Dan Gohmana8665142007-06-25 16:23:39 +00005774 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00005775 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00005776 unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0));
Chris Lattner32206f52006-03-18 01:44:44 +00005777 assert(NumElements > 1 && "Cannot split a single element vector!");
5778 unsigned NewNumElts = NumElements/2;
Dan Gohmana8665142007-06-25 16:23:39 +00005779 MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0));
5780 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattner32206f52006-03-18 01:44:44 +00005781
5782 // See if we already split it.
5783 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5784 = SplitNodes.find(Op);
5785 if (I != SplitNodes.end()) {
5786 Lo = I->second.first;
5787 Hi = I->second.second;
5788 return;
5789 }
5790
5791 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005792 default:
5793#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005794 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005795#endif
5796 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohmana8665142007-06-25 16:23:39 +00005797 case ISD::BUILD_PAIR:
5798 Lo = Node->getOperand(0);
5799 Hi = Node->getOperand(1);
5800 break;
5801 case ISD::BUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005802 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5803 Node->op_begin()+NewNumElts);
Dan Gohmana8665142007-06-25 16:23:39 +00005804 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005805
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005806 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohmana8665142007-06-25 16:23:39 +00005807 Node->op_end());
5808 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005809 break;
5810 }
Dan Gohmana8665142007-06-25 16:23:39 +00005811 case ISD::CONCAT_VECTORS: {
5812 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5813 if (NewNumSubvectors == 1) {
5814 Lo = Node->getOperand(0);
5815 Hi = Node->getOperand(1);
5816 } else {
5817 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5818 Node->op_begin()+NewNumSubvectors);
5819 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00005820
Dan Gohmana8665142007-06-25 16:23:39 +00005821 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5822 Node->op_end());
5823 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5824 }
Dan Gohman26455c42007-06-13 15:12:02 +00005825 break;
5826 }
Dan Gohmana8665142007-06-25 16:23:39 +00005827 case ISD::ADD:
5828 case ISD::SUB:
5829 case ISD::MUL:
5830 case ISD::FADD:
5831 case ISD::FSUB:
5832 case ISD::FMUL:
5833 case ISD::SDIV:
5834 case ISD::UDIV:
5835 case ISD::FDIV:
5836 case ISD::AND:
5837 case ISD::OR:
5838 case ISD::XOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00005839 SDOperand LL, LH, RL, RH;
5840 SplitVectorOp(Node->getOperand(0), LL, LH);
5841 SplitVectorOp(Node->getOperand(1), RL, RH);
5842
Dan Gohmana8665142007-06-25 16:23:39 +00005843 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5844 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00005845 break;
5846 }
Dan Gohmana8665142007-06-25 16:23:39 +00005847 case ISD::LOAD: {
5848 LoadSDNode *LD = cast<LoadSDNode>(Node);
5849 SDOperand Ch = LD->getChain();
5850 SDOperand Ptr = LD->getBasePtr();
5851 const Value *SV = LD->getSrcValue();
5852 int SVOffset = LD->getSrcValueOffset();
5853 unsigned Alignment = LD->getAlignment();
5854 bool isVolatile = LD->isVolatile();
5855
5856 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5857 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattner32206f52006-03-18 01:44:44 +00005858 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5859 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005860 SVOffset += IncrementSize;
5861 if (Alignment > IncrementSize)
5862 Alignment = IncrementSize;
5863 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00005864
5865 // Build a factor node to remember that this load is independent of the
5866 // other one.
5867 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5868 Hi.getValue(1));
5869
5870 // Remember that we legalized the chain.
5871 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00005872 break;
5873 }
Dan Gohmana8665142007-06-25 16:23:39 +00005874 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005875 // We know the result is a vector. The input may be either a vector or a
5876 // scalar value.
Dan Gohman0de76942007-06-29 00:09:08 +00005877 SDOperand InOp = Node->getOperand(0);
5878 if (!MVT::isVector(InOp.getValueType()) ||
5879 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
5880 // The input is a scalar or single-element vector.
5881 // Lower to a store/load so that it can be split.
5882 // FIXME: this could be improved probably.
5883 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005884
Evan Chengdf9ac472006-10-05 23:01:46 +00005885 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman0de76942007-06-29 00:09:08 +00005886 InOp, Ptr, NULL, 0);
5887 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005888 }
Dan Gohman0de76942007-06-29 00:09:08 +00005889 // Split the vector and convert each of the pieces now.
5890 SplitVectorOp(InOp, Lo, Hi);
5891 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5892 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
5893 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005894 }
Chris Lattner32206f52006-03-18 01:44:44 +00005895 }
5896
5897 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005898 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00005899 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00005900 assert(isNew && "Value already split?!?");
Chris Lattner32206f52006-03-18 01:44:44 +00005901}
5902
5903
Dan Gohmanf4e86da2007-06-27 14:06:22 +00005904/// ScalarizeVectorOp - Given an operand of single-element vector type
5905/// (e.g. v1f32), convert it into the equivalent operation that returns a
5906/// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +00005907SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5908 assert(MVT::isVector(Op.getValueType()) &&
5909 "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00005910 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00005911 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5912 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00005913
Dan Gohmana8665142007-06-25 16:23:39 +00005914 // See if we already scalarized it.
5915 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5916 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00005917
5918 SDOperand Result;
5919 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00005920 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005921#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005922 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005923#endif
Dan Gohmana8665142007-06-25 16:23:39 +00005924 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5925 case ISD::ADD:
5926 case ISD::FADD:
5927 case ISD::SUB:
5928 case ISD::FSUB:
5929 case ISD::MUL:
5930 case ISD::FMUL:
5931 case ISD::SDIV:
5932 case ISD::UDIV:
5933 case ISD::FDIV:
5934 case ISD::SREM:
5935 case ISD::UREM:
5936 case ISD::FREM:
5937 case ISD::AND:
5938 case ISD::OR:
5939 case ISD::XOR:
5940 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00005941 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00005942 ScalarizeVectorOp(Node->getOperand(0)),
5943 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00005944 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005945 case ISD::FNEG:
5946 case ISD::FABS:
5947 case ISD::FSQRT:
5948 case ISD::FSIN:
5949 case ISD::FCOS:
5950 Result = DAG.getNode(Node->getOpcode(),
5951 NewVT,
5952 ScalarizeVectorOp(Node->getOperand(0)));
5953 break;
5954 case ISD::LOAD: {
5955 LoadSDNode *LD = cast<LoadSDNode>(Node);
5956 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5957 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00005958
Dan Gohmana8665142007-06-25 16:23:39 +00005959 const Value *SV = LD->getSrcValue();
5960 int SVOffset = LD->getSrcValueOffset();
5961 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5962 LD->isVolatile(), LD->getAlignment());
5963
Chris Lattner32206f52006-03-18 01:44:44 +00005964 // Remember that we legalized the chain.
5965 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5966 break;
5967 }
Dan Gohmana8665142007-06-25 16:23:39 +00005968 case ISD::BUILD_VECTOR:
5969 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00005970 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005971 case ISD::INSERT_VECTOR_ELT:
5972 // Returning the inserted scalar element.
5973 Result = Node->getOperand(1);
5974 break;
5975 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00005976 assert(Node->getOperand(0).getValueType() == NewVT &&
5977 "Concat of non-legal vectors not yet supported!");
5978 Result = Node->getOperand(0);
5979 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005980 case ISD::VECTOR_SHUFFLE: {
5981 // Figure out if the scalar is the LHS or RHS and return it.
5982 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5983 if (cast<ConstantSDNode>(EltNum)->getValue())
5984 Result = ScalarizeVectorOp(Node->getOperand(1));
5985 else
5986 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00005987 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005988 }
5989 case ISD::EXTRACT_SUBVECTOR:
5990 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00005991 assert(Result.getValueType() == NewVT);
5992 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005993 case ISD::BIT_CONVERT:
5994 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattnerf6f94d32006-03-28 20:24:43 +00005995 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005996 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00005997 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00005998 ScalarizeVectorOp(Op.getOperand(1)),
5999 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00006000 break;
Chris Lattner32206f52006-03-18 01:44:44 +00006001 }
6002
6003 if (TLI.isTypeLegal(NewVT))
6004 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00006005 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6006 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00006007 return Result;
6008}
6009
Chris Lattnerdc750592005-01-07 07:47:09 +00006010
6011// SelectionDAG::Legalize - This is the entry point for the file.
6012//
Chris Lattner4add7e32005-01-23 04:42:50 +00006013void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00006014 if (ViewLegalizeDAGs) viewGraph();
6015
Chris Lattnerdc750592005-01-07 07:47:09 +00006016 /// run - This is the main entry point to this class.
6017 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00006018 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00006019}
6020