blob: ce28c79dc9cbd829b6916687086ef9fbeae57de2 [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;
Dale Johannesen7f724e92007-09-16 16:51:49 +0000489 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen98d3a082007-09-14 22:26:36 +0000490 CFP->getValueAPF());
Evan Cheng22cf8992006-12-13 20:57:08 +0000491 if (!UseCP) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000492 if (VT!=MVT::f64 && VT!=MVT::f32)
493 assert(0 && "Invalid type expansion");
Dale Johannesen028084e2007-09-12 03:30:33 +0000494 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
495 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000496 }
497
Dale Johannesend246b2c2007-08-30 00:23:21 +0000498 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng47833a12006-12-12 21:32:44 +0000499 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen98d3a082007-09-14 22:26:36 +0000500 // Do not try to be clever about long doubles (so far)
Evan Cheng47833a12006-12-12 21:32:44 +0000501 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
502 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
503 VT = MVT::f32;
504 Extend = true;
505 }
506
507 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
508 if (Extend) {
509 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
510 CPIdx, NULL, 0, MVT::f32);
511 } else {
512 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
513 }
514}
515
Chris Lattner462505f2006-02-13 09:18:02 +0000516
Evan Cheng003feb02007-01-04 21:56:39 +0000517/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
518/// operations.
519static
520SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
521 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng3b841dd2007-01-05 21:31:51 +0000522 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000523 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000524 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
525 "fcopysign expansion only supported for f32 and f64");
Evan Cheng003feb02007-01-04 21:56:39 +0000526 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000527
Evan Cheng003feb02007-01-04 21:56:39 +0000528 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000529 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000530 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
531 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000532 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000533 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000534 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000535 // Shift right or sign-extend it if the two operands have different types.
536 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
537 if (SizeDiff > 0) {
538 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
539 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
540 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
541 } else if (SizeDiff < 0)
542 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000543
544 // Clear the sign bit of first operand.
545 SDOperand Mask2 = (VT == MVT::f64)
546 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
547 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
548 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng003feb02007-01-04 21:56:39 +0000549 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000550 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
551
552 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000553 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
554 return Result;
555}
556
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000557/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
558static
559SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
560 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000561 SDOperand Chain = ST->getChain();
562 SDOperand Ptr = ST->getBasePtr();
563 SDOperand Val = ST->getValue();
564 MVT::ValueType VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000565 int Alignment = ST->getAlignment();
566 int SVOffset = ST->getSrcValueOffset();
567 if (MVT::isFloatingPoint(ST->getStoredVT())) {
568 // Expand to a bitconvert of the value to the integer type of the
569 // same size, then a (misaligned) int store.
570 MVT::ValueType intVT;
571 if (VT==MVT::f64)
572 intVT = MVT::i64;
573 else if (VT==MVT::f32)
574 intVT = MVT::i32;
575 else
576 assert(0 && "Unaligned load of unsupported floating point type");
577
578 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
579 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
580 SVOffset, ST->isVolatile(), Alignment);
581 }
582 assert(MVT::isInteger(ST->getStoredVT()) &&
583 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000584 // Get the half-size VT
585 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
586 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000587 int IncrementSize = NumBits / 8;
588
589 // Divide the stored value in two parts.
590 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
591 SDOperand Lo = Val;
592 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
593
594 // Store the two parts
595 SDOperand Store1, Store2;
596 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
597 ST->getSrcValue(), SVOffset, NewStoredVT,
598 ST->isVolatile(), Alignment);
599 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
600 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
601 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
602 ST->getSrcValue(), SVOffset + IncrementSize,
603 NewStoredVT, ST->isVolatile(), Alignment);
604
605 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
606}
607
608/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
609static
610SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
611 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000612 int SVOffset = LD->getSrcValueOffset();
613 SDOperand Chain = LD->getChain();
614 SDOperand Ptr = LD->getBasePtr();
615 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000616 MVT::ValueType LoadedVT = LD->getLoadedVT();
617 if (MVT::isFloatingPoint(VT)) {
618 // Expand to a (misaligned) integer load of the same size,
619 // then bitconvert to floating point.
620 MVT::ValueType intVT;
621 if (LoadedVT==MVT::f64)
622 intVT = MVT::i64;
623 else if (LoadedVT==MVT::f32)
624 intVT = MVT::i32;
625 else
626 assert(0 && "Unaligned load of unsupported floating point type");
627
628 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
629 SVOffset, LD->isVolatile(),
630 LD->getAlignment());
631 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
632 if (LoadedVT != VT)
633 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
634
635 SDOperand Ops[] = { Result, Chain };
636 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
637 Ops, 2);
638 }
639 assert(MVT::isInteger(LoadedVT) && "Unaligned load of unsupported type.");
640 MVT::ValueType NewLoadedVT = LoadedVT - 1;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000641 int NumBits = MVT::getSizeInBits(NewLoadedVT);
642 int Alignment = LD->getAlignment();
643 int IncrementSize = NumBits / 8;
644 ISD::LoadExtType HiExtType = LD->getExtensionType();
645
646 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
647 if (HiExtType == ISD::NON_EXTLOAD)
648 HiExtType = ISD::ZEXTLOAD;
649
650 // Load the value in two parts
651 SDOperand Lo, Hi;
652 if (TLI.isLittleEndian()) {
653 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
654 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
655 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
656 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
657 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
658 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
659 Alignment);
660 } else {
661 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
662 NewLoadedVT,LD->isVolatile(), Alignment);
663 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
664 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
665 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
666 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
667 Alignment);
668 }
669
670 // aggregate the two parts
671 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
672 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
673 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
674
675 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
676 Hi.getValue(1));
677
678 SDOperand Ops[] = { Result, TF };
679 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
680}
Evan Cheng003feb02007-01-04 21:56:39 +0000681
Dan Gohmanff727882007-07-13 20:14:11 +0000682/// LegalizeOp - We know that the specified value has a legal type, and
683/// that its operands are legal. Now ensure that the operation itself
684/// is legal, recursively ensuring that the operands' operations remain
685/// legal.
Chris Lattnerdc750592005-01-07 07:47:09 +0000686SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner2ed652f2007-08-25 01:00:22 +0000687 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
688 return Op;
689
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000690 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000691 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000692 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000693
Chris Lattnerdc750592005-01-07 07:47:09 +0000694 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000695 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000696 if (Node->getNumValues() > 1) {
697 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000698 if (getTypeAction(Node->getValueType(i)) != Legal) {
699 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000700 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000701 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000702 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000703 }
704 }
705
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000706 // Note that LegalizeOp may be reentered even from single-use nodes, which
707 // means that we always must cache transformed nodes.
Chris Lattnered39c862007-02-04 00:50:02 +0000708 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000709 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000710
Nate Begemane5b86d72005-08-10 20:51:12 +0000711 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000712 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000713 bool isCustom = false;
714
Chris Lattnerdc750592005-01-07 07:47:09 +0000715 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000716 case ISD::FrameIndex:
717 case ISD::EntryToken:
718 case ISD::Register:
719 case ISD::BasicBlock:
720 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000721 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000722 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000723 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000724 case ISD::TargetConstantPool:
725 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000726 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000727 case ISD::TargetExternalSymbol:
728 case ISD::VALUETYPE:
729 case ISD::SRCVALUE:
730 case ISD::STRING:
731 case ISD::CONDCODE:
732 // Primitives must all be legal.
733 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
734 "This must be legal!");
735 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000736 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000737 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
738 // If this is a target node, legalize it by legalizing the operands then
739 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000740 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000741 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000742 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000743
Chris Lattner97af9d52006-08-08 01:09:31 +0000744 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000745
746 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
747 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
748 return Result.getValue(Op.ResNo);
749 }
750 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000751#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000752 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000753#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000754 assert(0 && "Do not know how to legalize this operator!");
755 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000756 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000757 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000758 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000759 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000760 case ISD::ConstantPool:
761 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000762 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
763 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000764 case TargetLowering::Custom:
765 Tmp1 = TLI.LowerOperation(Op, DAG);
766 if (Tmp1.Val) Result = Tmp1;
767 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000768 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000769 break;
770 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000771 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000772 case ISD::FRAMEADDR:
773 case ISD::RETURNADDR:
774 // The only option for these nodes is to custom lower them. If the target
775 // does not custom lower them, then return zero.
776 Tmp1 = TLI.LowerOperation(Op, DAG);
777 if (Tmp1.Val)
778 Result = Tmp1;
779 else
780 Result = DAG.getConstant(0, TLI.getPointerTy());
781 break;
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000782 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000783 MVT::ValueType VT = Node->getValueType(0);
784 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
785 default: assert(0 && "This action is not supported yet!");
786 case TargetLowering::Custom:
787 Result = TLI.LowerOperation(Op, DAG);
788 if (Result.Val) break;
789 // Fall Thru
790 case TargetLowering::Legal:
791 Result = DAG.getConstant(0, VT);
792 break;
793 }
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000794 }
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000795 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000796 case ISD::EXCEPTIONADDR: {
797 Tmp1 = LegalizeOp(Node->getOperand(0));
798 MVT::ValueType VT = Node->getValueType(0);
799 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
800 default: assert(0 && "This action is not supported yet!");
801 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000802 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey7f5872c2007-02-22 15:37:19 +0000803 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
804 }
805 break;
806 case TargetLowering::Custom:
807 Result = TLI.LowerOperation(Op, DAG);
808 if (Result.Val) break;
809 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000810 case TargetLowering::Legal: {
811 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
812 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
813 Ops, 2).getValue(Op.ResNo);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000814 break;
815 }
816 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000817 }
Jim Laskey7f5872c2007-02-22 15:37:19 +0000818 break;
Jim Laskey644af6b2007-02-28 20:43:58 +0000819 case ISD::EHSELECTION: {
820 Tmp1 = LegalizeOp(Node->getOperand(0));
821 Tmp2 = LegalizeOp(Node->getOperand(1));
822 MVT::ValueType VT = Node->getValueType(0);
823 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
824 default: assert(0 && "This action is not supported yet!");
825 case TargetLowering::Expand: {
826 unsigned Reg = TLI.getExceptionSelectorRegister();
827 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
828 }
829 break;
830 case TargetLowering::Custom:
831 Result = TLI.LowerOperation(Op, DAG);
832 if (Result.Val) break;
833 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000834 case TargetLowering::Legal: {
835 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
836 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
837 Ops, 2).getValue(Op.ResNo);
Jim Laskey644af6b2007-02-28 20:43:58 +0000838 break;
839 }
840 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000841 }
Jim Laskey644af6b2007-02-28 20:43:58 +0000842 break;
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000843 case ISD::EH_RETURN: {
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000844 MVT::ValueType VT = Node->getValueType(0);
845 // The only "good" option for this node is to custom lower it.
846 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
847 default: assert(0 && "This action is not supported at all!");
848 case TargetLowering::Custom:
849 Result = TLI.LowerOperation(Op, DAG);
850 if (Result.Val) break;
851 // Fall Thru
852 case TargetLowering::Legal:
853 // Target does not know, how to lower this, lower to noop
854 Result = LegalizeOp(Node->getOperand(0));
855 break;
856 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000857 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000858 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000859 case ISD::AssertSext:
860 case ISD::AssertZext:
861 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000862 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000863 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000864 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000865 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000866 Result = Node->getOperand(Op.ResNo);
867 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000868 case ISD::CopyFromReg:
869 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000870 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000871 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000872 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000873 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000874 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000875 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000876 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000877 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
878 } else {
879 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
880 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000881 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
882 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000883 // Since CopyFromReg produces two values, make sure to remember that we
884 // legalized both of them.
885 AddLegalizedOperand(Op.getValue(0), Result);
886 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
887 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000888 case ISD::UNDEF: {
889 MVT::ValueType VT = Op.getValueType();
890 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000891 default: assert(0 && "This action is not supported yet!");
892 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000893 if (MVT::isInteger(VT))
894 Result = DAG.getConstant(0, VT);
895 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf04d37d2007-09-26 17:26:49 +0000896 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
897 VT);
Nate Begemancda9aa72005-04-01 22:34:39 +0000898 else
899 assert(0 && "Unknown value type!");
900 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000901 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000902 break;
903 }
904 break;
905 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000906
Chris Lattnere55d1712006-03-28 00:40:33 +0000907 case ISD::INTRINSIC_W_CHAIN:
908 case ISD::INTRINSIC_WO_CHAIN:
909 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +0000910 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +0000911 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
912 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000913 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +0000914
915 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000916 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000917 TargetLowering::Custom) {
918 Tmp3 = TLI.LowerOperation(Result, DAG);
919 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000920 }
921
922 if (Result.Val->getNumValues() == 1) break;
923
924 // Must have return value and chain result.
925 assert(Result.Val->getNumValues() == 2 &&
926 "Cannot return more than two values!");
927
928 // Since loads produce two values, make sure to remember that we
929 // legalized both of them.
930 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
931 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
932 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000933 }
Chris Lattner435b4022005-11-29 06:21:05 +0000934
935 case ISD::LOCATION:
936 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
937 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
938
939 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
940 case TargetLowering::Promote:
941 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000942 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +0000943 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000944 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +0000945 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000946
Jim Laskeyc56315c2007-01-26 21:22:28 +0000947 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000948 const std::string &FName =
949 cast<StringSDNode>(Node->getOperand(3))->getValue();
950 const std::string &DirName =
951 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000952 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000953
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000954 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +0000955 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000956 SDOperand LineOp = Node->getOperand(1);
957 SDOperand ColOp = Node->getOperand(2);
958
959 if (useDEBUG_LOC) {
960 Ops.push_back(LineOp); // line #
961 Ops.push_back(ColOp); // col #
962 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000963 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000964 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000965 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
966 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000967 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000968 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskeyf9e54452007-01-26 14:34:52 +0000969 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000970 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000971 } else {
972 Result = Tmp1; // chain
973 }
Chris Lattner435b4022005-11-29 06:21:05 +0000974 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000975 }
Chris Lattner435b4022005-11-29 06:21:05 +0000976 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000977 if (Tmp1 != Node->getOperand(0) ||
978 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +0000979 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +0000980 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000981 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
982 Ops.push_back(Node->getOperand(1)); // line # must be legal.
983 Ops.push_back(Node->getOperand(2)); // col # must be legal.
984 } else {
985 // Otherwise promote them.
986 Ops.push_back(PromoteOp(Node->getOperand(1)));
987 Ops.push_back(PromoteOp(Node->getOperand(2)));
988 }
Chris Lattner435b4022005-11-29 06:21:05 +0000989 Ops.push_back(Node->getOperand(3)); // filename must be legal.
990 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +0000991 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +0000992 }
993 break;
994 }
995 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000996
997 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000998 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000999 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +00001000 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +00001001 case TargetLowering::Legal:
1002 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1003 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1004 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1005 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001006 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001007 break;
1008 }
1009 break;
1010
Jim Laskeyf9e54452007-01-26 14:34:52 +00001011 case ISD::LABEL:
1012 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1013 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001014 default: assert(0 && "This action is not supported yet!");
1015 case TargetLowering::Legal:
1016 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1017 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001018 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +00001019 break;
Chris Lattner567b9252007-03-03 19:21:38 +00001020 case TargetLowering::Expand:
1021 Result = LegalizeOp(Node->getOperand(0));
1022 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001023 }
Nate Begeman5965bd12006-02-17 05:43:56 +00001024 break;
Chris Lattner435b4022005-11-29 06:21:05 +00001025
Scott Michel9d09c5c2007-08-08 23:23:31 +00001026 case ISD::Constant: {
1027 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1028 unsigned opAction =
1029 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1030
Chris Lattnerdc750592005-01-07 07:47:09 +00001031 // We know we don't need to expand constants here, constants only have one
1032 // value and we check that it is fine above.
1033
Scott Michel9d09c5c2007-08-08 23:23:31 +00001034 if (opAction == TargetLowering::Custom) {
1035 Tmp1 = TLI.LowerOperation(Result, DAG);
1036 if (Tmp1.Val)
1037 Result = Tmp1;
1038 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001039 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +00001040 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001041 case ISD::ConstantFP: {
1042 // Spill FP immediates to the constant pool if the target cannot directly
1043 // codegen them. Targets often have some immediate values that can be
1044 // efficiently generated into an FP register without a load. We explicitly
1045 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +00001046 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1047
1048 // Check to see if this FP immediate is already legal.
1049 bool isLegal = false;
1050 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1051 E = TLI.legal_fpimm_end(); I != E; ++I)
1052 if (CFP->isExactlyValue(*I)) {
1053 isLegal = true;
1054 break;
1055 }
1056
Chris Lattner758b0ac2006-01-29 06:26:56 +00001057 // If this is a legal constant, turn it into a TargetConstantFP node.
1058 if (isLegal) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00001059 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1060 CFP->getValueType(0));
Chris Lattner758b0ac2006-01-29 06:26:56 +00001061 break;
1062 }
1063
1064 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1065 default: assert(0 && "This action is not supported yet!");
1066 case TargetLowering::Custom:
1067 Tmp3 = TLI.LowerOperation(Result, DAG);
1068 if (Tmp3.Val) {
1069 Result = Tmp3;
1070 break;
1071 }
1072 // FALLTHROUGH
1073 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +00001074 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001075 }
1076 break;
1077 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001078 case ISD::TokenFactor:
1079 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001080 Tmp1 = LegalizeOp(Node->getOperand(0));
1081 Tmp2 = LegalizeOp(Node->getOperand(1));
1082 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1083 } else if (Node->getNumOperands() == 3) {
1084 Tmp1 = LegalizeOp(Node->getOperand(0));
1085 Tmp2 = LegalizeOp(Node->getOperand(1));
1086 Tmp3 = LegalizeOp(Node->getOperand(2));
1087 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001088 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +00001089 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001090 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001091 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1092 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001093 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001094 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001095 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001096
1097 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001098 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001099 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001100 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1101 assert(Tmp3.Val && "Target didn't custom lower this node!");
1102 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1103 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001104
Chris Lattneraaa23d92006-05-16 22:53:20 +00001105 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001106 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +00001107 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1108 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001109 if (Op.ResNo == i)
1110 Tmp2 = Tmp1;
1111 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1112 }
1113 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001114 case ISD::EXTRACT_SUBREG: {
1115 Tmp1 = LegalizeOp(Node->getOperand(0));
1116 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1117 assert(idx && "Operand must be a constant");
1118 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1119 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1120 }
1121 break;
1122 case ISD::INSERT_SUBREG: {
1123 Tmp1 = LegalizeOp(Node->getOperand(0));
1124 Tmp2 = LegalizeOp(Node->getOperand(1));
1125 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1126 assert(idx && "Operand must be a constant");
1127 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1128 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1129 }
1130 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001131 case ISD::BUILD_VECTOR:
1132 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001133 default: assert(0 && "This action is not supported yet!");
1134 case TargetLowering::Custom:
1135 Tmp3 = TLI.LowerOperation(Result, DAG);
1136 if (Tmp3.Val) {
1137 Result = Tmp3;
1138 break;
1139 }
1140 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001141 case TargetLowering::Expand:
1142 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001143 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001144 }
Chris Lattner29b23012006-03-19 01:17:20 +00001145 break;
1146 case ISD::INSERT_VECTOR_ELT:
1147 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1148 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1149 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1150 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1151
1152 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1153 Node->getValueType(0))) {
1154 default: assert(0 && "This action is not supported yet!");
1155 case TargetLowering::Legal:
1156 break;
1157 case TargetLowering::Custom:
1158 Tmp3 = TLI.LowerOperation(Result, DAG);
1159 if (Tmp3.Val) {
1160 Result = Tmp3;
1161 break;
1162 }
1163 // FALLTHROUGH
1164 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001165 // If the insert index is a constant, codegen this as a scalar_to_vector,
1166 // then a shuffle that inserts it into the right position in the vector.
1167 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1168 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1169 Tmp1.getValueType(), Tmp2);
1170
1171 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1172 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman5c441312007-06-14 22:58:02 +00001173 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner326870b2006-04-17 19:21:01 +00001174
1175 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1176 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1177 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001178 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +00001179 for (unsigned i = 0; i != NumElts; ++i) {
1180 if (i != InsertPos->getValue())
1181 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1182 else
1183 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1184 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001185 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1186 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +00001187
1188 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1189 Tmp1, ScVec, ShufMask);
1190 Result = LegalizeOp(Result);
1191 break;
1192 }
1193
Chris Lattner29b23012006-03-19 01:17:20 +00001194 // If the target doesn't support this, we have to spill the input vector
1195 // to a temporary stack slot, update the element, then reload it. This is
1196 // badness. We could also load the value into a vector register (either
1197 // with a "move to register" or "extload into register" instruction, then
1198 // permute it into place, if the idx is a constant and if the idx is
1199 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001200 MVT::ValueType VT = Tmp1.getValueType();
1201 MVT::ValueType EltVT = Tmp2.getValueType();
1202 MVT::ValueType IdxVT = Tmp3.getValueType();
1203 MVT::ValueType PtrVT = TLI.getPointerTy();
1204 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001205 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +00001206 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001207
1208 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001209 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1210 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001211 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001212 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1213 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1214 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001215 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +00001216 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001217 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +00001218 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +00001219 break;
1220 }
1221 }
1222 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001223 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001224 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1225 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1226 break;
1227 }
1228
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001229 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1230 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1231 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1232 Node->getValueType(0))) {
1233 default: assert(0 && "This action is not supported yet!");
1234 case TargetLowering::Legal:
1235 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001236 case TargetLowering::Custom:
1237 Tmp3 = TLI.LowerOperation(Result, DAG);
1238 if (Tmp3.Val) {
1239 Result = Tmp3;
1240 break;
1241 }
1242 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001243 case TargetLowering::Expand:
1244 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001245 break;
1246 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001247 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001248 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001249 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1250 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1251 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1252
1253 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001254 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1255 default: assert(0 && "Unknown operation action!");
1256 case TargetLowering::Legal:
1257 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1258 "vector shuffle should not be created if not legal!");
1259 break;
1260 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001261 Tmp3 = TLI.LowerOperation(Result, DAG);
1262 if (Tmp3.Val) {
1263 Result = Tmp3;
1264 break;
1265 }
1266 // FALLTHROUGH
1267 case TargetLowering::Expand: {
1268 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00001269 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng9fa89592006-04-05 06:07:11 +00001270 MVT::ValueType PtrVT = TLI.getPointerTy();
1271 SDOperand Mask = Node->getOperand(2);
1272 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001273 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001274 for (unsigned i = 0; i != NumElems; ++i) {
1275 SDOperand Arg = Mask.getOperand(i);
1276 if (Arg.getOpcode() == ISD::UNDEF) {
1277 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1278 } else {
1279 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1280 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1281 if (Idx < NumElems)
1282 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1283 DAG.getConstant(Idx, PtrVT)));
1284 else
1285 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1286 DAG.getConstant(Idx - NumElems, PtrVT)));
1287 }
1288 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001289 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001290 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001291 }
Chris Lattner6be79822006-04-04 17:23:26 +00001292 case TargetLowering::Promote: {
1293 // Change base type to a different vector type.
1294 MVT::ValueType OVT = Node->getValueType(0);
1295 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1296
1297 // Cast the two input vectors.
1298 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1299 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1300
1301 // Convert the shuffle mask to the right # elements.
1302 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1303 assert(Tmp3.Val && "Shuffle not legal?");
1304 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1305 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1306 break;
1307 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001308 }
1309 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001310
1311 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001312 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001313 Tmp2 = LegalizeOp(Node->getOperand(1));
1314 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001315 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001316 break;
1317
Dan Gohmana8665142007-06-25 16:23:39 +00001318 case ISD::EXTRACT_SUBVECTOR:
1319 Tmp1 = Node->getOperand(0);
1320 Tmp2 = LegalizeOp(Node->getOperand(1));
1321 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1322 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001323 break;
1324
Chris Lattner462505f2006-02-13 09:18:02 +00001325 case ISD::CALLSEQ_START: {
1326 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1327
1328 // Recursively Legalize all of the inputs of the call end that do not lead
1329 // to this call start. This ensures that any libcalls that need be inserted
1330 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001331 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001332 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001333 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1334 NodesLeadingTo);
1335 }
Chris Lattner462505f2006-02-13 09:18:02 +00001336
1337 // Now that we legalized all of the inputs (which may have inserted
1338 // libcalls) create the new CALLSEQ_START node.
1339 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1340
1341 // Merge in the last call, to ensure that this call start after the last
1342 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001343 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001344 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1345 Tmp1 = LegalizeOp(Tmp1);
1346 }
Chris Lattner462505f2006-02-13 09:18:02 +00001347
1348 // Do not try to legalize the target-specific arguments (#1+).
1349 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001350 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001351 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001352 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001353 }
1354
1355 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001356 AddLegalizedOperand(Op.getValue(0), Result);
1357 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1358 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1359
Chris Lattner462505f2006-02-13 09:18:02 +00001360 // Now that the callseq_start and all of the non-call nodes above this call
1361 // sequence have been legalized, legalize the call itself. During this
1362 // process, no libcalls can/will be inserted, guaranteeing that no calls
1363 // can overlap.
1364 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1365 SDOperand InCallSEQ = LastCALLSEQ_END;
1366 // Note that we are selecting this call!
1367 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1368 IsLegalizingCall = true;
1369
1370 // Legalize the call, starting from the CALLSEQ_END.
1371 LegalizeOp(LastCALLSEQ_END);
1372 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1373 return Result;
1374 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001375 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001376 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1377 // will cause this node to be legalized as well as handling libcalls right.
1378 if (LastCALLSEQ_END.Val != Node) {
1379 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001380 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001381 assert(I != LegalizedNodes.end() &&
1382 "Legalizing the call start should have legalized this node!");
1383 return I->second;
1384 }
1385
1386 // Otherwise, the call start has been legalized and everything is going
1387 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001388 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001389 // Do not try to legalize the target-specific arguments (#1+), except for
1390 // an optional flag input.
1391 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1392 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001393 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001394 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001395 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001396 }
1397 } else {
1398 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1399 if (Tmp1 != Node->getOperand(0) ||
1400 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001401 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001402 Ops[0] = Tmp1;
1403 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001404 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001405 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001406 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001407 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001408 // This finishes up call legalization.
1409 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001410
1411 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1412 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1413 if (Node->getNumValues() == 2)
1414 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1415 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001416 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng631ccc62007-08-16 23:50:06 +00001417 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerec26b482005-01-09 19:03:49 +00001418 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1419 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1420 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001421 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001422
Chris Lattnerd02b0542006-01-28 10:58:55 +00001423 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001424 Tmp2 = Result.getValue(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001425 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Cheng7f4ec822006-01-11 22:14:47 +00001426 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001427 case TargetLowering::Expand: {
1428 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1429 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1430 " not tell us which reg is the stack pointer!");
1431 SDOperand Chain = Tmp1.getOperand(0);
1432 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001433 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1434 Chain = SP.getValue(1);
1435 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1436 unsigned StackAlign =
1437 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1438 if (Align > StackAlign)
Evan Chengcb6d65e2007-08-17 18:02:22 +00001439 SP = DAG.getNode(ISD::AND, VT, SP,
1440 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng631ccc62007-08-16 23:50:06 +00001441 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
1442 Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001443 Tmp1 = LegalizeOp(Tmp1);
1444 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001445 break;
1446 }
1447 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001448 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1449 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001450 Tmp1 = LegalizeOp(Tmp3);
1451 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001452 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001453 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001454 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001455 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001456 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001457 // Since this op produce two values, make sure to remember that we
1458 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001459 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1460 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001461 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001462 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001463 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001464 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001465 bool Changed = false;
1466 // Legalize all of the operands of the inline asm, in case they are nodes
1467 // that need to be expanded or something. Note we skip the asm string and
1468 // all of the TargetConstant flags.
1469 SDOperand Op = LegalizeOp(Ops[0]);
1470 Changed = Op != Ops[0];
1471 Ops[0] = Op;
1472
1473 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1474 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1475 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1476 for (++i; NumVals; ++i, --NumVals) {
1477 SDOperand Op = LegalizeOp(Ops[i]);
1478 if (Op != Ops[i]) {
1479 Changed = true;
1480 Ops[i] = Op;
1481 }
1482 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001483 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001484
1485 if (HasInFlag) {
1486 Op = LegalizeOp(Ops.back());
1487 Changed |= Op != Ops.back();
1488 Ops.back() = Op;
1489 }
1490
1491 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001492 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001493
1494 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001495 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001496 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1497 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001498 }
Chris Lattner68a12142005-01-07 22:12:08 +00001499 case ISD::BR:
1500 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001501 // Ensure that libcalls are emitted before a branch.
1502 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1503 Tmp1 = LegalizeOp(Tmp1);
1504 LastCALLSEQ_END = DAG.getEntryNode();
1505
Chris Lattnerd02b0542006-01-28 10:58:55 +00001506 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001507 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001508 case ISD::BRIND:
1509 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1510 // Ensure that libcalls are emitted before a branch.
1511 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1512 Tmp1 = LegalizeOp(Tmp1);
1513 LastCALLSEQ_END = DAG.getEntryNode();
1514
1515 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1516 default: assert(0 && "Indirect target must be legal type (pointer)!");
1517 case Legal:
1518 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1519 break;
1520 }
1521 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1522 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001523 case ISD::BR_JT:
1524 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1525 // Ensure that libcalls are emitted before a branch.
1526 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1527 Tmp1 = LegalizeOp(Tmp1);
1528 LastCALLSEQ_END = DAG.getEntryNode();
1529
1530 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1531 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1532
1533 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1534 default: assert(0 && "This action is not supported yet!");
1535 case TargetLowering::Legal: break;
1536 case TargetLowering::Custom:
1537 Tmp1 = TLI.LowerOperation(Result, DAG);
1538 if (Tmp1.Val) Result = Tmp1;
1539 break;
1540 case TargetLowering::Expand: {
1541 SDOperand Chain = Result.getOperand(0);
1542 SDOperand Table = Result.getOperand(1);
1543 SDOperand Index = Result.getOperand(2);
1544
1545 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001546 MachineFunction &MF = DAG.getMachineFunction();
1547 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001548 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1549 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001550
1551 SDOperand LD;
1552 switch (EntrySize) {
1553 default: assert(0 && "Size of jump table not supported yet."); break;
1554 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1555 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1556 }
1557
1558 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001559 // For PIC, the sequence is:
1560 // BRIND(load(Jumptable + index) + RelocBase)
1561 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1562 SDOperand Reloc;
1563 if (TLI.usesGlobalOffsetTable())
1564 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1565 else
1566 Reloc = Table;
Evan Chenge6d58472006-10-31 02:31:00 +00001567 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng84a28d42006-10-30 08:00:44 +00001568 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1569 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1570 } else {
1571 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1572 }
1573 }
1574 }
1575 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001576 case ISD::BRCOND:
1577 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001578 // Ensure that libcalls are emitted before a return.
1579 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1580 Tmp1 = LegalizeOp(Tmp1);
1581 LastCALLSEQ_END = DAG.getEntryNode();
1582
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001583 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1584 case Expand: assert(0 && "It's impossible to expand bools");
1585 case Legal:
1586 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1587 break;
1588 case Promote:
1589 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001590
1591 // The top bits of the promoted condition are not necessarily zero, ensure
1592 // that the value is properly zero extended.
Dan Gohman309d3d52007-06-22 14:59:07 +00001593 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerdb189382006-11-27 04:39:56 +00001594 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1595 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001596 break;
1597 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001598
1599 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001600 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001601
1602 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1603 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001604 case TargetLowering::Legal: break;
1605 case TargetLowering::Custom:
1606 Tmp1 = TLI.LowerOperation(Result, DAG);
1607 if (Tmp1.Val) Result = Tmp1;
1608 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001609 case TargetLowering::Expand:
1610 // Expand brcond's setcc into its constituent parts and create a BR_CC
1611 // Node.
1612 if (Tmp2.getOpcode() == ISD::SETCC) {
1613 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1614 Tmp2.getOperand(0), Tmp2.getOperand(1),
1615 Node->getOperand(2));
1616 } else {
1617 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1618 DAG.getCondCode(ISD::SETNE), Tmp2,
1619 DAG.getConstant(0, Tmp2.getValueType()),
1620 Node->getOperand(2));
1621 }
1622 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001623 }
1624 break;
1625 case ISD::BR_CC:
1626 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001627 // Ensure that libcalls are emitted before a branch.
1628 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1629 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001630 Tmp2 = Node->getOperand(2); // LHS
1631 Tmp3 = Node->getOperand(3); // RHS
1632 Tmp4 = Node->getOperand(1); // CC
1633
1634 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001635 LastCALLSEQ_END = DAG.getEntryNode();
1636
Nate Begeman7e7f4392006-02-01 07:19:44 +00001637 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1638 // the LHS is a legal SETCC itself. In this case, we need to compare
1639 // the result against zero to select between true and false values.
1640 if (Tmp3.Val == 0) {
1641 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1642 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001643 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001644
1645 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1646 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001647
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001648 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1649 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001650 case TargetLowering::Legal: break;
1651 case TargetLowering::Custom:
1652 Tmp4 = TLI.LowerOperation(Result, DAG);
1653 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001654 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001655 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001656 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001657 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001658 LoadSDNode *LD = cast<LoadSDNode>(Node);
1659 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1660 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001661
Evan Chenge71fe34d2006-10-09 20:57:25 +00001662 ISD::LoadExtType ExtType = LD->getExtensionType();
1663 if (ExtType == ISD::NON_EXTLOAD) {
1664 MVT::ValueType VT = Node->getValueType(0);
1665 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1666 Tmp3 = Result.getValue(0);
1667 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001668
Evan Chenge71fe34d2006-10-09 20:57:25 +00001669 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1670 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001671 case TargetLowering::Legal:
1672 // If this is an unaligned load and the target doesn't support it,
1673 // expand it.
1674 if (!TLI.allowsUnalignedMemoryAccesses()) {
1675 unsigned ABIAlignment = TLI.getTargetData()->
1676 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1677 if (LD->getAlignment() < ABIAlignment){
1678 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1679 TLI);
1680 Tmp3 = Result.getOperand(0);
1681 Tmp4 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001682 Tmp3 = LegalizeOp(Tmp3);
1683 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001684 }
1685 }
1686 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001687 case TargetLowering::Custom:
1688 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1689 if (Tmp1.Val) {
1690 Tmp3 = LegalizeOp(Tmp1);
1691 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001692 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001693 break;
1694 case TargetLowering::Promote: {
1695 // Only promote a load of vector type to another.
1696 assert(MVT::isVector(VT) && "Cannot promote this load!");
1697 // Change base type to a different vector type.
1698 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1699
1700 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001701 LD->getSrcValueOffset(),
1702 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001703 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1704 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001705 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001706 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001707 }
1708 // Since loads produce two values, make sure to remember that we
1709 // legalized both of them.
1710 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1711 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1712 return Op.ResNo ? Tmp4 : Tmp3;
1713 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001714 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001715 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1716 default: assert(0 && "This action is not supported yet!");
1717 case TargetLowering::Promote:
1718 assert(SrcVT == MVT::i1 &&
1719 "Can only promote extending LOAD from i1 -> i8!");
1720 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1721 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00001722 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001723 Tmp1 = Result.getValue(0);
1724 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001725 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001726 case TargetLowering::Custom:
1727 isCustom = true;
1728 // FALLTHROUGH
1729 case TargetLowering::Legal:
1730 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1731 Tmp1 = Result.getValue(0);
1732 Tmp2 = Result.getValue(1);
1733
1734 if (isCustom) {
1735 Tmp3 = TLI.LowerOperation(Result, DAG);
1736 if (Tmp3.Val) {
1737 Tmp1 = LegalizeOp(Tmp3);
1738 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1739 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001740 } else {
1741 // If this is an unaligned load and the target doesn't support it,
1742 // expand it.
1743 if (!TLI.allowsUnalignedMemoryAccesses()) {
1744 unsigned ABIAlignment = TLI.getTargetData()->
1745 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1746 if (LD->getAlignment() < ABIAlignment){
1747 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1748 TLI);
1749 Tmp1 = Result.getOperand(0);
1750 Tmp2 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001751 Tmp1 = LegalizeOp(Tmp1);
1752 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001753 }
1754 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001755 }
1756 break;
1757 case TargetLowering::Expand:
1758 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1759 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1760 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001761 LD->getSrcValueOffset(),
1762 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001763 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1764 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1765 Tmp2 = LegalizeOp(Load.getValue(1));
1766 break;
1767 }
Chris Lattner296a83c2007-02-01 04:55:59 +00001768 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001769 // Turn the unsupported load into an EXTLOAD followed by an explicit
1770 // zero/sign extend inreg.
1771 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1772 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001773 LD->getSrcValueOffset(), SrcVT,
1774 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001775 SDOperand ValRes;
1776 if (ExtType == ISD::SEXTLOAD)
1777 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1778 Result, DAG.getValueType(SrcVT));
1779 else
1780 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1781 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1782 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1783 break;
1784 }
1785 // Since loads produce two values, make sure to remember that we legalized
1786 // both of them.
1787 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1788 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1789 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001790 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001791 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001792 case ISD::EXTRACT_ELEMENT: {
1793 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1794 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001795 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001796 case Legal:
1797 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1798 // 1 -> Hi
1799 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1800 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1801 TLI.getShiftAmountTy()));
1802 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1803 } else {
1804 // 0 -> Lo
1805 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1806 Node->getOperand(0));
1807 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001808 break;
1809 case Expand:
1810 // Get both the low and high parts.
1811 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1812 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1813 Result = Tmp2; // 1 -> Hi
1814 else
1815 Result = Tmp1; // 0 -> Lo
1816 break;
1817 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001818 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001819 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001820
1821 case ISD::CopyToReg:
1822 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001823
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001824 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001825 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001826 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001827 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001828 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001829 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001830 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001831 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001832 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001833 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001834 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1835 Tmp3);
1836 } else {
1837 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001838 }
1839
1840 // Since this produces two values, make sure to remember that we legalized
1841 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001842 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001843 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001844 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001845 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001846 break;
1847
1848 case ISD::RET:
1849 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001850
1851 // Ensure that libcalls are emitted before a return.
1852 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1853 Tmp1 = LegalizeOp(Tmp1);
1854 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001855
Chris Lattnerdc750592005-01-07 07:47:09 +00001856 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001857 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001858 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001859 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001860 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001861 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001862 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001863 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001864 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +00001865 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001866 SDOperand Lo, Hi;
1867 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00001868
1869 // Big endian systems want the hi reg first.
1870 if (!TLI.isLittleEndian())
1871 std::swap(Lo, Hi);
1872
Evan Cheng3432ab92006-12-11 19:27:14 +00001873 if (Hi.Val)
1874 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1875 else
1876 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001877 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001878 } else {
1879 SDNode *InVal = Tmp2.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00001880 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1881 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001882
Dan Gohmana8665142007-06-25 16:23:39 +00001883 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001884 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001885 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00001886 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001887 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00001888 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001889 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001890 } else if (NumElems == 1) {
1891 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00001892 Tmp2 = ScalarizeVectorOp(Tmp2);
1893 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001894 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001895
1896 // FIXME: Returns of gcc generic vectors smaller than a legal type
1897 // should be returned in integer registers!
1898
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001899 // The scalarized value type may not be legal, e.g. it might require
1900 // promotion or expansion. Relegalize the return.
1901 Result = LegalizeOp(Result);
1902 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001903 // FIXME: Returns of gcc generic vectors larger than a legal vector
1904 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001905 SDOperand Lo, Hi;
1906 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00001907 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001908 Result = LegalizeOp(Result);
1909 }
1910 }
Misha Brukman835702a2005-04-21 22:36:52 +00001911 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001912 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001913 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00001914 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001915 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001916 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001917 }
1918 break;
1919 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001920 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001921 break;
1922 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00001923 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00001924 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00001925 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00001926 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1927 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001928 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00001929 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001930 break;
1931 case Expand: {
1932 SDOperand Lo, Hi;
Dan Gohman3b62d722007-06-27 16:08:04 +00001933 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001934 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001935 ExpandOp(Node->getOperand(i), Lo, Hi);
1936 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00001937 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00001938 if (Hi.Val) {
1939 NewValues.push_back(Hi);
1940 NewValues.push_back(Node->getOperand(i+1));
1941 }
Misha Brukman835702a2005-04-21 22:36:52 +00001942 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001943 }
1944 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001945 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001946 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001947
1948 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00001949 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00001950 else
Chris Lattner97af9d52006-08-08 01:09:31 +00001951 Result = DAG.getNode(ISD::RET, MVT::Other,
1952 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00001953 break;
1954 }
1955 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001956
Chris Lattner4d1ea712006-01-29 21:02:23 +00001957 if (Result.getOpcode() == ISD::RET) {
1958 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1959 default: assert(0 && "This action is not supported yet!");
1960 case TargetLowering::Legal: break;
1961 case TargetLowering::Custom:
1962 Tmp1 = TLI.LowerOperation(Result, DAG);
1963 if (Tmp1.Val) Result = Tmp1;
1964 break;
1965 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001966 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001967 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001968 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00001969 StoreSDNode *ST = cast<StoreSDNode>(Node);
1970 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1971 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00001972 int SVOffset = ST->getSrcValueOffset();
1973 unsigned Alignment = ST->getAlignment();
1974 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00001975
Evan Chengab51cf22006-10-13 21:14:26 +00001976 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001977 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1978 // FIXME: We shouldn't do this for TargetConstantFP's.
1979 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1980 // to phase ordering between legalized code and the dag combiner. This
1981 // probably means that we need to integrate dag combiner and legalizer
1982 // together.
Dale Johannesen98d3a082007-09-14 22:26:36 +00001983 // We generally can't do this one for long doubles.
1984 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001985 if (CFP->getValueType(0) == MVT::f32) {
Dale Johannesen028084e2007-09-12 03:30:33 +00001986 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
1987 convertToAPInt().getZExtValue(),
Dale Johannesen245dceb2007-09-11 18:32:33 +00001988 MVT::i32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00001989 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1990 SVOffset, isVolatile, Alignment);
1991 break;
1992 } else if (CFP->getValueType(0) == MVT::f64) {
Dale Johannesen028084e2007-09-12 03:30:33 +00001993 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
1994 getZExtValue(), MVT::i64);
Dale Johannesen98d3a082007-09-14 22:26:36 +00001995 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1996 SVOffset, isVolatile, Alignment);
1997 break;
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001998 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001999 }
2000
Evan Chengab51cf22006-10-13 21:14:26 +00002001 switch (getTypeAction(ST->getStoredVT())) {
2002 case Legal: {
2003 Tmp3 = LegalizeOp(ST->getValue());
2004 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2005 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00002006
Evan Chengab51cf22006-10-13 21:14:26 +00002007 MVT::ValueType VT = Tmp3.getValueType();
2008 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2009 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002010 case TargetLowering::Legal:
2011 // If this is an unaligned store and the target doesn't support it,
2012 // expand it.
2013 if (!TLI.allowsUnalignedMemoryAccesses()) {
2014 unsigned ABIAlignment = TLI.getTargetData()->
2015 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2016 if (ST->getAlignment() < ABIAlignment)
2017 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2018 TLI);
2019 }
2020 break;
Evan Chengab51cf22006-10-13 21:14:26 +00002021 case TargetLowering::Custom:
2022 Tmp1 = TLI.LowerOperation(Result, DAG);
2023 if (Tmp1.Val) Result = Tmp1;
2024 break;
2025 case TargetLowering::Promote:
2026 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2027 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2028 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2029 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00002030 ST->getSrcValue(), SVOffset, isVolatile,
2031 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002032 break;
2033 }
2034 break;
2035 }
2036 case Promote:
2037 // Truncate the value and store the result.
2038 Tmp3 = PromoteOp(ST->getValue());
2039 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002040 SVOffset, ST->getStoredVT(),
2041 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002042 break;
2043
2044 case Expand:
2045 unsigned IncrementSize = 0;
2046 SDOperand Lo, Hi;
2047
2048 // If this is a vector type, then we have to calculate the increment as
2049 // the product of the element size in bytes, and the number of elements
2050 // in the high half of the vector.
Dan Gohmana8665142007-06-25 16:23:39 +00002051 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002052 SDNode *InVal = ST->getValue().Val;
Dan Gohmana8665142007-06-25 16:23:39 +00002053 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
2054 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Chengab51cf22006-10-13 21:14:26 +00002055
Dan Gohmana8665142007-06-25 16:23:39 +00002056 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002057 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00002058 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002059 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002060 // Turn this into a normal store of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002061 Tmp3 = LegalizeOp(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 Result = LegalizeOp(Result);
2065 break;
2066 } else if (NumElems == 1) {
2067 // Turn this into a normal store of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002068 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00002069 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002070 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002071 // The scalarized value type may not be legal, e.g. it might require
2072 // promotion or expansion. Relegalize the scalar store.
2073 Result = LegalizeOp(Result);
2074 break;
2075 } else {
2076 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2077 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2078 }
2079 } else {
2080 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00002081 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002082
2083 if (!TLI.isLittleEndian())
2084 std::swap(Lo, Hi);
2085 }
2086
2087 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002088 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002089
2090 if (Hi.Val == NULL) {
2091 // Must be int <-> float one-to-one expansion.
2092 Result = Lo;
2093 break;
2094 }
2095
Evan Chengab51cf22006-10-13 21:14:26 +00002096 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2097 getIntPtrConstant(IncrementSize));
2098 assert(isTypeLegal(Tmp2.getValueType()) &&
2099 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002100 SVOffset += IncrementSize;
2101 if (Alignment > IncrementSize)
2102 Alignment = IncrementSize;
Evan Chengab51cf22006-10-13 21:14:26 +00002103 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002104 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002105 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2106 break;
2107 }
2108 } else {
2109 // Truncating store
2110 assert(isTypeLegal(ST->getValue().getValueType()) &&
2111 "Cannot handle illegal TRUNCSTORE yet!");
2112 Tmp3 = LegalizeOp(ST->getValue());
2113
2114 // The only promote case we handle is TRUNCSTORE:i1 X into
2115 // -> TRUNCSTORE:i8 (and X, 1)
2116 if (ST->getStoredVT() == MVT::i1 &&
2117 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2118 // Promote the bool to a mask then store.
2119 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2120 DAG.getConstant(1, Tmp3.getValueType()));
2121 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002122 SVOffset, MVT::i8,
2123 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002124 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2125 Tmp2 != ST->getBasePtr()) {
2126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2127 ST->getOffset());
2128 }
2129
2130 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2131 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002132 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002133 case TargetLowering::Legal:
2134 // If this is an unaligned store and the target doesn't support it,
2135 // expand it.
2136 if (!TLI.allowsUnalignedMemoryAccesses()) {
2137 unsigned ABIAlignment = TLI.getTargetData()->
2138 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2139 if (ST->getAlignment() < ABIAlignment)
2140 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2141 TLI);
2142 }
2143 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002144 case TargetLowering::Custom:
2145 Tmp1 = TLI.LowerOperation(Result, DAG);
2146 if (Tmp1.Val) Result = Tmp1;
2147 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002148 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002149 }
2150 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002151 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002152 case ISD::PCMARKER:
2153 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002154 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002155 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002156 case ISD::STACKSAVE:
2157 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002158 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2159 Tmp1 = Result.getValue(0);
2160 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002161
Chris Lattnerb3266452006-01-13 02:50:02 +00002162 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2163 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002164 case TargetLowering::Legal: break;
2165 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002166 Tmp3 = TLI.LowerOperation(Result, DAG);
2167 if (Tmp3.Val) {
2168 Tmp1 = LegalizeOp(Tmp3);
2169 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002170 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002171 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002172 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002173 // Expand to CopyFromReg if the target set
2174 // StackPointerRegisterToSaveRestore.
2175 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002176 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002177 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002178 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002179 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002180 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2181 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002182 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002183 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002184 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002185
2186 // Since stacksave produce two values, make sure to remember that we
2187 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002188 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2189 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2190 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002191
Chris Lattnerb3266452006-01-13 02:50:02 +00002192 case ISD::STACKRESTORE:
2193 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2194 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002195 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002196
2197 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2198 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002199 case TargetLowering::Legal: break;
2200 case TargetLowering::Custom:
2201 Tmp1 = TLI.LowerOperation(Result, DAG);
2202 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002203 break;
2204 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002205 // Expand to CopyToReg if the target set
2206 // StackPointerRegisterToSaveRestore.
2207 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2208 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2209 } else {
2210 Result = Tmp1;
2211 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002212 break;
2213 }
2214 break;
2215
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002216 case ISD::READCYCLECOUNTER:
2217 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002218 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002219 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2220 Node->getValueType(0))) {
2221 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002222 case TargetLowering::Legal:
2223 Tmp1 = Result.getValue(0);
2224 Tmp2 = Result.getValue(1);
2225 break;
Evan Cheng69739932006-11-29 08:26:18 +00002226 case TargetLowering::Custom:
2227 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002228 Tmp1 = LegalizeOp(Result.getValue(0));
2229 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002230 break;
2231 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002232
2233 // Since rdcc produce two values, make sure to remember that we legalized
2234 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002235 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2236 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002237 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002238
Chris Lattner39c67442005-01-14 22:08:15 +00002239 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002240 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2241 case Expand: assert(0 && "It's impossible to expand bools");
2242 case Legal:
2243 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2244 break;
2245 case Promote:
2246 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002247 // Make sure the condition is either zero or one.
Dan Gohman309d3d52007-06-22 14:59:07 +00002248 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattner3abb6362006-11-28 01:03:30 +00002249 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2250 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002251 break;
2252 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002253 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002254 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002255
Chris Lattnerd02b0542006-01-28 10:58:55 +00002256 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002257
Nate Begeman987121a2005-08-23 04:29:48 +00002258 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002259 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002260 case TargetLowering::Legal: break;
2261 case TargetLowering::Custom: {
2262 Tmp1 = TLI.LowerOperation(Result, DAG);
2263 if (Tmp1.Val) Result = Tmp1;
2264 break;
2265 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002266 case TargetLowering::Expand:
2267 if (Tmp1.getOpcode() == ISD::SETCC) {
2268 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2269 Tmp2, Tmp3,
2270 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2271 } else {
2272 Result = DAG.getSelectCC(Tmp1,
2273 DAG.getConstant(0, Tmp1.getValueType()),
2274 Tmp2, Tmp3, ISD::SETNE);
2275 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002276 break;
2277 case TargetLowering::Promote: {
2278 MVT::ValueType NVT =
2279 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2280 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002281 if (MVT::isVector(Tmp2.getValueType())) {
2282 ExtOp = ISD::BIT_CONVERT;
2283 TruncOp = ISD::BIT_CONVERT;
2284 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002285 ExtOp = ISD::ANY_EXTEND;
2286 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002287 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002288 ExtOp = ISD::FP_EXTEND;
2289 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002290 }
2291 // Promote each of the values to the new type.
2292 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2293 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2294 // Perform the larger operation, then round down.
2295 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2296 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2297 break;
2298 }
2299 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002300 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002301 case ISD::SELECT_CC: {
2302 Tmp1 = Node->getOperand(0); // LHS
2303 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002304 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2305 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002306 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002307
Nate Begeman7e7f4392006-02-01 07:19:44 +00002308 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2309
2310 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2311 // the LHS is a legal SETCC itself. In this case, we need to compare
2312 // the result against zero to select between true and false values.
2313 if (Tmp2.Val == 0) {
2314 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2315 CC = DAG.getCondCode(ISD::SETNE);
2316 }
2317 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2318
2319 // Everything is legal, see if we should expand this op or something.
2320 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2321 default: assert(0 && "This action is not supported yet!");
2322 case TargetLowering::Legal: break;
2323 case TargetLowering::Custom:
2324 Tmp1 = TLI.LowerOperation(Result, DAG);
2325 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002326 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002327 }
2328 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002329 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002330 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002331 Tmp1 = Node->getOperand(0);
2332 Tmp2 = Node->getOperand(1);
2333 Tmp3 = Node->getOperand(2);
2334 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2335
2336 // If we had to Expand the SetCC operands into a SELECT node, then it may
2337 // not always be possible to return a true LHS & RHS. In this case, just
2338 // return the value we legalized, returned in the LHS
2339 if (Tmp2.Val == 0) {
2340 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002341 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002342 }
Nate Begeman987121a2005-08-23 04:29:48 +00002343
Chris Lattnerf263a232006-01-30 22:43:50 +00002344 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002345 default: assert(0 && "Cannot handle this action for SETCC yet!");
2346 case TargetLowering::Custom:
2347 isCustom = true;
2348 // FALLTHROUGH.
2349 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002350 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002351 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002352 Tmp4 = TLI.LowerOperation(Result, DAG);
2353 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002354 }
Nate Begeman987121a2005-08-23 04:29:48 +00002355 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002356 case TargetLowering::Promote: {
2357 // First step, figure out the appropriate operation to use.
2358 // Allow SETCC to not be supported for all legal data types
2359 // Mostly this targets FP
2360 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002361 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002362
2363 // Scan for the appropriate larger type to use.
2364 while (1) {
2365 NewInTy = (MVT::ValueType)(NewInTy+1);
2366
2367 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2368 "Fell off of the edge of the integer world");
2369 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2370 "Fell off of the edge of the floating point world");
2371
2372 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002373 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002374 break;
2375 }
2376 if (MVT::isInteger(NewInTy))
2377 assert(0 && "Cannot promote Legal Integer SETCC yet");
2378 else {
2379 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2380 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2381 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002382 Tmp1 = LegalizeOp(Tmp1);
2383 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002384 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002385 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002386 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002387 }
Nate Begeman987121a2005-08-23 04:29:48 +00002388 case TargetLowering::Expand:
2389 // Expand a setcc node into a select_cc of the same condition, lhs, and
2390 // rhs that selects between const 1 (true) and const 0 (false).
2391 MVT::ValueType VT = Node->getValueType(0);
2392 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2393 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002394 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002395 break;
2396 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002397 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002398 case ISD::MEMSET:
2399 case ISD::MEMCPY:
2400 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002401 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002402 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2403
2404 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2405 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2406 case Expand: assert(0 && "Cannot expand a byte!");
2407 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002408 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002409 break;
2410 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002411 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002412 break;
2413 }
2414 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002415 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002416 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002417
2418 SDOperand Tmp4;
2419 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002420 case Expand: {
2421 // Length is too big, just take the lo-part of the length.
2422 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002423 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002424 break;
2425 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002426 case Legal:
2427 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002428 break;
2429 case Promote:
2430 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002431 break;
2432 }
2433
2434 SDOperand Tmp5;
2435 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2436 case Expand: assert(0 && "Cannot expand this yet!");
2437 case Legal:
2438 Tmp5 = LegalizeOp(Node->getOperand(4));
2439 break;
2440 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002441 Tmp5 = PromoteOp(Node->getOperand(4));
2442 break;
2443 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002444
2445 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2446 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002447 case TargetLowering::Custom:
2448 isCustom = true;
2449 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00002450 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002451 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002452 if (isCustom) {
2453 Tmp1 = TLI.LowerOperation(Result, DAG);
2454 if (Tmp1.Val) Result = Tmp1;
2455 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002456 break;
2457 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002458 // Otherwise, the target does not support this operation. Lower the
2459 // operation to an explicit libcall as appropriate.
2460 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002461 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002462 TargetLowering::ArgListTy Args;
2463 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002464
Reid Spencer6dced922005-01-12 14:53:45 +00002465 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002466 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002467 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00002468 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002469 // Extend the (previously legalized) ubyte argument to be an int value
2470 // for the call.
2471 if (Tmp3.getValueType() > MVT::i32)
2472 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2473 else
2474 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002475 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencere63b6512006-12-31 05:55:36 +00002476 Args.push_back(Entry);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002477 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002478 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002479
2480 FnName = "memset";
2481 } else if (Node->getOpcode() == ISD::MEMCPY ||
2482 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002483 Entry.Ty = IntPtrTy;
Reid Spencer791864c2007-01-03 04:22:32 +00002484 Entry.Node = Tmp2; Args.push_back(Entry);
2485 Entry.Node = Tmp3; Args.push_back(Entry);
2486 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002487 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2488 } else {
2489 assert(0 && "Unknown op!");
2490 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002491
Chris Lattner85d70c62005-01-11 05:57:22 +00002492 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002493 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002494 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002495 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002496 break;
2497 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002498 }
2499 break;
2500 }
Chris Lattner5385db52005-05-09 20:23:03 +00002501
Chris Lattner4157c412005-04-02 04:00:59 +00002502 case ISD::SHL_PARTS:
2503 case ISD::SRA_PARTS:
2504 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002505 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002506 bool Changed = false;
2507 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2508 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2509 Changed |= Ops.back() != Node->getOperand(i);
2510 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002511 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002512 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002513
Evan Cheng870e4f82006-01-09 18:31:59 +00002514 switch (TLI.getOperationAction(Node->getOpcode(),
2515 Node->getValueType(0))) {
2516 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002517 case TargetLowering::Legal: break;
2518 case TargetLowering::Custom:
2519 Tmp1 = TLI.LowerOperation(Result, DAG);
2520 if (Tmp1.Val) {
2521 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002522 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002523 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002524 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2525 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002526 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002527 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002528 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002529 return RetVal;
2530 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002531 break;
2532 }
2533
Chris Lattner13fe99c2005-04-02 05:00:07 +00002534 // Since these produce multiple values, make sure to remember that we
2535 // legalized all of them.
2536 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2537 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2538 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002539 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002540
2541 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002542 case ISD::ADD:
2543 case ISD::SUB:
2544 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002545 case ISD::MULHS:
2546 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002547 case ISD::UDIV:
2548 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002549 case ISD::AND:
2550 case ISD::OR:
2551 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002552 case ISD::SHL:
2553 case ISD::SRL:
2554 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002555 case ISD::FADD:
2556 case ISD::FSUB:
2557 case ISD::FMUL:
2558 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002559 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002560 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2561 case Expand: assert(0 && "Not possible");
2562 case Legal:
2563 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2564 break;
2565 case Promote:
2566 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2567 break;
2568 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002569
2570 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002571
Andrew Lenharth72594262005-12-24 23:42:32 +00002572 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002573 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002574 case TargetLowering::Legal: break;
2575 case TargetLowering::Custom:
2576 Tmp1 = TLI.LowerOperation(Result, DAG);
2577 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002578 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002579 case TargetLowering::Expand: {
Dan Gohmana1603612007-10-08 18:33:35 +00002580 MVT::ValueType VT = Op.getValueType();
2581
2582 // See if multiply or divide can be lowered using two-result operations.
2583 SDVTList VTs = DAG.getVTList(VT, VT);
2584 if (Node->getOpcode() == ISD::MUL) {
2585 // We just need the low half of the multiply; try both the signed
2586 // and unsigned forms. If the target supports both SMUL_LOHI and
2587 // UMUL_LOHI, form a preference by checking which forms of plain
2588 // MULH it supports.
2589 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2590 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2591 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2592 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2593 unsigned OpToUse = 0;
2594 if (HasSMUL_LOHI && !HasMULHS) {
2595 OpToUse = ISD::SMUL_LOHI;
2596 } else if (HasUMUL_LOHI && !HasMULHU) {
2597 OpToUse = ISD::UMUL_LOHI;
2598 } else if (HasSMUL_LOHI) {
2599 OpToUse = ISD::SMUL_LOHI;
2600 } else if (HasUMUL_LOHI) {
2601 OpToUse = ISD::UMUL_LOHI;
2602 }
2603 if (OpToUse) {
2604 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2605 break;
2606 }
2607 }
2608 if (Node->getOpcode() == ISD::MULHS &&
2609 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2610 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2611 break;
2612 }
2613 if (Node->getOpcode() == ISD::MULHU &&
2614 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2615 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2616 break;
2617 }
2618 if (Node->getOpcode() == ISD::SDIV &&
2619 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2620 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2621 break;
2622 }
2623 if (Node->getOpcode() == ISD::UDIV &&
2624 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2625 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2626 break;
2627 }
2628
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002629 if (Node->getValueType(0) == MVT::i32) {
2630 switch (Node->getOpcode()) {
2631 default: assert(0 && "Do not know how to expand this integer BinOp!");
2632 case ISD::UDIV:
2633 case ISD::SDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00002634 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2635 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002636 SDOperand Dummy;
Reid Spencere63b6512006-12-31 05:55:36 +00002637 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002638 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002639 };
2640 break;
2641 }
2642
Chris Lattner87f08092006-04-02 03:57:31 +00002643 assert(MVT::isVector(Node->getValueType(0)) &&
2644 "Cannot expand this binary operator!");
2645 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002646 SmallVector<SDOperand, 8> Ops;
Dan Gohman5c441312007-06-14 22:58:02 +00002647 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattner87f08092006-04-02 03:57:31 +00002648 MVT::ValueType PtrVT = TLI.getPointerTy();
2649 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2650 i != e; ++i) {
2651 SDOperand Idx = DAG.getConstant(i, PtrVT);
2652 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2653 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2654 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2655 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002656 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2657 &Ops[0], Ops.size());
Chris Lattner87f08092006-04-02 03:57:31 +00002658 break;
2659 }
Evan Cheng119266e2006-04-12 21:20:24 +00002660 case TargetLowering::Promote: {
2661 switch (Node->getOpcode()) {
2662 default: assert(0 && "Do not know how to promote this BinOp!");
2663 case ISD::AND:
2664 case ISD::OR:
2665 case ISD::XOR: {
2666 MVT::ValueType OVT = Node->getValueType(0);
2667 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2668 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2669 // Bit convert each of the values to the new type.
2670 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2671 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2672 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2673 // Bit convert the result back the original type.
2674 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2675 break;
2676 }
2677 }
2678 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002679 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002680 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002681
Dan Gohman12334ac2007-10-05 14:17:22 +00002682 case ISD::SMUL_LOHI:
2683 case ISD::UMUL_LOHI:
2684 case ISD::SDIVREM:
2685 case ISD::UDIVREM:
2686 // These nodes will only be produced by target-specific lowering, so
2687 // they shouldn't be here if they aren't legal.
2688 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
2689 "This must be legal!");
Dan Gohmana1603612007-10-08 18:33:35 +00002690
2691 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2692 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2693 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman12334ac2007-10-05 14:17:22 +00002694 break;
2695
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002696 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2697 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2698 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2699 case Expand: assert(0 && "Not possible");
2700 case Legal:
2701 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2702 break;
2703 case Promote:
2704 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2705 break;
2706 }
2707
2708 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2709
2710 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2711 default: assert(0 && "Operation not supported");
2712 case TargetLowering::Custom:
2713 Tmp1 = TLI.LowerOperation(Result, DAG);
2714 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002715 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002716 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002717 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00002718 // If this target supports fabs/fneg natively and select is cheap,
2719 // do this efficiently.
2720 if (!TLI.isSelectExpensive() &&
2721 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2722 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00002723 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00002724 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002725 // Get the sign bit of the RHS.
2726 MVT::ValueType IVT =
2727 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2728 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2729 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2730 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2731 // Get the absolute value of the result.
2732 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2733 // Select between the nabs and abs value based on the sign bit of
2734 // the input.
2735 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2736 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2737 AbsVal),
2738 AbsVal);
2739 Result = LegalizeOp(Result);
2740 break;
2741 }
2742
2743 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002744 MVT::ValueType NVT =
2745 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2746 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2747 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2748 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002749 break;
2750 }
Evan Cheng003feb02007-01-04 21:56:39 +00002751 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002752 break;
2753
Nate Begeman5965bd12006-02-17 05:43:56 +00002754 case ISD::ADDC:
2755 case ISD::SUBC:
2756 Tmp1 = LegalizeOp(Node->getOperand(0));
2757 Tmp2 = LegalizeOp(Node->getOperand(1));
2758 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2759 // Since this produces two values, make sure to remember that we legalized
2760 // both of them.
2761 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2762 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2763 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002764
Nate Begeman5965bd12006-02-17 05:43:56 +00002765 case ISD::ADDE:
2766 case ISD::SUBE:
2767 Tmp1 = LegalizeOp(Node->getOperand(0));
2768 Tmp2 = LegalizeOp(Node->getOperand(1));
2769 Tmp3 = LegalizeOp(Node->getOperand(2));
2770 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2771 // Since this produces two values, make sure to remember that we legalized
2772 // both of them.
2773 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2774 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2775 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002776
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002777 case ISD::BUILD_PAIR: {
2778 MVT::ValueType PairTy = Node->getValueType(0);
2779 // TODO: handle the case where the Lo and Hi operands are not of legal type
2780 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2781 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2782 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002783 case TargetLowering::Promote:
2784 case TargetLowering::Custom:
2785 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002786 case TargetLowering::Legal:
2787 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2788 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2789 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002790 case TargetLowering::Expand:
2791 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2792 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2793 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2794 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2795 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002796 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002797 break;
2798 }
2799 break;
2800 }
2801
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002802 case ISD::UREM:
2803 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002804 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002805 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2806 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002807
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002808 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002809 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2810 case TargetLowering::Custom:
2811 isCustom = true;
2812 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002813 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002814 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002815 if (isCustom) {
2816 Tmp1 = TLI.LowerOperation(Result, DAG);
2817 if (Tmp1.Val) Result = Tmp1;
2818 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002819 break;
Dan Gohmana1603612007-10-08 18:33:35 +00002820 case TargetLowering::Expand: {
Evan Cheng1fc7c362006-09-18 23:28:33 +00002821 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002822 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohmana1603612007-10-08 18:33:35 +00002823 MVT::ValueType VT = Node->getValueType(0);
2824
2825 // See if remainder can be lowered using two-result operations.
2826 SDVTList VTs = DAG.getVTList(VT, VT);
2827 if (Node->getOpcode() == ISD::SREM &&
2828 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2829 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2830 break;
2831 }
2832 if (Node->getOpcode() == ISD::UREM &&
2833 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2834 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2835 break;
2836 }
2837
2838 if (MVT::isInteger(VT)) {
2839 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002840 TargetLowering::Legal) {
2841 // X % Y -> X-X/Y*Y
Evan Cheng1fc7c362006-09-18 23:28:33 +00002842 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002843 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2844 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2845 } else {
Dan Gohmana1603612007-10-08 18:33:35 +00002846 assert(VT == MVT::i32 &&
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002847 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00002848 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2849 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002850 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002851 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002852 }
Chris Lattner81914422005-08-03 20:31:37 +00002853 } else {
2854 // Floating point mod -> fmod libcall.
Dan Gohmana1603612007-10-08 18:33:35 +00002855 RTLIB::Libcall LC = VT == MVT::f32
Evan Cheng31cbddf2007-01-12 02:11:51 +00002856 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner81914422005-08-03 20:31:37 +00002857 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002858 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2859 false/*sign irrelevant*/, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002860 }
2861 break;
2862 }
Dan Gohmana1603612007-10-08 18:33:35 +00002863 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002864 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002865 case ISD::VAARG: {
2866 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2867 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2868
Chris Lattner364b89a2006-01-28 07:42:08 +00002869 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002870 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2871 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002872 case TargetLowering::Custom:
2873 isCustom = true;
2874 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002875 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002876 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2877 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002878 Tmp1 = Result.getValue(1);
2879
2880 if (isCustom) {
2881 Tmp2 = TLI.LowerOperation(Result, DAG);
2882 if (Tmp2.Val) {
2883 Result = LegalizeOp(Tmp2);
2884 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2885 }
2886 }
Nate Begemane74795c2006-01-25 18:21:52 +00002887 break;
2888 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002889 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00002890 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00002891 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002892 // Increment the pointer, VAList, to the next vaarg
2893 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2894 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2895 TLI.getPointerTy()));
2896 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00002897 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2898 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002899 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00002900 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002901 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002902 Result = LegalizeOp(Result);
2903 break;
2904 }
2905 }
2906 // Since VAARG produces two values, make sure to remember that we
2907 // legalized both of them.
2908 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002909 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2910 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002911 }
2912
2913 case ISD::VACOPY:
2914 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2915 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2916 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2917
2918 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2919 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002920 case TargetLowering::Custom:
2921 isCustom = true;
2922 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002923 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002924 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2925 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002926 if (isCustom) {
2927 Tmp1 = TLI.LowerOperation(Result, DAG);
2928 if (Tmp1.Val) Result = Tmp1;
2929 }
Nate Begemane74795c2006-01-25 18:21:52 +00002930 break;
2931 case TargetLowering::Expand:
2932 // This defaults to loading a pointer from the input and storing it to the
2933 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002934 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00002935 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00002936 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2937 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00002938 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2939 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002940 break;
2941 }
2942 break;
2943
2944 case ISD::VAEND:
2945 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2946 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2947
2948 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2949 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002950 case TargetLowering::Custom:
2951 isCustom = true;
2952 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002953 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002954 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002955 if (isCustom) {
2956 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2957 if (Tmp1.Val) Result = Tmp1;
2958 }
Nate Begemane74795c2006-01-25 18:21:52 +00002959 break;
2960 case TargetLowering::Expand:
2961 Result = Tmp1; // Default to a no-op, return the chain
2962 break;
2963 }
2964 break;
2965
2966 case ISD::VASTART:
2967 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2968 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2969
Chris Lattnerd02b0542006-01-28 10:58:55 +00002970 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2971
Nate Begemane74795c2006-01-25 18:21:52 +00002972 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2973 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002974 case TargetLowering::Legal: break;
2975 case TargetLowering::Custom:
2976 Tmp1 = TLI.LowerOperation(Result, DAG);
2977 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002978 break;
2979 }
2980 break;
2981
Nate Begeman1b8121b2006-01-11 21:21:00 +00002982 case ISD::ROTL:
2983 case ISD::ROTR:
2984 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2985 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00002986 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00002987 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2988 default:
2989 assert(0 && "ROTL/ROTR legalize operation not supported");
2990 break;
2991 case TargetLowering::Legal:
2992 break;
2993 case TargetLowering::Custom:
2994 Tmp1 = TLI.LowerOperation(Result, DAG);
2995 if (Tmp1.Val) Result = Tmp1;
2996 break;
2997 case TargetLowering::Promote:
2998 assert(0 && "Do not know how to promote ROTL/ROTR");
2999 break;
3000 case TargetLowering::Expand:
3001 assert(0 && "Do not know how to expand ROTL/ROTR");
3002 break;
3003 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00003004 break;
3005
Nate Begeman2fba8a32006-01-14 03:14:10 +00003006 case ISD::BSWAP:
3007 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3008 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003009 case TargetLowering::Custom:
3010 assert(0 && "Cannot custom legalize this yet!");
3011 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003012 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003013 break;
3014 case TargetLowering::Promote: {
3015 MVT::ValueType OVT = Tmp1.getValueType();
3016 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohman1796f1f2007-05-18 17:52:13 +00003017 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00003018
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003019 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3020 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3021 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3022 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3023 break;
3024 }
3025 case TargetLowering::Expand:
3026 Result = ExpandBSWAP(Tmp1);
3027 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003028 }
3029 break;
3030
Andrew Lenharth5e177822005-05-03 17:19:30 +00003031 case ISD::CTPOP:
3032 case ISD::CTTZ:
3033 case ISD::CTLZ:
3034 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3035 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00003036 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00003037 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003038 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00003039 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00003040 TargetLowering::Custom) {
3041 Tmp1 = TLI.LowerOperation(Result, DAG);
3042 if (Tmp1.Val) {
3043 Result = Tmp1;
3044 }
Scott Michel34e2d222007-07-30 21:00:31 +00003045 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003046 break;
3047 case TargetLowering::Promote: {
3048 MVT::ValueType OVT = Tmp1.getValueType();
3049 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00003050
3051 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00003052 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3053 // Perform the larger operation, then subtract if needed.
3054 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003055 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00003056 case ISD::CTPOP:
3057 Result = Tmp1;
3058 break;
3059 case ISD::CTTZ:
3060 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00003061 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003062 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00003063 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003064 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel34e2d222007-07-30 21:00:31 +00003065 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003066 break;
3067 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003068 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003069 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003070 DAG.getConstant(MVT::getSizeInBits(NVT) -
3071 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00003072 break;
3073 }
3074 break;
3075 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003076 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003077 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003078 break;
3079 }
3080 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003081
Chris Lattner13fe99c2005-04-02 05:00:07 +00003082 // Unary operators
3083 case ISD::FABS:
3084 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00003085 case ISD::FSQRT:
3086 case ISD::FSIN:
3087 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003088 Tmp1 = LegalizeOp(Node->getOperand(0));
3089 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003090 case TargetLowering::Promote:
3091 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00003092 isCustom = true;
3093 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00003094 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003095 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00003096 if (isCustom) {
3097 Tmp1 = TLI.LowerOperation(Result, DAG);
3098 if (Tmp1.Val) Result = Tmp1;
3099 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003100 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00003101 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003102 switch (Node->getOpcode()) {
3103 default: assert(0 && "Unreachable!");
3104 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003105 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3106 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003107 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00003108 break;
Chris Lattner80026402005-04-30 04:43:14 +00003109 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003110 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3111 MVT::ValueType VT = Node->getValueType(0);
3112 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003113 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003114 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3115 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00003116 break;
3117 }
3118 case ISD::FSQRT:
3119 case ISD::FSIN:
3120 case ISD::FCOS: {
3121 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003122 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00003123 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00003124 case ISD::FSQRT:
Dale Johannesen25a00a62007-09-28 01:08:20 +00003125 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00003126 VT == MVT::f64 ? RTLIB::SQRT_F64 :
3127 VT == MVT::f80 ? RTLIB::SQRT_F80 :
3128 VT == MVT::ppcf128 ? RTLIB::SQRT_PPCF128 :
3129 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003130 break;
3131 case ISD::FSIN:
3132 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3133 break;
3134 case ISD::FCOS:
3135 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3136 break;
Chris Lattner80026402005-04-30 04:43:14 +00003137 default: assert(0 && "Unreachable!");
3138 }
Nate Begeman77558da2005-08-04 21:43:28 +00003139 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003140 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3141 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00003142 break;
3143 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003144 }
3145 break;
3146 }
3147 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003148 case ISD::FPOWI: {
3149 // We always lower FPOWI into a libcall. No target support it yet.
Dale Johannesen25a00a62007-09-28 01:08:20 +00003150 RTLIB::Libcall LC =
3151 Node->getValueType(0) == MVT::f32 ? RTLIB::POWI_F32 :
3152 Node->getValueType(0) == MVT::f64 ? RTLIB::POWI_F64 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00003153 Node->getValueType(0) == MVT::f80 ? RTLIB::POWI_F80 :
3154 Node->getValueType(0) == MVT::ppcf128 ? RTLIB::POWI_PPCF128 :
3155 RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003156 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003157 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3158 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003159 break;
3160 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003161 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003162 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00003163 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohmana8665142007-06-25 16:23:39 +00003164 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3165 // The input has to be a vector type, we have to either scalarize it, pack
3166 // it, or convert it based on whether the input vector type is legal.
3167 SDNode *InVal = Node->getOperand(0).Val;
3168 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
3169 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
3170
3171 // Figure out if there is a simple type corresponding to this Vector
3172 // type. If so, convert to the vector type.
3173 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3174 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003175 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003176 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3177 LegalizeOp(Node->getOperand(0)));
3178 break;
3179 } else if (NumElems == 1) {
3180 // Turn this into a bit convert of the scalar input.
3181 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3182 ScalarizeVectorOp(Node->getOperand(0)));
3183 break;
3184 } else {
3185 // FIXME: UNIMP! Store then reload
3186 assert(0 && "Cast from unsupported vector type not implemented yet!");
3187 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003188 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003189 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3190 Node->getOperand(0).getValueType())) {
3191 default: assert(0 && "Unknown operation action!");
3192 case TargetLowering::Expand:
3193 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3194 break;
3195 case TargetLowering::Legal:
3196 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003197 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003198 break;
3199 }
3200 }
3201 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00003202
Chris Lattner13fe99c2005-04-02 05:00:07 +00003203 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00003204 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003205 case ISD::UINT_TO_FP: {
3206 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00003207 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3208 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00003209 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003210 Node->getOperand(0).getValueType())) {
3211 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003212 case TargetLowering::Custom:
3213 isCustom = true;
3214 // FALLTHROUGH
3215 case TargetLowering::Legal:
3216 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003217 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003218 if (isCustom) {
3219 Tmp1 = TLI.LowerOperation(Result, DAG);
3220 if (Tmp1.Val) Result = Tmp1;
3221 }
3222 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003223 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00003224 Result = ExpandLegalINT_TO_FP(isSigned,
3225 LegalizeOp(Node->getOperand(0)),
3226 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003227 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003228 case TargetLowering::Promote:
3229 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3230 Node->getValueType(0),
3231 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003232 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00003233 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003234 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00003235 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003236 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3237 Node->getValueType(0), Node->getOperand(0));
3238 break;
3239 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003240 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003241 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003242 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3243 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003244 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003245 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3246 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003247 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003248 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3249 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003250 break;
3251 }
3252 break;
3253 }
3254 case ISD::TRUNCATE:
3255 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3256 case Legal:
3257 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003258 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003259 break;
3260 case Expand:
3261 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3262
3263 // Since the result is legal, we should just be able to truncate the low
3264 // part of the source.
3265 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3266 break;
3267 case Promote:
3268 Result = PromoteOp(Node->getOperand(0));
3269 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3270 break;
3271 }
3272 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003273
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003274 case ISD::FP_TO_SINT:
3275 case ISD::FP_TO_UINT:
3276 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3277 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00003278 Tmp1 = LegalizeOp(Node->getOperand(0));
3279
Chris Lattner44fe26f2005-07-29 00:11:56 +00003280 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3281 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003282 case TargetLowering::Custom:
3283 isCustom = true;
3284 // FALLTHROUGH
3285 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003286 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003287 if (isCustom) {
3288 Tmp1 = TLI.LowerOperation(Result, DAG);
3289 if (Tmp1.Val) Result = Tmp1;
3290 }
3291 break;
3292 case TargetLowering::Promote:
3293 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3294 Node->getOpcode() == ISD::FP_TO_SINT);
3295 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00003296 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00003297 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3298 SDOperand True, False;
3299 MVT::ValueType VT = Node->getOperand(0).getValueType();
3300 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenb59d25f2007-09-19 17:53:26 +00003301 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003302 const uint64_t zero[] = {0, 0};
3303 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3304 uint64_t x = 1ULL << ShiftAmt;
Neil Booth5f009732007-10-07 11:45:55 +00003305 (void)apf.convertFromZeroExtendedInteger
3306 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003307 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begeman36853ee2005-08-14 01:20:53 +00003308 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3309 Node->getOperand(0), Tmp2, ISD::SETLT);
3310 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3311 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00003312 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00003313 Tmp2));
3314 False = DAG.getNode(ISD::XOR, NVT, False,
3315 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003316 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3317 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00003318 } else {
3319 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3320 }
3321 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003322 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003323 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003324 case Expand: {
3325 // Convert f32 / f64 to i32 / i64.
3326 MVT::ValueType VT = Op.getValueType();
Dale Johannesen666323e2007-10-10 01:01:31 +00003327 MVT::ValueType OVT = Node->getOperand(0).getValueType();
3328 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
3329 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
3330 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3331 (DAG.getNode(ISD::FP_ROUND_INREG,
3332 MVT::ppcf128, Node->getOperand(0),
3333 DAG.getValueType(MVT::f64)))));
3334 break;
3335 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00003336 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003337 switch (Node->getOpcode()) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003338 case ISD::FP_TO_SINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003339 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003340 LC = (VT == MVT::i32)
3341 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003342 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003343 LC = (VT == MVT::i32)
3344 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003345 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003346 assert(VT == MVT::i64);
Dale Johannesenc0154c02007-10-05 20:04:43 +00003347 LC = RTLIB::FPTOSINT_F80_I64;
3348 }
3349 else if (OVT == MVT::ppcf128) {
3350 assert(VT == MVT::i64);
3351 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003352 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003353 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003354 }
3355 case ISD::FP_TO_UINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003356 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003357 LC = (VT == MVT::i32)
3358 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003359 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003360 LC = (VT == MVT::i32)
3361 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003362 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003363 LC = (VT == MVT::i32)
Dale Johannesenc0154c02007-10-05 20:04:43 +00003364 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3365 }
3366 else if (OVT == MVT::ppcf128) {
3367 assert(VT == MVT::i64);
3368 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003369 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003370 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003371 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003372 default: assert(0 && "Unreachable!");
3373 }
3374 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003375 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3376 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003377 break;
3378 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003379 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003380 Tmp1 = PromoteOp(Node->getOperand(0));
3381 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3382 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003383 break;
3384 }
3385 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003386
Dale Johannesenc339e452007-08-09 17:27:48 +00003387 case ISD::FP_EXTEND:
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003388 case ISD::FP_ROUND: {
3389 MVT::ValueType newVT = Op.getValueType();
3390 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3391 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesenf864ac92007-10-06 01:24:11 +00003392 if (Node->getOpcode() == ISD::FP_ROUND && oldVT == MVT::ppcf128) {
3393 SDOperand Lo, Hi;
3394 ExpandOp(Node->getOperand(0), Lo, Hi);
3395 if (newVT == MVT::f64)
3396 Result = Hi;
3397 else
3398 Result = DAG.getNode(ISD::FP_ROUND, newVT, Hi);
3399 break;
Dale Johannesenc339e452007-08-09 17:27:48 +00003400 } else {
Dale Johannesenf864ac92007-10-06 01:24:11 +00003401 // The only other way we can lower this is to turn it into a STORE,
3402 // LOAD pair, targetting a temporary location (a stack slot).
3403
3404 // NOTE: there is a choice here between constantly creating new stack
3405 // slots and always reusing the same one. We currently always create
3406 // new ones, as reuse may inhibit scheduling.
3407 MVT::ValueType slotVT =
3408 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3409 const Type *Ty = MVT::getTypeForValueType(slotVT);
3410 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3411 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3412 MachineFunction &MF = DAG.getMachineFunction();
3413 int SSFI =
3414 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3415 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3416 if (Node->getOpcode() == ISD::FP_EXTEND) {
3417 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3418 StackSlot, NULL, 0);
3419 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3420 Result, StackSlot, NULL, 0, oldVT);
3421 } else {
3422 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3423 StackSlot, NULL, 0, newVT);
3424 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0, newVT);
3425 }
3426 break;
Dale Johannesenc339e452007-08-09 17:27:48 +00003427 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003428 }
Dale Johannesena2b3c172007-07-03 00:53:03 +00003429 }
3430 // FALL THROUGH
Chris Lattner7753f172005-09-02 00:18:10 +00003431 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003432 case ISD::ZERO_EXTEND:
3433 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003434 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003435 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003436 case Legal:
3437 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003438 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003439 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003440 case Promote:
3441 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003442 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003443 Tmp1 = PromoteOp(Node->getOperand(0));
3444 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003445 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003446 case ISD::ZERO_EXTEND:
3447 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003448 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003449 Result = DAG.getZeroExtendInReg(Result,
3450 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003451 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003452 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003453 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003454 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003455 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003456 Result,
3457 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003458 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003459 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003460 Result = PromoteOp(Node->getOperand(0));
3461 if (Result.getValueType() != Op.getValueType())
3462 // Dynamically dead while we have only 2 FP types.
3463 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3464 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003465 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00003466 Result = PromoteOp(Node->getOperand(0));
3467 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3468 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003469 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003470 }
3471 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003472 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003473 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003474 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003475 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003476
3477 // If this operation is not supported, convert it to a shl/shr or load/store
3478 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003479 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3480 default: assert(0 && "This action not supported for this op yet!");
3481 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003482 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003483 break;
3484 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003485 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003486 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003487 // NOTE: we could fall back on load/store here too for targets without
3488 // SAR. However, it is doubtful that any exist.
3489 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3490 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003491 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003492 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3493 Node->getOperand(0), ShiftCst);
3494 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3495 Result, ShiftCst);
3496 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003497 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003498 // EXTLOAD pair, targetting a temporary location (a stack slot).
3499
3500 // NOTE: there is a choice here between constantly creating new stack
3501 // slots and always reusing the same one. We currently always create
3502 // new ones, as reuse may inhibit scheduling.
3503 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner1deacd62007-04-28 06:42:38 +00003504 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattner945e4372007-02-14 05:52:17 +00003505 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner99222f72005-01-15 07:15:18 +00003506 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00003507 int SSFI =
Chris Lattner1deacd62007-04-28 06:42:38 +00003508 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner99222f72005-01-15 07:15:18 +00003509 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003510 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3511 StackSlot, NULL, 0, ExtraVT);
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003512 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003513 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00003514 } else {
3515 assert(0 && "Unknown op");
3516 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003517 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003518 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003519 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003520 }
Duncan Sands644f9172007-07-27 12:58:54 +00003521 case ISD::TRAMPOLINE: {
3522 SDOperand Ops[6];
3523 for (unsigned i = 0; i != 6; ++i)
3524 Ops[i] = LegalizeOp(Node->getOperand(i));
3525 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3526 // The only option for this node is to custom lower it.
3527 Result = TLI.LowerOperation(Result, DAG);
3528 assert(Result.Val && "Should always custom lower!");
Duncan Sands86e01192007-09-11 14:10:23 +00003529
3530 // Since trampoline produces two values, make sure to remember that we
3531 // legalized both of them.
3532 Tmp1 = LegalizeOp(Result.getValue(1));
3533 Result = LegalizeOp(Result);
3534 AddLegalizedOperand(SDOperand(Node, 0), Result);
3535 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3536 return Op.ResNo ? Tmp1 : Result;
Duncan Sands644f9172007-07-27 12:58:54 +00003537 }
Chris Lattner99222f72005-01-15 07:15:18 +00003538 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003539
Chris Lattner101ea662006-04-08 04:13:17 +00003540 assert(Result.getValueType() == Op.getValueType() &&
3541 "Bad legalization!");
3542
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003543 // Make sure that the generated code is itself legal.
3544 if (Result != Op)
3545 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003546
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003547 // Note that LegalizeOp may be reentered even from single-use nodes, which
3548 // means that we always must cache transformed nodes.
3549 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003550 return Result;
3551}
3552
Chris Lattner4d978642005-01-15 22:16:26 +00003553/// PromoteOp - Given an operation that produces a value in an invalid type,
3554/// promote it to compute the value into a larger type. The produced value will
3555/// have the correct bits for the low portion of the register, but no guarantee
3556/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003557SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3558 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003559 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003560 assert(getTypeAction(VT) == Promote &&
3561 "Caller should expand or legalize operands that are not promotable!");
3562 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3563 "Cannot promote to smaller type!");
3564
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003565 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003566 SDOperand Result;
3567 SDNode *Node = Op.Val;
3568
Chris Lattner4b0ddb22007-02-04 01:17:38 +00003569 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00003570 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003571
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003572 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003573 case ISD::CopyFromReg:
3574 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003575 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003576#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00003577 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003578#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003579 assert(0 && "Do not know how to promote this operator!");
3580 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003581 case ISD::UNDEF:
3582 Result = DAG.getNode(ISD::UNDEF, NVT);
3583 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003584 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003585 if (VT != MVT::i1)
3586 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3587 else
3588 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003589 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3590 break;
3591 case ISD::ConstantFP:
3592 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3593 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3594 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003595
Chris Lattner2cb338d2005-01-18 02:59:52 +00003596 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003597 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003598 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3599 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003600 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003601
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003602 case ISD::TRUNCATE:
3603 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3604 case Legal:
3605 Result = LegalizeOp(Node->getOperand(0));
3606 assert(Result.getValueType() >= NVT &&
3607 "This truncation doesn't make sense!");
3608 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3609 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3610 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003611 case Promote:
3612 // The truncation is not required, because we don't guarantee anything
3613 // about high bits anyway.
3614 Result = PromoteOp(Node->getOperand(0));
3615 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003616 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003617 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3618 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003619 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003620 }
3621 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003622 case ISD::SIGN_EXTEND:
3623 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003624 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003625 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3626 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3627 case Legal:
3628 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003629 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003630 break;
3631 case Promote:
3632 // Promote the reg if it's smaller.
3633 Result = PromoteOp(Node->getOperand(0));
3634 // The high bits are not guaranteed to be anything. Insert an extend.
3635 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003636 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003637 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003638 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003639 Result = DAG.getZeroExtendInReg(Result,
3640 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003641 break;
3642 }
3643 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003644 case ISD::BIT_CONVERT:
3645 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3646 Result = PromoteOp(Result);
3647 break;
3648
Chris Lattner4d978642005-01-15 22:16:26 +00003649 case ISD::FP_EXTEND:
3650 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3651 case ISD::FP_ROUND:
3652 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3653 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3654 case Promote: assert(0 && "Unreachable with 2 FP types!");
3655 case Legal:
3656 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003657 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003658 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003659 break;
3660 }
3661 break;
3662
3663 case ISD::SINT_TO_FP:
3664 case ISD::UINT_TO_FP:
3665 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3666 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003667 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003668 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003669 break;
3670
3671 case Promote:
3672 Result = PromoteOp(Node->getOperand(0));
3673 if (Node->getOpcode() == ISD::SINT_TO_FP)
3674 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003675 Result,
3676 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003677 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003678 Result = DAG.getZeroExtendInReg(Result,
3679 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003680 // No extra round required here.
3681 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003682 break;
3683 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003684 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3685 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003686 // Round if we cannot tolerate excess precision.
3687 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003688 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3689 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003690 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003691 }
Chris Lattner4d978642005-01-15 22:16:26 +00003692 break;
3693
Chris Lattner268d4572005-12-09 17:32:47 +00003694 case ISD::SIGN_EXTEND_INREG:
3695 Result = PromoteOp(Node->getOperand(0));
3696 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3697 Node->getOperand(1));
3698 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003699 case ISD::FP_TO_SINT:
3700 case ISD::FP_TO_UINT:
3701 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3702 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003703 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003704 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003705 break;
3706 case Promote:
3707 // The input result is prerounded, so we don't have to do anything
3708 // special.
3709 Tmp1 = PromoteOp(Node->getOperand(0));
3710 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003711 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003712 // If we're promoting a UINT to a larger size, check to see if the new node
3713 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3714 // we can use that instead. This allows us to generate better code for
3715 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3716 // legal, such as PowerPC.
3717 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003718 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003719 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3720 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003721 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3722 } else {
3723 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3724 }
Chris Lattner4d978642005-01-15 22:16:26 +00003725 break;
3726
Chris Lattner13fe99c2005-04-02 05:00:07 +00003727 case ISD::FABS:
3728 case ISD::FNEG:
3729 Tmp1 = PromoteOp(Node->getOperand(0));
3730 assert(Tmp1.getValueType() == NVT);
3731 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3732 // NOTE: we do not have to do any extra rounding here for
3733 // NoExcessFPPrecision, because we know the input will have the appropriate
3734 // precision, and these operations don't modify precision at all.
3735 break;
3736
Chris Lattner9d6fa982005-04-28 21:44:33 +00003737 case ISD::FSQRT:
3738 case ISD::FSIN:
3739 case ISD::FCOS:
3740 Tmp1 = PromoteOp(Node->getOperand(0));
3741 assert(Tmp1.getValueType() == NVT);
3742 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003743 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003744 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3745 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003746 break;
3747
Chris Lattnerca401aa2007-03-03 23:43:21 +00003748 case ISD::FPOWI: {
3749 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3750 // directly as well, which may be better.
3751 Tmp1 = PromoteOp(Node->getOperand(0));
3752 assert(Tmp1.getValueType() == NVT);
3753 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3754 if (NoExcessFPPrecision)
3755 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3756 DAG.getValueType(VT));
3757 break;
3758 }
3759
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003760 case ISD::AND:
3761 case ISD::OR:
3762 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003763 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003764 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003765 case ISD::MUL:
3766 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003767 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003768 // that too is okay if they are integer operations.
3769 Tmp1 = PromoteOp(Node->getOperand(0));
3770 Tmp2 = PromoteOp(Node->getOperand(1));
3771 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3772 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003773 break;
3774 case ISD::FADD:
3775 case ISD::FSUB:
3776 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003777 Tmp1 = PromoteOp(Node->getOperand(0));
3778 Tmp2 = PromoteOp(Node->getOperand(1));
3779 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3780 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3781
3782 // Floating point operations will give excess precision that we may not be
3783 // able to tolerate. If we DO allow excess precision, just leave it,
3784 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003785 // FIXME: Why would we need to round FP ops more than integer ones?
3786 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003787 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003788 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3789 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003790 break;
3791
Chris Lattner4d978642005-01-15 22:16:26 +00003792 case ISD::SDIV:
3793 case ISD::SREM:
3794 // These operators require that their input be sign extended.
3795 Tmp1 = PromoteOp(Node->getOperand(0));
3796 Tmp2 = PromoteOp(Node->getOperand(1));
3797 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003798 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3799 DAG.getValueType(VT));
3800 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3801 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003802 }
3803 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3804
3805 // Perform FP_ROUND: this is probably overly pessimistic.
3806 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003807 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3808 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003809 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003810 case ISD::FDIV:
3811 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003812 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003813 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00003814 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3815 case Legal:
3816 Tmp1 = LegalizeOp(Node->getOperand(0));
3817 break;
3818 case Promote:
3819 Tmp1 = PromoteOp(Node->getOperand(0));
3820 break;
3821 case Expand:
3822 assert(0 && "not implemented");
3823 }
3824 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3825 case Legal:
3826 Tmp2 = LegalizeOp(Node->getOperand(1));
3827 break;
3828 case Promote:
3829 Tmp2 = PromoteOp(Node->getOperand(1));
3830 break;
3831 case Expand:
3832 assert(0 && "not implemented");
3833 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00003834 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3835
3836 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003837 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003838 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3839 DAG.getValueType(VT));
3840 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003841
3842 case ISD::UDIV:
3843 case ISD::UREM:
3844 // These operators require that their input be zero extended.
3845 Tmp1 = PromoteOp(Node->getOperand(0));
3846 Tmp2 = PromoteOp(Node->getOperand(1));
3847 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003848 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3849 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003850 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3851 break;
3852
3853 case ISD::SHL:
3854 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003855 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003856 break;
3857 case ISD::SRA:
3858 // The input value must be properly sign extended.
3859 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003860 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3861 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003862 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003863 break;
3864 case ISD::SRL:
3865 // The input value must be properly zero extended.
3866 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003867 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003868 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003869 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003870
3871 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003872 Tmp1 = Node->getOperand(0); // Get the chain.
3873 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003874 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3875 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3876 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3877 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00003878 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00003879 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00003880 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003881 // Increment the pointer, VAList, to the next vaarg
3882 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3883 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3884 TLI.getPointerTy()));
3885 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00003886 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3887 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003888 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003889 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00003890 }
3891 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003892 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00003893 break;
3894
Evan Chenge71fe34d2006-10-09 20:57:25 +00003895 case ISD::LOAD: {
3896 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00003897 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3898 ? ISD::EXTLOAD : LD->getExtensionType();
3899 Result = DAG.getExtLoad(ExtType, NVT,
3900 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00003901 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00003902 LD->getLoadedVT(),
3903 LD->isVolatile(),
3904 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003905 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003906 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003907 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00003908 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003909 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003910 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3911 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003912 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003913 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003914 case ISD::SELECT_CC:
3915 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3916 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3917 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003918 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00003919 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003920 case ISD::BSWAP:
3921 Tmp1 = Node->getOperand(0);
3922 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3923 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3924 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003925 DAG.getConstant(MVT::getSizeInBits(NVT) -
3926 MVT::getSizeInBits(VT),
Nate Begeman2fba8a32006-01-14 03:14:10 +00003927 TLI.getShiftAmountTy()));
3928 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003929 case ISD::CTPOP:
3930 case ISD::CTTZ:
3931 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003932 // Zero extend the argument
3933 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003934 // Perform the larger operation, then subtract if needed.
3935 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003936 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003937 case ISD::CTPOP:
3938 Result = Tmp1;
3939 break;
3940 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003941 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003942 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003943 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3944 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003945 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003946 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003947 break;
3948 case ISD::CTLZ:
3949 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003950 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003951 DAG.getConstant(MVT::getSizeInBits(NVT) -
3952 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003953 break;
3954 }
3955 break;
Dan Gohmana8665142007-06-25 16:23:39 +00003956 case ISD::EXTRACT_SUBVECTOR:
3957 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00003958 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00003959 case ISD::EXTRACT_VECTOR_ELT:
3960 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3961 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003962 }
3963
3964 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003965
3966 // Make sure the result is itself legal.
3967 Result = LegalizeOp(Result);
3968
3969 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003970 AddPromotedOperand(Op, Result);
3971 return Result;
3972}
Chris Lattnerdc750592005-01-07 07:47:09 +00003973
Dan Gohmana8665142007-06-25 16:23:39 +00003974/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3975/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3976/// based on the vector type. The return type of this matches the element type
3977/// of the vector, which may not be legal for the target.
3978SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00003979 // We know that operand #0 is the Vec vector. If the index is a constant
3980 // or if the invec is a supported hardware type, we can use it. Otherwise,
3981 // lower to a store then an indexed load.
3982 SDOperand Vec = Op.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00003983 SDOperand Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00003984
Dan Gohman60028182007-09-24 15:54:53 +00003985 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +00003986 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner6f423252006-03-31 17:55:51 +00003987
Dan Gohmana8665142007-06-25 16:23:39 +00003988 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3989 default: assert(0 && "This action is not supported yet!");
3990 case TargetLowering::Custom: {
3991 Vec = LegalizeOp(Vec);
3992 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3993 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3994 if (Tmp3.Val)
3995 return Tmp3;
3996 break;
3997 }
3998 case TargetLowering::Legal:
3999 if (isTypeLegal(TVT)) {
4000 Vec = LegalizeOp(Vec);
4001 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00004002 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00004003 }
4004 break;
4005 case TargetLowering::Expand:
4006 break;
4007 }
4008
4009 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00004010 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004011 Op = ScalarizeVectorOp(Vec);
4012 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4013 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner6f423252006-03-31 17:55:51 +00004014 SDOperand Lo, Hi;
4015 SplitVectorOp(Vec, Lo, Hi);
4016 if (CIdx->getValue() < NumElems/2) {
4017 Vec = Lo;
4018 } else {
4019 Vec = Hi;
Dan Gohmana8665142007-06-25 16:23:39 +00004020 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4021 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00004022 }
Dan Gohmana8665142007-06-25 16:23:39 +00004023
Chris Lattner6f423252006-03-31 17:55:51 +00004024 // It's now an extract from the appropriate high or low part. Recurse.
4025 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004026 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00004027 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00004028 // Store the value to a temporary stack slot, then LOAD the scalar
4029 // element back out.
4030 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
4031 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4032
4033 // Add the offset to the index.
4034 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4035 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4036 DAG.getConstant(EltSize, Idx.getValueType()));
4037 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4038
4039 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00004040 }
Dan Gohmana8665142007-06-25 16:23:39 +00004041 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00004042}
4043
Dan Gohmana8665142007-06-25 16:23:39 +00004044/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00004045/// we assume the operation can be split if it is not already legal.
Dan Gohmana8665142007-06-25 16:23:39 +00004046SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00004047 // We know that operand #0 is the Vec vector. For now we assume the index
4048 // is a constant and that the extracted result is a supported hardware type.
4049 SDOperand Vec = Op.getOperand(0);
4050 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4051
Dan Gohmana8665142007-06-25 16:23:39 +00004052 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00004053
4054 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4055 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004056 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00004057 }
4058
4059 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4060 SDOperand Lo, Hi;
4061 SplitVectorOp(Vec, Lo, Hi);
4062 if (CIdx->getValue() < NumElems/2) {
4063 Vec = Lo;
4064 } else {
4065 Vec = Hi;
4066 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4067 }
4068
4069 // It's now an extract from the appropriate high or low part. Recurse.
4070 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004071 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00004072}
4073
Nate Begeman7e7f4392006-02-01 07:19:44 +00004074/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4075/// with condition CC on the current target. This usually involves legalizing
4076/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4077/// there may be no choice but to create a new SetCC node to represent the
4078/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4079/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4080void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4081 SDOperand &RHS,
4082 SDOperand &CC) {
Dale Johannesenf864ac92007-10-06 01:24:11 +00004083 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman7e7f4392006-02-01 07:19:44 +00004084
4085 switch (getTypeAction(LHS.getValueType())) {
4086 case Legal:
4087 Tmp1 = LegalizeOp(LHS); // LHS
4088 Tmp2 = LegalizeOp(RHS); // RHS
4089 break;
4090 case Promote:
4091 Tmp1 = PromoteOp(LHS); // LHS
4092 Tmp2 = PromoteOp(RHS); // RHS
4093
4094 // If this is an FP compare, the operands have already been extended.
4095 if (MVT::isInteger(LHS.getValueType())) {
4096 MVT::ValueType VT = LHS.getValueType();
4097 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4098
4099 // Otherwise, we have to insert explicit sign or zero extends. Note
4100 // that we could insert sign extends for ALL conditions, but zero extend
4101 // is cheaper on many machines (an AND instead of two shifts), so prefer
4102 // it.
4103 switch (cast<CondCodeSDNode>(CC)->get()) {
4104 default: assert(0 && "Unknown integer comparison!");
4105 case ISD::SETEQ:
4106 case ISD::SETNE:
4107 case ISD::SETUGE:
4108 case ISD::SETUGT:
4109 case ISD::SETULE:
4110 case ISD::SETULT:
4111 // ALL of these operations will work if we either sign or zero extend
4112 // the operands (including the unsigned comparisons!). Zero extend is
4113 // usually a simpler/cheaper operation, so prefer it.
4114 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4115 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4116 break;
4117 case ISD::SETGE:
4118 case ISD::SETGT:
4119 case ISD::SETLT:
4120 case ISD::SETLE:
4121 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4122 DAG.getValueType(VT));
4123 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4124 DAG.getValueType(VT));
4125 break;
4126 }
4127 }
4128 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004129 case Expand: {
4130 MVT::ValueType VT = LHS.getValueType();
4131 if (VT == MVT::f32 || VT == MVT::f64) {
4132 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00004133 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004134 switch (cast<CondCodeSDNode>(CC)->get()) {
4135 case ISD::SETEQ:
4136 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004137 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004138 break;
4139 case ISD::SETNE:
4140 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004141 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004142 break;
4143 case ISD::SETGE:
4144 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004145 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004146 break;
4147 case ISD::SETLT:
4148 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004149 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004150 break;
4151 case ISD::SETLE:
4152 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004153 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004154 break;
4155 case ISD::SETGT:
4156 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004157 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004158 break;
4159 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00004160 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4161 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004162 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00004163 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004164 break;
4165 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004166 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004167 switch (cast<CondCodeSDNode>(CC)->get()) {
4168 case ISD::SETONE:
4169 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00004170 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004171 // Fallthrough
4172 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004173 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004174 break;
4175 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004176 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004177 break;
4178 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004179 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004180 break;
4181 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004182 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004183 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004184 case ISD::SETUEQ:
4185 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004186 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004187 default: assert(0 && "Unsupported FP setcc!");
4188 }
4189 }
4190
4191 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004192 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00004193 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004194 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004195 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00004196 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00004197 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004198 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00004199 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00004200 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004201 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004202 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00004203 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004204 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4205 Tmp2 = SDOperand();
4206 }
4207 LHS = Tmp1;
4208 RHS = Tmp2;
4209 return;
4210 }
4211
Nate Begeman7e7f4392006-02-01 07:19:44 +00004212 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4213 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004214 ExpandOp(RHS, RHSLo, RHSHi);
4215 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4216
4217 if (VT==MVT::ppcf128) {
4218 // FIXME: This generated code sucks. We want to generate
4219 // FCMP crN, hi1, hi2
4220 // BNE crN, L:
4221 // FCMP crN, lo1, lo2
4222 // The following can be improved, but not that much.
4223 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4224 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4225 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4226 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4227 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4228 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4229 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4230 Tmp2 = SDOperand();
4231 break;
4232 }
4233
4234 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004235 case ISD::SETEQ:
4236 case ISD::SETNE:
4237 if (RHSLo == RHSHi)
4238 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4239 if (RHSCST->isAllOnesValue()) {
4240 // Comparison to -1.
4241 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4242 Tmp2 = RHSLo;
4243 break;
4244 }
4245
4246 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4247 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4248 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4249 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4250 break;
4251 default:
4252 // If this is a comparison of the sign bit, just look at the top part.
4253 // X > -1, x < 0
4254 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4255 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4256 CST->getValue() == 0) || // X < 0
4257 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4258 CST->isAllOnesValue())) { // X > -1
4259 Tmp1 = LHSHi;
4260 Tmp2 = RHSHi;
4261 break;
4262 }
4263
4264 // FIXME: This generated code sucks.
4265 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00004266 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004267 default: assert(0 && "Unknown integer setcc!");
4268 case ISD::SETLT:
4269 case ISD::SETULT: LowCC = ISD::SETULT; break;
4270 case ISD::SETGT:
4271 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4272 case ISD::SETLE:
4273 case ISD::SETULE: LowCC = ISD::SETULE; break;
4274 case ISD::SETGE:
4275 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4276 }
4277
4278 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4279 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4280 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4281
4282 // NOTE: on targets without efficient SELECT of bools, we can always use
4283 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00004284 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4285 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4286 false, DagCombineInfo);
4287 if (!Tmp1.Val)
4288 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4289 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4290 CCCode, false, DagCombineInfo);
4291 if (!Tmp2.Val)
4292 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
4293
4294 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4295 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4296 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4297 (Tmp2C && Tmp2C->getValue() == 0 &&
4298 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4299 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4300 (Tmp2C && Tmp2C->getValue() == 1 &&
4301 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4302 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4303 // low part is known false, returns high part.
4304 // For LE / GE, if high part is known false, ignore the low part.
4305 // For LT / GT, if high part is known true, ignore the low part.
4306 Tmp1 = Tmp2;
4307 Tmp2 = SDOperand();
4308 } else {
4309 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4310 ISD::SETEQ, false, DagCombineInfo);
4311 if (!Result.Val)
4312 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4313 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4314 Result, Tmp1, Tmp2));
4315 Tmp1 = Result;
4316 Tmp2 = SDOperand();
4317 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004318 }
4319 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004320 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004321 LHS = Tmp1;
4322 RHS = Tmp2;
4323}
4324
Chris Lattner36e663d2005-12-23 00:16:34 +00004325/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00004326/// The resultant code need not be legal. Note that SrcOp is the input operand
4327/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00004328SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4329 SDOperand SrcOp) {
4330 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004331 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004332
4333 // Emit a store to the stack slot.
Evan Chengab51cf22006-10-13 21:14:26 +00004334 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004335 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004336 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004337}
4338
Chris Lattner6be79822006-04-04 17:23:26 +00004339SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4340 // Create a vector sized/aligned stack slot, store the value to element #0,
4341 // then load the whole vector back out.
4342 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00004343 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00004344 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004345 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00004346}
4347
4348
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004349/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00004350/// support the operation, but do support the resultant vector type.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004351SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4352
4353 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00004354 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004355 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004356 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00004357 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004358 std::map<SDOperand, std::vector<unsigned> > Values;
4359 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004360 bool isConstant = true;
4361 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4362 SplatValue.getOpcode() != ISD::UNDEF)
4363 isConstant = false;
4364
Evan Cheng1d2e9952006-03-24 01:17:21 +00004365 for (unsigned i = 1; i < NumElems; ++i) {
4366 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00004367 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004368 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004369 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004370 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00004371 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004372
4373 // If this isn't a constant element or an undef, we can't use a constant
4374 // pool load.
4375 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4376 V.getOpcode() != ISD::UNDEF)
4377 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004378 }
4379
4380 if (isOnlyLowElement) {
4381 // If the low element is an undef too, then this whole things is an undef.
4382 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4383 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4384 // Otherwise, turn this into a scalar_to_vector node.
4385 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4386 Node->getOperand(0));
4387 }
4388
Chris Lattner77e271c2006-03-24 07:29:17 +00004389 // If all elements are constants, create a load from the constant pool.
4390 if (isConstant) {
4391 MVT::ValueType VT = Node->getValueType(0);
4392 const Type *OpNTy =
4393 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4394 std::vector<Constant*> CV;
4395 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4396 if (ConstantFPSDNode *V =
4397 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00004398 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004399 } else if (ConstantSDNode *V =
4400 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00004401 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004402 } else {
4403 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4404 CV.push_back(UndefValue::get(OpNTy));
4405 }
4406 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00004407 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00004408 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00004409 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004410 }
4411
Chris Lattner21e68c82006-03-20 01:52:29 +00004412 if (SplatValue.Val) { // Splat of one value?
4413 // Build the shuffle constant vector: <0, 0, 0, 0>
4414 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00004415 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00004416 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004417 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004418 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4419 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00004420
4421 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004422 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00004423 // Get the splatted value into the low element of a vector register.
4424 SDOperand LowValVec =
4425 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4426
4427 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4428 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4429 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4430 SplatMask);
4431 }
4432 }
4433
Evan Cheng1d2e9952006-03-24 01:17:21 +00004434 // If there are only two unique elements, we may be able to turn this into a
4435 // vector shuffle.
4436 if (Values.size() == 2) {
4437 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4438 MVT::ValueType MaskVT =
4439 MVT::getIntVectorWithNumElements(NumElems);
4440 std::vector<SDOperand> MaskVec(NumElems);
4441 unsigned i = 0;
4442 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4443 E = Values.end(); I != E; ++I) {
4444 for (std::vector<unsigned>::iterator II = I->second.begin(),
4445 EE = I->second.end(); II != EE; ++II)
Dan Gohman5c441312007-06-14 22:58:02 +00004446 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004447 i += NumElems;
4448 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004449 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4450 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004451
4452 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004453 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4454 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004455 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004456 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4457 E = Values.end(); I != E; ++I) {
4458 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4459 I->first);
4460 Ops.push_back(Op);
4461 }
4462 Ops.push_back(ShuffleMask);
4463
4464 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004465 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4466 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004467 }
4468 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004469
4470 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4471 // aligned object on the stack, store each element into it, then load
4472 // the result as a vector.
4473 MVT::ValueType VT = Node->getValueType(0);
4474 // Create the stack frame object.
4475 SDOperand FIPtr = CreateStackTemporary(VT);
4476
4477 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004478 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004479 unsigned TypeByteSize =
4480 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004481 // Store (in the right endianness) the elements to memory.
4482 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4483 // Ignore undef elements.
4484 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4485
Chris Lattner8fa445a2006-03-22 01:46:54 +00004486 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004487
4488 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4489 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4490
Evan Chengdf9ac472006-10-05 23:01:46 +00004491 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00004492 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004493 }
4494
4495 SDOperand StoreChain;
4496 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004497 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4498 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004499 else
4500 StoreChain = DAG.getEntryNode();
4501
4502 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004503 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004504}
4505
4506/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4507/// specified value type.
4508SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4509 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4510 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner50ee0e42007-01-20 22:35:55 +00004511 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattner945e4372007-02-14 05:52:17 +00004512 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004513 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004514 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4515}
4516
Chris Lattner4157c412005-04-02 04:00:59 +00004517void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4518 SDOperand Op, SDOperand Amt,
4519 SDOperand &Lo, SDOperand &Hi) {
4520 // Expand the subcomponents.
4521 SDOperand LHSL, LHSH;
4522 ExpandOp(Op, LHSL, LHSH);
4523
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004524 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00004525 MVT::ValueType VT = LHSL.getValueType();
4526 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00004527 Hi = Lo.getValue(1);
4528}
4529
4530
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004531/// ExpandShift - Try to find a clever way to expand this shift operation out to
4532/// smaller elements. If we can't find a way that is more efficient than a
4533/// libcall on this target, return false. Otherwise, return true with the
4534/// low-parts expanded into Lo and Hi.
4535bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4536 SDOperand &Lo, SDOperand &Hi) {
4537 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4538 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00004539
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004540 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00004541 SDOperand ShAmt = LegalizeOp(Amt);
4542 MVT::ValueType ShTy = ShAmt.getValueType();
4543 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4544 unsigned NVTBits = MVT::getSizeInBits(NVT);
4545
4546 // Handle the case when Amt is an immediate. Other cases are currently broken
4547 // and are disabled.
4548 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4549 unsigned Cst = CN->getValue();
4550 // Expand the incoming operand to be shifted, so that we have its parts
4551 SDOperand InL, InH;
4552 ExpandOp(Op, InL, InH);
4553 switch(Opc) {
4554 case ISD::SHL:
4555 if (Cst > VTBits) {
4556 Lo = DAG.getConstant(0, NVT);
4557 Hi = DAG.getConstant(0, NVT);
4558 } else if (Cst > NVTBits) {
4559 Lo = DAG.getConstant(0, NVT);
4560 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004561 } else if (Cst == NVTBits) {
4562 Lo = DAG.getConstant(0, NVT);
4563 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00004564 } else {
4565 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4566 Hi = DAG.getNode(ISD::OR, NVT,
4567 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4568 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4569 }
4570 return true;
4571 case ISD::SRL:
4572 if (Cst > VTBits) {
4573 Lo = DAG.getConstant(0, NVT);
4574 Hi = DAG.getConstant(0, NVT);
4575 } else if (Cst > NVTBits) {
4576 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4577 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00004578 } else if (Cst == NVTBits) {
4579 Lo = InH;
4580 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00004581 } else {
4582 Lo = DAG.getNode(ISD::OR, NVT,
4583 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4584 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4585 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4586 }
4587 return true;
4588 case ISD::SRA:
4589 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004590 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004591 DAG.getConstant(NVTBits-1, ShTy));
4592 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004593 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004594 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004595 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004596 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004597 } else if (Cst == NVTBits) {
4598 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004599 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004600 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004601 } else {
4602 Lo = DAG.getNode(ISD::OR, NVT,
4603 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4604 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4605 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4606 }
4607 return true;
4608 }
4609 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004610
4611 // Okay, the shift amount isn't constant. However, if we can tell that it is
4612 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4613 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00004614 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00004615
4616 // If we know that the high bit of the shift amount is one, then we can do
4617 // this as a couple of simple shifts.
4618 if (KnownOne & Mask) {
4619 // Mask out the high bit, which we know is set.
4620 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4621 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4622
4623 // Expand the incoming operand to be shifted, so that we have its parts
4624 SDOperand InL, InH;
4625 ExpandOp(Op, InL, InH);
4626 switch(Opc) {
4627 case ISD::SHL:
4628 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4629 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4630 return true;
4631 case ISD::SRL:
4632 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4633 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4634 return true;
4635 case ISD::SRA:
4636 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4637 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4638 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4639 return true;
4640 }
4641 }
4642
4643 // If we know that the high bit of the shift amount is zero, then we can do
4644 // this as a couple of simple shifts.
4645 if (KnownZero & Mask) {
4646 // Compute 32-amt.
4647 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4648 DAG.getConstant(NVTBits, Amt.getValueType()),
4649 Amt);
4650
4651 // Expand the incoming operand to be shifted, so that we have its parts
4652 SDOperand InL, InH;
4653 ExpandOp(Op, InL, InH);
4654 switch(Opc) {
4655 case ISD::SHL:
4656 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4657 Hi = DAG.getNode(ISD::OR, NVT,
4658 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4659 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4660 return true;
4661 case ISD::SRL:
4662 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4663 Lo = DAG.getNode(ISD::OR, NVT,
4664 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4665 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4666 return true;
4667 case ISD::SRA:
4668 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4669 Lo = DAG.getNode(ISD::OR, NVT,
4670 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4671 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4672 return true;
4673 }
4674 }
4675
Nate Begemanb0674922005-04-06 21:13:14 +00004676 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004677}
Chris Lattneraac464e2005-01-21 06:05:23 +00004678
Chris Lattner4add7e32005-01-23 04:42:50 +00004679
Chris Lattneraac464e2005-01-21 06:05:23 +00004680// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4681// does not fit into a register, return the lo part and set the hi part to the
4682// by-reg argument. If it does fit into a single register, return the result
4683// and leave the Hi part unset.
4684SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004685 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004686 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4687 // The input chain to this libcall is the entry node of the function.
4688 // Legalizing the call will automatically add the previous call to the
4689 // dependence.
4690 SDOperand InChain = DAG.getEntryNode();
4691
Chris Lattneraac464e2005-01-21 06:05:23 +00004692 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004693 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004694 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4695 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4696 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004697 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00004698 Entry.isSExt = isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00004699 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004700 }
4701 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004702
Chris Lattner06bbeb62005-05-11 19:02:11 +00004703 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004704 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004705 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004706 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004707 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004708
Chris Lattner462505f2006-02-13 09:18:02 +00004709 // Legalize the call sequence, starting with the chain. This will advance
4710 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4711 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4712 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004713 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004714 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004715 default: assert(0 && "Unknown thing");
4716 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004717 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004718 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004719 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004720 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004721 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004722 }
Chris Lattner63022662005-09-02 20:26:58 +00004723 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004724}
4725
Chris Lattner4add7e32005-01-23 04:42:50 +00004726
Evan Chengbf535fc2007-04-27 07:33:31 +00004727/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4728///
Chris Lattneraac464e2005-01-21 06:05:23 +00004729SDOperand SelectionDAGLegalize::
4730ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004731 assert(getTypeAction(Source.getValueType()) == Expand &&
4732 "This is not an expansion!");
4733 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4734
Chris Lattner06bbeb62005-05-11 19:02:11 +00004735 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004736 assert(Source.getValueType() == MVT::i64 &&
4737 "This only works for 64-bit -> FP");
4738 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4739 // incoming integer is set. To handle this, we dynamically test to see if
4740 // it is set, and, if so, add a fudge factor.
4741 SDOperand Lo, Hi;
4742 ExpandOp(Source, Lo, Hi);
4743
Chris Lattner2a4f7312005-05-13 04:45:13 +00004744 // If this is unsigned, and not supported, first perform the conversion to
4745 // signed, then adjust the result if the sign bit is set.
4746 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4747 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4748
Chris Lattnerd47675e2005-08-09 20:20:18 +00004749 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4750 DAG.getConstant(0, Hi.getValueType()),
4751 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004752 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4753 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4754 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004755 uint64_t FF = 0x5f800000ULL;
4756 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004757 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004758
Chris Lattnerc30405e2005-08-26 17:15:30 +00004759 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004760 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4761 SDOperand FudgeInReg;
4762 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004763 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen7f724e92007-09-16 16:51:49 +00004764 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Chengbf535fc2007-04-27 07:33:31 +00004765 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen7f724e92007-09-16 16:51:49 +00004766 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00004767 CPIdx, NULL, 0, MVT::f32);
4768 else
4769 assert(0 && "Unexpected conversion");
4770
Evan Chengbf535fc2007-04-27 07:33:31 +00004771 MVT::ValueType SCVT = SignedConv.getValueType();
4772 if (SCVT != DestTy) {
4773 // Destination type needs to be expanded as well. The FADD now we are
4774 // constructing will be expanded into a libcall.
4775 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4776 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4777 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4778 SignedConv, SignedConv.getValue(1));
4779 }
4780 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4781 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00004782 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00004783 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00004784
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004785 // Check to see if the target has a custom way to lower this. If so, use it.
4786 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4787 default: assert(0 && "This action not implemented for this operation!");
4788 case TargetLowering::Legal:
4789 case TargetLowering::Expand:
4790 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004791 case TargetLowering::Custom: {
4792 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4793 Source), DAG);
4794 if (NV.Val)
4795 return LegalizeOp(NV);
4796 break; // The target decided this was legal after all
4797 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004798 }
4799
Chris Lattner153587e2005-05-12 07:00:44 +00004800 // Expand the source, then glue it back together for the call. We must expand
4801 // the source in case it is shared (this pass of legalize must traverse it).
4802 SDOperand SrcLo, SrcHi;
4803 ExpandOp(Source, SrcLo, SrcHi);
4804 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4805
Evan Cheng31cbddf2007-01-12 02:11:51 +00004806 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004807 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004808 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004809 else {
4810 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00004811 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004812 }
Chris Lattner462505f2006-02-13 09:18:02 +00004813
Evan Chengbf535fc2007-04-27 07:33:31 +00004814 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner462505f2006-02-13 09:18:02 +00004815 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4816 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004817 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4818 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00004819}
Misha Brukman835702a2005-04-21 22:36:52 +00004820
Chris Lattner689bdcc2006-01-28 08:25:58 +00004821/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4822/// INT_TO_FP operation of the specified operand when the target requests that
4823/// we expand it. At this point, we know that the result and operand types are
4824/// legal for the target.
4825SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4826 SDOperand Op0,
4827 MVT::ValueType DestVT) {
4828 if (Op0.getValueType() == MVT::i32) {
4829 // simple 32-bit [signed|unsigned] integer to float/double expansion
4830
Chris Lattner50ee0e42007-01-20 22:35:55 +00004831 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner689bdcc2006-01-28 08:25:58 +00004832 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner50ee0e42007-01-20 22:35:55 +00004833 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4834 unsigned StackAlign =
Chris Lattner945e4372007-02-14 05:52:17 +00004835 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004836 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004837 // get address of 8 byte buffer
4838 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4839 // word offset constant for Hi/Lo address computation
4840 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4841 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00004842 SDOperand Hi = StackSlot;
4843 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4844 if (TLI.isLittleEndian())
4845 std::swap(Hi, Lo);
4846
Chris Lattner689bdcc2006-01-28 08:25:58 +00004847 // if signed map to unsigned space
4848 SDOperand Op0Mapped;
4849 if (isSigned) {
4850 // constant used to invert sign bit (signed to unsigned mapping)
4851 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4852 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4853 } else {
4854 Op0Mapped = Op0;
4855 }
4856 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00004857 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00004858 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004859 // initial hi portion of constructed double
4860 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4861 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00004862 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004863 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00004864 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004865 // FP constant to bias correct the final result
4866 SDOperand Bias = DAG.getConstantFP(isSigned ?
4867 BitsToDouble(0x4330000080000000ULL)
4868 : BitsToDouble(0x4330000000000000ULL),
4869 MVT::f64);
4870 // subtract the bias
4871 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4872 // final result
4873 SDOperand Result;
4874 // handle final rounding
4875 if (DestVT == MVT::f64) {
4876 // do nothing
4877 Result = Sub;
Dale Johannesen7f724e92007-09-16 16:51:49 +00004878 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
4879 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
4880 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
4881 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004882 }
4883 return Result;
4884 }
4885 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4886 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4887
4888 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4889 DAG.getConstant(0, Op0.getValueType()),
4890 ISD::SETLT);
4891 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4892 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4893 SignSet, Four, Zero);
4894
4895 // If the sign bit of the integer is set, the large number will be treated
4896 // as a negative number. To counteract this, the dynamic code adds an
4897 // offset depending on the data type.
4898 uint64_t FF;
4899 switch (Op0.getValueType()) {
4900 default: assert(0 && "Unsupported integer type!");
4901 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4902 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4903 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4904 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4905 }
4906 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004907 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004908
4909 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4910 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4911 SDOperand FudgeInReg;
4912 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004913 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004914 else {
Dale Johannesen7d67e542007-09-19 23:55:34 +00004915 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner689bdcc2006-01-28 08:25:58 +00004916 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00004917 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00004918 }
4919
4920 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4921}
4922
4923/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4924/// *INT_TO_FP operation of the specified operand when the target requests that
4925/// we promote it. At this point, we know that the result and operand types are
4926/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4927/// operation that takes a larger input.
4928SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4929 MVT::ValueType DestVT,
4930 bool isSigned) {
4931 // First step, figure out the appropriate *INT_TO_FP operation to use.
4932 MVT::ValueType NewInTy = LegalOp.getValueType();
4933
4934 unsigned OpToUse = 0;
4935
4936 // Scan for the appropriate larger type to use.
4937 while (1) {
4938 NewInTy = (MVT::ValueType)(NewInTy+1);
4939 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4940
4941 // If the target supports SINT_TO_FP of this type, use it.
4942 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4943 default: break;
4944 case TargetLowering::Legal:
4945 if (!TLI.isTypeLegal(NewInTy))
4946 break; // Can't use this datatype.
4947 // FALL THROUGH.
4948 case TargetLowering::Custom:
4949 OpToUse = ISD::SINT_TO_FP;
4950 break;
4951 }
4952 if (OpToUse) break;
4953 if (isSigned) continue;
4954
4955 // If the target supports UINT_TO_FP of this type, use it.
4956 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4957 default: break;
4958 case TargetLowering::Legal:
4959 if (!TLI.isTypeLegal(NewInTy))
4960 break; // Can't use this datatype.
4961 // FALL THROUGH.
4962 case TargetLowering::Custom:
4963 OpToUse = ISD::UINT_TO_FP;
4964 break;
4965 }
4966 if (OpToUse) break;
4967
4968 // Otherwise, try a larger type.
4969 }
4970
4971 // Okay, we found the operation and type to use. Zero extend our input to the
4972 // desired type then run the operation on it.
4973 return DAG.getNode(OpToUse, DestVT,
4974 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4975 NewInTy, LegalOp));
4976}
4977
4978/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4979/// FP_TO_*INT operation of the specified operand when the target requests that
4980/// we promote it. At this point, we know that the result and operand types are
4981/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4982/// operation that returns a larger result.
4983SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4984 MVT::ValueType DestVT,
4985 bool isSigned) {
4986 // First step, figure out the appropriate FP_TO*INT operation to use.
4987 MVT::ValueType NewOutTy = DestVT;
4988
4989 unsigned OpToUse = 0;
4990
4991 // Scan for the appropriate larger type to use.
4992 while (1) {
4993 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4994 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4995
4996 // If the target supports FP_TO_SINT returning this type, use it.
4997 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4998 default: break;
4999 case TargetLowering::Legal:
5000 if (!TLI.isTypeLegal(NewOutTy))
5001 break; // Can't use this datatype.
5002 // FALL THROUGH.
5003 case TargetLowering::Custom:
5004 OpToUse = ISD::FP_TO_SINT;
5005 break;
5006 }
5007 if (OpToUse) break;
5008
5009 // If the target supports FP_TO_UINT of this type, use it.
5010 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5011 default: break;
5012 case TargetLowering::Legal:
5013 if (!TLI.isTypeLegal(NewOutTy))
5014 break; // Can't use this datatype.
5015 // FALL THROUGH.
5016 case TargetLowering::Custom:
5017 OpToUse = ISD::FP_TO_UINT;
5018 break;
5019 }
5020 if (OpToUse) break;
5021
5022 // Otherwise, try a larger type.
5023 }
5024
5025 // Okay, we found the operation and type to use. Truncate the result of the
5026 // extended FP_TO_*INT operation to the desired size.
5027 return DAG.getNode(ISD::TRUNCATE, DestVT,
5028 DAG.getNode(OpToUse, NewOutTy, LegalOp));
5029}
5030
5031/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5032///
5033SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5034 MVT::ValueType VT = Op.getValueType();
5035 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5036 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5037 switch (VT) {
5038 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5039 case MVT::i16:
5040 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5041 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5042 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5043 case MVT::i32:
5044 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5045 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5046 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5047 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5048 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5049 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5050 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5051 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5052 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5053 case MVT::i64:
5054 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5055 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5056 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5057 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5058 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5059 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5060 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5061 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5062 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5063 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5064 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5065 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5066 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5067 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5068 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5069 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5070 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5071 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5072 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5073 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5074 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5075 }
5076}
5077
5078/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5079///
5080SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5081 switch (Opc) {
5082 default: assert(0 && "Cannot expand this yet!");
5083 case ISD::CTPOP: {
5084 static const uint64_t mask[6] = {
5085 0x5555555555555555ULL, 0x3333333333333333ULL,
5086 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5087 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5088 };
5089 MVT::ValueType VT = Op.getValueType();
5090 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005091 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005092 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5093 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5094 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5095 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5096 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5097 DAG.getNode(ISD::AND, VT,
5098 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5099 }
5100 return Op;
5101 }
5102 case ISD::CTLZ: {
5103 // for now, we do this:
5104 // x = x | (x >> 1);
5105 // x = x | (x >> 2);
5106 // ...
5107 // x = x | (x >>16);
5108 // x = x | (x >>32); // for 64-bit input
5109 // return popcount(~x);
5110 //
5111 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5112 MVT::ValueType VT = Op.getValueType();
5113 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005114 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005115 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5116 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5117 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5118 }
5119 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5120 return DAG.getNode(ISD::CTPOP, VT, Op);
5121 }
5122 case ISD::CTTZ: {
5123 // for now, we use: { return popcount(~x & (x - 1)); }
5124 // unless the target has ctlz but not ctpop, in which case we use:
5125 // { return 32 - nlz(~x & (x-1)); }
5126 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5127 MVT::ValueType VT = Op.getValueType();
5128 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5129 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5130 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5131 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5132 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5133 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5134 TLI.isOperationLegal(ISD::CTLZ, VT))
5135 return DAG.getNode(ISD::SUB, VT,
Dan Gohman1796f1f2007-05-18 17:52:13 +00005136 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00005137 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5138 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5139 }
5140 }
5141}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005142
Chris Lattnerdc750592005-01-07 07:47:09 +00005143/// ExpandOp - Expand the specified SDOperand into its two component pieces
5144/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5145/// LegalizeNodes map is filled in for any results that are not expanded, the
5146/// ExpandedNodes map is filled in for any results that are expanded, and the
5147/// Lo/Hi values are returned.
5148void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5149 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00005150 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00005151 SDNode *Node = Op.Val;
5152 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00005153 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohmana8665142007-06-25 16:23:39 +00005154 MVT::isVector(VT)) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00005155 "Cannot expand to FP value or to larger int value!");
5156
Chris Lattner1a570f12005-09-02 20:32:45 +00005157 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005158 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00005159 = ExpandedNodes.find(Op);
5160 if (I != ExpandedNodes.end()) {
5161 Lo = I->second.first;
5162 Hi = I->second.second;
5163 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00005164 }
5165
Chris Lattnerdc750592005-01-07 07:47:09 +00005166 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00005167 case ISD::CopyFromReg:
5168 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen666323e2007-10-10 01:01:31 +00005169 case ISD::FP_ROUND_INREG:
5170 if (VT == MVT::ppcf128 &&
5171 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5172 TargetLowering::Custom) {
5173 SDOperand Result = TLI.LowerOperation(Op, DAG);
5174 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5175 Lo = Result.Val->getOperand(0);
5176 Hi = Result.Val->getOperand(1);
5177 break;
5178 }
5179 // fall through
Chris Lattner44cab002006-01-21 04:27:00 +00005180 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005181#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005182 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005183#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00005184 assert(0 && "Do not know how to expand this operator!");
5185 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00005186 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00005187 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00005188 Lo = DAG.getNode(ISD::UNDEF, NVT);
5189 Hi = DAG.getNode(ISD::UNDEF, NVT);
5190 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005191 case ISD::Constant: {
5192 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5193 Lo = DAG.getConstant(Cst, NVT);
5194 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5195 break;
5196 }
Evan Cheng47833a12006-12-12 21:32:44 +00005197 case ISD::ConstantFP: {
5198 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng3766fc602006-12-12 22:19:28 +00005199 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00005200 if (getTypeAction(Lo.getValueType()) == Expand)
5201 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005202 break;
5203 }
Chris Lattner32e08b72005-03-28 22:03:13 +00005204 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005205 // Return the operands.
5206 Lo = Node->getOperand(0);
5207 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00005208 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005209
5210 case ISD::SIGN_EXTEND_INREG:
5211 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00005212 // sext_inreg the low part if needed.
5213 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5214
5215 // The high part gets the sign extension from the lo-part. This handles
5216 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005217 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5218 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5219 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005220 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00005221
Nate Begeman2fba8a32006-01-14 03:14:10 +00005222 case ISD::BSWAP: {
5223 ExpandOp(Node->getOperand(0), Lo, Hi);
5224 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5225 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5226 Lo = TempLo;
5227 break;
5228 }
5229
Chris Lattner55e9cde2005-05-11 04:51:16 +00005230 case ISD::CTPOP:
5231 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00005232 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5233 DAG.getNode(ISD::CTPOP, NVT, Lo),
5234 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00005235 Hi = DAG.getConstant(0, NVT);
5236 break;
5237
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005238 case ISD::CTLZ: {
5239 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005240 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005241 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5242 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005243 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5244 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005245 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5246 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5247
5248 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5249 Hi = DAG.getConstant(0, NVT);
5250 break;
5251 }
5252
5253 case ISD::CTTZ: {
5254 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005255 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005256 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5257 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005258 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5259 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005260 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5261 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5262
5263 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5264 Hi = DAG.getConstant(0, NVT);
5265 break;
5266 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00005267
Nate Begemane74795c2006-01-25 18:21:52 +00005268 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005269 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5270 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00005271 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5272 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5273
5274 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005275 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00005276 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5277 if (!TLI.isLittleEndian())
5278 std::swap(Lo, Hi);
5279 break;
5280 }
5281
Chris Lattnerdc750592005-01-07 07:47:09 +00005282 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005283 LoadSDNode *LD = cast<LoadSDNode>(Node);
5284 SDOperand Ch = LD->getChain(); // Legalize the chain.
5285 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5286 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman2af30632007-07-09 22:18:38 +00005287 int SVOffset = LD->getSrcValueOffset();
5288 unsigned Alignment = LD->getAlignment();
5289 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00005290
Evan Chenge71fe34d2006-10-09 20:57:25 +00005291 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman2af30632007-07-09 22:18:38 +00005292 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5293 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00005294 if (VT == MVT::f32 || VT == MVT::f64) {
5295 // f32->i32 or f64->i64 one to one expansion.
5296 // Remember that we legalized the chain.
5297 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00005298 // Recursively expand the new load.
5299 if (getTypeAction(NVT) == Expand)
5300 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005301 break;
5302 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00005303
Evan Chenge71fe34d2006-10-09 20:57:25 +00005304 // Increment the pointer to the other half.
5305 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5306 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5307 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005308 SVOffset += IncrementSize;
Dan Gohman2af30632007-07-09 22:18:38 +00005309 if (Alignment > IncrementSize)
5310 Alignment = IncrementSize;
5311 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5312 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00005313
Evan Chenge71fe34d2006-10-09 20:57:25 +00005314 // Build a factor node to remember that this load is independent of the
5315 // other one.
5316 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5317 Hi.getValue(1));
5318
5319 // Remember that we legalized the chain.
5320 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5321 if (!TLI.isLittleEndian())
5322 std::swap(Lo, Hi);
5323 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00005324 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00005325
5326 if (VT == MVT::f64 && EVT == MVT::f32) {
5327 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5328 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005329 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00005330 // Remember that we legalized the chain.
5331 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5332 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5333 break;
5334 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00005335
5336 if (EVT == NVT)
5337 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005338 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005339 else
5340 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005341 SVOffset, EVT, isVolatile,
5342 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005343
5344 // Remember that we legalized the chain.
5345 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5346
5347 if (ExtType == ISD::SEXTLOAD) {
5348 // The high part is obtained by SRA'ing all but one of the bits of the
5349 // lo part.
5350 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5351 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5352 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5353 } else if (ExtType == ISD::ZEXTLOAD) {
5354 // The high part is just a zero.
5355 Hi = DAG.getConstant(0, NVT);
5356 } else /* if (ExtType == ISD::EXTLOAD) */ {
5357 // The high part is undefined.
5358 Hi = DAG.getNode(ISD::UNDEF, NVT);
5359 }
5360 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005361 break;
5362 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005363 case ISD::AND:
5364 case ISD::OR:
5365 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5366 SDOperand LL, LH, RL, RH;
5367 ExpandOp(Node->getOperand(0), LL, LH);
5368 ExpandOp(Node->getOperand(1), RL, RH);
5369 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5370 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5371 break;
5372 }
5373 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005374 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00005375 ExpandOp(Node->getOperand(1), LL, LH);
5376 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00005377 if (getTypeAction(NVT) == Expand)
5378 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005379 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00005380 if (VT != MVT::f32)
5381 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00005382 break;
5383 }
Nate Begemane5b86d72005-08-10 20:51:12 +00005384 case ISD::SELECT_CC: {
5385 SDOperand TL, TH, FL, FH;
5386 ExpandOp(Node->getOperand(2), TL, TH);
5387 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00005388 if (getTypeAction(NVT) == Expand)
5389 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00005390 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5391 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00005392 if (VT != MVT::f32)
5393 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5394 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00005395 break;
5396 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005397 case ISD::ANY_EXTEND:
5398 // The low part is any extension of the input (which degenerates to a copy).
5399 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5400 // The high part is undefined.
5401 Hi = DAG.getNode(ISD::UNDEF, NVT);
5402 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005403 case ISD::SIGN_EXTEND: {
5404 // The low part is just a sign extension of the input (which degenerates to
5405 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005406 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005407
Chris Lattnerdc750592005-01-07 07:47:09 +00005408 // The high part is obtained by SRA'ing all but one of the bits of the lo
5409 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00005410 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005411 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5412 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00005413 break;
5414 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005415 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00005416 // The low part is just a zero extension of the input (which degenerates to
5417 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005418 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005419
Chris Lattnerdc750592005-01-07 07:47:09 +00005420 // The high part is just a zero.
5421 Hi = DAG.getConstant(0, NVT);
5422 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00005423
Chris Lattner59b27fa372007-02-13 23:55:16 +00005424 case ISD::TRUNCATE: {
5425 // The input value must be larger than this value. Expand *it*.
5426 SDOperand NewLo;
5427 ExpandOp(Node->getOperand(0), NewLo, Hi);
5428
5429 // The low part is now either the right size, or it is closer. If not the
5430 // right size, make an illegal truncate so we recursively expand it.
5431 if (NewLo.getValueType() != Node->getValueType(0))
5432 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5433 ExpandOp(NewLo, Lo, Hi);
5434 break;
5435 }
5436
Chris Lattner36e663d2005-12-23 00:16:34 +00005437 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005438 SDOperand Tmp;
5439 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5440 // If the target wants to, allow it to lower this itself.
5441 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5442 case Expand: assert(0 && "cannot expand FP!");
5443 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5444 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5445 }
5446 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5447 }
5448
Evan Cheng4eee7242006-12-09 02:42:38 +00005449 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00005450 if (VT == MVT::f32 || VT == MVT::f64) {
5451 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00005452 if (getTypeAction(NVT) == Expand)
5453 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00005454 break;
5455 }
5456
5457 // If source operand will be expanded to the same type as VT, i.e.
5458 // i64 <- f64, i32 <- f32, expand the source operand instead.
5459 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5460 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5461 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005462 break;
5463 }
5464
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005465 // Turn this into a load/store pair by default.
5466 if (Tmp.Val == 0)
Evan Cheng3432ab92006-12-11 19:27:14 +00005467 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005468
Chris Lattner36e663d2005-12-23 00:16:34 +00005469 ExpandOp(Tmp, Lo, Hi);
5470 break;
5471 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005472
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005473 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00005474 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5475 TargetLowering::Custom &&
5476 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005477 Lo = TLI.LowerOperation(Op, DAG);
5478 assert(Lo.Val && "Node must be custom expanded!");
5479 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00005480 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005481 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005482 break;
5483
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005484 // These operators cannot be expanded directly, emit them as calls to
5485 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005486 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005487 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00005488 SDOperand Op;
5489 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5490 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005491 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5492 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00005493 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005494
Chris Lattner941d84a2005-07-30 01:40:57 +00005495 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5496
Chris Lattnerfe68d752005-07-29 00:33:32 +00005497 // Now that the custom expander is done, expand the result, which is still
5498 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005499 if (Op.Val) {
5500 ExpandOp(Op, Lo, Hi);
5501 break;
5502 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005503 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005504
Dale Johannesenc0154c02007-10-05 20:04:43 +00005505 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005506 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005507 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00005508 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005509 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00005510 else if (Node->getOperand(0).getValueType() == MVT::f80)
5511 LC = RTLIB::FPTOSINT_F80_I64;
5512 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5513 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005514 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5515 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005516 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005517 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005518
Evan Cheng31cbddf2007-01-12 02:11:51 +00005519 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005520 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005521 SDOperand Op;
5522 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5523 case Expand: assert(0 && "cannot expand FP!");
5524 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5525 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5526 }
5527
5528 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5529
5530 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005531 if (Op.Val) {
5532 ExpandOp(Op, Lo, Hi);
5533 break;
5534 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005535 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005536
Evan Chengfd11ef42007-10-05 01:09:32 +00005537 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005538 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005539 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen789b5a52007-09-28 18:44:17 +00005540 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005541 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00005542 else if (Node->getOperand(0).getValueType() == MVT::f80)
5543 LC = RTLIB::FPTOUINT_F80_I64;
5544 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5545 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005546 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5547 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005548 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005549 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005550
Evan Cheng870e4f82006-01-09 18:31:59 +00005551 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005552 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005553 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005554 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005555 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005556 Op = TLI.LowerOperation(Op, DAG);
5557 if (Op.Val) {
5558 // Now that the custom expander is done, expand the result, which is
5559 // still VT.
5560 ExpandOp(Op, Lo, Hi);
5561 break;
5562 }
5563 }
5564
Chris Lattner72b503b2006-09-13 03:50:39 +00005565 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5566 // this X << 1 as X+X.
5567 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5568 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5569 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5570 SDOperand LoOps[2], HiOps[3];
5571 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5572 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5573 LoOps[1] = LoOps[0];
5574 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5575
5576 HiOps[1] = HiOps[0];
5577 HiOps[2] = Lo.getValue(1);
5578 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5579 break;
5580 }
5581 }
5582
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005583 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005584 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005585 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005586
5587 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005588 TargetLowering::LegalizeAction Action =
5589 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5590 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5591 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005592 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005593 break;
5594 }
5595
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005596 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005597 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5598 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005599 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005600 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005601
Evan Cheng870e4f82006-01-09 18:31:59 +00005602 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005603 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005604 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005605 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005606 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005607 Op = TLI.LowerOperation(Op, DAG);
5608 if (Op.Val) {
5609 // Now that the custom expander is done, expand the result, which is
5610 // still VT.
5611 ExpandOp(Op, Lo, Hi);
5612 break;
5613 }
5614 }
5615
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005616 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005617 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005618 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005619
5620 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005621 TargetLowering::LegalizeAction Action =
5622 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5623 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5624 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005625 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005626 break;
5627 }
5628
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005629 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005630 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5631 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005632 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005633 }
5634
5635 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005636 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005637 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005638 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005639 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005640 Op = TLI.LowerOperation(Op, DAG);
5641 if (Op.Val) {
5642 // Now that the custom expander is done, expand the result, which is
5643 // still VT.
5644 ExpandOp(Op, Lo, Hi);
5645 break;
5646 }
5647 }
5648
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005649 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005650 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005651 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005652
5653 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005654 TargetLowering::LegalizeAction Action =
5655 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5656 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5657 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005658 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005659 break;
5660 }
5661
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005662 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005663 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5664 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005665 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005666 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005667
Misha Brukman835702a2005-04-21 22:36:52 +00005668 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005669 case ISD::SUB: {
5670 // If the target wants to custom expand this, let them.
5671 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5672 TargetLowering::Custom) {
5673 Op = TLI.LowerOperation(Op, DAG);
5674 if (Op.Val) {
5675 ExpandOp(Op, Lo, Hi);
5676 break;
5677 }
5678 }
5679
5680 // Expand the subcomponents.
5681 SDOperand LHSL, LHSH, RHSL, RHSH;
5682 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5683 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005684 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5685 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005686 LoOps[0] = LHSL;
5687 LoOps[1] = RHSL;
5688 HiOps[0] = LHSH;
5689 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005690 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005691 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005692 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005693 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005694 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005695 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005696 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005697 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005698 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005699 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005700 }
Chris Lattner2135bc02007-05-17 18:15:41 +00005701
5702 case ISD::ADDC:
5703 case ISD::SUBC: {
5704 // Expand the subcomponents.
5705 SDOperand LHSL, LHSH, RHSL, RHSH;
5706 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5707 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5708 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5709 SDOperand LoOps[2] = { LHSL, RHSL };
5710 SDOperand HiOps[3] = { LHSH, RHSH };
5711
5712 if (Node->getOpcode() == ISD::ADDC) {
5713 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5714 HiOps[2] = Lo.getValue(1);
5715 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5716 } else {
5717 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5718 HiOps[2] = Lo.getValue(1);
5719 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5720 }
5721 // Remember that we legalized the flag.
5722 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5723 break;
5724 }
5725 case ISD::ADDE:
5726 case ISD::SUBE: {
5727 // Expand the subcomponents.
5728 SDOperand LHSL, LHSH, RHSL, RHSH;
5729 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5730 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5731 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5732 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5733 SDOperand HiOps[3] = { LHSH, RHSH };
5734
5735 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5736 HiOps[2] = Lo.getValue(1);
5737 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5738
5739 // Remember that we legalized the flag.
5740 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5741 break;
5742 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005743 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005744 // If the target wants to custom expand this, let them.
5745 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00005746 SDOperand New = TLI.LowerOperation(Op, DAG);
5747 if (New.Val) {
5748 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005749 break;
5750 }
5751 }
5752
Evan Chenge93762d2006-09-01 18:17:58 +00005753 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5754 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohmana1603612007-10-08 18:33:35 +00005755 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
5756 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
5757 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanadd0c632005-04-11 03:01:51 +00005758 SDOperand LL, LH, RL, RH;
5759 ExpandOp(Node->getOperand(0), LL, LH);
5760 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohmana1603612007-10-08 18:33:35 +00005761 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
5762 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
5763 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
5764 // FIXME: generalize this to handle other bit sizes
5765 if (LHSSB == 32 && RHSSB == 32 &&
5766 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
5767 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
5768 // The inputs are both zero-extended.
5769 if (HasUMUL_LOHI) {
5770 // We can emit a umul_lohi.
5771 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5772 Hi = SDOperand(Lo.Val, 1);
5773 break;
5774 }
5775 if (HasMULHU) {
5776 // We can emit a mulhu+mul.
5777 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5778 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5779 break;
5780 }
Chris Lattner1b633912006-09-16 00:21:44 +00005781 break;
Dan Gohmana1603612007-10-08 18:33:35 +00005782 }
5783 if (LHSSB > BitSize && RHSSB > BitSize) {
5784 // The input values are both sign-extended.
5785 if (HasSMUL_LOHI) {
5786 // We can emit a smul_lohi.
5787 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5788 Hi = SDOperand(Lo.Val, 1);
5789 break;
5790 }
5791 if (HasMULHS) {
5792 // We can emit a mulhs+mul.
5793 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5794 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
5795 break;
5796 }
5797 }
5798 if (HasUMUL_LOHI) {
5799 // Lo,Hi = umul LHS, RHS.
5800 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
5801 DAG.getVTList(NVT, NVT), LL, RL);
5802 Lo = UMulLOHI;
5803 Hi = UMulLOHI.getValue(1);
Nate Begeman43144a22005-08-30 02:44:00 +00005804 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5805 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5806 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5807 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00005808 break;
Nate Begeman43144a22005-08-30 02:44:00 +00005809 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005810 }
Evan Chenge93762d2006-09-01 18:17:58 +00005811
Dan Gohmana1603612007-10-08 18:33:35 +00005812 // If nothing else, we can make a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005813 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5814 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00005815 break;
5816 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005817 case ISD::SDIV:
5818 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5819 break;
5820 case ISD::UDIV:
5821 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5822 break;
5823 case ISD::SREM:
5824 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5825 break;
5826 case ISD::UREM:
5827 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5828 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00005829
Evan Cheng4eee7242006-12-09 02:42:38 +00005830 case ISD::FADD:
Dale Johannesenc0154c02007-10-05 20:04:43 +00005831 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::ADD_F32 :
5832 VT == MVT::f64 ? RTLIB::ADD_F64 :
5833 VT == MVT::ppcf128 ?
5834 RTLIB::ADD_PPCF128 :
5835 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00005836 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005837 break;
5838 case ISD::FSUB:
Dale Johannesenc0154c02007-10-05 20:04:43 +00005839 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::SUB_F32 :
5840 VT == MVT::f64 ? RTLIB::SUB_F64 :
5841 VT == MVT::ppcf128 ?
5842 RTLIB::SUB_PPCF128 :
5843 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00005844 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005845 break;
5846 case ISD::FMUL:
Dale Johannesenc0154c02007-10-05 20:04:43 +00005847 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::MUL_F32 :
5848 VT == MVT::f64 ? RTLIB::MUL_F64 :
5849 VT == MVT::ppcf128 ?
5850 RTLIB::MUL_PPCF128 :
5851 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00005852 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005853 break;
5854 case ISD::FDIV:
Dale Johannesenc0154c02007-10-05 20:04:43 +00005855 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::DIV_F32 :
5856 VT == MVT::f64 ? RTLIB::DIV_F64 :
5857 VT == MVT::ppcf128 ?
5858 RTLIB::DIV_PPCF128 :
5859 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00005860 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005861 break;
5862 case ISD::FP_EXTEND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005863 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005864 break;
5865 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005866 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005867 break;
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00005868 case ISD::FPOWI:
Dale Johannesen25a00a62007-09-28 01:08:20 +00005869 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) ? RTLIB::POWI_F32 :
5870 (VT == MVT::f64) ? RTLIB::POWI_F64 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00005871 (VT == MVT::f80) ? RTLIB::POWI_F80 :
5872 (VT == MVT::ppcf128) ?
5873 RTLIB::POWI_PPCF128 :
5874 RTLIB::UNKNOWN_LIBCALL),
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00005875 Node, false, Hi);
5876 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005877 case ISD::FSQRT:
5878 case ISD::FSIN:
5879 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005880 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00005881 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005882 case ISD::FSQRT:
Dale Johannesen25a00a62007-09-28 01:08:20 +00005883 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00005884 (VT == MVT::f64) ? RTLIB::SQRT_F64 :
5885 (VT == MVT::f80) ? RTLIB::SQRT_F80 :
5886 (VT == MVT::ppcf128) ? RTLIB::SQRT_PPCF128 :
5887 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005888 break;
5889 case ISD::FSIN:
5890 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5891 break;
5892 case ISD::FCOS:
5893 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5894 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005895 default: assert(0 && "Unreachable!");
5896 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005897 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00005898 break;
5899 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00005900 case ISD::FABS: {
5901 SDOperand Mask = (VT == MVT::f64)
5902 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5903 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5904 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5905 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5906 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5907 if (getTypeAction(NVT) == Expand)
5908 ExpandOp(Lo, Lo, Hi);
5909 break;
5910 }
5911 case ISD::FNEG: {
5912 SDOperand Mask = (VT == MVT::f64)
5913 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5914 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5915 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5916 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5917 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5918 if (getTypeAction(NVT) == Expand)
5919 ExpandOp(Lo, Lo, Hi);
5920 break;
5921 }
Evan Cheng003feb02007-01-04 21:56:39 +00005922 case ISD::FCOPYSIGN: {
5923 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5924 if (getTypeAction(NVT) == Expand)
5925 ExpandOp(Lo, Lo, Hi);
5926 break;
5927 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005928 case ISD::SINT_TO_FP:
5929 case ISD::UINT_TO_FP: {
5930 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5931 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng75439b32007-09-27 07:35:39 +00005932 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005933 if (Node->getOperand(0).getValueType() == MVT::i64) {
5934 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005935 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen7d67e542007-09-19 23:55:34 +00005936 else if (VT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005937 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00005938 else if (VT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00005939 assert(isSigned);
Dale Johannesenc0154c02007-10-05 20:04:43 +00005940 LC = RTLIB::SINTTOFP_I64_F80;
5941 }
5942 else if (VT == MVT::ppcf128) {
5943 assert(isSigned);
5944 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen7d67e542007-09-19 23:55:34 +00005945 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005946 } else {
5947 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005948 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005949 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005950 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005951 }
5952
5953 // Promote the operand if needed.
5954 if (getTypeAction(SrcVT) == Promote) {
5955 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5956 Tmp = isSigned
5957 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5958 DAG.getValueType(SrcVT))
5959 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5960 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5961 }
Evan Chengbf535fc2007-04-27 07:33:31 +00005962
5963 const char *LibCall = TLI.getLibcallName(LC);
5964 if (LibCall)
5965 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5966 else {
5967 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5968 Node->getOperand(0));
5969 if (getTypeAction(Lo.getValueType()) == Expand)
5970 ExpandOp(Lo, Lo, Hi);
5971 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005972 break;
5973 }
Evan Chengf3a80c62006-12-13 02:38:13 +00005974 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005975
Chris Lattnerac12f682005-12-21 18:02:52 +00005976 // Make sure the resultant values have been legalized themselves, unless this
5977 // is a type that requires multi-step expansion.
5978 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5979 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00005980 if (Hi.Val)
5981 // Don't legalize the high part if it is expanded to a single node.
5982 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00005983 }
Evan Cheng870e4f82006-01-09 18:31:59 +00005984
5985 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005986 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00005987 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00005988}
5989
Dan Gohmana8665142007-06-25 16:23:39 +00005990/// SplitVectorOp - Given an operand of vector type, break it down into
5991/// two smaller values, still of vector type.
Chris Lattner32206f52006-03-18 01:44:44 +00005992void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5993 SDOperand &Hi) {
Dan Gohmana8665142007-06-25 16:23:39 +00005994 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00005995 SDNode *Node = Op.Val;
Dan Gohman60028182007-09-24 15:54:53 +00005996 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattner32206f52006-03-18 01:44:44 +00005997 assert(NumElements > 1 && "Cannot split a single element vector!");
5998 unsigned NewNumElts = NumElements/2;
Dan Gohman60028182007-09-24 15:54:53 +00005999 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Dan Gohmana8665142007-06-25 16:23:39 +00006000 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattner32206f52006-03-18 01:44:44 +00006001
6002 // See if we already split it.
6003 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6004 = SplitNodes.find(Op);
6005 if (I != SplitNodes.end()) {
6006 Lo = I->second.first;
6007 Hi = I->second.second;
6008 return;
6009 }
6010
6011 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006012 default:
6013#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006014 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006015#endif
6016 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohmana8665142007-06-25 16:23:39 +00006017 case ISD::BUILD_PAIR:
6018 Lo = Node->getOperand(0);
6019 Hi = Node->getOperand(1);
6020 break;
Dan Gohmana90183e2007-09-28 23:53:40 +00006021 case ISD::INSERT_VECTOR_ELT: {
6022 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6023 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6024 SDOperand ScalarOp = Node->getOperand(1);
6025 if (Index < NewNumElts)
6026 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Lo, ScalarOp,
6027 DAG.getConstant(Index, TLI.getPointerTy()));
6028 else
6029 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Hi, ScalarOp,
6030 DAG.getConstant(Index - NewNumElts, TLI.getPointerTy()));
6031 break;
6032 }
Dan Gohmana8665142007-06-25 16:23:39 +00006033 case ISD::BUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006034 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6035 Node->op_begin()+NewNumElts);
Dan Gohmana8665142007-06-25 16:23:39 +00006036 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006037
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006038 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohmana8665142007-06-25 16:23:39 +00006039 Node->op_end());
6040 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006041 break;
6042 }
Dan Gohmana8665142007-06-25 16:23:39 +00006043 case ISD::CONCAT_VECTORS: {
6044 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6045 if (NewNumSubvectors == 1) {
6046 Lo = Node->getOperand(0);
6047 Hi = Node->getOperand(1);
6048 } else {
6049 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6050 Node->op_begin()+NewNumSubvectors);
6051 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00006052
Dan Gohmana8665142007-06-25 16:23:39 +00006053 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6054 Node->op_end());
6055 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
6056 }
Dan Gohman26455c42007-06-13 15:12:02 +00006057 break;
6058 }
Dan Gohmana8665142007-06-25 16:23:39 +00006059 case ISD::ADD:
6060 case ISD::SUB:
6061 case ISD::MUL:
6062 case ISD::FADD:
6063 case ISD::FSUB:
6064 case ISD::FMUL:
6065 case ISD::SDIV:
6066 case ISD::UDIV:
6067 case ISD::FDIV:
6068 case ISD::AND:
6069 case ISD::OR:
6070 case ISD::XOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00006071 SDOperand LL, LH, RL, RH;
6072 SplitVectorOp(Node->getOperand(0), LL, LH);
6073 SplitVectorOp(Node->getOperand(1), RL, RH);
6074
Dan Gohmana8665142007-06-25 16:23:39 +00006075 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
6076 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00006077 break;
6078 }
Dan Gohmana8665142007-06-25 16:23:39 +00006079 case ISD::LOAD: {
6080 LoadSDNode *LD = cast<LoadSDNode>(Node);
6081 SDOperand Ch = LD->getChain();
6082 SDOperand Ptr = LD->getBasePtr();
6083 const Value *SV = LD->getSrcValue();
6084 int SVOffset = LD->getSrcValueOffset();
6085 unsigned Alignment = LD->getAlignment();
6086 bool isVolatile = LD->isVolatile();
6087
6088 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6089 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattner32206f52006-03-18 01:44:44 +00006090 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6091 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00006092 SVOffset += IncrementSize;
6093 if (Alignment > IncrementSize)
6094 Alignment = IncrementSize;
6095 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00006096
6097 // Build a factor node to remember that this load is independent of the
6098 // other one.
6099 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6100 Hi.getValue(1));
6101
6102 // Remember that we legalized the chain.
6103 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00006104 break;
6105 }
Dan Gohmana8665142007-06-25 16:23:39 +00006106 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006107 // We know the result is a vector. The input may be either a vector or a
6108 // scalar value.
Dan Gohman0de76942007-06-29 00:09:08 +00006109 SDOperand InOp = Node->getOperand(0);
6110 if (!MVT::isVector(InOp.getValueType()) ||
6111 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6112 // The input is a scalar or single-element vector.
6113 // Lower to a store/load so that it can be split.
6114 // FIXME: this could be improved probably.
6115 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006116
Evan Chengdf9ac472006-10-05 23:01:46 +00006117 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman0de76942007-06-29 00:09:08 +00006118 InOp, Ptr, NULL, 0);
6119 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006120 }
Dan Gohman0de76942007-06-29 00:09:08 +00006121 // Split the vector and convert each of the pieces now.
6122 SplitVectorOp(InOp, Lo, Hi);
6123 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
6124 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
6125 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006126 }
Chris Lattner32206f52006-03-18 01:44:44 +00006127 }
6128
6129 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006130 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00006131 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00006132 assert(isNew && "Value already split?!?");
Chris Lattner32206f52006-03-18 01:44:44 +00006133}
6134
6135
Dan Gohmanf4e86da2007-06-27 14:06:22 +00006136/// ScalarizeVectorOp - Given an operand of single-element vector type
6137/// (e.g. v1f32), convert it into the equivalent operation that returns a
6138/// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +00006139SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6140 assert(MVT::isVector(Op.getValueType()) &&
6141 "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00006142 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00006143 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6144 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00006145
Dan Gohmana8665142007-06-25 16:23:39 +00006146 // See if we already scalarized it.
6147 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6148 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00006149
6150 SDOperand Result;
6151 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00006152 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006153#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006154 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006155#endif
Dan Gohmana8665142007-06-25 16:23:39 +00006156 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6157 case ISD::ADD:
6158 case ISD::FADD:
6159 case ISD::SUB:
6160 case ISD::FSUB:
6161 case ISD::MUL:
6162 case ISD::FMUL:
6163 case ISD::SDIV:
6164 case ISD::UDIV:
6165 case ISD::FDIV:
6166 case ISD::SREM:
6167 case ISD::UREM:
6168 case ISD::FREM:
6169 case ISD::AND:
6170 case ISD::OR:
6171 case ISD::XOR:
6172 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00006173 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00006174 ScalarizeVectorOp(Node->getOperand(0)),
6175 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00006176 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006177 case ISD::FNEG:
6178 case ISD::FABS:
6179 case ISD::FSQRT:
6180 case ISD::FSIN:
6181 case ISD::FCOS:
6182 Result = DAG.getNode(Node->getOpcode(),
6183 NewVT,
6184 ScalarizeVectorOp(Node->getOperand(0)));
6185 break;
6186 case ISD::LOAD: {
6187 LoadSDNode *LD = cast<LoadSDNode>(Node);
6188 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6189 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00006190
Dan Gohmana8665142007-06-25 16:23:39 +00006191 const Value *SV = LD->getSrcValue();
6192 int SVOffset = LD->getSrcValueOffset();
6193 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6194 LD->isVolatile(), LD->getAlignment());
6195
Chris Lattner32206f52006-03-18 01:44:44 +00006196 // Remember that we legalized the chain.
6197 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6198 break;
6199 }
Dan Gohmana8665142007-06-25 16:23:39 +00006200 case ISD::BUILD_VECTOR:
6201 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00006202 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006203 case ISD::INSERT_VECTOR_ELT:
6204 // Returning the inserted scalar element.
6205 Result = Node->getOperand(1);
6206 break;
6207 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00006208 assert(Node->getOperand(0).getValueType() == NewVT &&
6209 "Concat of non-legal vectors not yet supported!");
6210 Result = Node->getOperand(0);
6211 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006212 case ISD::VECTOR_SHUFFLE: {
6213 // Figure out if the scalar is the LHS or RHS and return it.
6214 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6215 if (cast<ConstantSDNode>(EltNum)->getValue())
6216 Result = ScalarizeVectorOp(Node->getOperand(1));
6217 else
6218 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00006219 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006220 }
6221 case ISD::EXTRACT_SUBVECTOR:
6222 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00006223 assert(Result.getValueType() == NewVT);
6224 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006225 case ISD::BIT_CONVERT:
6226 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattnerf6f94d32006-03-28 20:24:43 +00006227 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006228 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00006229 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00006230 ScalarizeVectorOp(Op.getOperand(1)),
6231 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00006232 break;
Chris Lattner32206f52006-03-18 01:44:44 +00006233 }
6234
6235 if (TLI.isTypeLegal(NewVT))
6236 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00006237 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6238 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00006239 return Result;
6240}
6241
Chris Lattnerdc750592005-01-07 07:47:09 +00006242
6243// SelectionDAG::Legalize - This is the entry point for the file.
6244//
Chris Lattner4add7e32005-01-23 04:42:50 +00006245void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00006246 if (ViewLegalizeDAGs) viewGraph();
6247
Chris Lattnerdc750592005-01-07 07:47:09 +00006248 /// run - This is the main entry point to this class.
6249 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00006250 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00006251}
6252