blob: 8e80121ab3c0840085dedf4c16e888ddf5f8fdf4 [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"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Evan Cheng84a28d42006-10-30 08:00:44 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000022#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000023#include "llvm/Constants.h"
Reid Spencera94d3942007-01-19 21:13:56 +000024#include "llvm/DerivedTypes.h"
Chris Lattneref598052006-04-02 03:07:27 +000025#include "llvm/Support/MathExtras.h"
26#include "llvm/Support/CommandLine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Chris Lattnere83030b2007-02-03 01:12:36 +000028#include "llvm/ADT/DenseMap.h"
Chris Lattner97af9d52006-08-08 01:09:31 +000029#include "llvm/ADT/SmallVector.h"
Chris Lattnerebeb48d2007-02-04 00:27:56 +000030#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng1d2e9952006-03-24 01:17:21 +000031#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000032using namespace llvm;
33
Chris Lattneref598052006-04-02 03:07:27 +000034#ifndef NDEBUG
35static cl::opt<bool>
36ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
37 cl::desc("Pop up a window to show dags before legalize"));
38#else
39static const bool ViewLegalizeDAGs = 0;
40#endif
41
Chris Lattnered39c862007-02-04 00:50:02 +000042namespace llvm {
43template<>
44struct DenseMapKeyInfo<SDOperand> {
45 static inline SDOperand getEmptyKey() { return SDOperand((SDNode*)-1, -1U); }
46 static inline SDOperand getTombstoneKey() { return SDOperand((SDNode*)-1, 0);}
47 static unsigned getHashValue(const SDOperand &Val) {
48 return DenseMapKeyInfo<void*>::getHashValue(Val.Val) + Val.ResNo;
49 }
50 static bool isPod() { return true; }
51};
52}
53
Chris Lattnerdc750592005-01-07 07:47:09 +000054//===----------------------------------------------------------------------===//
55/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
56/// hacks on it until the target machine can handle it. This involves
57/// eliminating value sizes the machine cannot handle (promoting small sizes to
58/// large sizes or splitting up large values into small values) as well as
59/// eliminating operations the machine cannot handle.
60///
61/// This code also does a small amount of optimization and recognition of idioms
62/// as part of its processing. For example, if a target does not support a
63/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
64/// will attempt merge setcc and brc instructions into brcc's.
65///
66namespace {
Chris Lattner54a34cd2006-06-28 21:58:30 +000067class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattnerdc750592005-01-07 07:47:09 +000068 TargetLowering &TLI;
69 SelectionDAG &DAG;
70
Chris Lattner462505f2006-02-13 09:18:02 +000071 // Libcall insertion helpers.
72
73 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
74 /// legalized. We use this to ensure that calls are properly serialized
75 /// against each other, including inserted libcalls.
76 SDOperand LastCALLSEQ_END;
77
78 /// IsLegalizingCall - This member is used *only* for purposes of providing
79 /// helpful assertions that a libcall isn't created while another call is
80 /// being legalized (which could lead to non-serialized call sequences).
81 bool IsLegalizingCall;
82
Chris Lattnerdc750592005-01-07 07:47:09 +000083 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000084 Legal, // The target natively supports this operation.
85 Promote, // This operation should be executed in a larger type.
Chris Lattneraa2372562006-05-24 17:04:05 +000086 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000087 };
Chris Lattner462505f2006-02-13 09:18:02 +000088
Chris Lattnerdc750592005-01-07 07:47:09 +000089 /// ValueTypeActions - This is a bitvector that contains two bits for each
90 /// value type, where the two bits correspond to the LegalizeAction enum.
91 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000092 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000093
Chris Lattnerdc750592005-01-07 07:47:09 +000094 /// LegalizedNodes - For nodes that are of legal width, and that have more
95 /// than one use, this map indicates what regularized operand to use. This
96 /// allows us to avoid legalizing the same thing more than once.
Chris Lattnered39c862007-02-04 00:50:02 +000097 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000098
Chris Lattner1f2c9d82005-01-15 05:21:40 +000099 /// PromotedNodes - For nodes that are below legal width, and that have more
100 /// than one use, this map indicates what promoted value to use. This allows
101 /// us to avoid promoting the same thing more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +0000102 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000103
Chris Lattner32206f52006-03-18 01:44:44 +0000104 /// ExpandedNodes - For nodes that need to be expanded this map indicates
105 /// which which operands are the expanded version of the input. This allows
106 /// us to avoid expanding the same node more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +0000107 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +0000108
Chris Lattner32206f52006-03-18 01:44:44 +0000109 /// SplitNodes - For vector nodes that need to be split, this map indicates
110 /// which which operands are the split version of the input. This allows us
111 /// to avoid splitting the same node more than once.
112 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
113
114 /// PackedNodes - For nodes that need to be packed from MVT::Vector types to
115 /// concrete packed types, this contains the mapping of ones we have already
116 /// processed to the result.
117 std::map<SDOperand, SDOperand> PackedNodes;
118
Chris Lattnerea4ca942005-01-07 22:28:47 +0000119 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +0000120 LegalizedNodes.insert(std::make_pair(From, To));
121 // If someone requests legalization of the new node, return itself.
122 if (From != To)
123 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000124 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000125 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner4b0ddb22007-02-04 01:17:38 +0000126 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000127 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +0000128 // If someone requests legalization of the new node, return itself.
129 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000130 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000131
Chris Lattnerdc750592005-01-07 07:47:09 +0000132public:
133
Chris Lattner4add7e32005-01-23 04:42:50 +0000134 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000135
Chris Lattnerdc750592005-01-07 07:47:09 +0000136 /// getTypeAction - Return how we should legalize values of this type, either
137 /// it is already legal or we need to expand it into multiple registers of
138 /// smaller integer type, or we need to promote it to a larger type.
139 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +0000140 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +0000141 }
142
143 /// isTypeLegal - Return true if this type is legal on this target.
144 ///
145 bool isTypeLegal(MVT::ValueType VT) const {
146 return getTypeAction(VT) == Legal;
147 }
148
Chris Lattnerdc750592005-01-07 07:47:09 +0000149 void LegalizeDAG();
150
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000151private:
Chris Lattner32206f52006-03-18 01:44:44 +0000152 /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
153 /// appropriate for its type.
154 void HandleOp(SDOperand Op);
155
156 /// LegalizeOp - We know that the specified value has a legal type.
157 /// Recursively ensure that the operands have legal types, then return the
158 /// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000159 SDOperand LegalizeOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000160
161 /// PromoteOp - Given an operation that produces a value in an invalid type,
162 /// promote it to compute the value into a larger type. The produced value
163 /// will have the correct bits for the low portion of the register, but no
164 /// guarantee is made about the top bits: it may be zero, sign-extended, or
165 /// garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000166 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000167
Chris Lattner32206f52006-03-18 01:44:44 +0000168 /// ExpandOp - Expand the specified SDOperand into its two component pieces
169 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
170 /// the LegalizeNodes map is filled in for any results that are not expanded,
171 /// the ExpandedNodes map is filled in for any results that are expanded, and
172 /// the Lo/Hi values are returned. This applies to integer types and Vector
173 /// types.
174 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
175
176 /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
177 /// two smaller values of MVT::Vector type.
178 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
179
180 /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
181 /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When
182 /// this is called, we know that PackedVT is the right type for the result and
183 /// we know that this type is legal for the target.
184 SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT);
185
Chris Lattner6be79822006-04-04 17:23:26 +0000186 /// isShuffleLegal - Return true if a vector shuffle is legal with the
187 /// specified mask and type. Targets can specify exactly which masks they
188 /// support and the code generator is tasked with not creating illegal masks.
189 ///
190 /// Note that this will also return true for shuffles that are promoted to a
191 /// different type.
192 ///
193 /// If this is a legal shuffle, this method returns the (possibly promoted)
194 /// build_vector Mask. If it's not a legal shuffle, it returns null.
195 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
196
Chris Lattner4488f0c2006-07-26 23:55:56 +0000197 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000198 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000199
Nate Begeman7e7f4392006-02-01 07:19:44 +0000200 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
201
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000202 SDOperand CreateStackTemporary(MVT::ValueType VT);
203
Reid Spencere63b6512006-12-31 05:55:36 +0000204 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattneraac464e2005-01-21 06:05:23 +0000205 SDOperand &Hi);
206 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
207 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000208
Chris Lattner36e663d2005-12-23 00:16:34 +0000209 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000210 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner6be79822006-04-04 17:23:26 +0000211 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000212 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
213 SDOperand LegalOp,
214 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000215 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
216 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000217 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
218 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000219
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000220 SDOperand ExpandBSWAP(SDOperand Op);
221 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000222 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
223 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000224 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
225 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000226
Chris Lattner6f423252006-03-31 17:55:51 +0000227 SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner42a5fca2006-04-02 05:06:04 +0000228 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner6f423252006-03-31 17:55:51 +0000229
Chris Lattnerdc750592005-01-07 07:47:09 +0000230 SDOperand getIntPtrConstant(uint64_t Val) {
231 return DAG.getConstant(Val, TLI.getPointerTy());
232 }
233};
234}
235
Chris Lattner6be79822006-04-04 17:23:26 +0000236/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
237/// specified mask and type. Targets can specify exactly which masks they
238/// support and the code generator is tasked with not creating illegal masks.
239///
240/// Note that this will also return true for shuffles that are promoted to a
241/// different type.
242SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
243 SDOperand Mask) const {
244 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
245 default: return 0;
246 case TargetLowering::Legal:
247 case TargetLowering::Custom:
248 break;
249 case TargetLowering::Promote: {
250 // If this is promoted to a different type, convert the shuffle mask and
251 // ask if it is legal in the promoted type!
252 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
253
254 // If we changed # elements, change the shuffle mask.
255 unsigned NumEltsGrowth =
256 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
257 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
258 if (NumEltsGrowth > 1) {
259 // Renumber the elements.
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000260 SmallVector<SDOperand, 8> Ops;
Chris Lattner6be79822006-04-04 17:23:26 +0000261 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
262 SDOperand InOp = Mask.getOperand(i);
263 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
264 if (InOp.getOpcode() == ISD::UNDEF)
265 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
266 else {
267 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
268 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
269 }
270 }
271 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000272 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +0000273 }
274 VT = NVT;
275 break;
276 }
277 }
278 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
279}
280
Chris Lattner32206f52006-03-18 01:44:44 +0000281/// getScalarizedOpcode - Return the scalar opcode that corresponds to the
282/// specified vector opcode.
Chris Lattner301015a2005-11-19 05:51:46 +0000283static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000284 switch (VecOp) {
285 default: assert(0 && "Don't know how to scalarize this opcode!");
Evan Cheng3bf916d2006-03-03 07:01:07 +0000286 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
287 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
288 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
289 case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV;
290 case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV;
291 case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0;
292 case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0;
293 case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0;
Nate Begemanb2e089c2005-11-19 00:36:38 +0000294 }
295}
Chris Lattnerdc750592005-01-07 07:47:09 +0000296
Chris Lattner4add7e32005-01-23 04:42:50 +0000297SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
298 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
299 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000300 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000301 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000302}
303
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000304/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
305/// not been visited yet and if all of its operands have already been visited.
Chris Lattner94c44c92007-02-04 01:20:02 +0000306static void ComputeTopDownOrdering(SDNode *N, SmallVector<SDNode*, 64> &Order,
Chris Lattnere83030b2007-02-03 01:12:36 +0000307 DenseMap<SDNode*, unsigned> &Visited) {
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000308 if (++Visited[N] != N->getNumOperands())
309 return; // Haven't visited all operands yet
310
311 Order.push_back(N);
312
313 if (N->hasOneUse()) { // Tail recurse in common case.
314 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
315 return;
316 }
317
318 // Now that we have N in, add anything that uses it if all of their operands
319 // are now done.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000320 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
321 ComputeTopDownOrdering(*UI, Order, Visited);
322}
323
Chris Lattner44fe26f2005-07-29 00:11:56 +0000324
Chris Lattnerdc750592005-01-07 07:47:09 +0000325void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000326 LastCALLSEQ_END = DAG.getEntryNode();
327 IsLegalizingCall = false;
328
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000329 // The legalize process is inherently a bottom-up recursive process (users
330 // legalize their uses before themselves). Given infinite stack space, we
331 // could just start legalizing on the root and traverse the whole graph. In
332 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000333 // blocks. To avoid this problem, compute an ordering of the nodes where each
334 // node is only legalized after all of its operands are legalized.
Chris Lattnere83030b2007-02-03 01:12:36 +0000335 DenseMap<SDNode*, unsigned> Visited;
Chris Lattner94c44c92007-02-04 01:20:02 +0000336 SmallVector<SDNode*, 64> Order;
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000337
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000338 // Compute ordering from all of the leaves in the graphs, those (like the
339 // entry node) that have no operands.
340 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
341 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000342 if (I->getNumOperands() == 0) {
343 Visited[I] = 0 - 1U;
344 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000345 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000346 }
347
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000348 assert(Order.size() == Visited.size() &&
349 Order.size() ==
350 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000351 "Error: DAG is cyclic!");
352 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000353
Chris Lattner32206f52006-03-18 01:44:44 +0000354 for (unsigned i = 0, e = Order.size(); i != e; ++i)
355 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000356
357 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000358 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000359 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
360 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000361
362 ExpandedNodes.clear();
363 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000364 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000365 SplitNodes.clear();
366 PackedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000367
368 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000369 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000370}
371
Chris Lattner462505f2006-02-13 09:18:02 +0000372
373/// FindCallEndFromCallStart - Given a chained node that is part of a call
374/// sequence, find the CALLSEQ_END node that terminates the call sequence.
375static SDNode *FindCallEndFromCallStart(SDNode *Node) {
376 if (Node->getOpcode() == ISD::CALLSEQ_END)
377 return Node;
378 if (Node->use_empty())
379 return 0; // No CallSeqEnd
380
381 // The chain is usually at the end.
382 SDOperand TheChain(Node, Node->getNumValues()-1);
383 if (TheChain.getValueType() != MVT::Other) {
384 // Sometimes it's at the beginning.
385 TheChain = SDOperand(Node, 0);
386 if (TheChain.getValueType() != MVT::Other) {
387 // Otherwise, hunt for it.
388 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
389 if (Node->getValueType(i) == MVT::Other) {
390 TheChain = SDOperand(Node, i);
391 break;
392 }
393
394 // Otherwise, we walked into a node without a chain.
395 if (TheChain.getValueType() != MVT::Other)
396 return 0;
397 }
398 }
399
400 for (SDNode::use_iterator UI = Node->use_begin(),
401 E = Node->use_end(); UI != E; ++UI) {
402
403 // Make sure to only follow users of our token chain.
404 SDNode *User = *UI;
405 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
406 if (User->getOperand(i) == TheChain)
407 if (SDNode *Result = FindCallEndFromCallStart(User))
408 return Result;
409 }
410 return 0;
411}
412
413/// FindCallStartFromCallEnd - Given a chained node that is part of a call
414/// sequence, find the CALLSEQ_START node that initiates the call sequence.
415static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
416 assert(Node && "Didn't find callseq_start for a call??");
417 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
418
419 assert(Node->getOperand(0).getValueType() == MVT::Other &&
420 "Node doesn't have a token chain argument!");
421 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
422}
423
424/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
425/// see if any uses can reach Dest. If no dest operands can get to dest,
426/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000427///
428/// Keep track of the nodes we fine that actually do lead to Dest in
429/// NodesLeadingTo. This avoids retraversing them exponential number of times.
430///
431bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000432 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000433 if (N == Dest) return true; // N certainly leads to Dest :)
434
Chris Lattner4488f0c2006-07-26 23:55:56 +0000435 // If we've already processed this node and it does lead to Dest, there is no
436 // need to reprocess it.
437 if (NodesLeadingTo.count(N)) return true;
438
Chris Lattner462505f2006-02-13 09:18:02 +0000439 // If the first result of this node has been already legalized, then it cannot
440 // reach N.
441 switch (getTypeAction(N->getValueType(0))) {
442 case Legal:
443 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
444 break;
445 case Promote:
446 if (PromotedNodes.count(SDOperand(N, 0))) return false;
447 break;
448 case Expand:
449 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
450 break;
451 }
452
453 // Okay, this node has not already been legalized. Check and legalize all
454 // operands. If none lead to Dest, then we can legalize this node.
455 bool OperandsLeadToDest = false;
456 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
457 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000458 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000459
Chris Lattner4488f0c2006-07-26 23:55:56 +0000460 if (OperandsLeadToDest) {
461 NodesLeadingTo.insert(N);
462 return true;
463 }
Chris Lattner462505f2006-02-13 09:18:02 +0000464
465 // Okay, this node looks safe, legalize it and return false.
Chris Lattner916ae072006-04-17 22:10:08 +0000466 HandleOp(SDOperand(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000467 return false;
468}
469
Chris Lattner32206f52006-03-18 01:44:44 +0000470/// HandleOp - Legalize, Promote, Expand or Pack the specified operand as
471/// appropriate for its type.
472void SelectionDAGLegalize::HandleOp(SDOperand Op) {
473 switch (getTypeAction(Op.getValueType())) {
474 default: assert(0 && "Bad type action!");
475 case Legal: LegalizeOp(Op); break;
476 case Promote: PromoteOp(Op); break;
477 case Expand:
478 if (Op.getValueType() != MVT::Vector) {
479 SDOperand X, Y;
480 ExpandOp(Op, X, Y);
481 } else {
482 SDNode *N = Op.Val;
483 unsigned NumOps = N->getNumOperands();
484 unsigned NumElements =
485 cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue();
486 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT();
487 MVT::ValueType PackedVT = getVectorType(EVT, NumElements);
488 if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) {
489 // In the common case, this is a legal vector type, convert it to the
490 // packed operation and type now.
491 PackVectorOp(Op, PackedVT);
492 } else if (NumElements == 1) {
493 // Otherwise, if this is a single element vector, convert it to a
494 // scalar operation.
495 PackVectorOp(Op, EVT);
496 } else {
497 // Otherwise, this is a multiple element vector that isn't supported.
498 // Split it in half and legalize both parts.
499 SDOperand X, Y;
Chris Lattner93640542006-03-19 00:07:49 +0000500 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000501 }
502 }
503 break;
504 }
505}
Chris Lattner462505f2006-02-13 09:18:02 +0000506
Evan Cheng22cf8992006-12-13 20:57:08 +0000507/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
508/// a load from the constant pool.
509static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000510 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000511 bool Extend = false;
512
513 // If a FP immediate is precise when represented as a float and if the
514 // target can do an extending load from float to double, we put it into
515 // the constant pool as a float, even if it's is statically typed as a
516 // double.
517 MVT::ValueType VT = CFP->getValueType(0);
518 bool isDouble = VT == MVT::f64;
519 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
520 Type::FloatTy, CFP->getValue());
Evan Cheng22cf8992006-12-13 20:57:08 +0000521 if (!UseCP) {
Evan Cheng3766fc602006-12-12 22:19:28 +0000522 double Val = LLVMC->getValue();
523 return isDouble
524 ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
525 : DAG.getConstant(FloatToBits(Val), MVT::i32);
526 }
527
Evan Cheng47833a12006-12-12 21:32:44 +0000528 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
529 // Only do this if the target has a native EXTLOAD instruction from f32.
530 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
531 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
532 VT = MVT::f32;
533 Extend = true;
534 }
535
536 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
537 if (Extend) {
538 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
539 CPIdx, NULL, 0, MVT::f32);
540 } else {
541 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
542 }
543}
544
Chris Lattner462505f2006-02-13 09:18:02 +0000545
Evan Cheng003feb02007-01-04 21:56:39 +0000546/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
547/// operations.
548static
549SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
550 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng3b841dd2007-01-05 21:31:51 +0000551 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000552 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
553 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000554
Evan Cheng003feb02007-01-04 21:56:39 +0000555 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000556 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000557 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
558 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000559 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000560 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000561 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000562 // Shift right or sign-extend it if the two operands have different types.
563 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
564 if (SizeDiff > 0) {
565 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
566 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
567 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
568 } else if (SizeDiff < 0)
569 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000570
571 // Clear the sign bit of first operand.
572 SDOperand Mask2 = (VT == MVT::f64)
573 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
574 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
575 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng003feb02007-01-04 21:56:39 +0000576 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000577 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
578
579 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000580 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
581 return Result;
582}
583
584
Chris Lattner32206f52006-03-18 01:44:44 +0000585/// LegalizeOp - We know that the specified value has a legal type.
586/// Recursively ensure that the operands have legal types, then return the
587/// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000588SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000589 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000590 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000591 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000592
Chris Lattnerdc750592005-01-07 07:47:09 +0000593 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000594 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000595 if (Node->getNumValues() > 1) {
596 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000597 if (getTypeAction(Node->getValueType(i)) != Legal) {
598 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000599 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000600 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000601 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000602 }
603 }
604
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000605 // Note that LegalizeOp may be reentered even from single-use nodes, which
606 // means that we always must cache transformed nodes.
Chris Lattnered39c862007-02-04 00:50:02 +0000607 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000608 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000609
Nate Begemane5b86d72005-08-10 20:51:12 +0000610 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000611 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000612 bool isCustom = false;
613
Chris Lattnerdc750592005-01-07 07:47:09 +0000614 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000615 case ISD::FrameIndex:
616 case ISD::EntryToken:
617 case ISD::Register:
618 case ISD::BasicBlock:
619 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000620 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000621 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000622 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000623 case ISD::TargetConstantPool:
624 case ISD::TargetGlobalAddress:
625 case ISD::TargetExternalSymbol:
626 case ISD::VALUETYPE:
627 case ISD::SRCVALUE:
628 case ISD::STRING:
629 case ISD::CONDCODE:
Andrew Lenhartha6bbf332006-10-11 04:29:42 +0000630 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnereb637512006-01-28 08:31:04 +0000631 // Primitives must all be legal.
632 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
633 "This must be legal!");
634 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000635 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000636 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
637 // If this is a target node, legalize it by legalizing the operands then
638 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000639 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000640 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000641 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000642
Chris Lattner97af9d52006-08-08 01:09:31 +0000643 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000644
645 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
646 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
647 return Result.getValue(Op.ResNo);
648 }
649 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000650#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +0000651 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000652#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000653 assert(0 && "Do not know how to legalize this operator!");
654 abort();
Chris Lattnerdc750592005-01-07 07:47:09 +0000655 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000656 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000657 case ISD::ConstantPool:
658 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000659 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
660 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000661 case TargetLowering::Custom:
662 Tmp1 = TLI.LowerOperation(Op, DAG);
663 if (Tmp1.Val) Result = Tmp1;
664 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000665 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000666 break;
667 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000668 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000669 case ISD::FRAMEADDR:
670 case ISD::RETURNADDR:
671 // The only option for these nodes is to custom lower them. If the target
672 // does not custom lower them, then return zero.
673 Tmp1 = TLI.LowerOperation(Op, DAG);
674 if (Tmp1.Val)
675 Result = Tmp1;
676 else
677 Result = DAG.getConstant(0, TLI.getPointerTy());
678 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000679 case ISD::AssertSext:
680 case ISD::AssertZext:
681 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000682 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000683 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000684 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000685 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000686 Result = Node->getOperand(Op.ResNo);
687 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000688 case ISD::CopyFromReg:
689 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000690 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000691 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000692 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000693 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000694 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000695 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000696 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000697 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
698 } else {
699 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
700 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000701 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
702 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000703 // Since CopyFromReg produces two values, make sure to remember that we
704 // legalized both of them.
705 AddLegalizedOperand(Op.getValue(0), Result);
706 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
707 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000708 case ISD::UNDEF: {
709 MVT::ValueType VT = Op.getValueType();
710 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000711 default: assert(0 && "This action is not supported yet!");
712 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000713 if (MVT::isInteger(VT))
714 Result = DAG.getConstant(0, VT);
715 else if (MVT::isFloatingPoint(VT))
716 Result = DAG.getConstantFP(0, VT);
717 else
718 assert(0 && "Unknown value type!");
719 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000720 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000721 break;
722 }
723 break;
724 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000725
Chris Lattnere55d1712006-03-28 00:40:33 +0000726 case ISD::INTRINSIC_W_CHAIN:
727 case ISD::INTRINSIC_WO_CHAIN:
728 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +0000729 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +0000730 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
731 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000732 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +0000733
734 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000735 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000736 TargetLowering::Custom) {
737 Tmp3 = TLI.LowerOperation(Result, DAG);
738 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000739 }
740
741 if (Result.Val->getNumValues() == 1) break;
742
743 // Must have return value and chain result.
744 assert(Result.Val->getNumValues() == 2 &&
745 "Cannot return more than two values!");
746
747 // Since loads produce two values, make sure to remember that we
748 // legalized both of them.
749 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
750 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
751 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000752 }
Chris Lattner435b4022005-11-29 06:21:05 +0000753
754 case ISD::LOCATION:
755 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
756 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
757
758 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
759 case TargetLowering::Promote:
760 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000761 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +0000762 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000763 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +0000764 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000765
Jim Laskeyc56315c2007-01-26 21:22:28 +0000766 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000767 const std::string &FName =
768 cast<StringSDNode>(Node->getOperand(3))->getValue();
769 const std::string &DirName =
770 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000771 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000772
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000773 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +0000774 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000775 SDOperand LineOp = Node->getOperand(1);
776 SDOperand ColOp = Node->getOperand(2);
777
778 if (useDEBUG_LOC) {
779 Ops.push_back(LineOp); // line #
780 Ops.push_back(ColOp); // col #
781 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000782 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000783 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000784 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
785 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000786 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000787 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskeyf9e54452007-01-26 14:34:52 +0000788 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000789 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000790 } else {
791 Result = Tmp1; // chain
792 }
Chris Lattner435b4022005-11-29 06:21:05 +0000793 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000794 }
Chris Lattner435b4022005-11-29 06:21:05 +0000795 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000796 if (Tmp1 != Node->getOperand(0) ||
797 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +0000798 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +0000799 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000800 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
801 Ops.push_back(Node->getOperand(1)); // line # must be legal.
802 Ops.push_back(Node->getOperand(2)); // col # must be legal.
803 } else {
804 // Otherwise promote them.
805 Ops.push_back(PromoteOp(Node->getOperand(1)));
806 Ops.push_back(PromoteOp(Node->getOperand(2)));
807 }
Chris Lattner435b4022005-11-29 06:21:05 +0000808 Ops.push_back(Node->getOperand(3)); // filename must be legal.
809 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +0000810 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +0000811 }
812 break;
813 }
814 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000815
816 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000817 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000818 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000819 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000820 case TargetLowering::Legal:
821 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
822 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
823 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
824 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000825 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000826 break;
827 }
828 break;
829
Jim Laskeyf9e54452007-01-26 14:34:52 +0000830 case ISD::LABEL:
831 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
832 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000833 default: assert(0 && "This action is not supported yet!");
834 case TargetLowering::Legal:
835 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
836 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000837 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000838 break;
839 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000840 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000841
Chris Lattnerdc750592005-01-07 07:47:09 +0000842 case ISD::Constant:
843 // We know we don't need to expand constants here, constants only have one
844 // value and we check that it is fine above.
845
846 // FIXME: Maybe we should handle things like targets that don't support full
847 // 32-bit immediates?
848 break;
849 case ISD::ConstantFP: {
850 // Spill FP immediates to the constant pool if the target cannot directly
851 // codegen them. Targets often have some immediate values that can be
852 // efficiently generated into an FP register without a load. We explicitly
853 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000854 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
855
856 // Check to see if this FP immediate is already legal.
857 bool isLegal = false;
858 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
859 E = TLI.legal_fpimm_end(); I != E; ++I)
860 if (CFP->isExactlyValue(*I)) {
861 isLegal = true;
862 break;
863 }
864
Chris Lattner758b0ac2006-01-29 06:26:56 +0000865 // If this is a legal constant, turn it into a TargetConstantFP node.
866 if (isLegal) {
867 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
868 break;
869 }
870
871 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
872 default: assert(0 && "This action is not supported yet!");
873 case TargetLowering::Custom:
874 Tmp3 = TLI.LowerOperation(Result, DAG);
875 if (Tmp3.Val) {
876 Result = Tmp3;
877 break;
878 }
879 // FALLTHROUGH
880 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +0000881 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +0000882 }
883 break;
884 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000885 case ISD::TokenFactor:
886 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000887 Tmp1 = LegalizeOp(Node->getOperand(0));
888 Tmp2 = LegalizeOp(Node->getOperand(1));
889 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
890 } else if (Node->getNumOperands() == 3) {
891 Tmp1 = LegalizeOp(Node->getOperand(0));
892 Tmp2 = LegalizeOp(Node->getOperand(1));
893 Tmp3 = LegalizeOp(Node->getOperand(2));
894 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000895 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +0000896 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000897 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000898 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
899 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000900 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +0000901 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000902 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000903
904 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +0000905 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000906 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +0000907 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
908 assert(Tmp3.Val && "Target didn't custom lower this node!");
909 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
910 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000911
Chris Lattneraaa23d92006-05-16 22:53:20 +0000912 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000913 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +0000914 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
915 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000916 if (Op.ResNo == i)
917 Tmp2 = Tmp1;
918 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
919 }
920 return Tmp2;
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000921
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000922 case ISD::BUILD_VECTOR:
923 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000924 default: assert(0 && "This action is not supported yet!");
925 case TargetLowering::Custom:
926 Tmp3 = TLI.LowerOperation(Result, DAG);
927 if (Tmp3.Val) {
928 Result = Tmp3;
929 break;
930 }
931 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000932 case TargetLowering::Expand:
933 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000934 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000935 }
Chris Lattner29b23012006-03-19 01:17:20 +0000936 break;
937 case ISD::INSERT_VECTOR_ELT:
938 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
939 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
940 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
941 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
942
943 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
944 Node->getValueType(0))) {
945 default: assert(0 && "This action is not supported yet!");
946 case TargetLowering::Legal:
947 break;
948 case TargetLowering::Custom:
949 Tmp3 = TLI.LowerOperation(Result, DAG);
950 if (Tmp3.Val) {
951 Result = Tmp3;
952 break;
953 }
954 // FALLTHROUGH
955 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +0000956 // If the insert index is a constant, codegen this as a scalar_to_vector,
957 // then a shuffle that inserts it into the right position in the vector.
958 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
959 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
960 Tmp1.getValueType(), Tmp2);
961
962 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
963 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
964 MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT);
965
966 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
967 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
968 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000969 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +0000970 for (unsigned i = 0; i != NumElts; ++i) {
971 if (i != InsertPos->getValue())
972 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
973 else
974 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
975 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000976 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
977 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +0000978
979 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
980 Tmp1, ScVec, ShufMask);
981 Result = LegalizeOp(Result);
982 break;
983 }
984
Chris Lattner29b23012006-03-19 01:17:20 +0000985 // If the target doesn't support this, we have to spill the input vector
986 // to a temporary stack slot, update the element, then reload it. This is
987 // badness. We could also load the value into a vector register (either
988 // with a "move to register" or "extload into register" instruction, then
989 // permute it into place, if the idx is a constant and if the idx is
990 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +0000991 MVT::ValueType VT = Tmp1.getValueType();
992 MVT::ValueType EltVT = Tmp2.getValueType();
993 MVT::ValueType IdxVT = Tmp3.getValueType();
994 MVT::ValueType PtrVT = TLI.getPointerTy();
995 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +0000996 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +0000997 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +0000998
999 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001000 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1001 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001002 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001003 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1004 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1005 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001006 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +00001007 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001008 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +00001009 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +00001010 break;
1011 }
1012 }
1013 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001014 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001015 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1016 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1017 break;
1018 }
1019
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001020 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1021 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1022 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1023 Node->getValueType(0))) {
1024 default: assert(0 && "This action is not supported yet!");
1025 case TargetLowering::Legal:
1026 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001027 case TargetLowering::Custom:
1028 Tmp3 = TLI.LowerOperation(Result, DAG);
1029 if (Tmp3.Val) {
1030 Result = Tmp3;
1031 break;
1032 }
1033 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001034 case TargetLowering::Expand:
1035 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001036 break;
1037 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001038 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001039 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001040 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1041 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1042 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1043
1044 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001045 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1046 default: assert(0 && "Unknown operation action!");
1047 case TargetLowering::Legal:
1048 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1049 "vector shuffle should not be created if not legal!");
1050 break;
1051 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001052 Tmp3 = TLI.LowerOperation(Result, DAG);
1053 if (Tmp3.Val) {
1054 Result = Tmp3;
1055 break;
1056 }
1057 // FALLTHROUGH
1058 case TargetLowering::Expand: {
1059 MVT::ValueType VT = Node->getValueType(0);
1060 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
1061 MVT::ValueType PtrVT = TLI.getPointerTy();
1062 SDOperand Mask = Node->getOperand(2);
1063 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001064 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001065 for (unsigned i = 0; i != NumElems; ++i) {
1066 SDOperand Arg = Mask.getOperand(i);
1067 if (Arg.getOpcode() == ISD::UNDEF) {
1068 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1069 } else {
1070 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1071 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1072 if (Idx < NumElems)
1073 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1074 DAG.getConstant(Idx, PtrVT)));
1075 else
1076 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1077 DAG.getConstant(Idx - NumElems, PtrVT)));
1078 }
1079 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001080 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001081 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001082 }
Chris Lattner6be79822006-04-04 17:23:26 +00001083 case TargetLowering::Promote: {
1084 // Change base type to a different vector type.
1085 MVT::ValueType OVT = Node->getValueType(0);
1086 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1087
1088 // Cast the two input vectors.
1089 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1090 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1091
1092 // Convert the shuffle mask to the right # elements.
1093 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1094 assert(Tmp3.Val && "Shuffle not legal?");
1095 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1096 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1097 break;
1098 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001099 }
1100 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001101
1102 case ISD::EXTRACT_VECTOR_ELT:
1103 Tmp1 = LegalizeOp(Node->getOperand(0));
1104 Tmp2 = LegalizeOp(Node->getOperand(1));
1105 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner340a6b52006-03-21 21:02:03 +00001106
1107 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
1108 Tmp1.getValueType())) {
1109 default: assert(0 && "This action is not supported yet!");
1110 case TargetLowering::Legal:
1111 break;
1112 case TargetLowering::Custom:
1113 Tmp3 = TLI.LowerOperation(Result, DAG);
1114 if (Tmp3.Val) {
1115 Result = Tmp3;
1116 break;
1117 }
1118 // FALLTHROUGH
Chris Lattner42a5fca2006-04-02 05:06:04 +00001119 case TargetLowering::Expand:
1120 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner340a6b52006-03-21 21:02:03 +00001121 break;
1122 }
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001123 break;
1124
Chris Lattner6f423252006-03-31 17:55:51 +00001125 case ISD::VEXTRACT_VECTOR_ELT:
1126 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001127 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001128
Chris Lattner462505f2006-02-13 09:18:02 +00001129 case ISD::CALLSEQ_START: {
1130 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1131
1132 // Recursively Legalize all of the inputs of the call end that do not lead
1133 // to this call start. This ensures that any libcalls that need be inserted
1134 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001135 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001136 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001137 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1138 NodesLeadingTo);
1139 }
Chris Lattner462505f2006-02-13 09:18:02 +00001140
1141 // Now that we legalized all of the inputs (which may have inserted
1142 // libcalls) create the new CALLSEQ_START node.
1143 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1144
1145 // Merge in the last call, to ensure that this call start after the last
1146 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001147 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001148 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1149 Tmp1 = LegalizeOp(Tmp1);
1150 }
Chris Lattner462505f2006-02-13 09:18:02 +00001151
1152 // Do not try to legalize the target-specific arguments (#1+).
1153 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001154 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001155 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001156 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001157 }
1158
1159 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001160 AddLegalizedOperand(Op.getValue(0), Result);
1161 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1162 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1163
Chris Lattner462505f2006-02-13 09:18:02 +00001164 // Now that the callseq_start and all of the non-call nodes above this call
1165 // sequence have been legalized, legalize the call itself. During this
1166 // process, no libcalls can/will be inserted, guaranteeing that no calls
1167 // can overlap.
1168 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1169 SDOperand InCallSEQ = LastCALLSEQ_END;
1170 // Note that we are selecting this call!
1171 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1172 IsLegalizingCall = true;
1173
1174 // Legalize the call, starting from the CALLSEQ_END.
1175 LegalizeOp(LastCALLSEQ_END);
1176 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1177 return Result;
1178 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001179 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001180 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1181 // will cause this node to be legalized as well as handling libcalls right.
1182 if (LastCALLSEQ_END.Val != Node) {
1183 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001184 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001185 assert(I != LegalizedNodes.end() &&
1186 "Legalizing the call start should have legalized this node!");
1187 return I->second;
1188 }
1189
1190 // Otherwise, the call start has been legalized and everything is going
1191 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001192 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001193 // Do not try to legalize the target-specific arguments (#1+), except for
1194 // an optional flag input.
1195 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1196 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001197 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001198 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001199 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001200 }
1201 } else {
1202 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1203 if (Tmp1 != Node->getOperand(0) ||
1204 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001205 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001206 Ops[0] = Tmp1;
1207 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001208 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001209 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001210 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001211 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001212 // This finishes up call legalization.
1213 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001214
1215 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1216 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1217 if (Node->getNumValues() == 2)
1218 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1219 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001220 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001221 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1222 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1223 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001224 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001225
Chris Lattnerd02b0542006-01-28 10:58:55 +00001226 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001227 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001228 switch (TLI.getOperationAction(Node->getOpcode(),
1229 Node->getValueType(0))) {
1230 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001231 case TargetLowering::Expand: {
1232 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1233 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1234 " not tell us which reg is the stack pointer!");
1235 SDOperand Chain = Tmp1.getOperand(0);
1236 SDOperand Size = Tmp2.getOperand(1);
1237 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1238 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1239 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001240 Tmp1 = LegalizeOp(Tmp1);
1241 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001242 break;
1243 }
1244 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001245 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1246 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001247 Tmp1 = LegalizeOp(Tmp3);
1248 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001249 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001250 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001251 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001252 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001253 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001254 // Since this op produce two values, make sure to remember that we
1255 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001256 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1257 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001258 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001259 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001260 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001261 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001262 bool Changed = false;
1263 // Legalize all of the operands of the inline asm, in case they are nodes
1264 // that need to be expanded or something. Note we skip the asm string and
1265 // all of the TargetConstant flags.
1266 SDOperand Op = LegalizeOp(Ops[0]);
1267 Changed = Op != Ops[0];
1268 Ops[0] = Op;
1269
1270 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1271 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1272 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1273 for (++i; NumVals; ++i, --NumVals) {
1274 SDOperand Op = LegalizeOp(Ops[i]);
1275 if (Op != Ops[i]) {
1276 Changed = true;
1277 Ops[i] = Op;
1278 }
1279 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001280 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001281
1282 if (HasInFlag) {
1283 Op = LegalizeOp(Ops.back());
1284 Changed |= Op != Ops.back();
1285 Ops.back() = Op;
1286 }
1287
1288 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001289 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001290
1291 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001292 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001293 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1294 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001295 }
Chris Lattner68a12142005-01-07 22:12:08 +00001296 case ISD::BR:
1297 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001298 // Ensure that libcalls are emitted before a branch.
1299 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1300 Tmp1 = LegalizeOp(Tmp1);
1301 LastCALLSEQ_END = DAG.getEntryNode();
1302
Chris Lattnerd02b0542006-01-28 10:58:55 +00001303 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001304 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001305 case ISD::BRIND:
1306 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1307 // Ensure that libcalls are emitted before a branch.
1308 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1309 Tmp1 = LegalizeOp(Tmp1);
1310 LastCALLSEQ_END = DAG.getEntryNode();
1311
1312 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1313 default: assert(0 && "Indirect target must be legal type (pointer)!");
1314 case Legal:
1315 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1316 break;
1317 }
1318 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1319 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001320 case ISD::BR_JT:
1321 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1322 // Ensure that libcalls are emitted before a branch.
1323 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1324 Tmp1 = LegalizeOp(Tmp1);
1325 LastCALLSEQ_END = DAG.getEntryNode();
1326
1327 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1328 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1329
1330 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1331 default: assert(0 && "This action is not supported yet!");
1332 case TargetLowering::Legal: break;
1333 case TargetLowering::Custom:
1334 Tmp1 = TLI.LowerOperation(Result, DAG);
1335 if (Tmp1.Val) Result = Tmp1;
1336 break;
1337 case TargetLowering::Expand: {
1338 SDOperand Chain = Result.getOperand(0);
1339 SDOperand Table = Result.getOperand(1);
1340 SDOperand Index = Result.getOperand(2);
1341
1342 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001343 MachineFunction &MF = DAG.getMachineFunction();
1344 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001345 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1346 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001347
1348 SDOperand LD;
1349 switch (EntrySize) {
1350 default: assert(0 && "Size of jump table not supported yet."); break;
1351 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1352 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1353 }
1354
1355 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001356 // For PIC, the sequence is:
1357 // BRIND(load(Jumptable + index) + RelocBase)
1358 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1359 SDOperand Reloc;
1360 if (TLI.usesGlobalOffsetTable())
1361 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1362 else
1363 Reloc = Table;
Evan Chenge6d58472006-10-31 02:31:00 +00001364 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng84a28d42006-10-30 08:00:44 +00001365 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1366 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1367 } else {
1368 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1369 }
1370 }
1371 }
1372 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001373 case ISD::BRCOND:
1374 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001375 // Ensure that libcalls are emitted before a return.
1376 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1377 Tmp1 = LegalizeOp(Tmp1);
1378 LastCALLSEQ_END = DAG.getEntryNode();
1379
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001380 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1381 case Expand: assert(0 && "It's impossible to expand bools");
1382 case Legal:
1383 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1384 break;
1385 case Promote:
1386 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001387
1388 // The top bits of the promoted condition are not necessarily zero, ensure
1389 // that the value is properly zero extended.
1390 if (!TLI.MaskedValueIsZero(Tmp2,
1391 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1392 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001393 break;
1394 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001395
1396 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001397 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001398
1399 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1400 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001401 case TargetLowering::Legal: break;
1402 case TargetLowering::Custom:
1403 Tmp1 = TLI.LowerOperation(Result, DAG);
1404 if (Tmp1.Val) Result = Tmp1;
1405 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001406 case TargetLowering::Expand:
1407 // Expand brcond's setcc into its constituent parts and create a BR_CC
1408 // Node.
1409 if (Tmp2.getOpcode() == ISD::SETCC) {
1410 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1411 Tmp2.getOperand(0), Tmp2.getOperand(1),
1412 Node->getOperand(2));
1413 } else {
1414 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1415 DAG.getCondCode(ISD::SETNE), Tmp2,
1416 DAG.getConstant(0, Tmp2.getValueType()),
1417 Node->getOperand(2));
1418 }
1419 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001420 }
1421 break;
1422 case ISD::BR_CC:
1423 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001424 // Ensure that libcalls are emitted before a branch.
1425 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1426 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001427 Tmp2 = Node->getOperand(2); // LHS
1428 Tmp3 = Node->getOperand(3); // RHS
1429 Tmp4 = Node->getOperand(1); // CC
1430
1431 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001432 LastCALLSEQ_END = DAG.getEntryNode();
1433
Nate Begeman7e7f4392006-02-01 07:19:44 +00001434 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1435 // the LHS is a legal SETCC itself. In this case, we need to compare
1436 // the result against zero to select between true and false values.
1437 if (Tmp3.Val == 0) {
1438 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1439 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001440 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001441
1442 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1443 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001444
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001445 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1446 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001447 case TargetLowering::Legal: break;
1448 case TargetLowering::Custom:
1449 Tmp4 = TLI.LowerOperation(Result, DAG);
1450 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001451 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001452 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001453 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001454 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001455 LoadSDNode *LD = cast<LoadSDNode>(Node);
1456 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1457 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001458
Evan Chenge71fe34d2006-10-09 20:57:25 +00001459 ISD::LoadExtType ExtType = LD->getExtensionType();
1460 if (ExtType == ISD::NON_EXTLOAD) {
1461 MVT::ValueType VT = Node->getValueType(0);
1462 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1463 Tmp3 = Result.getValue(0);
1464 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001465
Evan Chenge71fe34d2006-10-09 20:57:25 +00001466 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1467 default: assert(0 && "This action is not supported yet!");
1468 case TargetLowering::Legal: break;
1469 case TargetLowering::Custom:
1470 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1471 if (Tmp1.Val) {
1472 Tmp3 = LegalizeOp(Tmp1);
1473 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001474 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001475 break;
1476 case TargetLowering::Promote: {
1477 // Only promote a load of vector type to another.
1478 assert(MVT::isVector(VT) && "Cannot promote this load!");
1479 // Change base type to a different vector type.
1480 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1481
1482 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1483 LD->getSrcValueOffset());
1484 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1485 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001486 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001487 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001488 }
1489 // Since loads produce two values, make sure to remember that we
1490 // legalized both of them.
1491 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1492 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1493 return Op.ResNo ? Tmp4 : Tmp3;
1494 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001495 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001496 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1497 default: assert(0 && "This action is not supported yet!");
1498 case TargetLowering::Promote:
1499 assert(SrcVT == MVT::i1 &&
1500 "Can only promote extending LOAD from i1 -> i8!");
1501 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1502 LD->getSrcValue(), LD->getSrcValueOffset(),
1503 MVT::i8);
1504 Tmp1 = Result.getValue(0);
1505 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001506 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001507 case TargetLowering::Custom:
1508 isCustom = true;
1509 // FALLTHROUGH
1510 case TargetLowering::Legal:
1511 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1512 Tmp1 = Result.getValue(0);
1513 Tmp2 = Result.getValue(1);
1514
1515 if (isCustom) {
1516 Tmp3 = TLI.LowerOperation(Result, DAG);
1517 if (Tmp3.Val) {
1518 Tmp1 = LegalizeOp(Tmp3);
1519 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1520 }
1521 }
1522 break;
1523 case TargetLowering::Expand:
1524 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1525 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1526 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1527 LD->getSrcValueOffset());
1528 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1529 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1530 Tmp2 = LegalizeOp(Load.getValue(1));
1531 break;
1532 }
Chris Lattner296a83c2007-02-01 04:55:59 +00001533 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001534 // Turn the unsupported load into an EXTLOAD followed by an explicit
1535 // zero/sign extend inreg.
1536 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1537 Tmp1, Tmp2, LD->getSrcValue(),
1538 LD->getSrcValueOffset(), SrcVT);
1539 SDOperand ValRes;
1540 if (ExtType == ISD::SEXTLOAD)
1541 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1542 Result, DAG.getValueType(SrcVT));
1543 else
1544 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1545 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1546 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1547 break;
1548 }
1549 // Since loads produce two values, make sure to remember that we legalized
1550 // both of them.
1551 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1552 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1553 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001554 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001555 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001556 case ISD::EXTRACT_ELEMENT: {
1557 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1558 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001559 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001560 case Legal:
1561 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1562 // 1 -> Hi
1563 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1564 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1565 TLI.getShiftAmountTy()));
1566 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1567 } else {
1568 // 0 -> Lo
1569 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1570 Node->getOperand(0));
1571 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001572 break;
1573 case Expand:
1574 // Get both the low and high parts.
1575 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1576 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1577 Result = Tmp2; // 1 -> Hi
1578 else
1579 Result = Tmp1; // 0 -> Lo
1580 break;
1581 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001582 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001583 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001584
1585 case ISD::CopyToReg:
1586 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001587
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001588 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001589 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001590 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001591 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001592 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001593 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001594 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001595 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001596 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001597 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001598 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1599 Tmp3);
1600 } else {
1601 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001602 }
1603
1604 // Since this produces two values, make sure to remember that we legalized
1605 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001606 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001607 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001608 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001609 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001610 break;
1611
1612 case ISD::RET:
1613 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001614
1615 // Ensure that libcalls are emitted before a return.
1616 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1617 Tmp1 = LegalizeOp(Tmp1);
1618 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001619
Chris Lattnerdc750592005-01-07 07:47:09 +00001620 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001621 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001622 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001623 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001624 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001625 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001626 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001627 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001628 case Expand:
1629 if (Tmp2.getValueType() != MVT::Vector) {
1630 SDOperand Lo, Hi;
1631 ExpandOp(Tmp2, Lo, Hi);
Evan Cheng3432ab92006-12-11 19:27:14 +00001632 if (Hi.Val)
1633 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1634 else
1635 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001636 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001637 } else {
1638 SDNode *InVal = Tmp2.Val;
1639 unsigned NumElems =
1640 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1641 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1642
1643 // Figure out if there is a Packed type corresponding to this Vector
1644 // type. If so, convert to the packed type.
1645 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1646 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1647 // Turn this into a return of the packed type.
1648 Tmp2 = PackVectorOp(Tmp2, TVT);
Evan Chenga2e99532006-05-26 23:09:09 +00001649 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001650 } else if (NumElems == 1) {
1651 // Turn this into a return of the scalar type.
1652 Tmp2 = PackVectorOp(Tmp2, EVT);
Evan Chenga2e99532006-05-26 23:09:09 +00001653 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001654
1655 // FIXME: Returns of gcc generic vectors smaller than a legal type
1656 // should be returned in integer registers!
1657
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001658 // The scalarized value type may not be legal, e.g. it might require
1659 // promotion or expansion. Relegalize the return.
1660 Result = LegalizeOp(Result);
1661 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001662 // FIXME: Returns of gcc generic vectors larger than a legal vector
1663 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001664 SDOperand Lo, Hi;
1665 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00001666 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001667 Result = LegalizeOp(Result);
1668 }
1669 }
Misha Brukman835702a2005-04-21 22:36:52 +00001670 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001671 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001672 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00001673 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001674 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001675 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001676 }
1677 break;
1678 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001679 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001680 break;
1681 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00001682 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00001683 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00001684 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00001685 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1686 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001687 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00001688 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001689 break;
1690 case Expand: {
1691 SDOperand Lo, Hi;
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001692 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1693 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001694 ExpandOp(Node->getOperand(i), Lo, Hi);
1695 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00001696 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00001697 if (Hi.Val) {
1698 NewValues.push_back(Hi);
1699 NewValues.push_back(Node->getOperand(i+1));
1700 }
Misha Brukman835702a2005-04-21 22:36:52 +00001701 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001702 }
1703 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001704 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001705 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001706
1707 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00001708 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00001709 else
Chris Lattner97af9d52006-08-08 01:09:31 +00001710 Result = DAG.getNode(ISD::RET, MVT::Other,
1711 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00001712 break;
1713 }
1714 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001715
Chris Lattner4d1ea712006-01-29 21:02:23 +00001716 if (Result.getOpcode() == ISD::RET) {
1717 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1718 default: assert(0 && "This action is not supported yet!");
1719 case TargetLowering::Legal: break;
1720 case TargetLowering::Custom:
1721 Tmp1 = TLI.LowerOperation(Result, DAG);
1722 if (Tmp1.Val) Result = Tmp1;
1723 break;
1724 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001725 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001726 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001727 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00001728 StoreSDNode *ST = cast<StoreSDNode>(Node);
1729 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1730 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Chris Lattnerdc750592005-01-07 07:47:09 +00001731
Evan Chengab51cf22006-10-13 21:14:26 +00001732 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001733 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1734 // FIXME: We shouldn't do this for TargetConstantFP's.
1735 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1736 // to phase ordering between legalized code and the dag combiner. This
1737 // probably means that we need to integrate dag combiner and legalizer
1738 // together.
1739 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1740 if (CFP->getValueType(0) == MVT::f32) {
1741 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1742 } else {
1743 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1744 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1745 }
1746 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1747 ST->getSrcValueOffset());
1748 break;
1749 }
1750
Evan Chengab51cf22006-10-13 21:14:26 +00001751 switch (getTypeAction(ST->getStoredVT())) {
1752 case Legal: {
1753 Tmp3 = LegalizeOp(ST->getValue());
1754 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1755 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00001756
Evan Chengab51cf22006-10-13 21:14:26 +00001757 MVT::ValueType VT = Tmp3.getValueType();
1758 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1759 default: assert(0 && "This action is not supported yet!");
1760 case TargetLowering::Legal: break;
1761 case TargetLowering::Custom:
1762 Tmp1 = TLI.LowerOperation(Result, DAG);
1763 if (Tmp1.Val) Result = Tmp1;
1764 break;
1765 case TargetLowering::Promote:
1766 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1767 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1768 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1769 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
1770 ST->getSrcValue(), ST->getSrcValueOffset());
1771 break;
1772 }
1773 break;
1774 }
1775 case Promote:
1776 // Truncate the value and store the result.
1777 Tmp3 = PromoteOp(ST->getValue());
1778 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1779 ST->getSrcValueOffset(), ST->getStoredVT());
1780 break;
1781
1782 case Expand:
1783 unsigned IncrementSize = 0;
1784 SDOperand Lo, Hi;
1785
1786 // If this is a vector type, then we have to calculate the increment as
1787 // the product of the element size in bytes, and the number of elements
1788 // in the high half of the vector.
1789 if (ST->getValue().getValueType() == MVT::Vector) {
1790 SDNode *InVal = ST->getValue().Val;
1791 unsigned NumElems =
1792 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1793 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1794
1795 // Figure out if there is a Packed type corresponding to this Vector
1796 // type. If so, convert to the packed type.
1797 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1798 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
1799 // Turn this into a normal store of the packed type.
1800 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1801 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1802 ST->getSrcValueOffset());
1803 Result = LegalizeOp(Result);
1804 break;
1805 } else if (NumElems == 1) {
1806 // Turn this into a normal store of the scalar type.
1807 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1808 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1809 ST->getSrcValueOffset());
1810 // The scalarized value type may not be legal, e.g. it might require
1811 // promotion or expansion. Relegalize the scalar store.
1812 Result = LegalizeOp(Result);
1813 break;
1814 } else {
1815 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1816 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1817 }
1818 } else {
1819 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00001820 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00001821
1822 if (!TLI.isLittleEndian())
1823 std::swap(Lo, Hi);
1824 }
1825
1826 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
1827 ST->getSrcValueOffset());
Evan Cheng0076ca02006-12-12 19:53:13 +00001828
1829 if (Hi.Val == NULL) {
1830 // Must be int <-> float one-to-one expansion.
1831 Result = Lo;
1832 break;
1833 }
1834
Evan Chengab51cf22006-10-13 21:14:26 +00001835 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1836 getIntPtrConstant(IncrementSize));
1837 assert(isTypeLegal(Tmp2.getValueType()) &&
1838 "Pointers must be legal!");
1839 // FIXME: This sets the srcvalue of both halves to be the same, which is
1840 // wrong.
1841 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
1842 ST->getSrcValueOffset());
1843 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1844 break;
1845 }
1846 } else {
1847 // Truncating store
1848 assert(isTypeLegal(ST->getValue().getValueType()) &&
1849 "Cannot handle illegal TRUNCSTORE yet!");
1850 Tmp3 = LegalizeOp(ST->getValue());
1851
1852 // The only promote case we handle is TRUNCSTORE:i1 X into
1853 // -> TRUNCSTORE:i8 (and X, 1)
1854 if (ST->getStoredVT() == MVT::i1 &&
1855 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1856 // Promote the bool to a mask then store.
1857 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1858 DAG.getConstant(1, Tmp3.getValueType()));
1859 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1860 ST->getSrcValueOffset(), MVT::i8);
1861 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1862 Tmp2 != ST->getBasePtr()) {
1863 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1864 ST->getOffset());
1865 }
1866
1867 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1868 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001869 default: assert(0 && "This action is not supported yet!");
Evan Chengab51cf22006-10-13 21:14:26 +00001870 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001871 case TargetLowering::Custom:
1872 Tmp1 = TLI.LowerOperation(Result, DAG);
1873 if (Tmp1.Val) Result = Tmp1;
1874 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001875 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001876 }
1877 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001878 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001879 case ISD::PCMARKER:
1880 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001881 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001882 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001883 case ISD::STACKSAVE:
1884 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001885 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1886 Tmp1 = Result.getValue(0);
1887 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001888
Chris Lattnerb3266452006-01-13 02:50:02 +00001889 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1890 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001891 case TargetLowering::Legal: break;
1892 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001893 Tmp3 = TLI.LowerOperation(Result, DAG);
1894 if (Tmp3.Val) {
1895 Tmp1 = LegalizeOp(Tmp3);
1896 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001897 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001898 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001899 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001900 // Expand to CopyFromReg if the target set
1901 // StackPointerRegisterToSaveRestore.
1902 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001903 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001904 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001905 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001906 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001907 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1908 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001909 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001910 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001911 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001912
1913 // Since stacksave produce two values, make sure to remember that we
1914 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001915 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1916 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1917 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001918
Chris Lattnerb3266452006-01-13 02:50:02 +00001919 case ISD::STACKRESTORE:
1920 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1921 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001922 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001923
1924 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1925 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001926 case TargetLowering::Legal: break;
1927 case TargetLowering::Custom:
1928 Tmp1 = TLI.LowerOperation(Result, DAG);
1929 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001930 break;
1931 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001932 // Expand to CopyToReg if the target set
1933 // StackPointerRegisterToSaveRestore.
1934 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1935 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1936 } else {
1937 Result = Tmp1;
1938 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001939 break;
1940 }
1941 break;
1942
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001943 case ISD::READCYCLECOUNTER:
1944 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001945 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00001946 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
1947 Node->getValueType(0))) {
1948 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00001949 case TargetLowering::Legal:
1950 Tmp1 = Result.getValue(0);
1951 Tmp2 = Result.getValue(1);
1952 break;
Evan Cheng69739932006-11-29 08:26:18 +00001953 case TargetLowering::Custom:
1954 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00001955 Tmp1 = LegalizeOp(Result.getValue(0));
1956 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00001957 break;
1958 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00001959
1960 // Since rdcc produce two values, make sure to remember that we legalized
1961 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00001962 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1963 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001964 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001965
Chris Lattner39c67442005-01-14 22:08:15 +00001966 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001967 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1968 case Expand: assert(0 && "It's impossible to expand bools");
1969 case Legal:
1970 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1971 break;
1972 case Promote:
1973 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00001974 // Make sure the condition is either zero or one.
1975 if (!TLI.MaskedValueIsZero(Tmp1,
1976 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
1977 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001978 break;
1979 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001980 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001981 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001982
Chris Lattnerd02b0542006-01-28 10:58:55 +00001983 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001984
Nate Begeman987121a2005-08-23 04:29:48 +00001985 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001986 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001987 case TargetLowering::Legal: break;
1988 case TargetLowering::Custom: {
1989 Tmp1 = TLI.LowerOperation(Result, DAG);
1990 if (Tmp1.Val) Result = Tmp1;
1991 break;
1992 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001993 case TargetLowering::Expand:
1994 if (Tmp1.getOpcode() == ISD::SETCC) {
1995 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1996 Tmp2, Tmp3,
1997 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1998 } else {
1999 Result = DAG.getSelectCC(Tmp1,
2000 DAG.getConstant(0, Tmp1.getValueType()),
2001 Tmp2, Tmp3, ISD::SETNE);
2002 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002003 break;
2004 case TargetLowering::Promote: {
2005 MVT::ValueType NVT =
2006 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2007 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002008 if (MVT::isVector(Tmp2.getValueType())) {
2009 ExtOp = ISD::BIT_CONVERT;
2010 TruncOp = ISD::BIT_CONVERT;
2011 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002012 ExtOp = ISD::ANY_EXTEND;
2013 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002014 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002015 ExtOp = ISD::FP_EXTEND;
2016 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002017 }
2018 // Promote each of the values to the new type.
2019 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2020 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2021 // Perform the larger operation, then round down.
2022 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2023 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2024 break;
2025 }
2026 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002027 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002028 case ISD::SELECT_CC: {
2029 Tmp1 = Node->getOperand(0); // LHS
2030 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002031 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2032 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002033 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002034
Nate Begeman7e7f4392006-02-01 07:19:44 +00002035 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2036
2037 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2038 // the LHS is a legal SETCC itself. In this case, we need to compare
2039 // the result against zero to select between true and false values.
2040 if (Tmp2.Val == 0) {
2041 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2042 CC = DAG.getCondCode(ISD::SETNE);
2043 }
2044 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2045
2046 // Everything is legal, see if we should expand this op or something.
2047 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2048 default: assert(0 && "This action is not supported yet!");
2049 case TargetLowering::Legal: break;
2050 case TargetLowering::Custom:
2051 Tmp1 = TLI.LowerOperation(Result, DAG);
2052 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002053 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002054 }
2055 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002056 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002057 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002058 Tmp1 = Node->getOperand(0);
2059 Tmp2 = Node->getOperand(1);
2060 Tmp3 = Node->getOperand(2);
2061 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2062
2063 // If we had to Expand the SetCC operands into a SELECT node, then it may
2064 // not always be possible to return a true LHS & RHS. In this case, just
2065 // return the value we legalized, returned in the LHS
2066 if (Tmp2.Val == 0) {
2067 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002068 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002069 }
Nate Begeman987121a2005-08-23 04:29:48 +00002070
Chris Lattnerf263a232006-01-30 22:43:50 +00002071 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002072 default: assert(0 && "Cannot handle this action for SETCC yet!");
2073 case TargetLowering::Custom:
2074 isCustom = true;
2075 // FALLTHROUGH.
2076 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002077 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002078 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002079 Tmp4 = TLI.LowerOperation(Result, DAG);
2080 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002081 }
Nate Begeman987121a2005-08-23 04:29:48 +00002082 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002083 case TargetLowering::Promote: {
2084 // First step, figure out the appropriate operation to use.
2085 // Allow SETCC to not be supported for all legal data types
2086 // Mostly this targets FP
2087 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002088 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002089
2090 // Scan for the appropriate larger type to use.
2091 while (1) {
2092 NewInTy = (MVT::ValueType)(NewInTy+1);
2093
2094 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2095 "Fell off of the edge of the integer world");
2096 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2097 "Fell off of the edge of the floating point world");
2098
2099 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002100 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002101 break;
2102 }
2103 if (MVT::isInteger(NewInTy))
2104 assert(0 && "Cannot promote Legal Integer SETCC yet");
2105 else {
2106 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2107 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2108 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002109 Tmp1 = LegalizeOp(Tmp1);
2110 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002111 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002112 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002113 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002114 }
Nate Begeman987121a2005-08-23 04:29:48 +00002115 case TargetLowering::Expand:
2116 // Expand a setcc node into a select_cc of the same condition, lhs, and
2117 // rhs that selects between const 1 (true) and const 0 (false).
2118 MVT::ValueType VT = Node->getValueType(0);
2119 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2120 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002121 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002122 break;
2123 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002124 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002125 case ISD::MEMSET:
2126 case ISD::MEMCPY:
2127 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002128 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002129 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2130
2131 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2132 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2133 case Expand: assert(0 && "Cannot expand a byte!");
2134 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002135 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002136 break;
2137 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002138 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002139 break;
2140 }
2141 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002142 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002143 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002144
2145 SDOperand Tmp4;
2146 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002147 case Expand: {
2148 // Length is too big, just take the lo-part of the length.
2149 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002150 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002151 break;
2152 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002153 case Legal:
2154 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002155 break;
2156 case Promote:
2157 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002158 break;
2159 }
2160
2161 SDOperand Tmp5;
2162 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2163 case Expand: assert(0 && "Cannot expand this yet!");
2164 case Legal:
2165 Tmp5 = LegalizeOp(Node->getOperand(4));
2166 break;
2167 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002168 Tmp5 = PromoteOp(Node->getOperand(4));
2169 break;
2170 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002171
2172 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2173 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002174 case TargetLowering::Custom:
2175 isCustom = true;
2176 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00002177 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002178 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002179 if (isCustom) {
2180 Tmp1 = TLI.LowerOperation(Result, DAG);
2181 if (Tmp1.Val) Result = Tmp1;
2182 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002183 break;
2184 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002185 // Otherwise, the target does not support this operation. Lower the
2186 // operation to an explicit libcall as appropriate.
2187 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002188 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002189 TargetLowering::ArgListTy Args;
2190 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002191
Reid Spencer6dced922005-01-12 14:53:45 +00002192 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002193 if (Node->getOpcode() == ISD::MEMSET) {
Reid Spencer791864c2007-01-03 04:22:32 +00002194 Entry.Node = Tmp2; Entry.isSigned = false; Entry.Ty = IntPtrTy;
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002195 Entry.isInReg = false; Entry.isSRet = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002196 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002197 // Extend the (previously legalized) ubyte argument to be an int value
2198 // for the call.
2199 if (Tmp3.getValueType() > MVT::i32)
2200 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2201 else
2202 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Reid Spencere63b6512006-12-31 05:55:36 +00002203 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSigned = true;
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002204 Entry.isInReg = false; Entry.isSRet = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002205 Args.push_back(Entry);
2206 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSigned = false;
2207 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002208
2209 FnName = "memset";
2210 } else if (Node->getOpcode() == ISD::MEMCPY ||
2211 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002212 Entry.Ty = IntPtrTy;
2213 Entry.isSigned = false; Entry.isInReg = false; Entry.isSRet = false;
Reid Spencer791864c2007-01-03 04:22:32 +00002214 Entry.Node = Tmp2; Args.push_back(Entry);
2215 Entry.Node = Tmp3; Args.push_back(Entry);
2216 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002217 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2218 } else {
2219 assert(0 && "Unknown op!");
2220 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002221
Chris Lattner85d70c62005-01-11 05:57:22 +00002222 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002223 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002224 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002225 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002226 break;
2227 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002228 }
2229 break;
2230 }
Chris Lattner5385db52005-05-09 20:23:03 +00002231
Chris Lattner4157c412005-04-02 04:00:59 +00002232 case ISD::SHL_PARTS:
2233 case ISD::SRA_PARTS:
2234 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002235 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002236 bool Changed = false;
2237 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2238 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2239 Changed |= Ops.back() != Node->getOperand(i);
2240 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002241 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002242 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002243
Evan Cheng870e4f82006-01-09 18:31:59 +00002244 switch (TLI.getOperationAction(Node->getOpcode(),
2245 Node->getValueType(0))) {
2246 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002247 case TargetLowering::Legal: break;
2248 case TargetLowering::Custom:
2249 Tmp1 = TLI.LowerOperation(Result, DAG);
2250 if (Tmp1.Val) {
2251 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002252 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002253 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002254 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2255 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002256 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002257 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002258 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002259 return RetVal;
2260 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002261 break;
2262 }
2263
Chris Lattner13fe99c2005-04-02 05:00:07 +00002264 // Since these produce multiple values, make sure to remember that we
2265 // legalized all of them.
2266 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2267 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2268 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002269 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002270
2271 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002272 case ISD::ADD:
2273 case ISD::SUB:
2274 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002275 case ISD::MULHS:
2276 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002277 case ISD::UDIV:
2278 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002279 case ISD::AND:
2280 case ISD::OR:
2281 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002282 case ISD::SHL:
2283 case ISD::SRL:
2284 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002285 case ISD::FADD:
2286 case ISD::FSUB:
2287 case ISD::FMUL:
2288 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002289 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002290 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2291 case Expand: assert(0 && "Not possible");
2292 case Legal:
2293 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2294 break;
2295 case Promote:
2296 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2297 break;
2298 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002299
2300 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002301
Andrew Lenharth72594262005-12-24 23:42:32 +00002302 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002303 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002304 case TargetLowering::Legal: break;
2305 case TargetLowering::Custom:
2306 Tmp1 = TLI.LowerOperation(Result, DAG);
2307 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002308 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002309 case TargetLowering::Expand: {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002310 if (Node->getValueType(0) == MVT::i32) {
2311 switch (Node->getOpcode()) {
2312 default: assert(0 && "Do not know how to expand this integer BinOp!");
2313 case ISD::UDIV:
2314 case ISD::SDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00002315 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2316 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002317 SDOperand Dummy;
Reid Spencere63b6512006-12-31 05:55:36 +00002318 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002319 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002320 };
2321 break;
2322 }
2323
Chris Lattner87f08092006-04-02 03:57:31 +00002324 assert(MVT::isVector(Node->getValueType(0)) &&
2325 "Cannot expand this binary operator!");
2326 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002327 SmallVector<SDOperand, 8> Ops;
Chris Lattner87f08092006-04-02 03:57:31 +00002328 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2329 MVT::ValueType PtrVT = TLI.getPointerTy();
2330 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2331 i != e; ++i) {
2332 SDOperand Idx = DAG.getConstant(i, PtrVT);
2333 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2334 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2335 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2336 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002337 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2338 &Ops[0], Ops.size());
Chris Lattner87f08092006-04-02 03:57:31 +00002339 break;
2340 }
Evan Cheng119266e2006-04-12 21:20:24 +00002341 case TargetLowering::Promote: {
2342 switch (Node->getOpcode()) {
2343 default: assert(0 && "Do not know how to promote this BinOp!");
2344 case ISD::AND:
2345 case ISD::OR:
2346 case ISD::XOR: {
2347 MVT::ValueType OVT = Node->getValueType(0);
2348 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2349 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2350 // Bit convert each of the values to the new type.
2351 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2352 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2353 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2354 // Bit convert the result back the original type.
2355 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2356 break;
2357 }
2358 }
2359 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002360 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002361 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002362
2363 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2364 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2365 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2366 case Expand: assert(0 && "Not possible");
2367 case Legal:
2368 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2369 break;
2370 case Promote:
2371 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2372 break;
2373 }
2374
2375 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2376
2377 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2378 default: assert(0 && "Operation not supported");
2379 case TargetLowering::Custom:
2380 Tmp1 = TLI.LowerOperation(Result, DAG);
2381 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002382 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002383 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002384 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00002385 // If this target supports fabs/fneg natively and select is cheap,
2386 // do this efficiently.
2387 if (!TLI.isSelectExpensive() &&
2388 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2389 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00002390 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00002391 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002392 // Get the sign bit of the RHS.
2393 MVT::ValueType IVT =
2394 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2395 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2396 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2397 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2398 // Get the absolute value of the result.
2399 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2400 // Select between the nabs and abs value based on the sign bit of
2401 // the input.
2402 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2403 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2404 AbsVal),
2405 AbsVal);
2406 Result = LegalizeOp(Result);
2407 break;
2408 }
2409
2410 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002411 MVT::ValueType NVT =
2412 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2413 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2414 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2415 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002416 break;
2417 }
Evan Cheng003feb02007-01-04 21:56:39 +00002418 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002419 break;
2420
Nate Begeman5965bd12006-02-17 05:43:56 +00002421 case ISD::ADDC:
2422 case ISD::SUBC:
2423 Tmp1 = LegalizeOp(Node->getOperand(0));
2424 Tmp2 = LegalizeOp(Node->getOperand(1));
2425 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2426 // Since this produces two values, make sure to remember that we legalized
2427 // both of them.
2428 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2429 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2430 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002431
Nate Begeman5965bd12006-02-17 05:43:56 +00002432 case ISD::ADDE:
2433 case ISD::SUBE:
2434 Tmp1 = LegalizeOp(Node->getOperand(0));
2435 Tmp2 = LegalizeOp(Node->getOperand(1));
2436 Tmp3 = LegalizeOp(Node->getOperand(2));
2437 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2438 // Since this produces two values, make sure to remember that we legalized
2439 // both of them.
2440 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2441 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2442 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002443
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002444 case ISD::BUILD_PAIR: {
2445 MVT::ValueType PairTy = Node->getValueType(0);
2446 // TODO: handle the case where the Lo and Hi operands are not of legal type
2447 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2448 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2449 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002450 case TargetLowering::Promote:
2451 case TargetLowering::Custom:
2452 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002453 case TargetLowering::Legal:
2454 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2455 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2456 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002457 case TargetLowering::Expand:
2458 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2459 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2460 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2461 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2462 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002463 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002464 break;
2465 }
2466 break;
2467 }
2468
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002469 case ISD::UREM:
2470 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002471 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002472 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2473 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002474
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002475 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002476 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2477 case TargetLowering::Custom:
2478 isCustom = true;
2479 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002480 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002481 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002482 if (isCustom) {
2483 Tmp1 = TLI.LowerOperation(Result, DAG);
2484 if (Tmp1.Val) Result = Tmp1;
2485 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002486 break;
Chris Lattner81914422005-08-03 20:31:37 +00002487 case TargetLowering::Expand:
Evan Cheng1fc7c362006-09-18 23:28:33 +00002488 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002489 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002490 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002491 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2492 TargetLowering::Legal) {
2493 // X % Y -> X-X/Y*Y
2494 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng1fc7c362006-09-18 23:28:33 +00002495 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002496 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2497 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2498 } else {
2499 assert(Node->getValueType(0) == MVT::i32 &&
2500 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00002501 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2502 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002503 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002504 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002505 }
Chris Lattner81914422005-08-03 20:31:37 +00002506 } else {
2507 // Floating point mod -> fmod libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002508 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2509 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner81914422005-08-03 20:31:37 +00002510 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002511 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2512 false/*sign irrelevant*/, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002513 }
2514 break;
2515 }
2516 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002517 case ISD::VAARG: {
2518 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2519 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2520
Chris Lattner364b89a2006-01-28 07:42:08 +00002521 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002522 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2523 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002524 case TargetLowering::Custom:
2525 isCustom = true;
2526 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002527 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002528 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2529 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002530 Tmp1 = Result.getValue(1);
2531
2532 if (isCustom) {
2533 Tmp2 = TLI.LowerOperation(Result, DAG);
2534 if (Tmp2.Val) {
2535 Result = LegalizeOp(Tmp2);
2536 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2537 }
2538 }
Nate Begemane74795c2006-01-25 18:21:52 +00002539 break;
2540 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002541 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00002542 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00002543 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002544 // Increment the pointer, VAList, to the next vaarg
2545 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2546 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2547 TLI.getPointerTy()));
2548 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00002549 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2550 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002551 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00002552 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002553 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002554 Result = LegalizeOp(Result);
2555 break;
2556 }
2557 }
2558 // Since VAARG produces two values, make sure to remember that we
2559 // legalized both of them.
2560 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002561 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2562 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002563 }
2564
2565 case ISD::VACOPY:
2566 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2567 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2568 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2569
2570 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2571 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002572 case TargetLowering::Custom:
2573 isCustom = true;
2574 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002575 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002576 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2577 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002578 if (isCustom) {
2579 Tmp1 = TLI.LowerOperation(Result, DAG);
2580 if (Tmp1.Val) Result = Tmp1;
2581 }
Nate Begemane74795c2006-01-25 18:21:52 +00002582 break;
2583 case TargetLowering::Expand:
2584 // This defaults to loading a pointer from the input and storing it to the
2585 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002586 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00002587 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00002588 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2589 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00002590 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2591 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002592 break;
2593 }
2594 break;
2595
2596 case ISD::VAEND:
2597 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2598 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2599
2600 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2601 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002602 case TargetLowering::Custom:
2603 isCustom = true;
2604 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002605 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002606 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002607 if (isCustom) {
2608 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2609 if (Tmp1.Val) Result = Tmp1;
2610 }
Nate Begemane74795c2006-01-25 18:21:52 +00002611 break;
2612 case TargetLowering::Expand:
2613 Result = Tmp1; // Default to a no-op, return the chain
2614 break;
2615 }
2616 break;
2617
2618 case ISD::VASTART:
2619 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2620 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2621
Chris Lattnerd02b0542006-01-28 10:58:55 +00002622 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2623
Nate Begemane74795c2006-01-25 18:21:52 +00002624 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2625 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002626 case TargetLowering::Legal: break;
2627 case TargetLowering::Custom:
2628 Tmp1 = TLI.LowerOperation(Result, DAG);
2629 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002630 break;
2631 }
2632 break;
2633
Nate Begeman1b8121b2006-01-11 21:21:00 +00002634 case ISD::ROTL:
2635 case ISD::ROTR:
2636 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2637 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002638
2639 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2640 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002641 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002642 break;
2643
Nate Begeman2fba8a32006-01-14 03:14:10 +00002644 case ISD::BSWAP:
2645 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2646 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002647 case TargetLowering::Custom:
2648 assert(0 && "Cannot custom legalize this yet!");
2649 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002650 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002651 break;
2652 case TargetLowering::Promote: {
2653 MVT::ValueType OVT = Tmp1.getValueType();
2654 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2655 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002656
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002657 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2658 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2659 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2660 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2661 break;
2662 }
2663 case TargetLowering::Expand:
2664 Result = ExpandBSWAP(Tmp1);
2665 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002666 }
2667 break;
2668
Andrew Lenharth5e177822005-05-03 17:19:30 +00002669 case ISD::CTPOP:
2670 case ISD::CTTZ:
2671 case ISD::CTLZ:
2672 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2673 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002674 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002675 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002676 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002677 break;
2678 case TargetLowering::Promote: {
2679 MVT::ValueType OVT = Tmp1.getValueType();
2680 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002681
2682 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002683 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2684 // Perform the larger operation, then subtract if needed.
2685 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002686 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002687 case ISD::CTPOP:
2688 Result = Tmp1;
2689 break;
2690 case ISD::CTTZ:
2691 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002692 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2693 DAG.getConstant(getSizeInBits(NVT), NVT),
2694 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002695 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002696 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2697 break;
2698 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002699 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002700 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2701 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002702 getSizeInBits(OVT), NVT));
2703 break;
2704 }
2705 break;
2706 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002707 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002708 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002709 break;
2710 }
2711 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002712
Chris Lattner13fe99c2005-04-02 05:00:07 +00002713 // Unary operators
2714 case ISD::FABS:
2715 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002716 case ISD::FSQRT:
2717 case ISD::FSIN:
2718 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002719 Tmp1 = LegalizeOp(Node->getOperand(0));
2720 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002721 case TargetLowering::Promote:
2722 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002723 isCustom = true;
2724 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002725 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002726 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002727 if (isCustom) {
2728 Tmp1 = TLI.LowerOperation(Result, DAG);
2729 if (Tmp1.Val) Result = Tmp1;
2730 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002731 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002732 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002733 switch (Node->getOpcode()) {
2734 default: assert(0 && "Unreachable!");
2735 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002736 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2737 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002738 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002739 break;
Chris Lattner80026402005-04-30 04:43:14 +00002740 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002741 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2742 MVT::ValueType VT = Node->getValueType(0);
2743 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002744 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002745 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2746 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002747 break;
2748 }
2749 case ISD::FSQRT:
2750 case ISD::FSIN:
2751 case ISD::FCOS: {
2752 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng31cbddf2007-01-12 02:11:51 +00002753 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00002754 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00002755 case ISD::FSQRT:
2756 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2757 break;
2758 case ISD::FSIN:
2759 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2760 break;
2761 case ISD::FCOS:
2762 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2763 break;
Chris Lattner80026402005-04-30 04:43:14 +00002764 default: assert(0 && "Unreachable!");
2765 }
Nate Begeman77558da2005-08-04 21:43:28 +00002766 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002767 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2768 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002769 break;
2770 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002771 }
2772 break;
2773 }
2774 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00002775 case ISD::FPOWI: {
2776 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002777 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2778 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattnerf0359b32006-09-09 06:03:30 +00002779 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002780 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2781 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00002782 break;
2783 }
Chris Lattner36e663d2005-12-23 00:16:34 +00002784 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002785 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002786 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002787 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002788 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2789 Node->getOperand(0).getValueType())) {
2790 default: assert(0 && "Unknown operation action!");
2791 case TargetLowering::Expand:
2792 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2793 break;
2794 case TargetLowering::Legal:
2795 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002796 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002797 break;
2798 }
2799 }
2800 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00002801 case ISD::VBIT_CONVERT: {
2802 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2803 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2804
2805 // The input has to be a vector type, we have to either scalarize it, pack
2806 // it, or convert it based on whether the input vector type is legal.
2807 SDNode *InVal = Node->getOperand(0).Val;
2808 unsigned NumElems =
2809 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2810 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2811
2812 // Figure out if there is a Packed type corresponding to this Vector
2813 // type. If so, convert to the packed type.
2814 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2815 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2816 // Turn this into a bit convert of the packed input.
2817 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2818 PackVectorOp(Node->getOperand(0), TVT));
2819 break;
2820 } else if (NumElems == 1) {
2821 // Turn this into a bit convert of the scalar input.
2822 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2823 PackVectorOp(Node->getOperand(0), EVT));
2824 break;
2825 } else {
2826 // FIXME: UNIMP! Store then reload
2827 assert(0 && "Cast from unsupported vector type not implemented yet!");
2828 }
2829 }
2830
Chris Lattner13fe99c2005-04-02 05:00:07 +00002831 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002832 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002833 case ISD::UINT_TO_FP: {
2834 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002835 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2836 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002837 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002838 Node->getOperand(0).getValueType())) {
2839 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002840 case TargetLowering::Custom:
2841 isCustom = true;
2842 // FALLTHROUGH
2843 case TargetLowering::Legal:
2844 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002845 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002846 if (isCustom) {
2847 Tmp1 = TLI.LowerOperation(Result, DAG);
2848 if (Tmp1.Val) Result = Tmp1;
2849 }
2850 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002851 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002852 Result = ExpandLegalINT_TO_FP(isSigned,
2853 LegalizeOp(Node->getOperand(0)),
2854 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002855 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002856 case TargetLowering::Promote:
2857 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2858 Node->getValueType(0),
2859 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002860 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002861 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002862 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002863 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002864 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2865 Node->getValueType(0), Node->getOperand(0));
2866 break;
2867 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002868 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002869 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002870 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2871 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002872 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002873 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2874 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002875 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002876 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2877 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002878 break;
2879 }
2880 break;
2881 }
2882 case ISD::TRUNCATE:
2883 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2884 case Legal:
2885 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002886 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002887 break;
2888 case Expand:
2889 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2890
2891 // Since the result is legal, we should just be able to truncate the low
2892 // part of the source.
2893 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2894 break;
2895 case Promote:
2896 Result = PromoteOp(Node->getOperand(0));
2897 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2898 break;
2899 }
2900 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002901
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002902 case ISD::FP_TO_SINT:
2903 case ISD::FP_TO_UINT:
2904 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2905 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002906 Tmp1 = LegalizeOp(Node->getOperand(0));
2907
Chris Lattner44fe26f2005-07-29 00:11:56 +00002908 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2909 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002910 case TargetLowering::Custom:
2911 isCustom = true;
2912 // FALLTHROUGH
2913 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002914 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002915 if (isCustom) {
2916 Tmp1 = TLI.LowerOperation(Result, DAG);
2917 if (Tmp1.Val) Result = Tmp1;
2918 }
2919 break;
2920 case TargetLowering::Promote:
2921 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2922 Node->getOpcode() == ISD::FP_TO_SINT);
2923 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002924 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002925 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2926 SDOperand True, False;
2927 MVT::ValueType VT = Node->getOperand(0).getValueType();
2928 MVT::ValueType NVT = Node->getValueType(0);
2929 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2930 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2931 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2932 Node->getOperand(0), Tmp2, ISD::SETLT);
2933 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2934 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002935 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002936 Tmp2));
2937 False = DAG.getNode(ISD::XOR, NVT, False,
2938 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002939 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2940 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002941 } else {
2942 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2943 }
2944 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002945 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002946 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002947 case Expand: {
2948 // Convert f32 / f64 to i32 / i64.
2949 MVT::ValueType VT = Op.getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00002950 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002951 switch (Node->getOpcode()) {
2952 case ISD::FP_TO_SINT:
2953 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00002954 LC = (VT == MVT::i32)
2955 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002956 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00002957 LC = (VT == MVT::i32)
2958 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002959 break;
2960 case ISD::FP_TO_UINT:
2961 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00002962 LC = (VT == MVT::i32)
2963 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002964 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00002965 LC = (VT == MVT::i32)
2966 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002967 break;
2968 default: assert(0 && "Unreachable!");
2969 }
2970 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002971 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2972 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002973 break;
2974 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002975 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002976 Tmp1 = PromoteOp(Node->getOperand(0));
2977 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2978 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002979 break;
2980 }
2981 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002982
Chris Lattner7753f172005-09-02 00:18:10 +00002983 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002984 case ISD::ZERO_EXTEND:
2985 case ISD::SIGN_EXTEND:
2986 case ISD::FP_EXTEND:
2987 case ISD::FP_ROUND:
2988 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002989 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002990 case Legal:
2991 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002992 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002993 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002994 case Promote:
2995 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002996 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002997 Tmp1 = PromoteOp(Node->getOperand(0));
2998 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002999 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003000 case ISD::ZERO_EXTEND:
3001 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003002 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003003 Result = DAG.getZeroExtendInReg(Result,
3004 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003005 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003006 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003007 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003008 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003009 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003010 Result,
3011 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003012 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003013 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003014 Result = PromoteOp(Node->getOperand(0));
3015 if (Result.getValueType() != Op.getValueType())
3016 // Dynamically dead while we have only 2 FP types.
3017 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3018 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003019 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00003020 Result = PromoteOp(Node->getOperand(0));
3021 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3022 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003023 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003024 }
3025 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003026 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003027 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003028 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003029 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003030
3031 // If this operation is not supported, convert it to a shl/shr or load/store
3032 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003033 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3034 default: assert(0 && "This action not supported for this op yet!");
3035 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003036 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003037 break;
3038 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003039 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003040 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003041 // NOTE: we could fall back on load/store here too for targets without
3042 // SAR. However, it is doubtful that any exist.
3043 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3044 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003045 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003046 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3047 Node->getOperand(0), ShiftCst);
3048 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3049 Result, ShiftCst);
3050 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003051 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003052 // EXTLOAD pair, targetting a temporary location (a stack slot).
3053
3054 // NOTE: there is a choice here between constantly creating new stack
3055 // slots and always reusing the same one. We currently always create
3056 // new ones, as reuse may inhibit scheduling.
3057 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Owen Anderson20a631f2006-05-03 01:29:57 +00003058 unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
Chris Lattner50ee0e42007-01-20 22:35:55 +00003059 unsigned Align = TLI.getTargetData()->getTypeAlignmentPref(Ty);
Chris Lattner99222f72005-01-15 07:15:18 +00003060 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00003061 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00003062 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
3063 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003064 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3065 StackSlot, NULL, 0, ExtraVT);
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003066 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003067 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00003068 } else {
3069 assert(0 && "Unknown op");
3070 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003071 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003072 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003073 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003074 }
Chris Lattner99222f72005-01-15 07:15:18 +00003075 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003076
Chris Lattner101ea662006-04-08 04:13:17 +00003077 assert(Result.getValueType() == Op.getValueType() &&
3078 "Bad legalization!");
3079
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003080 // Make sure that the generated code is itself legal.
3081 if (Result != Op)
3082 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003083
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003084 // Note that LegalizeOp may be reentered even from single-use nodes, which
3085 // means that we always must cache transformed nodes.
3086 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003087 return Result;
3088}
3089
Chris Lattner4d978642005-01-15 22:16:26 +00003090/// PromoteOp - Given an operation that produces a value in an invalid type,
3091/// promote it to compute the value into a larger type. The produced value will
3092/// have the correct bits for the low portion of the register, but no guarantee
3093/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003094SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3095 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003096 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003097 assert(getTypeAction(VT) == Promote &&
3098 "Caller should expand or legalize operands that are not promotable!");
3099 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3100 "Cannot promote to smaller type!");
3101
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003102 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003103 SDOperand Result;
3104 SDNode *Node = Op.Val;
3105
Chris Lattner4b0ddb22007-02-04 01:17:38 +00003106 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00003107 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003108
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003109 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003110 case ISD::CopyFromReg:
3111 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003112 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003113#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +00003114 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003115#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003116 assert(0 && "Do not know how to promote this operator!");
3117 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003118 case ISD::UNDEF:
3119 Result = DAG.getNode(ISD::UNDEF, NVT);
3120 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003121 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003122 if (VT != MVT::i1)
3123 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3124 else
3125 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003126 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3127 break;
3128 case ISD::ConstantFP:
3129 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3130 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3131 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003132
Chris Lattner2cb338d2005-01-18 02:59:52 +00003133 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003134 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003135 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3136 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003137 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003138
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003139 case ISD::TRUNCATE:
3140 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3141 case Legal:
3142 Result = LegalizeOp(Node->getOperand(0));
3143 assert(Result.getValueType() >= NVT &&
3144 "This truncation doesn't make sense!");
3145 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3146 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3147 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003148 case Promote:
3149 // The truncation is not required, because we don't guarantee anything
3150 // about high bits anyway.
3151 Result = PromoteOp(Node->getOperand(0));
3152 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003153 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003154 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3155 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003156 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003157 }
3158 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003159 case ISD::SIGN_EXTEND:
3160 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003161 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003162 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3163 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3164 case Legal:
3165 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003166 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003167 break;
3168 case Promote:
3169 // Promote the reg if it's smaller.
3170 Result = PromoteOp(Node->getOperand(0));
3171 // The high bits are not guaranteed to be anything. Insert an extend.
3172 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003173 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003174 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003175 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003176 Result = DAG.getZeroExtendInReg(Result,
3177 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003178 break;
3179 }
3180 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003181 case ISD::BIT_CONVERT:
3182 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3183 Result = PromoteOp(Result);
3184 break;
3185
Chris Lattner4d978642005-01-15 22:16:26 +00003186 case ISD::FP_EXTEND:
3187 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3188 case ISD::FP_ROUND:
3189 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3190 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3191 case Promote: assert(0 && "Unreachable with 2 FP types!");
3192 case Legal:
3193 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003194 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003195 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003196 break;
3197 }
3198 break;
3199
3200 case ISD::SINT_TO_FP:
3201 case ISD::UINT_TO_FP:
3202 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3203 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003204 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003205 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003206 break;
3207
3208 case Promote:
3209 Result = PromoteOp(Node->getOperand(0));
3210 if (Node->getOpcode() == ISD::SINT_TO_FP)
3211 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003212 Result,
3213 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003214 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003215 Result = DAG.getZeroExtendInReg(Result,
3216 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003217 // No extra round required here.
3218 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003219 break;
3220 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003221 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3222 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003223 // Round if we cannot tolerate excess precision.
3224 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003225 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3226 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003227 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003228 }
Chris Lattner4d978642005-01-15 22:16:26 +00003229 break;
3230
Chris Lattner268d4572005-12-09 17:32:47 +00003231 case ISD::SIGN_EXTEND_INREG:
3232 Result = PromoteOp(Node->getOperand(0));
3233 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3234 Node->getOperand(1));
3235 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003236 case ISD::FP_TO_SINT:
3237 case ISD::FP_TO_UINT:
3238 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3239 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003240 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003241 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003242 break;
3243 case Promote:
3244 // The input result is prerounded, so we don't have to do anything
3245 // special.
3246 Tmp1 = PromoteOp(Node->getOperand(0));
3247 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003248 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003249 // If we're promoting a UINT to a larger size, check to see if the new node
3250 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3251 // we can use that instead. This allows us to generate better code for
3252 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3253 // legal, such as PowerPC.
3254 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003255 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003256 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3257 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003258 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3259 } else {
3260 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3261 }
Chris Lattner4d978642005-01-15 22:16:26 +00003262 break;
3263
Chris Lattner13fe99c2005-04-02 05:00:07 +00003264 case ISD::FABS:
3265 case ISD::FNEG:
3266 Tmp1 = PromoteOp(Node->getOperand(0));
3267 assert(Tmp1.getValueType() == NVT);
3268 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3269 // NOTE: we do not have to do any extra rounding here for
3270 // NoExcessFPPrecision, because we know the input will have the appropriate
3271 // precision, and these operations don't modify precision at all.
3272 break;
3273
Chris Lattner9d6fa982005-04-28 21:44:33 +00003274 case ISD::FSQRT:
3275 case ISD::FSIN:
3276 case ISD::FCOS:
3277 Tmp1 = PromoteOp(Node->getOperand(0));
3278 assert(Tmp1.getValueType() == NVT);
3279 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003280 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003281 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3282 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003283 break;
3284
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003285 case ISD::AND:
3286 case ISD::OR:
3287 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003288 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003289 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003290 case ISD::MUL:
3291 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003292 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003293 // that too is okay if they are integer operations.
3294 Tmp1 = PromoteOp(Node->getOperand(0));
3295 Tmp2 = PromoteOp(Node->getOperand(1));
3296 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3297 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003298 break;
3299 case ISD::FADD:
3300 case ISD::FSUB:
3301 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003302 Tmp1 = PromoteOp(Node->getOperand(0));
3303 Tmp2 = PromoteOp(Node->getOperand(1));
3304 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3305 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3306
3307 // Floating point operations will give excess precision that we may not be
3308 // able to tolerate. If we DO allow excess precision, just leave it,
3309 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003310 // FIXME: Why would we need to round FP ops more than integer ones?
3311 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003312 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003313 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3314 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003315 break;
3316
Chris Lattner4d978642005-01-15 22:16:26 +00003317 case ISD::SDIV:
3318 case ISD::SREM:
3319 // These operators require that their input be sign extended.
3320 Tmp1 = PromoteOp(Node->getOperand(0));
3321 Tmp2 = PromoteOp(Node->getOperand(1));
3322 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003323 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3324 DAG.getValueType(VT));
3325 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3326 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003327 }
3328 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3329
3330 // Perform FP_ROUND: this is probably overly pessimistic.
3331 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003332 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3333 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003334 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003335 case ISD::FDIV:
3336 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003337 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003338 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00003339 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3340 case Legal:
3341 Tmp1 = LegalizeOp(Node->getOperand(0));
3342 break;
3343 case Promote:
3344 Tmp1 = PromoteOp(Node->getOperand(0));
3345 break;
3346 case Expand:
3347 assert(0 && "not implemented");
3348 }
3349 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3350 case Legal:
3351 Tmp2 = LegalizeOp(Node->getOperand(1));
3352 break;
3353 case Promote:
3354 Tmp2 = PromoteOp(Node->getOperand(1));
3355 break;
3356 case Expand:
3357 assert(0 && "not implemented");
3358 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00003359 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3360
3361 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003362 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003363 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3364 DAG.getValueType(VT));
3365 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003366
3367 case ISD::UDIV:
3368 case ISD::UREM:
3369 // These operators require that their input be zero extended.
3370 Tmp1 = PromoteOp(Node->getOperand(0));
3371 Tmp2 = PromoteOp(Node->getOperand(1));
3372 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003373 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3374 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003375 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3376 break;
3377
3378 case ISD::SHL:
3379 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003380 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003381 break;
3382 case ISD::SRA:
3383 // The input value must be properly sign extended.
3384 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003385 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3386 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003387 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003388 break;
3389 case ISD::SRL:
3390 // The input value must be properly zero extended.
3391 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003392 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003393 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003394 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003395
3396 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003397 Tmp1 = Node->getOperand(0); // Get the chain.
3398 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003399 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3400 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3401 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3402 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00003403 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00003404 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00003405 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003406 // Increment the pointer, VAList, to the next vaarg
3407 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3408 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3409 TLI.getPointerTy()));
3410 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00003411 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3412 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003413 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003414 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00003415 }
3416 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003417 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00003418 break;
3419
Evan Chenge71fe34d2006-10-09 20:57:25 +00003420 case ISD::LOAD: {
3421 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00003422 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3423 ? ISD::EXTLOAD : LD->getExtensionType();
3424 Result = DAG.getExtLoad(ExtType, NVT,
3425 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00003426 LD->getSrcValue(), LD->getSrcValueOffset(),
Evan Chengd35734b2006-10-11 07:10:22 +00003427 LD->getLoadedVT());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003428 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003429 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003430 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00003431 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003432 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003433 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3434 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003435 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003436 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003437 case ISD::SELECT_CC:
3438 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3439 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3440 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003441 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00003442 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003443 case ISD::BSWAP:
3444 Tmp1 = Node->getOperand(0);
3445 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3446 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3447 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3448 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3449 TLI.getShiftAmountTy()));
3450 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003451 case ISD::CTPOP:
3452 case ISD::CTTZ:
3453 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003454 // Zero extend the argument
3455 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003456 // Perform the larger operation, then subtract if needed.
3457 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003458 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003459 case ISD::CTPOP:
3460 Result = Tmp1;
3461 break;
3462 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003463 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003464 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00003465 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003466 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003467 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003468 break;
3469 case ISD::CTLZ:
3470 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003471 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3472 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003473 getSizeInBits(VT), NVT));
3474 break;
3475 }
3476 break;
Chris Lattner6f423252006-03-31 17:55:51 +00003477 case ISD::VEXTRACT_VECTOR_ELT:
3478 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3479 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00003480 case ISD::EXTRACT_VECTOR_ELT:
3481 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3482 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003483 }
3484
3485 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003486
3487 // Make sure the result is itself legal.
3488 Result = LegalizeOp(Result);
3489
3490 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003491 AddPromotedOperand(Op, Result);
3492 return Result;
3493}
Chris Lattnerdc750592005-01-07 07:47:09 +00003494
Chris Lattner6f423252006-03-31 17:55:51 +00003495/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3496/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3497/// on the vector type. The return type of this matches the element type of the
3498/// vector, which may not be legal for the target.
3499SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3500 // We know that operand #0 is the Vec vector. If the index is a constant
3501 // or if the invec is a supported hardware type, we can use it. Otherwise,
3502 // lower to a store then an indexed load.
3503 SDOperand Vec = Op.getOperand(0);
3504 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3505
3506 SDNode *InVal = Vec.Val;
3507 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3508 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3509
3510 // Figure out if there is a Packed type corresponding to this Vector
3511 // type. If so, convert to the packed type.
3512 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3513 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3514 // Turn this into a packed extract_vector_elt operation.
3515 Vec = PackVectorOp(Vec, TVT);
3516 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3517 } else if (NumElems == 1) {
3518 // This must be an access of the only element. Return it.
3519 return PackVectorOp(Vec, EVT);
3520 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3521 SDOperand Lo, Hi;
3522 SplitVectorOp(Vec, Lo, Hi);
3523 if (CIdx->getValue() < NumElems/2) {
3524 Vec = Lo;
3525 } else {
3526 Vec = Hi;
3527 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3528 }
3529
3530 // It's now an extract from the appropriate high or low part. Recurse.
3531 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3532 return LowerVEXTRACT_VECTOR_ELT(Op);
3533 } else {
3534 // Variable index case for extract element.
3535 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3536 assert(0 && "unimp!");
3537 return SDOperand();
3538 }
3539}
3540
Chris Lattner42a5fca2006-04-02 05:06:04 +00003541/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3542/// memory traffic.
3543SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3544 SDOperand Vector = Op.getOperand(0);
3545 SDOperand Idx = Op.getOperand(1);
3546
3547 // If the target doesn't support this, store the value to a temporary
3548 // stack slot, then LOAD the scalar element back out.
3549 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
Evan Chengab51cf22006-10-13 21:14:26 +00003550 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vector, StackPtr, NULL, 0);
Chris Lattner42a5fca2006-04-02 05:06:04 +00003551
3552 // Add the offset to the index.
3553 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3554 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3555 DAG.getConstant(EltSize, Idx.getValueType()));
3556 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3557
Evan Chenge71fe34d2006-10-09 20:57:25 +00003558 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner42a5fca2006-04-02 05:06:04 +00003559}
3560
Chris Lattner6f423252006-03-31 17:55:51 +00003561
Nate Begeman7e7f4392006-02-01 07:19:44 +00003562/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3563/// with condition CC on the current target. This usually involves legalizing
3564/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3565/// there may be no choice but to create a new SetCC node to represent the
3566/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3567/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3568void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3569 SDOperand &RHS,
3570 SDOperand &CC) {
3571 SDOperand Tmp1, Tmp2, Result;
3572
3573 switch (getTypeAction(LHS.getValueType())) {
3574 case Legal:
3575 Tmp1 = LegalizeOp(LHS); // LHS
3576 Tmp2 = LegalizeOp(RHS); // RHS
3577 break;
3578 case Promote:
3579 Tmp1 = PromoteOp(LHS); // LHS
3580 Tmp2 = PromoteOp(RHS); // RHS
3581
3582 // If this is an FP compare, the operands have already been extended.
3583 if (MVT::isInteger(LHS.getValueType())) {
3584 MVT::ValueType VT = LHS.getValueType();
3585 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3586
3587 // Otherwise, we have to insert explicit sign or zero extends. Note
3588 // that we could insert sign extends for ALL conditions, but zero extend
3589 // is cheaper on many machines (an AND instead of two shifts), so prefer
3590 // it.
3591 switch (cast<CondCodeSDNode>(CC)->get()) {
3592 default: assert(0 && "Unknown integer comparison!");
3593 case ISD::SETEQ:
3594 case ISD::SETNE:
3595 case ISD::SETUGE:
3596 case ISD::SETUGT:
3597 case ISD::SETULE:
3598 case ISD::SETULT:
3599 // ALL of these operations will work if we either sign or zero extend
3600 // the operands (including the unsigned comparisons!). Zero extend is
3601 // usually a simpler/cheaper operation, so prefer it.
3602 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3603 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3604 break;
3605 case ISD::SETGE:
3606 case ISD::SETGT:
3607 case ISD::SETLT:
3608 case ISD::SETLE:
3609 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3610 DAG.getValueType(VT));
3611 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3612 DAG.getValueType(VT));
3613 break;
3614 }
3615 }
3616 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003617 case Expand: {
3618 MVT::ValueType VT = LHS.getValueType();
3619 if (VT == MVT::f32 || VT == MVT::f64) {
3620 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00003621 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003622 switch (cast<CondCodeSDNode>(CC)->get()) {
3623 case ISD::SETEQ:
3624 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003625 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003626 break;
3627 case ISD::SETNE:
3628 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003629 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003630 break;
3631 case ISD::SETGE:
3632 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003633 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003634 break;
3635 case ISD::SETLT:
3636 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003637 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003638 break;
3639 case ISD::SETLE:
3640 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003641 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003642 break;
3643 case ISD::SETGT:
3644 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003645 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003646 break;
3647 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00003648 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3649 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003650 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00003651 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003652 break;
3653 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003654 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003655 switch (cast<CondCodeSDNode>(CC)->get()) {
3656 case ISD::SETONE:
3657 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00003658 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003659 // Fallthrough
3660 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003661 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003662 break;
3663 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003664 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003665 break;
3666 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003667 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003668 break;
3669 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003670 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003671 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003672 case ISD::SETUEQ:
3673 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003674 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003675 default: assert(0 && "Unsupported FP setcc!");
3676 }
3677 }
3678
3679 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003680 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00003681 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00003682 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003683 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00003684 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00003685 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003686 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003687 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00003688 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00003689 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003690 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00003691 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003692 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3693 Tmp2 = SDOperand();
3694 }
3695 LHS = Tmp1;
3696 RHS = Tmp2;
3697 return;
3698 }
3699
Nate Begeman7e7f4392006-02-01 07:19:44 +00003700 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3701 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003702 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman7e7f4392006-02-01 07:19:44 +00003703 switch (cast<CondCodeSDNode>(CC)->get()) {
3704 case ISD::SETEQ:
3705 case ISD::SETNE:
3706 if (RHSLo == RHSHi)
3707 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3708 if (RHSCST->isAllOnesValue()) {
3709 // Comparison to -1.
3710 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3711 Tmp2 = RHSLo;
3712 break;
3713 }
3714
3715 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3716 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3717 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3718 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3719 break;
3720 default:
3721 // If this is a comparison of the sign bit, just look at the top part.
3722 // X > -1, x < 0
3723 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3724 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3725 CST->getValue() == 0) || // X < 0
3726 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3727 CST->isAllOnesValue())) { // X > -1
3728 Tmp1 = LHSHi;
3729 Tmp2 = RHSHi;
3730 break;
3731 }
3732
3733 // FIXME: This generated code sucks.
3734 ISD::CondCode LowCC;
3735 switch (cast<CondCodeSDNode>(CC)->get()) {
3736 default: assert(0 && "Unknown integer setcc!");
3737 case ISD::SETLT:
3738 case ISD::SETULT: LowCC = ISD::SETULT; break;
3739 case ISD::SETGT:
3740 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3741 case ISD::SETLE:
3742 case ISD::SETULE: LowCC = ISD::SETULE; break;
3743 case ISD::SETGE:
3744 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3745 }
3746
3747 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3748 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3749 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3750
3751 // NOTE: on targets without efficient SELECT of bools, we can always use
3752 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3753 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3754 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3755 Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3756 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3757 Result, Tmp1, Tmp2));
3758 Tmp1 = Result;
Nate Begeman01bd9d92006-02-01 19:05:15 +00003759 Tmp2 = SDOperand();
Nate Begeman7e7f4392006-02-01 07:19:44 +00003760 }
3761 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003762 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00003763 LHS = Tmp1;
3764 RHS = Tmp2;
3765}
3766
Chris Lattner36e663d2005-12-23 00:16:34 +00003767/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003768/// The resultant code need not be legal. Note that SrcOp is the input operand
3769/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003770SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3771 SDOperand SrcOp) {
3772 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003773 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003774
3775 // Emit a store to the stack slot.
Evan Chengab51cf22006-10-13 21:14:26 +00003776 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00003777 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003778 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00003779}
3780
Chris Lattner6be79822006-04-04 17:23:26 +00003781SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3782 // Create a vector sized/aligned stack slot, store the value to element #0,
3783 // then load the whole vector back out.
3784 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00003785 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00003786 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00003787 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00003788}
3789
3790
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003791/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3792/// support the operation, but do support the resultant packed vector type.
3793SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3794
3795 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003796 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00003797 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003798 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003799 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003800 std::map<SDOperand, std::vector<unsigned> > Values;
3801 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003802 bool isConstant = true;
3803 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3804 SplatValue.getOpcode() != ISD::UNDEF)
3805 isConstant = false;
3806
Evan Cheng1d2e9952006-03-24 01:17:21 +00003807 for (unsigned i = 1; i < NumElems; ++i) {
3808 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00003809 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003810 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003811 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003812 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00003813 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003814
3815 // If this isn't a constant element or an undef, we can't use a constant
3816 // pool load.
3817 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3818 V.getOpcode() != ISD::UNDEF)
3819 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003820 }
3821
3822 if (isOnlyLowElement) {
3823 // If the low element is an undef too, then this whole things is an undef.
3824 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3825 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3826 // Otherwise, turn this into a scalar_to_vector node.
3827 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3828 Node->getOperand(0));
3829 }
3830
Chris Lattner77e271c2006-03-24 07:29:17 +00003831 // If all elements are constants, create a load from the constant pool.
3832 if (isConstant) {
3833 MVT::ValueType VT = Node->getValueType(0);
3834 const Type *OpNTy =
3835 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3836 std::vector<Constant*> CV;
3837 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3838 if (ConstantFPSDNode *V =
3839 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3840 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3841 } else if (ConstantSDNode *V =
3842 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00003843 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00003844 } else {
3845 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3846 CV.push_back(UndefValue::get(OpNTy));
3847 }
3848 }
3849 Constant *CP = ConstantPacked::get(CV);
3850 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00003851 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003852 }
3853
Chris Lattner21e68c82006-03-20 01:52:29 +00003854 if (SplatValue.Val) { // Splat of one value?
3855 // Build the shuffle constant vector: <0, 0, 0, 0>
3856 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00003857 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner21e68c82006-03-20 01:52:29 +00003858 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00003859 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003860 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3861 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00003862
3863 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00003864 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00003865 // Get the splatted value into the low element of a vector register.
3866 SDOperand LowValVec =
3867 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3868
3869 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3870 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3871 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3872 SplatMask);
3873 }
3874 }
3875
Evan Cheng1d2e9952006-03-24 01:17:21 +00003876 // If there are only two unique elements, we may be able to turn this into a
3877 // vector shuffle.
3878 if (Values.size() == 2) {
3879 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3880 MVT::ValueType MaskVT =
3881 MVT::getIntVectorWithNumElements(NumElems);
3882 std::vector<SDOperand> MaskVec(NumElems);
3883 unsigned i = 0;
3884 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3885 E = Values.end(); I != E; ++I) {
3886 for (std::vector<unsigned>::iterator II = I->second.begin(),
3887 EE = I->second.end(); II != EE; ++II)
3888 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3889 i += NumElems;
3890 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003891 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3892 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00003893
3894 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00003895 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3896 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003897 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003898 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3899 E = Values.end(); I != E; ++I) {
3900 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3901 I->first);
3902 Ops.push_back(Op);
3903 }
3904 Ops.push_back(ShuffleMask);
3905
3906 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003907 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
3908 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00003909 }
3910 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003911
3912 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3913 // aligned object on the stack, store each element into it, then load
3914 // the result as a vector.
3915 MVT::ValueType VT = Node->getValueType(0);
3916 // Create the stack frame object.
3917 SDOperand FIPtr = CreateStackTemporary(VT);
3918
3919 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003920 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003921 unsigned TypeByteSize =
3922 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003923 // Store (in the right endianness) the elements to memory.
3924 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3925 // Ignore undef elements.
3926 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3927
Chris Lattner8fa445a2006-03-22 01:46:54 +00003928 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003929
3930 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
3931 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
3932
Evan Chengdf9ac472006-10-05 23:01:46 +00003933 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00003934 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003935 }
3936
3937 SDOperand StoreChain;
3938 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003939 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
3940 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003941 else
3942 StoreChain = DAG.getEntryNode();
3943
3944 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003945 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003946}
3947
3948/// CreateStackTemporary - Create a stack temporary, suitable for holding the
3949/// specified value type.
3950SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
3951 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3952 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner50ee0e42007-01-20 22:35:55 +00003953 const Type *Ty = MVT::getTypeForValueType(VT);
3954 unsigned StackAlign = (unsigned)TLI.getTargetData()->getTypeAlignmentPref(Ty);
3955 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003956 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
3957}
3958
Chris Lattner4157c412005-04-02 04:00:59 +00003959void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
3960 SDOperand Op, SDOperand Amt,
3961 SDOperand &Lo, SDOperand &Hi) {
3962 // Expand the subcomponents.
3963 SDOperand LHSL, LHSH;
3964 ExpandOp(Op, LHSL, LHSH);
3965
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003966 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00003967 MVT::ValueType VT = LHSL.getValueType();
3968 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00003969 Hi = Lo.getValue(1);
3970}
3971
3972
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003973/// ExpandShift - Try to find a clever way to expand this shift operation out to
3974/// smaller elements. If we can't find a way that is more efficient than a
3975/// libcall on this target, return false. Otherwise, return true with the
3976/// low-parts expanded into Lo and Hi.
3977bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
3978 SDOperand &Lo, SDOperand &Hi) {
3979 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
3980 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00003981
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003982 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00003983 SDOperand ShAmt = LegalizeOp(Amt);
3984 MVT::ValueType ShTy = ShAmt.getValueType();
3985 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
3986 unsigned NVTBits = MVT::getSizeInBits(NVT);
3987
3988 // Handle the case when Amt is an immediate. Other cases are currently broken
3989 // and are disabled.
3990 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
3991 unsigned Cst = CN->getValue();
3992 // Expand the incoming operand to be shifted, so that we have its parts
3993 SDOperand InL, InH;
3994 ExpandOp(Op, InL, InH);
3995 switch(Opc) {
3996 case ISD::SHL:
3997 if (Cst > VTBits) {
3998 Lo = DAG.getConstant(0, NVT);
3999 Hi = DAG.getConstant(0, NVT);
4000 } else if (Cst > NVTBits) {
4001 Lo = DAG.getConstant(0, NVT);
4002 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004003 } else if (Cst == NVTBits) {
4004 Lo = DAG.getConstant(0, NVT);
4005 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00004006 } else {
4007 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4008 Hi = DAG.getNode(ISD::OR, NVT,
4009 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4010 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4011 }
4012 return true;
4013 case ISD::SRL:
4014 if (Cst > VTBits) {
4015 Lo = DAG.getConstant(0, NVT);
4016 Hi = DAG.getConstant(0, NVT);
4017 } else if (Cst > NVTBits) {
4018 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4019 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00004020 } else if (Cst == NVTBits) {
4021 Lo = InH;
4022 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00004023 } else {
4024 Lo = DAG.getNode(ISD::OR, NVT,
4025 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4026 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4027 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4028 }
4029 return true;
4030 case ISD::SRA:
4031 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004032 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004033 DAG.getConstant(NVTBits-1, ShTy));
4034 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004035 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004036 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004037 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004038 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004039 } else if (Cst == NVTBits) {
4040 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004041 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004042 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004043 } else {
4044 Lo = DAG.getNode(ISD::OR, NVT,
4045 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4046 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4047 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4048 }
4049 return true;
4050 }
4051 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004052
4053 // Okay, the shift amount isn't constant. However, if we can tell that it is
4054 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4055 uint64_t Mask = NVTBits, KnownZero, KnownOne;
4056 TLI.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
4057
4058 // If we know that the high bit of the shift amount is one, then we can do
4059 // this as a couple of simple shifts.
4060 if (KnownOne & Mask) {
4061 // Mask out the high bit, which we know is set.
4062 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4063 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4064
4065 // Expand the incoming operand to be shifted, so that we have its parts
4066 SDOperand InL, InH;
4067 ExpandOp(Op, InL, InH);
4068 switch(Opc) {
4069 case ISD::SHL:
4070 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4071 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4072 return true;
4073 case ISD::SRL:
4074 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4075 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4076 return true;
4077 case ISD::SRA:
4078 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4079 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4080 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4081 return true;
4082 }
4083 }
4084
4085 // If we know that the high bit of the shift amount is zero, then we can do
4086 // this as a couple of simple shifts.
4087 if (KnownZero & Mask) {
4088 // Compute 32-amt.
4089 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4090 DAG.getConstant(NVTBits, Amt.getValueType()),
4091 Amt);
4092
4093 // Expand the incoming operand to be shifted, so that we have its parts
4094 SDOperand InL, InH;
4095 ExpandOp(Op, InL, InH);
4096 switch(Opc) {
4097 case ISD::SHL:
4098 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4099 Hi = DAG.getNode(ISD::OR, NVT,
4100 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4101 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4102 return true;
4103 case ISD::SRL:
4104 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4105 Lo = DAG.getNode(ISD::OR, NVT,
4106 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4107 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4108 return true;
4109 case ISD::SRA:
4110 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4111 Lo = DAG.getNode(ISD::OR, NVT,
4112 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4113 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4114 return true;
4115 }
4116 }
4117
Nate Begemanb0674922005-04-06 21:13:14 +00004118 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004119}
Chris Lattneraac464e2005-01-21 06:05:23 +00004120
Chris Lattner4add7e32005-01-23 04:42:50 +00004121
Chris Lattneraac464e2005-01-21 06:05:23 +00004122// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4123// does not fit into a register, return the lo part and set the hi part to the
4124// by-reg argument. If it does fit into a single register, return the result
4125// and leave the Hi part unset.
4126SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004127 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004128 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4129 // The input chain to this libcall is the entry node of the function.
4130 // Legalizing the call will automatically add the previous call to the
4131 // dependence.
4132 SDOperand InChain = DAG.getEntryNode();
4133
Chris Lattneraac464e2005-01-21 06:05:23 +00004134 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004135 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004136 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4137 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4138 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004139 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00004140 Entry.isSigned = isSigned; Entry.isInReg = false; Entry.isSRet = false;
Reid Spencere63b6512006-12-31 05:55:36 +00004141 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004142 }
4143 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004144
Chris Lattner06bbeb62005-05-11 19:02:11 +00004145 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004146 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004147 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004148 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004149 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004150
Chris Lattner462505f2006-02-13 09:18:02 +00004151 // Legalize the call sequence, starting with the chain. This will advance
4152 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4153 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4154 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004155 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004156 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004157 default: assert(0 && "Unknown thing");
4158 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004159 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004160 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004161 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004162 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004163 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004164 }
Chris Lattner63022662005-09-02 20:26:58 +00004165 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004166}
4167
Chris Lattner4add7e32005-01-23 04:42:50 +00004168
Chris Lattneraac464e2005-01-21 06:05:23 +00004169/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
4170/// destination type is legal.
4171SDOperand SelectionDAGLegalize::
4172ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004173 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00004174 assert(getTypeAction(Source.getValueType()) == Expand &&
4175 "This is not an expansion!");
4176 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4177
Chris Lattner06bbeb62005-05-11 19:02:11 +00004178 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004179 assert(Source.getValueType() == MVT::i64 &&
4180 "This only works for 64-bit -> FP");
4181 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4182 // incoming integer is set. To handle this, we dynamically test to see if
4183 // it is set, and, if so, add a fudge factor.
4184 SDOperand Lo, Hi;
4185 ExpandOp(Source, Lo, Hi);
4186
Chris Lattner2a4f7312005-05-13 04:45:13 +00004187 // If this is unsigned, and not supported, first perform the conversion to
4188 // signed, then adjust the result if the sign bit is set.
4189 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4190 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4191
Chris Lattnerd47675e2005-08-09 20:20:18 +00004192 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4193 DAG.getConstant(0, Hi.getValueType()),
4194 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004195 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4196 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4197 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004198 uint64_t FF = 0x5f800000ULL;
4199 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004200 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004201
Chris Lattnerc30405e2005-08-26 17:15:30 +00004202 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004203 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4204 SDOperand FudgeInReg;
4205 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004206 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004207 else {
4208 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004209 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004210 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004211 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00004212 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00004213 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00004214
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004215 // Check to see if the target has a custom way to lower this. If so, use it.
4216 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4217 default: assert(0 && "This action not implemented for this operation!");
4218 case TargetLowering::Legal:
4219 case TargetLowering::Expand:
4220 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004221 case TargetLowering::Custom: {
4222 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4223 Source), DAG);
4224 if (NV.Val)
4225 return LegalizeOp(NV);
4226 break; // The target decided this was legal after all
4227 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004228 }
4229
Chris Lattner153587e2005-05-12 07:00:44 +00004230 // Expand the source, then glue it back together for the call. We must expand
4231 // the source in case it is shared (this pass of legalize must traverse it).
4232 SDOperand SrcLo, SrcHi;
4233 ExpandOp(Source, SrcLo, SrcHi);
4234 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4235
Evan Cheng31cbddf2007-01-12 02:11:51 +00004236 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004237 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004238 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004239 else {
4240 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00004241 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004242 }
Chris Lattner462505f2006-02-13 09:18:02 +00004243
4244 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4245 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004246 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4247 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00004248}
Misha Brukman835702a2005-04-21 22:36:52 +00004249
Chris Lattner689bdcc2006-01-28 08:25:58 +00004250/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4251/// INT_TO_FP operation of the specified operand when the target requests that
4252/// we expand it. At this point, we know that the result and operand types are
4253/// legal for the target.
4254SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4255 SDOperand Op0,
4256 MVT::ValueType DestVT) {
4257 if (Op0.getValueType() == MVT::i32) {
4258 // simple 32-bit [signed|unsigned] integer to float/double expansion
4259
Chris Lattner50ee0e42007-01-20 22:35:55 +00004260 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner689bdcc2006-01-28 08:25:58 +00004261 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner50ee0e42007-01-20 22:35:55 +00004262 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4263 unsigned StackAlign =
4264 (unsigned)TLI.getTargetData()->getTypeAlignmentPref(F64Type);
4265 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004266 // get address of 8 byte buffer
4267 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4268 // word offset constant for Hi/Lo address computation
4269 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4270 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00004271 SDOperand Hi = StackSlot;
4272 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4273 if (TLI.isLittleEndian())
4274 std::swap(Hi, Lo);
4275
Chris Lattner689bdcc2006-01-28 08:25:58 +00004276 // if signed map to unsigned space
4277 SDOperand Op0Mapped;
4278 if (isSigned) {
4279 // constant used to invert sign bit (signed to unsigned mapping)
4280 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4281 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4282 } else {
4283 Op0Mapped = Op0;
4284 }
4285 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00004286 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00004287 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004288 // initial hi portion of constructed double
4289 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4290 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00004291 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004292 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00004293 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004294 // FP constant to bias correct the final result
4295 SDOperand Bias = DAG.getConstantFP(isSigned ?
4296 BitsToDouble(0x4330000080000000ULL)
4297 : BitsToDouble(0x4330000000000000ULL),
4298 MVT::f64);
4299 // subtract the bias
4300 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4301 // final result
4302 SDOperand Result;
4303 // handle final rounding
4304 if (DestVT == MVT::f64) {
4305 // do nothing
4306 Result = Sub;
4307 } else {
4308 // if f32 then cast to f32
4309 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4310 }
4311 return Result;
4312 }
4313 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4314 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4315
4316 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4317 DAG.getConstant(0, Op0.getValueType()),
4318 ISD::SETLT);
4319 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4320 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4321 SignSet, Four, Zero);
4322
4323 // If the sign bit of the integer is set, the large number will be treated
4324 // as a negative number. To counteract this, the dynamic code adds an
4325 // offset depending on the data type.
4326 uint64_t FF;
4327 switch (Op0.getValueType()) {
4328 default: assert(0 && "Unsupported integer type!");
4329 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4330 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4331 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4332 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4333 }
4334 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004335 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004336
4337 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4338 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4339 SDOperand FudgeInReg;
4340 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004341 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004342 else {
4343 assert(DestVT == MVT::f64 && "Unexpected conversion");
4344 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4345 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00004346 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00004347 }
4348
4349 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4350}
4351
4352/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4353/// *INT_TO_FP operation of the specified operand when the target requests that
4354/// we promote it. At this point, we know that the result and operand types are
4355/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4356/// operation that takes a larger input.
4357SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4358 MVT::ValueType DestVT,
4359 bool isSigned) {
4360 // First step, figure out the appropriate *INT_TO_FP operation to use.
4361 MVT::ValueType NewInTy = LegalOp.getValueType();
4362
4363 unsigned OpToUse = 0;
4364
4365 // Scan for the appropriate larger type to use.
4366 while (1) {
4367 NewInTy = (MVT::ValueType)(NewInTy+1);
4368 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4369
4370 // If the target supports SINT_TO_FP of this type, use it.
4371 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4372 default: break;
4373 case TargetLowering::Legal:
4374 if (!TLI.isTypeLegal(NewInTy))
4375 break; // Can't use this datatype.
4376 // FALL THROUGH.
4377 case TargetLowering::Custom:
4378 OpToUse = ISD::SINT_TO_FP;
4379 break;
4380 }
4381 if (OpToUse) break;
4382 if (isSigned) continue;
4383
4384 // If the target supports UINT_TO_FP of this type, use it.
4385 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4386 default: break;
4387 case TargetLowering::Legal:
4388 if (!TLI.isTypeLegal(NewInTy))
4389 break; // Can't use this datatype.
4390 // FALL THROUGH.
4391 case TargetLowering::Custom:
4392 OpToUse = ISD::UINT_TO_FP;
4393 break;
4394 }
4395 if (OpToUse) break;
4396
4397 // Otherwise, try a larger type.
4398 }
4399
4400 // Okay, we found the operation and type to use. Zero extend our input to the
4401 // desired type then run the operation on it.
4402 return DAG.getNode(OpToUse, DestVT,
4403 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4404 NewInTy, LegalOp));
4405}
4406
4407/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4408/// FP_TO_*INT operation of the specified operand when the target requests that
4409/// we promote it. At this point, we know that the result and operand types are
4410/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4411/// operation that returns a larger result.
4412SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4413 MVT::ValueType DestVT,
4414 bool isSigned) {
4415 // First step, figure out the appropriate FP_TO*INT operation to use.
4416 MVT::ValueType NewOutTy = DestVT;
4417
4418 unsigned OpToUse = 0;
4419
4420 // Scan for the appropriate larger type to use.
4421 while (1) {
4422 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4423 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4424
4425 // If the target supports FP_TO_SINT returning this type, use it.
4426 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4427 default: break;
4428 case TargetLowering::Legal:
4429 if (!TLI.isTypeLegal(NewOutTy))
4430 break; // Can't use this datatype.
4431 // FALL THROUGH.
4432 case TargetLowering::Custom:
4433 OpToUse = ISD::FP_TO_SINT;
4434 break;
4435 }
4436 if (OpToUse) break;
4437
4438 // If the target supports FP_TO_UINT of this type, use it.
4439 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4440 default: break;
4441 case TargetLowering::Legal:
4442 if (!TLI.isTypeLegal(NewOutTy))
4443 break; // Can't use this datatype.
4444 // FALL THROUGH.
4445 case TargetLowering::Custom:
4446 OpToUse = ISD::FP_TO_UINT;
4447 break;
4448 }
4449 if (OpToUse) break;
4450
4451 // Otherwise, try a larger type.
4452 }
4453
4454 // Okay, we found the operation and type to use. Truncate the result of the
4455 // extended FP_TO_*INT operation to the desired size.
4456 return DAG.getNode(ISD::TRUNCATE, DestVT,
4457 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4458}
4459
4460/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4461///
4462SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4463 MVT::ValueType VT = Op.getValueType();
4464 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4465 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4466 switch (VT) {
4467 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4468 case MVT::i16:
4469 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4470 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4471 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4472 case MVT::i32:
4473 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4474 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4475 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4476 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4477 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4478 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4479 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4480 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4481 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4482 case MVT::i64:
4483 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4484 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4485 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4486 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4487 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4488 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4489 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4490 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4491 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4492 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4493 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4494 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4495 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4496 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4497 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4498 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4499 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4500 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4501 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4502 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4503 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4504 }
4505}
4506
4507/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4508///
4509SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4510 switch (Opc) {
4511 default: assert(0 && "Cannot expand this yet!");
4512 case ISD::CTPOP: {
4513 static const uint64_t mask[6] = {
4514 0x5555555555555555ULL, 0x3333333333333333ULL,
4515 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4516 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4517 };
4518 MVT::ValueType VT = Op.getValueType();
4519 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4520 unsigned len = getSizeInBits(VT);
4521 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4522 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4523 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4524 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4525 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4526 DAG.getNode(ISD::AND, VT,
4527 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4528 }
4529 return Op;
4530 }
4531 case ISD::CTLZ: {
4532 // for now, we do this:
4533 // x = x | (x >> 1);
4534 // x = x | (x >> 2);
4535 // ...
4536 // x = x | (x >>16);
4537 // x = x | (x >>32); // for 64-bit input
4538 // return popcount(~x);
4539 //
4540 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4541 MVT::ValueType VT = Op.getValueType();
4542 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4543 unsigned len = getSizeInBits(VT);
4544 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4545 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4546 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4547 }
4548 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4549 return DAG.getNode(ISD::CTPOP, VT, Op);
4550 }
4551 case ISD::CTTZ: {
4552 // for now, we use: { return popcount(~x & (x - 1)); }
4553 // unless the target has ctlz but not ctpop, in which case we use:
4554 // { return 32 - nlz(~x & (x-1)); }
4555 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4556 MVT::ValueType VT = Op.getValueType();
4557 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4558 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4559 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4560 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4561 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4562 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4563 TLI.isOperationLegal(ISD::CTLZ, VT))
4564 return DAG.getNode(ISD::SUB, VT,
4565 DAG.getConstant(getSizeInBits(VT), VT),
4566 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4567 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4568 }
4569 }
4570}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004571
Chris Lattnerdc750592005-01-07 07:47:09 +00004572/// ExpandOp - Expand the specified SDOperand into its two component pieces
4573/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4574/// LegalizeNodes map is filled in for any results that are not expanded, the
4575/// ExpandedNodes map is filled in for any results that are expanded, and the
4576/// Lo/Hi values are returned.
4577void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4578 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00004579 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00004580 SDNode *Node = Op.Val;
4581 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00004582 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
4583 VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00004584 "Cannot expand to FP value or to larger int value!");
4585
Chris Lattner1a570f12005-09-02 20:32:45 +00004586 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00004587 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00004588 = ExpandedNodes.find(Op);
4589 if (I != ExpandedNodes.end()) {
4590 Lo = I->second.first;
4591 Hi = I->second.second;
4592 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00004593 }
4594
Chris Lattnerdc750592005-01-07 07:47:09 +00004595 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00004596 case ISD::CopyFromReg:
4597 assert(0 && "CopyFromReg must be legal!");
4598 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004599#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +00004600 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004601#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00004602 assert(0 && "Do not know how to expand this operator!");
4603 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004604 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00004605 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00004606 Lo = DAG.getNode(ISD::UNDEF, NVT);
4607 Hi = DAG.getNode(ISD::UNDEF, NVT);
4608 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004609 case ISD::Constant: {
4610 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4611 Lo = DAG.getConstant(Cst, NVT);
4612 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4613 break;
4614 }
Evan Cheng47833a12006-12-12 21:32:44 +00004615 case ISD::ConstantFP: {
4616 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng3766fc602006-12-12 22:19:28 +00004617 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00004618 if (getTypeAction(Lo.getValueType()) == Expand)
4619 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00004620 break;
4621 }
Chris Lattner32e08b72005-03-28 22:03:13 +00004622 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004623 // Return the operands.
4624 Lo = Node->getOperand(0);
4625 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00004626 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004627
4628 case ISD::SIGN_EXTEND_INREG:
4629 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00004630 // sext_inreg the low part if needed.
4631 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4632
4633 // The high part gets the sign extension from the lo-part. This handles
4634 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004635 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4636 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4637 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004638 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00004639
Nate Begeman2fba8a32006-01-14 03:14:10 +00004640 case ISD::BSWAP: {
4641 ExpandOp(Node->getOperand(0), Lo, Hi);
4642 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4643 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4644 Lo = TempLo;
4645 break;
4646 }
4647
Chris Lattner55e9cde2005-05-11 04:51:16 +00004648 case ISD::CTPOP:
4649 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00004650 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4651 DAG.getNode(ISD::CTPOP, NVT, Lo),
4652 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00004653 Hi = DAG.getConstant(0, NVT);
4654 break;
4655
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004656 case ISD::CTLZ: {
4657 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004658 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004659 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4660 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004661 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4662 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004663 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4664 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4665
4666 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4667 Hi = DAG.getConstant(0, NVT);
4668 break;
4669 }
4670
4671 case ISD::CTTZ: {
4672 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004673 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004674 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4675 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004676 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4677 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004678 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4679 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4680
4681 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4682 Hi = DAG.getConstant(0, NVT);
4683 break;
4684 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00004685
Nate Begemane74795c2006-01-25 18:21:52 +00004686 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004687 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4688 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00004689 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4690 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4691
4692 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004693 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00004694 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4695 if (!TLI.isLittleEndian())
4696 std::swap(Lo, Hi);
4697 break;
4698 }
4699
Chris Lattnerdc750592005-01-07 07:47:09 +00004700 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00004701 LoadSDNode *LD = cast<LoadSDNode>(Node);
4702 SDOperand Ch = LD->getChain(); // Legalize the chain.
4703 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4704 ISD::LoadExtType ExtType = LD->getExtensionType();
Chris Lattnerdc750592005-01-07 07:47:09 +00004705
Evan Chenge71fe34d2006-10-09 20:57:25 +00004706 if (ExtType == ISD::NON_EXTLOAD) {
Chris Lattner296a83c2007-02-01 04:55:59 +00004707 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),LD->getSrcValueOffset());
Evan Cheng47833a12006-12-12 21:32:44 +00004708 if (VT == MVT::f32 || VT == MVT::f64) {
4709 // f32->i32 or f64->i64 one to one expansion.
4710 // Remember that we legalized the chain.
4711 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00004712 // Recursively expand the new load.
4713 if (getTypeAction(NVT) == Expand)
4714 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00004715 break;
4716 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00004717
Evan Chenge71fe34d2006-10-09 20:57:25 +00004718 // Increment the pointer to the other half.
4719 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4720 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4721 getIntPtrConstant(IncrementSize));
4722 // FIXME: This creates a bogus srcvalue!
Chris Lattner296a83c2007-02-01 04:55:59 +00004723 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),LD->getSrcValueOffset());
Misha Brukman835702a2005-04-21 22:36:52 +00004724
Evan Chenge71fe34d2006-10-09 20:57:25 +00004725 // Build a factor node to remember that this load is independent of the
4726 // other one.
4727 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4728 Hi.getValue(1));
4729
4730 // Remember that we legalized the chain.
4731 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4732 if (!TLI.isLittleEndian())
4733 std::swap(Lo, Hi);
4734 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00004735 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00004736
4737 if (VT == MVT::f64 && EVT == MVT::f32) {
4738 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4739 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
4740 LD->getSrcValueOffset());
4741 // Remember that we legalized the chain.
4742 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4743 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4744 break;
4745 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00004746
4747 if (EVT == NVT)
4748 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
4749 LD->getSrcValueOffset());
4750 else
4751 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
4752 LD->getSrcValueOffset(), EVT);
4753
4754 // Remember that we legalized the chain.
4755 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4756
4757 if (ExtType == ISD::SEXTLOAD) {
4758 // The high part is obtained by SRA'ing all but one of the bits of the
4759 // lo part.
4760 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4761 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4762 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4763 } else if (ExtType == ISD::ZEXTLOAD) {
4764 // The high part is just a zero.
4765 Hi = DAG.getConstant(0, NVT);
4766 } else /* if (ExtType == ISD::EXTLOAD) */ {
4767 // The high part is undefined.
4768 Hi = DAG.getNode(ISD::UNDEF, NVT);
4769 }
4770 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004771 break;
4772 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004773 case ISD::AND:
4774 case ISD::OR:
4775 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4776 SDOperand LL, LH, RL, RH;
4777 ExpandOp(Node->getOperand(0), LL, LH);
4778 ExpandOp(Node->getOperand(1), RL, RH);
4779 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4780 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4781 break;
4782 }
4783 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004784 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00004785 ExpandOp(Node->getOperand(1), LL, LH);
4786 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00004787 if (getTypeAction(NVT) == Expand)
4788 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004789 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00004790 if (VT != MVT::f32)
4791 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00004792 break;
4793 }
Nate Begemane5b86d72005-08-10 20:51:12 +00004794 case ISD::SELECT_CC: {
4795 SDOperand TL, TH, FL, FH;
4796 ExpandOp(Node->getOperand(2), TL, TH);
4797 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00004798 if (getTypeAction(NVT) == Expand)
4799 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00004800 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4801 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00004802 if (VT != MVT::f32)
4803 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4804 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004805 break;
4806 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004807 case ISD::ANY_EXTEND:
4808 // The low part is any extension of the input (which degenerates to a copy).
4809 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4810 // The high part is undefined.
4811 Hi = DAG.getNode(ISD::UNDEF, NVT);
4812 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004813 case ISD::SIGN_EXTEND: {
4814 // The low part is just a sign extension of the input (which degenerates to
4815 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004816 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004817
Chris Lattnerdc750592005-01-07 07:47:09 +00004818 // The high part is obtained by SRA'ing all but one of the bits of the lo
4819 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004820 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004821 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4822 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004823 break;
4824 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004825 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00004826 // The low part is just a zero extension of the input (which degenerates to
4827 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004828 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004829
Chris Lattnerdc750592005-01-07 07:47:09 +00004830 // The high part is just a zero.
4831 Hi = DAG.getConstant(0, NVT);
4832 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004833
4834 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004835 SDOperand Tmp;
4836 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
4837 // If the target wants to, allow it to lower this itself.
4838 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4839 case Expand: assert(0 && "cannot expand FP!");
4840 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
4841 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
4842 }
4843 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
4844 }
4845
Evan Cheng4eee7242006-12-09 02:42:38 +00004846 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00004847 if (VT == MVT::f32 || VT == MVT::f64) {
4848 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00004849 if (getTypeAction(NVT) == Expand)
4850 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00004851 break;
4852 }
4853
4854 // If source operand will be expanded to the same type as VT, i.e.
4855 // i64 <- f64, i32 <- f32, expand the source operand instead.
4856 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
4857 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
4858 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00004859 break;
4860 }
4861
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004862 // Turn this into a load/store pair by default.
4863 if (Tmp.Val == 0)
Evan Cheng3432ab92006-12-11 19:27:14 +00004864 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004865
Chris Lattner36e663d2005-12-23 00:16:34 +00004866 ExpandOp(Tmp, Lo, Hi);
4867 break;
4868 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004869
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004870 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00004871 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4872 TargetLowering::Custom &&
4873 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004874 Lo = TLI.LowerOperation(Op, DAG);
4875 assert(Lo.Val && "Node must be custom expanded!");
4876 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00004877 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004878 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004879 break;
4880
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004881 // These operators cannot be expanded directly, emit them as calls to
4882 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00004883 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00004884 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00004885 SDOperand Op;
4886 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4887 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004888 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4889 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00004890 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004891
Chris Lattner941d84a2005-07-30 01:40:57 +00004892 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4893
Chris Lattnerfe68d752005-07-29 00:33:32 +00004894 // Now that the custom expander is done, expand the result, which is still
4895 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004896 if (Op.Val) {
4897 ExpandOp(Op, Lo, Hi);
4898 break;
4899 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004900 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004901
Evan Cheng31cbddf2007-01-12 02:11:51 +00004902 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004903 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004904 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004905 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00004906 LC = RTLIB::FPTOSINT_F64_I64;
4907 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
4908 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004909 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004910 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004911
Evan Cheng31cbddf2007-01-12 02:11:51 +00004912 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00004913 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004914 SDOperand Op;
4915 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4916 case Expand: assert(0 && "cannot expand FP!");
4917 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4918 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
4919 }
4920
4921 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
4922
4923 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004924 if (Op.Val) {
4925 ExpandOp(Op, Lo, Hi);
4926 break;
4927 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004928 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004929
Evan Cheng31cbddf2007-01-12 02:11:51 +00004930 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004931 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004932 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004933 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00004934 LC = RTLIB::FPTOUINT_F64_I64;
4935 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
4936 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004937 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004938 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004939
Evan Cheng870e4f82006-01-09 18:31:59 +00004940 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004941 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004942 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004943 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004944 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004945 Op = TLI.LowerOperation(Op, DAG);
4946 if (Op.Val) {
4947 // Now that the custom expander is done, expand the result, which is
4948 // still VT.
4949 ExpandOp(Op, Lo, Hi);
4950 break;
4951 }
4952 }
4953
Chris Lattner72b503b2006-09-13 03:50:39 +00004954 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
4955 // this X << 1 as X+X.
4956 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
4957 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
4958 TLI.isOperationLegal(ISD::ADDE, NVT)) {
4959 SDOperand LoOps[2], HiOps[3];
4960 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
4961 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
4962 LoOps[1] = LoOps[0];
4963 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
4964
4965 HiOps[1] = HiOps[0];
4966 HiOps[2] = Lo.getValue(1);
4967 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
4968 break;
4969 }
4970 }
4971
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004972 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00004973 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004974 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00004975
4976 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00004977 TargetLowering::LegalizeAction Action =
4978 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
4979 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4980 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00004981 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00004982 break;
4983 }
4984
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004985 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00004986 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
4987 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004988 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00004989 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004990
Evan Cheng870e4f82006-01-09 18:31:59 +00004991 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004992 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00004993 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004994 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004995 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00004996 Op = TLI.LowerOperation(Op, DAG);
4997 if (Op.Val) {
4998 // Now that the custom expander is done, expand the result, which is
4999 // still VT.
5000 ExpandOp(Op, Lo, Hi);
5001 break;
5002 }
5003 }
5004
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005005 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005006 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005007 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005008
5009 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005010 TargetLowering::LegalizeAction Action =
5011 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5012 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5013 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005014 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005015 break;
5016 }
5017
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005018 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005019 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5020 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005021 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005022 }
5023
5024 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005025 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005026 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005027 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005028 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005029 Op = TLI.LowerOperation(Op, DAG);
5030 if (Op.Val) {
5031 // Now that the custom expander is done, expand the result, which is
5032 // still VT.
5033 ExpandOp(Op, Lo, Hi);
5034 break;
5035 }
5036 }
5037
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005038 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005039 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005040 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005041
5042 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005043 TargetLowering::LegalizeAction Action =
5044 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5045 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5046 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005047 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005048 break;
5049 }
5050
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005051 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005052 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5053 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005054 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005055 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005056
Misha Brukman835702a2005-04-21 22:36:52 +00005057 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005058 case ISD::SUB: {
5059 // If the target wants to custom expand this, let them.
5060 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5061 TargetLowering::Custom) {
5062 Op = TLI.LowerOperation(Op, DAG);
5063 if (Op.Val) {
5064 ExpandOp(Op, Lo, Hi);
5065 break;
5066 }
5067 }
5068
5069 // Expand the subcomponents.
5070 SDOperand LHSL, LHSH, RHSL, RHSH;
5071 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5072 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005073 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5074 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005075 LoOps[0] = LHSL;
5076 LoOps[1] = RHSL;
5077 HiOps[0] = LHSH;
5078 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005079 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005080 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005081 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005082 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005083 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005084 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005085 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005086 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005087 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005088 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005089 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005090 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005091 // If the target wants to custom expand this, let them.
5092 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00005093 SDOperand New = TLI.LowerOperation(Op, DAG);
5094 if (New.Val) {
5095 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005096 break;
5097 }
5098 }
5099
Evan Chenge93762d2006-09-01 18:17:58 +00005100 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5101 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Chenge93762d2006-09-01 18:17:58 +00005102 if (HasMULHS || HasMULHU) {
Nate Begemanadd0c632005-04-11 03:01:51 +00005103 SDOperand LL, LH, RL, RH;
5104 ExpandOp(Node->getOperand(0), LL, LH);
5105 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00005106 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman8e20c762006-12-11 02:23:46 +00005107 // FIXME: Move this to the dag combiner.
Nate Begeman43144a22005-08-30 02:44:00 +00005108 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5109 // extended the sign bit of the low half through the upper half, and if so
5110 // emit a MULHS instead of the alternate sequence that is valid for any
5111 // i64 x i64 multiply.
Evan Chenge93762d2006-09-01 18:17:58 +00005112 if (HasMULHS &&
Nate Begeman43144a22005-08-30 02:44:00 +00005113 // is RH an extension of the sign bit of RL?
5114 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5115 RH.getOperand(1).getOpcode() == ISD::Constant &&
5116 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5117 // is LH an extension of the sign bit of LL?
5118 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5119 LH.getOperand(1).getOpcode() == ISD::Constant &&
5120 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattner1b633912006-09-16 00:21:44 +00005121 // Low part:
5122 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5123 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005124 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattner1b633912006-09-16 00:21:44 +00005125 break;
Evan Chenge93762d2006-09-01 18:17:58 +00005126 } else if (HasMULHU) {
Chris Lattner1b633912006-09-16 00:21:44 +00005127 // Low part:
5128 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5129
5130 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005131 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5132 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5133 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5134 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5135 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00005136 break;
Nate Begeman43144a22005-08-30 02:44:00 +00005137 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005138 }
Evan Chenge93762d2006-09-01 18:17:58 +00005139
Evan Cheng31cbddf2007-01-12 02:11:51 +00005140 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5141 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00005142 break;
5143 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005144 case ISD::SDIV:
5145 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5146 break;
5147 case ISD::UDIV:
5148 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5149 break;
5150 case ISD::SREM:
5151 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5152 break;
5153 case ISD::UREM:
5154 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5155 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00005156
Evan Cheng4eee7242006-12-09 02:42:38 +00005157 case ISD::FADD:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005158 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5159 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5160 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005161 break;
5162 case ISD::FSUB:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005163 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5164 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5165 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005166 break;
5167 case ISD::FMUL:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005168 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5169 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5170 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005171 break;
5172 case ISD::FDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005173 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5174 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5175 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005176 break;
5177 case ISD::FP_EXTEND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005178 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005179 break;
5180 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005181 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005182 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005183 case ISD::FSQRT:
5184 case ISD::FSIN:
5185 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005186 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00005187 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005188 case ISD::FSQRT:
5189 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5190 break;
5191 case ISD::FSIN:
5192 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5193 break;
5194 case ISD::FCOS:
5195 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5196 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005197 default: assert(0 && "Unreachable!");
5198 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005199 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00005200 break;
5201 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00005202 case ISD::FABS: {
5203 SDOperand Mask = (VT == MVT::f64)
5204 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5205 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5206 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5207 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5208 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5209 if (getTypeAction(NVT) == Expand)
5210 ExpandOp(Lo, Lo, Hi);
5211 break;
5212 }
5213 case ISD::FNEG: {
5214 SDOperand Mask = (VT == MVT::f64)
5215 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5216 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5217 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5218 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5219 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5220 if (getTypeAction(NVT) == Expand)
5221 ExpandOp(Lo, Lo, Hi);
5222 break;
5223 }
Evan Cheng003feb02007-01-04 21:56:39 +00005224 case ISD::FCOPYSIGN: {
5225 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5226 if (getTypeAction(NVT) == Expand)
5227 ExpandOp(Lo, Lo, Hi);
5228 break;
5229 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005230 case ISD::SINT_TO_FP:
5231 case ISD::UINT_TO_FP: {
5232 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5233 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00005234 RTLIB::Libcall LC;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005235 if (Node->getOperand(0).getValueType() == MVT::i64) {
5236 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005237 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005238 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005239 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005240 } else {
5241 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005242 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005243 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005244 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005245 }
5246
5247 // Promote the operand if needed.
5248 if (getTypeAction(SrcVT) == Promote) {
5249 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5250 Tmp = isSigned
5251 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5252 DAG.getValueType(SrcVT))
5253 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5254 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5255 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005256 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005257 break;
5258 }
Evan Chengf3a80c62006-12-13 02:38:13 +00005259 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005260
Chris Lattnerac12f682005-12-21 18:02:52 +00005261 // Make sure the resultant values have been legalized themselves, unless this
5262 // is a type that requires multi-step expansion.
5263 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5264 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00005265 if (Hi.Val)
5266 // Don't legalize the high part if it is expanded to a single node.
5267 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00005268 }
Evan Cheng870e4f82006-01-09 18:31:59 +00005269
5270 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005271 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00005272 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00005273}
5274
Chris Lattner32206f52006-03-18 01:44:44 +00005275/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
5276/// two smaller values of MVT::Vector type.
5277void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5278 SDOperand &Hi) {
5279 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
5280 SDNode *Node = Op.Val;
5281 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
5282 assert(NumElements > 1 && "Cannot split a single element vector!");
5283 unsigned NewNumElts = NumElements/2;
5284 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
5285 SDOperand TypeNode = *(Node->op_end()-1);
5286
5287 // See if we already split it.
5288 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5289 = SplitNodes.find(Op);
5290 if (I != SplitNodes.end()) {
5291 Lo = I->second.first;
5292 Hi = I->second.second;
5293 return;
5294 }
5295
5296 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005297 default:
5298#ifndef NDEBUG
5299 Node->dump();
5300#endif
5301 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005302 case ISD::VBUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005303 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5304 Node->op_begin()+NewNumElts);
Chris Lattner32206f52006-03-18 01:44:44 +00005305 LoOps.push_back(NewNumEltsNode);
5306 LoOps.push_back(TypeNode);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005307 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005308
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005309 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
5310 Node->op_end()-2);
Chris Lattner32206f52006-03-18 01:44:44 +00005311 HiOps.push_back(NewNumEltsNode);
5312 HiOps.push_back(TypeNode);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005313 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005314 break;
5315 }
5316 case ISD::VADD:
5317 case ISD::VSUB:
5318 case ISD::VMUL:
5319 case ISD::VSDIV:
5320 case ISD::VUDIV:
5321 case ISD::VAND:
5322 case ISD::VOR:
5323 case ISD::VXOR: {
5324 SDOperand LL, LH, RL, RH;
5325 SplitVectorOp(Node->getOperand(0), LL, LH);
5326 SplitVectorOp(Node->getOperand(1), RL, RH);
5327
5328 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
5329 NewNumEltsNode, TypeNode);
5330 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
5331 NewNumEltsNode, TypeNode);
5332 break;
5333 }
5334 case ISD::VLOAD: {
5335 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5336 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5337 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5338
5339 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5340 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
5341 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5342 getIntPtrConstant(IncrementSize));
5343 // FIXME: This creates a bogus srcvalue!
5344 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5345
5346 // Build a factor node to remember that this load is independent of the
5347 // other one.
5348 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5349 Hi.getValue(1));
5350
5351 // Remember that we legalized the chain.
5352 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00005353 break;
5354 }
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005355 case ISD::VBIT_CONVERT: {
5356 // We know the result is a vector. The input may be either a vector or a
5357 // scalar value.
5358 if (Op.getOperand(0).getValueType() != MVT::Vector) {
5359 // Lower to a store/load. FIXME: this could be improved probably.
5360 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
5361
Evan Chengdf9ac472006-10-05 23:01:46 +00005362 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00005363 Op.getOperand(0), Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005364 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5365 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
5366 SplitVectorOp(St, Lo, Hi);
5367 } else {
5368 // If the input is a vector type, we have to either scalarize it, pack it
5369 // or convert it based on whether the input vector type is legal.
5370 SDNode *InVal = Node->getOperand(0).Val;
5371 unsigned NumElems =
5372 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5373 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5374
5375 // If the input is from a single element vector, scalarize the vector,
5376 // then treat like a scalar.
5377 if (NumElems == 1) {
5378 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
5379 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
5380 Op.getOperand(1), Op.getOperand(2));
5381 SplitVectorOp(Scalar, Lo, Hi);
5382 } else {
5383 // Split the input vector.
5384 SplitVectorOp(Op.getOperand(0), Lo, Hi);
5385
5386 // Convert each of the pieces now.
5387 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
5388 NewNumEltsNode, TypeNode);
5389 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
5390 NewNumEltsNode, TypeNode);
5391 }
5392 break;
5393 }
5394 }
Chris Lattner32206f52006-03-18 01:44:44 +00005395 }
5396
5397 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005398 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00005399 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5400 assert(isNew && "Value already expanded?!?");
5401}
5402
5403
5404/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
5405/// equivalent operation that returns a scalar (e.g. F32) or packed value
5406/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
5407/// type for the result.
5408SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
5409 MVT::ValueType NewVT) {
5410 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
5411 SDNode *Node = Op.Val;
5412
5413 // See if we already packed it.
5414 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
5415 if (I != PackedNodes.end()) return I->second;
5416
5417 SDOperand Result;
5418 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00005419 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005420#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +00005421 Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005422#endif
Chris Lattner29b23012006-03-19 01:17:20 +00005423 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattner32206f52006-03-18 01:44:44 +00005424 case ISD::VADD:
5425 case ISD::VSUB:
5426 case ISD::VMUL:
5427 case ISD::VSDIV:
5428 case ISD::VUDIV:
5429 case ISD::VAND:
5430 case ISD::VOR:
5431 case ISD::VXOR:
5432 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
5433 NewVT,
5434 PackVectorOp(Node->getOperand(0), NewVT),
5435 PackVectorOp(Node->getOperand(1), NewVT));
5436 break;
5437 case ISD::VLOAD: {
Chris Lattner93640542006-03-19 00:07:49 +00005438 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
5439 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00005440
Evan Chenge71fe34d2006-10-09 20:57:25 +00005441 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
5442 Result = DAG.getLoad(NewVT, Ch, Ptr, SV->getValue(), SV->getOffset());
Chris Lattner32206f52006-03-18 01:44:44 +00005443
5444 // Remember that we legalized the chain.
5445 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5446 break;
5447 }
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005448 case ISD::VBUILD_VECTOR:
Chris Lattner5fe1f542006-03-31 02:06:56 +00005449 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005450 // Returning a scalar?
Chris Lattner32206f52006-03-18 01:44:44 +00005451 Result = Node->getOperand(0);
5452 } else {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005453 // Returning a BUILD_VECTOR?
Chris Lattnere1401e32006-04-08 05:34:25 +00005454
5455 // If all elements of the build_vector are undefs, return an undef.
5456 bool AllUndef = true;
5457 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
5458 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
5459 AllUndef = false;
5460 break;
5461 }
5462 if (AllUndef) {
5463 Result = DAG.getNode(ISD::UNDEF, NewVT);
5464 } else {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005465 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Node->op_begin(),
5466 Node->getNumOperands()-2);
Chris Lattnere1401e32006-04-08 05:34:25 +00005467 }
Chris Lattner32206f52006-03-18 01:44:44 +00005468 }
5469 break;
Chris Lattner29b23012006-03-19 01:17:20 +00005470 case ISD::VINSERT_VECTOR_ELT:
5471 if (!MVT::isVector(NewVT)) {
5472 // Returning a scalar? Must be the inserted element.
5473 Result = Node->getOperand(1);
5474 } else {
5475 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
5476 PackVectorOp(Node->getOperand(0), NewVT),
5477 Node->getOperand(1), Node->getOperand(2));
5478 }
5479 break;
Chris Lattnerf6f94d32006-03-28 20:24:43 +00005480 case ISD::VVECTOR_SHUFFLE:
5481 if (!MVT::isVector(NewVT)) {
5482 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
5483 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5484 if (cast<ConstantSDNode>(EltNum)->getValue())
5485 Result = PackVectorOp(Node->getOperand(1), NewVT);
5486 else
5487 Result = PackVectorOp(Node->getOperand(0), NewVT);
5488 } else {
5489 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
5490 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
5491 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
5492 Node->getOperand(2).Val->op_end()-2);
5493 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005494 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT,
5495 Node->getOperand(2).Val->op_begin(),
5496 Node->getOperand(2).Val->getNumOperands()-2);
Chris Lattnerf6f94d32006-03-28 20:24:43 +00005497
5498 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
5499 PackVectorOp(Node->getOperand(0), NewVT),
5500 PackVectorOp(Node->getOperand(1), NewVT), BV);
5501 }
5502 break;
Chris Lattner2f4119a2006-03-22 20:09:35 +00005503 case ISD::VBIT_CONVERT:
5504 if (Op.getOperand(0).getValueType() != MVT::Vector)
5505 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
5506 else {
5507 // If the input is a vector type, we have to either scalarize it, pack it
5508 // or convert it based on whether the input vector type is legal.
5509 SDNode *InVal = Node->getOperand(0).Val;
5510 unsigned NumElems =
5511 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5512 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5513
5514 // Figure out if there is a Packed type corresponding to this Vector
5515 // type. If so, convert to the packed type.
5516 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
5517 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
5518 // Turn this into a bit convert of the packed input.
5519 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5520 PackVectorOp(Node->getOperand(0), TVT));
5521 break;
5522 } else if (NumElems == 1) {
5523 // Turn this into a bit convert of the scalar input.
5524 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5525 PackVectorOp(Node->getOperand(0), EVT));
5526 break;
5527 } else {
5528 // FIXME: UNIMP!
5529 assert(0 && "Cast from unsupported vector type not implemented yet!");
5530 }
5531 }
Evan Chengcb73b8d2006-04-10 18:54:36 +00005532 break;
Chris Lattner02274a52006-04-08 22:22:57 +00005533 case ISD::VSELECT:
5534 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
5535 PackVectorOp(Op.getOperand(1), NewVT),
5536 PackVectorOp(Op.getOperand(2), NewVT));
5537 break;
Chris Lattner32206f52006-03-18 01:44:44 +00005538 }
5539
5540 if (TLI.isTypeLegal(NewVT))
5541 Result = LegalizeOp(Result);
5542 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
5543 assert(isNew && "Value already packed?");
5544 return Result;
5545}
5546
Chris Lattnerdc750592005-01-07 07:47:09 +00005547
5548// SelectionDAG::Legalize - This is the entry point for the file.
5549//
Chris Lattner4add7e32005-01-23 04:42:50 +00005550void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00005551 if (ViewLegalizeDAGs) viewGraph();
5552
Chris Lattnerdc750592005-01-07 07:47:09 +00005553 /// run - This is the main entry point to this class.
5554 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005555 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00005556}
5557