blob: c32311373ece11ae0e2b6f6969cbdd2267c0962e [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
Reid Spencer09575ba2007-02-15 03:39:18 +0000115 /// concrete vector types, this contains the mapping of ones we have already
Chris Lattner32206f52006-03-18 01:44:44 +0000116 /// 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;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000679 case ISD::EXCEPTIONADDR: {
680 Tmp1 = LegalizeOp(Node->getOperand(0));
681 MVT::ValueType VT = Node->getValueType(0);
682 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
683 default: assert(0 && "This action is not supported yet!");
684 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000685 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey7f5872c2007-02-22 15:37:19 +0000686 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
687 }
688 break;
689 case TargetLowering::Custom:
690 Result = TLI.LowerOperation(Op, DAG);
691 if (Result.Val) break;
692 // Fall Thru
693 case TargetLowering::Legal:
694 Result = DAG.getNode(ISD::MERGE_VALUES, VT, DAG.getConstant(0, VT), Tmp1).
695 getValue(Op.ResNo);
696 break;
697 }
698 }
699 break;
Jim Laskey644af6b2007-02-28 20:43:58 +0000700 case ISD::EHSELECTION: {
701 Tmp1 = LegalizeOp(Node->getOperand(0));
702 Tmp2 = LegalizeOp(Node->getOperand(1));
703 MVT::ValueType VT = Node->getValueType(0);
704 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
705 default: assert(0 && "This action is not supported yet!");
706 case TargetLowering::Expand: {
707 unsigned Reg = TLI.getExceptionSelectorRegister();
708 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
709 }
710 break;
711 case TargetLowering::Custom:
712 Result = TLI.LowerOperation(Op, DAG);
713 if (Result.Val) break;
714 // Fall Thru
715 case TargetLowering::Legal:
716 Result = DAG.getNode(ISD::MERGE_VALUES, VT, DAG.getConstant(0, VT), Tmp2).
717 getValue(Op.ResNo);
718 break;
719 }
720 }
721 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000722 case ISD::AssertSext:
723 case ISD::AssertZext:
724 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000725 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000726 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000727 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000728 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000729 Result = Node->getOperand(Op.ResNo);
730 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000731 case ISD::CopyFromReg:
732 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000733 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000734 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000735 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000736 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000737 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000738 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000739 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000740 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
741 } else {
742 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
743 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000744 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
745 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000746 // Since CopyFromReg produces two values, make sure to remember that we
747 // legalized both of them.
748 AddLegalizedOperand(Op.getValue(0), Result);
749 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
750 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000751 case ISD::UNDEF: {
752 MVT::ValueType VT = Op.getValueType();
753 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000754 default: assert(0 && "This action is not supported yet!");
755 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000756 if (MVT::isInteger(VT))
757 Result = DAG.getConstant(0, VT);
758 else if (MVT::isFloatingPoint(VT))
759 Result = DAG.getConstantFP(0, VT);
760 else
761 assert(0 && "Unknown value type!");
762 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000763 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000764 break;
765 }
766 break;
767 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000768
Chris Lattnere55d1712006-03-28 00:40:33 +0000769 case ISD::INTRINSIC_W_CHAIN:
770 case ISD::INTRINSIC_WO_CHAIN:
771 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +0000772 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +0000773 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
774 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000775 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +0000776
777 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000778 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000779 TargetLowering::Custom) {
780 Tmp3 = TLI.LowerOperation(Result, DAG);
781 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000782 }
783
784 if (Result.Val->getNumValues() == 1) break;
785
786 // Must have return value and chain result.
787 assert(Result.Val->getNumValues() == 2 &&
788 "Cannot return more than two values!");
789
790 // Since loads produce two values, make sure to remember that we
791 // legalized both of them.
792 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
793 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
794 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000795 }
Chris Lattner435b4022005-11-29 06:21:05 +0000796
797 case ISD::LOCATION:
798 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
799 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
800
801 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
802 case TargetLowering::Promote:
803 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000804 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +0000805 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000806 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +0000807 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000808
Jim Laskeyc56315c2007-01-26 21:22:28 +0000809 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000810 const std::string &FName =
811 cast<StringSDNode>(Node->getOperand(3))->getValue();
812 const std::string &DirName =
813 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000814 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000815
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000816 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +0000817 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000818 SDOperand LineOp = Node->getOperand(1);
819 SDOperand ColOp = Node->getOperand(2);
820
821 if (useDEBUG_LOC) {
822 Ops.push_back(LineOp); // line #
823 Ops.push_back(ColOp); // col #
824 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000825 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000826 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000827 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
828 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000829 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000830 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskeyf9e54452007-01-26 14:34:52 +0000831 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000832 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000833 } else {
834 Result = Tmp1; // chain
835 }
Chris Lattner435b4022005-11-29 06:21:05 +0000836 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000837 }
Chris Lattner435b4022005-11-29 06:21:05 +0000838 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000839 if (Tmp1 != Node->getOperand(0) ||
840 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +0000841 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +0000842 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000843 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
844 Ops.push_back(Node->getOperand(1)); // line # must be legal.
845 Ops.push_back(Node->getOperand(2)); // col # must be legal.
846 } else {
847 // Otherwise promote them.
848 Ops.push_back(PromoteOp(Node->getOperand(1)));
849 Ops.push_back(PromoteOp(Node->getOperand(2)));
850 }
Chris Lattner435b4022005-11-29 06:21:05 +0000851 Ops.push_back(Node->getOperand(3)); // filename must be legal.
852 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +0000853 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +0000854 }
855 break;
856 }
857 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000858
859 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000860 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000861 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000862 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000863 case TargetLowering::Legal:
864 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
865 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
866 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
867 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000868 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000869 break;
870 }
871 break;
872
Jim Laskeyf9e54452007-01-26 14:34:52 +0000873 case ISD::LABEL:
874 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
875 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000876 default: assert(0 && "This action is not supported yet!");
877 case TargetLowering::Legal:
878 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
879 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000880 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000881 break;
882 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000883 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000884
Chris Lattnerdc750592005-01-07 07:47:09 +0000885 case ISD::Constant:
886 // We know we don't need to expand constants here, constants only have one
887 // value and we check that it is fine above.
888
889 // FIXME: Maybe we should handle things like targets that don't support full
890 // 32-bit immediates?
891 break;
892 case ISD::ConstantFP: {
893 // Spill FP immediates to the constant pool if the target cannot directly
894 // codegen them. Targets often have some immediate values that can be
895 // efficiently generated into an FP register without a load. We explicitly
896 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000897 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
898
899 // Check to see if this FP immediate is already legal.
900 bool isLegal = false;
901 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
902 E = TLI.legal_fpimm_end(); I != E; ++I)
903 if (CFP->isExactlyValue(*I)) {
904 isLegal = true;
905 break;
906 }
907
Chris Lattner758b0ac2006-01-29 06:26:56 +0000908 // If this is a legal constant, turn it into a TargetConstantFP node.
909 if (isLegal) {
910 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
911 break;
912 }
913
914 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
915 default: assert(0 && "This action is not supported yet!");
916 case TargetLowering::Custom:
917 Tmp3 = TLI.LowerOperation(Result, DAG);
918 if (Tmp3.Val) {
919 Result = Tmp3;
920 break;
921 }
922 // FALLTHROUGH
923 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +0000924 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +0000925 }
926 break;
927 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000928 case ISD::TokenFactor:
929 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000930 Tmp1 = LegalizeOp(Node->getOperand(0));
931 Tmp2 = LegalizeOp(Node->getOperand(1));
932 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
933 } else if (Node->getNumOperands() == 3) {
934 Tmp1 = LegalizeOp(Node->getOperand(0));
935 Tmp2 = LegalizeOp(Node->getOperand(1));
936 Tmp3 = LegalizeOp(Node->getOperand(2));
937 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000938 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +0000939 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000940 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000941 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
942 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000943 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +0000944 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000945 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000946
947 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +0000948 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000949 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +0000950 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
951 assert(Tmp3.Val && "Target didn't custom lower this node!");
952 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
953 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000954
Chris Lattneraaa23d92006-05-16 22:53:20 +0000955 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000956 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +0000957 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
958 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000959 if (Op.ResNo == i)
960 Tmp2 = Tmp1;
961 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
962 }
963 return Tmp2;
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000964
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000965 case ISD::BUILD_VECTOR:
966 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000967 default: assert(0 && "This action is not supported yet!");
968 case TargetLowering::Custom:
969 Tmp3 = TLI.LowerOperation(Result, DAG);
970 if (Tmp3.Val) {
971 Result = Tmp3;
972 break;
973 }
974 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000975 case TargetLowering::Expand:
976 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000977 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000978 }
Chris Lattner29b23012006-03-19 01:17:20 +0000979 break;
980 case ISD::INSERT_VECTOR_ELT:
981 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
982 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
983 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
984 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
985
986 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
987 Node->getValueType(0))) {
988 default: assert(0 && "This action is not supported yet!");
989 case TargetLowering::Legal:
990 break;
991 case TargetLowering::Custom:
992 Tmp3 = TLI.LowerOperation(Result, DAG);
993 if (Tmp3.Val) {
994 Result = Tmp3;
995 break;
996 }
997 // FALLTHROUGH
998 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +0000999 // If the insert index is a constant, codegen this as a scalar_to_vector,
1000 // then a shuffle that inserts it into the right position in the vector.
1001 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1002 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1003 Tmp1.getValueType(), Tmp2);
1004
1005 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1006 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1007 MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT);
1008
1009 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1010 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1011 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001012 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +00001013 for (unsigned i = 0; i != NumElts; ++i) {
1014 if (i != InsertPos->getValue())
1015 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1016 else
1017 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1018 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001019 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1020 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +00001021
1022 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1023 Tmp1, ScVec, ShufMask);
1024 Result = LegalizeOp(Result);
1025 break;
1026 }
1027
Chris Lattner29b23012006-03-19 01:17:20 +00001028 // If the target doesn't support this, we have to spill the input vector
1029 // to a temporary stack slot, update the element, then reload it. This is
1030 // badness. We could also load the value into a vector register (either
1031 // with a "move to register" or "extload into register" instruction, then
1032 // permute it into place, if the idx is a constant and if the idx is
1033 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001034 MVT::ValueType VT = Tmp1.getValueType();
1035 MVT::ValueType EltVT = Tmp2.getValueType();
1036 MVT::ValueType IdxVT = Tmp3.getValueType();
1037 MVT::ValueType PtrVT = TLI.getPointerTy();
1038 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001039 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +00001040 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001041
1042 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001043 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1044 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001045 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001046 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1047 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1048 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001049 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +00001050 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001051 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +00001052 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +00001053 break;
1054 }
1055 }
1056 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001057 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001058 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1059 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1060 break;
1061 }
1062
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001063 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1064 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1065 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1066 Node->getValueType(0))) {
1067 default: assert(0 && "This action is not supported yet!");
1068 case TargetLowering::Legal:
1069 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001070 case TargetLowering::Custom:
1071 Tmp3 = TLI.LowerOperation(Result, DAG);
1072 if (Tmp3.Val) {
1073 Result = Tmp3;
1074 break;
1075 }
1076 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001077 case TargetLowering::Expand:
1078 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001079 break;
1080 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001081 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001082 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001083 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1084 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1085 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1086
1087 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001088 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1089 default: assert(0 && "Unknown operation action!");
1090 case TargetLowering::Legal:
1091 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1092 "vector shuffle should not be created if not legal!");
1093 break;
1094 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001095 Tmp3 = TLI.LowerOperation(Result, DAG);
1096 if (Tmp3.Val) {
1097 Result = Tmp3;
1098 break;
1099 }
1100 // FALLTHROUGH
1101 case TargetLowering::Expand: {
1102 MVT::ValueType VT = Node->getValueType(0);
1103 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
1104 MVT::ValueType PtrVT = TLI.getPointerTy();
1105 SDOperand Mask = Node->getOperand(2);
1106 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001107 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001108 for (unsigned i = 0; i != NumElems; ++i) {
1109 SDOperand Arg = Mask.getOperand(i);
1110 if (Arg.getOpcode() == ISD::UNDEF) {
1111 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1112 } else {
1113 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1114 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1115 if (Idx < NumElems)
1116 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1117 DAG.getConstant(Idx, PtrVT)));
1118 else
1119 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1120 DAG.getConstant(Idx - NumElems, PtrVT)));
1121 }
1122 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001123 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001124 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001125 }
Chris Lattner6be79822006-04-04 17:23:26 +00001126 case TargetLowering::Promote: {
1127 // Change base type to a different vector type.
1128 MVT::ValueType OVT = Node->getValueType(0);
1129 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1130
1131 // Cast the two input vectors.
1132 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1133 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1134
1135 // Convert the shuffle mask to the right # elements.
1136 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1137 assert(Tmp3.Val && "Shuffle not legal?");
1138 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1139 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1140 break;
1141 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001142 }
1143 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001144
1145 case ISD::EXTRACT_VECTOR_ELT:
1146 Tmp1 = LegalizeOp(Node->getOperand(0));
1147 Tmp2 = LegalizeOp(Node->getOperand(1));
1148 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner340a6b52006-03-21 21:02:03 +00001149
1150 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
1151 Tmp1.getValueType())) {
1152 default: assert(0 && "This action is not supported yet!");
1153 case TargetLowering::Legal:
1154 break;
1155 case TargetLowering::Custom:
1156 Tmp3 = TLI.LowerOperation(Result, DAG);
1157 if (Tmp3.Val) {
1158 Result = Tmp3;
1159 break;
1160 }
1161 // FALLTHROUGH
Chris Lattner42a5fca2006-04-02 05:06:04 +00001162 case TargetLowering::Expand:
1163 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner340a6b52006-03-21 21:02:03 +00001164 break;
1165 }
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001166 break;
1167
Chris Lattner6f423252006-03-31 17:55:51 +00001168 case ISD::VEXTRACT_VECTOR_ELT:
1169 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001170 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001171
Chris Lattner462505f2006-02-13 09:18:02 +00001172 case ISD::CALLSEQ_START: {
1173 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1174
1175 // Recursively Legalize all of the inputs of the call end that do not lead
1176 // to this call start. This ensures that any libcalls that need be inserted
1177 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001178 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001179 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001180 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1181 NodesLeadingTo);
1182 }
Chris Lattner462505f2006-02-13 09:18:02 +00001183
1184 // Now that we legalized all of the inputs (which may have inserted
1185 // libcalls) create the new CALLSEQ_START node.
1186 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1187
1188 // Merge in the last call, to ensure that this call start after the last
1189 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001190 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001191 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1192 Tmp1 = LegalizeOp(Tmp1);
1193 }
Chris Lattner462505f2006-02-13 09:18:02 +00001194
1195 // Do not try to legalize the target-specific arguments (#1+).
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 Lattner462505f2006-02-13 09:18:02 +00001198 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001199 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001200 }
1201
1202 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001203 AddLegalizedOperand(Op.getValue(0), Result);
1204 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1205 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1206
Chris Lattner462505f2006-02-13 09:18:02 +00001207 // Now that the callseq_start and all of the non-call nodes above this call
1208 // sequence have been legalized, legalize the call itself. During this
1209 // process, no libcalls can/will be inserted, guaranteeing that no calls
1210 // can overlap.
1211 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1212 SDOperand InCallSEQ = LastCALLSEQ_END;
1213 // Note that we are selecting this call!
1214 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1215 IsLegalizingCall = true;
1216
1217 // Legalize the call, starting from the CALLSEQ_END.
1218 LegalizeOp(LastCALLSEQ_END);
1219 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1220 return Result;
1221 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001222 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001223 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1224 // will cause this node to be legalized as well as handling libcalls right.
1225 if (LastCALLSEQ_END.Val != Node) {
1226 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001227 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001228 assert(I != LegalizedNodes.end() &&
1229 "Legalizing the call start should have legalized this node!");
1230 return I->second;
1231 }
1232
1233 // Otherwise, the call start has been legalized and everything is going
1234 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001235 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001236 // Do not try to legalize the target-specific arguments (#1+), except for
1237 // an optional flag input.
1238 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1239 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001240 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001241 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001242 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001243 }
1244 } else {
1245 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1246 if (Tmp1 != Node->getOperand(0) ||
1247 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001248 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001249 Ops[0] = Tmp1;
1250 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001251 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001252 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001253 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001254 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001255 // This finishes up call legalization.
1256 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001257
1258 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1259 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1260 if (Node->getNumValues() == 2)
1261 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1262 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001263 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001264 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1265 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1266 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001267 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001268
Chris Lattnerd02b0542006-01-28 10:58:55 +00001269 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001270 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001271 switch (TLI.getOperationAction(Node->getOpcode(),
1272 Node->getValueType(0))) {
1273 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001274 case TargetLowering::Expand: {
1275 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1276 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1277 " not tell us which reg is the stack pointer!");
1278 SDOperand Chain = Tmp1.getOperand(0);
1279 SDOperand Size = Tmp2.getOperand(1);
1280 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1281 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1282 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001283 Tmp1 = LegalizeOp(Tmp1);
1284 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001285 break;
1286 }
1287 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001288 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1289 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001290 Tmp1 = LegalizeOp(Tmp3);
1291 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001292 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001293 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001294 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001295 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001296 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001297 // Since this op produce two values, make sure to remember that we
1298 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001299 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1300 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001301 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001302 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001303 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001304 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001305 bool Changed = false;
1306 // Legalize all of the operands of the inline asm, in case they are nodes
1307 // that need to be expanded or something. Note we skip the asm string and
1308 // all of the TargetConstant flags.
1309 SDOperand Op = LegalizeOp(Ops[0]);
1310 Changed = Op != Ops[0];
1311 Ops[0] = Op;
1312
1313 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1314 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1315 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1316 for (++i; NumVals; ++i, --NumVals) {
1317 SDOperand Op = LegalizeOp(Ops[i]);
1318 if (Op != Ops[i]) {
1319 Changed = true;
1320 Ops[i] = Op;
1321 }
1322 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001323 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001324
1325 if (HasInFlag) {
1326 Op = LegalizeOp(Ops.back());
1327 Changed |= Op != Ops.back();
1328 Ops.back() = Op;
1329 }
1330
1331 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001332 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001333
1334 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001335 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001336 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1337 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001338 }
Chris Lattner68a12142005-01-07 22:12:08 +00001339 case ISD::BR:
1340 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001341 // Ensure that libcalls are emitted before a branch.
1342 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1343 Tmp1 = LegalizeOp(Tmp1);
1344 LastCALLSEQ_END = DAG.getEntryNode();
1345
Chris Lattnerd02b0542006-01-28 10:58:55 +00001346 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001347 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001348 case ISD::BRIND:
1349 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1350 // Ensure that libcalls are emitted before a branch.
1351 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1352 Tmp1 = LegalizeOp(Tmp1);
1353 LastCALLSEQ_END = DAG.getEntryNode();
1354
1355 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1356 default: assert(0 && "Indirect target must be legal type (pointer)!");
1357 case Legal:
1358 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1359 break;
1360 }
1361 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1362 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001363 case ISD::BR_JT:
1364 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1365 // Ensure that libcalls are emitted before a branch.
1366 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1367 Tmp1 = LegalizeOp(Tmp1);
1368 LastCALLSEQ_END = DAG.getEntryNode();
1369
1370 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1371 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1372
1373 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1374 default: assert(0 && "This action is not supported yet!");
1375 case TargetLowering::Legal: break;
1376 case TargetLowering::Custom:
1377 Tmp1 = TLI.LowerOperation(Result, DAG);
1378 if (Tmp1.Val) Result = Tmp1;
1379 break;
1380 case TargetLowering::Expand: {
1381 SDOperand Chain = Result.getOperand(0);
1382 SDOperand Table = Result.getOperand(1);
1383 SDOperand Index = Result.getOperand(2);
1384
1385 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001386 MachineFunction &MF = DAG.getMachineFunction();
1387 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001388 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1389 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001390
1391 SDOperand LD;
1392 switch (EntrySize) {
1393 default: assert(0 && "Size of jump table not supported yet."); break;
1394 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1395 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1396 }
1397
1398 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001399 // For PIC, the sequence is:
1400 // BRIND(load(Jumptable + index) + RelocBase)
1401 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1402 SDOperand Reloc;
1403 if (TLI.usesGlobalOffsetTable())
1404 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1405 else
1406 Reloc = Table;
Evan Chenge6d58472006-10-31 02:31:00 +00001407 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng84a28d42006-10-30 08:00:44 +00001408 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1409 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1410 } else {
1411 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1412 }
1413 }
1414 }
1415 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001416 case ISD::BRCOND:
1417 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001418 // Ensure that libcalls are emitted before a return.
1419 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1420 Tmp1 = LegalizeOp(Tmp1);
1421 LastCALLSEQ_END = DAG.getEntryNode();
1422
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001423 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1424 case Expand: assert(0 && "It's impossible to expand bools");
1425 case Legal:
1426 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1427 break;
1428 case Promote:
1429 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001430
1431 // The top bits of the promoted condition are not necessarily zero, ensure
1432 // that the value is properly zero extended.
1433 if (!TLI.MaskedValueIsZero(Tmp2,
1434 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1435 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001436 break;
1437 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001438
1439 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001440 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001441
1442 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1443 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001444 case TargetLowering::Legal: break;
1445 case TargetLowering::Custom:
1446 Tmp1 = TLI.LowerOperation(Result, DAG);
1447 if (Tmp1.Val) Result = Tmp1;
1448 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001449 case TargetLowering::Expand:
1450 // Expand brcond's setcc into its constituent parts and create a BR_CC
1451 // Node.
1452 if (Tmp2.getOpcode() == ISD::SETCC) {
1453 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1454 Tmp2.getOperand(0), Tmp2.getOperand(1),
1455 Node->getOperand(2));
1456 } else {
1457 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1458 DAG.getCondCode(ISD::SETNE), Tmp2,
1459 DAG.getConstant(0, Tmp2.getValueType()),
1460 Node->getOperand(2));
1461 }
1462 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001463 }
1464 break;
1465 case ISD::BR_CC:
1466 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001467 // Ensure that libcalls are emitted before a branch.
1468 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1469 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001470 Tmp2 = Node->getOperand(2); // LHS
1471 Tmp3 = Node->getOperand(3); // RHS
1472 Tmp4 = Node->getOperand(1); // CC
1473
1474 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001475 LastCALLSEQ_END = DAG.getEntryNode();
1476
Nate Begeman7e7f4392006-02-01 07:19:44 +00001477 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1478 // the LHS is a legal SETCC itself. In this case, we need to compare
1479 // the result against zero to select between true and false values.
1480 if (Tmp3.Val == 0) {
1481 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1482 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001483 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001484
1485 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1486 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001487
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001488 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1489 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001490 case TargetLowering::Legal: break;
1491 case TargetLowering::Custom:
1492 Tmp4 = TLI.LowerOperation(Result, DAG);
1493 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001494 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001495 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001496 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001497 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001498 LoadSDNode *LD = cast<LoadSDNode>(Node);
1499 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1500 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001501
Evan Chenge71fe34d2006-10-09 20:57:25 +00001502 ISD::LoadExtType ExtType = LD->getExtensionType();
1503 if (ExtType == ISD::NON_EXTLOAD) {
1504 MVT::ValueType VT = Node->getValueType(0);
1505 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1506 Tmp3 = Result.getValue(0);
1507 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001508
Evan Chenge71fe34d2006-10-09 20:57:25 +00001509 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1510 default: assert(0 && "This action is not supported yet!");
1511 case TargetLowering::Legal: break;
1512 case TargetLowering::Custom:
1513 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1514 if (Tmp1.Val) {
1515 Tmp3 = LegalizeOp(Tmp1);
1516 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001517 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001518 break;
1519 case TargetLowering::Promote: {
1520 // Only promote a load of vector type to another.
1521 assert(MVT::isVector(VT) && "Cannot promote this load!");
1522 // Change base type to a different vector type.
1523 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1524
1525 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1526 LD->getSrcValueOffset());
1527 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1528 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001529 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001530 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001531 }
1532 // Since loads produce two values, make sure to remember that we
1533 // legalized both of them.
1534 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1535 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1536 return Op.ResNo ? Tmp4 : Tmp3;
1537 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001538 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001539 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1540 default: assert(0 && "This action is not supported yet!");
1541 case TargetLowering::Promote:
1542 assert(SrcVT == MVT::i1 &&
1543 "Can only promote extending LOAD from i1 -> i8!");
1544 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1545 LD->getSrcValue(), LD->getSrcValueOffset(),
1546 MVT::i8);
1547 Tmp1 = Result.getValue(0);
1548 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001549 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001550 case TargetLowering::Custom:
1551 isCustom = true;
1552 // FALLTHROUGH
1553 case TargetLowering::Legal:
1554 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1555 Tmp1 = Result.getValue(0);
1556 Tmp2 = Result.getValue(1);
1557
1558 if (isCustom) {
1559 Tmp3 = TLI.LowerOperation(Result, DAG);
1560 if (Tmp3.Val) {
1561 Tmp1 = LegalizeOp(Tmp3);
1562 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1563 }
1564 }
1565 break;
1566 case TargetLowering::Expand:
1567 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1568 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1569 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1570 LD->getSrcValueOffset());
1571 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1572 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1573 Tmp2 = LegalizeOp(Load.getValue(1));
1574 break;
1575 }
Chris Lattner296a83c2007-02-01 04:55:59 +00001576 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001577 // Turn the unsupported load into an EXTLOAD followed by an explicit
1578 // zero/sign extend inreg.
1579 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1580 Tmp1, Tmp2, LD->getSrcValue(),
1581 LD->getSrcValueOffset(), SrcVT);
1582 SDOperand ValRes;
1583 if (ExtType == ISD::SEXTLOAD)
1584 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1585 Result, DAG.getValueType(SrcVT));
1586 else
1587 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1588 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1589 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1590 break;
1591 }
1592 // Since loads produce two values, make sure to remember that we legalized
1593 // both of them.
1594 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1595 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1596 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001597 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001598 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001599 case ISD::EXTRACT_ELEMENT: {
1600 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1601 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001602 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001603 case Legal:
1604 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1605 // 1 -> Hi
1606 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1607 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1608 TLI.getShiftAmountTy()));
1609 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1610 } else {
1611 // 0 -> Lo
1612 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1613 Node->getOperand(0));
1614 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001615 break;
1616 case Expand:
1617 // Get both the low and high parts.
1618 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1619 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1620 Result = Tmp2; // 1 -> Hi
1621 else
1622 Result = Tmp1; // 0 -> Lo
1623 break;
1624 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001625 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001626 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001627
1628 case ISD::CopyToReg:
1629 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001630
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001631 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001632 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001633 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001634 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001635 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001636 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001637 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001638 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001639 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001640 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001641 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1642 Tmp3);
1643 } else {
1644 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001645 }
1646
1647 // Since this produces two values, make sure to remember that we legalized
1648 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001649 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001650 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001651 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001652 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001653 break;
1654
1655 case ISD::RET:
1656 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001657
1658 // Ensure that libcalls are emitted before a return.
1659 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1660 Tmp1 = LegalizeOp(Tmp1);
1661 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001662
Chris Lattnerdc750592005-01-07 07:47:09 +00001663 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001664 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001665 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001666 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001667 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001668 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001669 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001670 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001671 case Expand:
1672 if (Tmp2.getValueType() != MVT::Vector) {
1673 SDOperand Lo, Hi;
1674 ExpandOp(Tmp2, Lo, Hi);
Evan Cheng3432ab92006-12-11 19:27:14 +00001675 if (Hi.Val)
1676 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1677 else
1678 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001679 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001680 } else {
1681 SDNode *InVal = Tmp2.Val;
1682 unsigned NumElems =
1683 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1684 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1685
1686 // Figure out if there is a Packed type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001687 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001688 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1689 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001690 // Turn this into a return of the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001691 Tmp2 = PackVectorOp(Tmp2, TVT);
Evan Chenga2e99532006-05-26 23:09:09 +00001692 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001693 } else if (NumElems == 1) {
1694 // Turn this into a return of the scalar type.
1695 Tmp2 = PackVectorOp(Tmp2, EVT);
Evan Chenga2e99532006-05-26 23:09:09 +00001696 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001697
1698 // FIXME: Returns of gcc generic vectors smaller than a legal type
1699 // should be returned in integer registers!
1700
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001701 // The scalarized value type may not be legal, e.g. it might require
1702 // promotion or expansion. Relegalize the return.
1703 Result = LegalizeOp(Result);
1704 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001705 // FIXME: Returns of gcc generic vectors larger than a legal vector
1706 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001707 SDOperand Lo, Hi;
1708 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00001709 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001710 Result = LegalizeOp(Result);
1711 }
1712 }
Misha Brukman835702a2005-04-21 22:36:52 +00001713 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001714 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001715 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00001716 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001717 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001718 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001719 }
1720 break;
1721 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001722 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001723 break;
1724 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00001725 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00001726 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00001727 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00001728 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1729 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001730 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00001731 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001732 break;
1733 case Expand: {
1734 SDOperand Lo, Hi;
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001735 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1736 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001737 ExpandOp(Node->getOperand(i), Lo, Hi);
1738 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00001739 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00001740 if (Hi.Val) {
1741 NewValues.push_back(Hi);
1742 NewValues.push_back(Node->getOperand(i+1));
1743 }
Misha Brukman835702a2005-04-21 22:36:52 +00001744 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001745 }
1746 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001747 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001748 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001749
1750 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00001751 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00001752 else
Chris Lattner97af9d52006-08-08 01:09:31 +00001753 Result = DAG.getNode(ISD::RET, MVT::Other,
1754 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00001755 break;
1756 }
1757 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001758
Chris Lattner4d1ea712006-01-29 21:02:23 +00001759 if (Result.getOpcode() == ISD::RET) {
1760 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1761 default: assert(0 && "This action is not supported yet!");
1762 case TargetLowering::Legal: break;
1763 case TargetLowering::Custom:
1764 Tmp1 = TLI.LowerOperation(Result, DAG);
1765 if (Tmp1.Val) Result = Tmp1;
1766 break;
1767 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001768 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001769 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001770 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00001771 StoreSDNode *ST = cast<StoreSDNode>(Node);
1772 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1773 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Chris Lattnerdc750592005-01-07 07:47:09 +00001774
Evan Chengab51cf22006-10-13 21:14:26 +00001775 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001776 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1777 // FIXME: We shouldn't do this for TargetConstantFP's.
1778 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1779 // to phase ordering between legalized code and the dag combiner. This
1780 // probably means that we need to integrate dag combiner and legalizer
1781 // together.
1782 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1783 if (CFP->getValueType(0) == MVT::f32) {
1784 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1785 } else {
1786 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1787 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1788 }
1789 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1790 ST->getSrcValueOffset());
1791 break;
1792 }
1793
Evan Chengab51cf22006-10-13 21:14:26 +00001794 switch (getTypeAction(ST->getStoredVT())) {
1795 case Legal: {
1796 Tmp3 = LegalizeOp(ST->getValue());
1797 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1798 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00001799
Evan Chengab51cf22006-10-13 21:14:26 +00001800 MVT::ValueType VT = Tmp3.getValueType();
1801 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1802 default: assert(0 && "This action is not supported yet!");
1803 case TargetLowering::Legal: break;
1804 case TargetLowering::Custom:
1805 Tmp1 = TLI.LowerOperation(Result, DAG);
1806 if (Tmp1.Val) Result = Tmp1;
1807 break;
1808 case TargetLowering::Promote:
1809 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1810 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1811 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1812 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
1813 ST->getSrcValue(), ST->getSrcValueOffset());
1814 break;
1815 }
1816 break;
1817 }
1818 case Promote:
1819 // Truncate the value and store the result.
1820 Tmp3 = PromoteOp(ST->getValue());
1821 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1822 ST->getSrcValueOffset(), ST->getStoredVT());
1823 break;
1824
1825 case Expand:
1826 unsigned IncrementSize = 0;
1827 SDOperand Lo, Hi;
1828
1829 // If this is a vector type, then we have to calculate the increment as
1830 // the product of the element size in bytes, and the number of elements
1831 // in the high half of the vector.
1832 if (ST->getValue().getValueType() == MVT::Vector) {
1833 SDNode *InVal = ST->getValue().Val;
1834 unsigned NumElems =
1835 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1836 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1837
1838 // Figure out if there is a Packed type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001839 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00001840 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1841 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001842 // Turn this into a normal store of the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00001843 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1844 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1845 ST->getSrcValueOffset());
1846 Result = LegalizeOp(Result);
1847 break;
1848 } else if (NumElems == 1) {
1849 // Turn this into a normal store of the scalar type.
1850 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1851 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1852 ST->getSrcValueOffset());
1853 // The scalarized value type may not be legal, e.g. it might require
1854 // promotion or expansion. Relegalize the scalar store.
1855 Result = LegalizeOp(Result);
1856 break;
1857 } else {
1858 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1859 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1860 }
1861 } else {
1862 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00001863 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00001864
1865 if (!TLI.isLittleEndian())
1866 std::swap(Lo, Hi);
1867 }
1868
1869 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
1870 ST->getSrcValueOffset());
Evan Cheng0076ca02006-12-12 19:53:13 +00001871
1872 if (Hi.Val == NULL) {
1873 // Must be int <-> float one-to-one expansion.
1874 Result = Lo;
1875 break;
1876 }
1877
Evan Chengab51cf22006-10-13 21:14:26 +00001878 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1879 getIntPtrConstant(IncrementSize));
1880 assert(isTypeLegal(Tmp2.getValueType()) &&
1881 "Pointers must be legal!");
1882 // FIXME: This sets the srcvalue of both halves to be the same, which is
1883 // wrong.
1884 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
1885 ST->getSrcValueOffset());
1886 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1887 break;
1888 }
1889 } else {
1890 // Truncating store
1891 assert(isTypeLegal(ST->getValue().getValueType()) &&
1892 "Cannot handle illegal TRUNCSTORE yet!");
1893 Tmp3 = LegalizeOp(ST->getValue());
1894
1895 // The only promote case we handle is TRUNCSTORE:i1 X into
1896 // -> TRUNCSTORE:i8 (and X, 1)
1897 if (ST->getStoredVT() == MVT::i1 &&
1898 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1899 // Promote the bool to a mask then store.
1900 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1901 DAG.getConstant(1, Tmp3.getValueType()));
1902 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1903 ST->getSrcValueOffset(), MVT::i8);
1904 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1905 Tmp2 != ST->getBasePtr()) {
1906 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1907 ST->getOffset());
1908 }
1909
1910 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1911 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001912 default: assert(0 && "This action is not supported yet!");
Evan Chengab51cf22006-10-13 21:14:26 +00001913 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001914 case TargetLowering::Custom:
1915 Tmp1 = TLI.LowerOperation(Result, DAG);
1916 if (Tmp1.Val) Result = Tmp1;
1917 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001918 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001919 }
1920 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001921 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001922 case ISD::PCMARKER:
1923 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001924 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001925 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001926 case ISD::STACKSAVE:
1927 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001928 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1929 Tmp1 = Result.getValue(0);
1930 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001931
Chris Lattnerb3266452006-01-13 02:50:02 +00001932 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1933 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001934 case TargetLowering::Legal: break;
1935 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001936 Tmp3 = TLI.LowerOperation(Result, DAG);
1937 if (Tmp3.Val) {
1938 Tmp1 = LegalizeOp(Tmp3);
1939 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001940 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001941 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001942 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001943 // Expand to CopyFromReg if the target set
1944 // StackPointerRegisterToSaveRestore.
1945 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001946 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001947 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001948 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001949 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001950 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1951 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001952 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001953 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001954 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001955
1956 // Since stacksave produce two values, make sure to remember that we
1957 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001958 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1959 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1960 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001961
Chris Lattnerb3266452006-01-13 02:50:02 +00001962 case ISD::STACKRESTORE:
1963 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1964 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001965 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001966
1967 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1968 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001969 case TargetLowering::Legal: break;
1970 case TargetLowering::Custom:
1971 Tmp1 = TLI.LowerOperation(Result, DAG);
1972 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001973 break;
1974 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001975 // Expand to CopyToReg if the target set
1976 // StackPointerRegisterToSaveRestore.
1977 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1978 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1979 } else {
1980 Result = Tmp1;
1981 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001982 break;
1983 }
1984 break;
1985
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001986 case ISD::READCYCLECOUNTER:
1987 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001988 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00001989 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
1990 Node->getValueType(0))) {
1991 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00001992 case TargetLowering::Legal:
1993 Tmp1 = Result.getValue(0);
1994 Tmp2 = Result.getValue(1);
1995 break;
Evan Cheng69739932006-11-29 08:26:18 +00001996 case TargetLowering::Custom:
1997 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00001998 Tmp1 = LegalizeOp(Result.getValue(0));
1999 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002000 break;
2001 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002002
2003 // Since rdcc produce two values, make sure to remember that we legalized
2004 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002005 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2006 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002007 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002008
Chris Lattner39c67442005-01-14 22:08:15 +00002009 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002010 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2011 case Expand: assert(0 && "It's impossible to expand bools");
2012 case Legal:
2013 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2014 break;
2015 case Promote:
2016 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002017 // Make sure the condition is either zero or one.
2018 if (!TLI.MaskedValueIsZero(Tmp1,
2019 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2020 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002021 break;
2022 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002023 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002024 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002025
Chris Lattnerd02b0542006-01-28 10:58:55 +00002026 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002027
Nate Begeman987121a2005-08-23 04:29:48 +00002028 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002029 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002030 case TargetLowering::Legal: break;
2031 case TargetLowering::Custom: {
2032 Tmp1 = TLI.LowerOperation(Result, DAG);
2033 if (Tmp1.Val) Result = Tmp1;
2034 break;
2035 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002036 case TargetLowering::Expand:
2037 if (Tmp1.getOpcode() == ISD::SETCC) {
2038 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2039 Tmp2, Tmp3,
2040 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2041 } else {
2042 Result = DAG.getSelectCC(Tmp1,
2043 DAG.getConstant(0, Tmp1.getValueType()),
2044 Tmp2, Tmp3, ISD::SETNE);
2045 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002046 break;
2047 case TargetLowering::Promote: {
2048 MVT::ValueType NVT =
2049 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2050 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002051 if (MVT::isVector(Tmp2.getValueType())) {
2052 ExtOp = ISD::BIT_CONVERT;
2053 TruncOp = ISD::BIT_CONVERT;
2054 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002055 ExtOp = ISD::ANY_EXTEND;
2056 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002057 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002058 ExtOp = ISD::FP_EXTEND;
2059 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002060 }
2061 // Promote each of the values to the new type.
2062 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2063 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2064 // Perform the larger operation, then round down.
2065 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2066 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2067 break;
2068 }
2069 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002070 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002071 case ISD::SELECT_CC: {
2072 Tmp1 = Node->getOperand(0); // LHS
2073 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002074 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2075 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002076 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002077
Nate Begeman7e7f4392006-02-01 07:19:44 +00002078 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2079
2080 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2081 // the LHS is a legal SETCC itself. In this case, we need to compare
2082 // the result against zero to select between true and false values.
2083 if (Tmp2.Val == 0) {
2084 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2085 CC = DAG.getCondCode(ISD::SETNE);
2086 }
2087 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2088
2089 // Everything is legal, see if we should expand this op or something.
2090 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2091 default: assert(0 && "This action is not supported yet!");
2092 case TargetLowering::Legal: break;
2093 case TargetLowering::Custom:
2094 Tmp1 = TLI.LowerOperation(Result, DAG);
2095 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002096 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002097 }
2098 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002099 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002100 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002101 Tmp1 = Node->getOperand(0);
2102 Tmp2 = Node->getOperand(1);
2103 Tmp3 = Node->getOperand(2);
2104 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2105
2106 // If we had to Expand the SetCC operands into a SELECT node, then it may
2107 // not always be possible to return a true LHS & RHS. In this case, just
2108 // return the value we legalized, returned in the LHS
2109 if (Tmp2.Val == 0) {
2110 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002111 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002112 }
Nate Begeman987121a2005-08-23 04:29:48 +00002113
Chris Lattnerf263a232006-01-30 22:43:50 +00002114 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002115 default: assert(0 && "Cannot handle this action for SETCC yet!");
2116 case TargetLowering::Custom:
2117 isCustom = true;
2118 // FALLTHROUGH.
2119 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002120 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002121 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002122 Tmp4 = TLI.LowerOperation(Result, DAG);
2123 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002124 }
Nate Begeman987121a2005-08-23 04:29:48 +00002125 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002126 case TargetLowering::Promote: {
2127 // First step, figure out the appropriate operation to use.
2128 // Allow SETCC to not be supported for all legal data types
2129 // Mostly this targets FP
2130 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002131 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002132
2133 // Scan for the appropriate larger type to use.
2134 while (1) {
2135 NewInTy = (MVT::ValueType)(NewInTy+1);
2136
2137 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2138 "Fell off of the edge of the integer world");
2139 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2140 "Fell off of the edge of the floating point world");
2141
2142 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002143 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002144 break;
2145 }
2146 if (MVT::isInteger(NewInTy))
2147 assert(0 && "Cannot promote Legal Integer SETCC yet");
2148 else {
2149 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2150 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2151 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002152 Tmp1 = LegalizeOp(Tmp1);
2153 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002154 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002155 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002156 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002157 }
Nate Begeman987121a2005-08-23 04:29:48 +00002158 case TargetLowering::Expand:
2159 // Expand a setcc node into a select_cc of the same condition, lhs, and
2160 // rhs that selects between const 1 (true) and const 0 (false).
2161 MVT::ValueType VT = Node->getValueType(0);
2162 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2163 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002164 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002165 break;
2166 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002167 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002168 case ISD::MEMSET:
2169 case ISD::MEMCPY:
2170 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002171 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002172 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2173
2174 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2175 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2176 case Expand: assert(0 && "Cannot expand a byte!");
2177 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002178 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002179 break;
2180 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002181 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002182 break;
2183 }
2184 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002185 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002186 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002187
2188 SDOperand Tmp4;
2189 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002190 case Expand: {
2191 // Length is too big, just take the lo-part of the length.
2192 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002193 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002194 break;
2195 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002196 case Legal:
2197 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002198 break;
2199 case Promote:
2200 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002201 break;
2202 }
2203
2204 SDOperand Tmp5;
2205 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2206 case Expand: assert(0 && "Cannot expand this yet!");
2207 case Legal:
2208 Tmp5 = LegalizeOp(Node->getOperand(4));
2209 break;
2210 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002211 Tmp5 = PromoteOp(Node->getOperand(4));
2212 break;
2213 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002214
2215 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2216 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002217 case TargetLowering::Custom:
2218 isCustom = true;
2219 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00002220 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002221 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002222 if (isCustom) {
2223 Tmp1 = TLI.LowerOperation(Result, DAG);
2224 if (Tmp1.Val) Result = Tmp1;
2225 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002226 break;
2227 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002228 // Otherwise, the target does not support this operation. Lower the
2229 // operation to an explicit libcall as appropriate.
2230 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002231 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002232 TargetLowering::ArgListTy Args;
2233 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002234
Reid Spencer6dced922005-01-12 14:53:45 +00002235 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002236 if (Node->getOpcode() == ISD::MEMSET) {
Reid Spencer791864c2007-01-03 04:22:32 +00002237 Entry.Node = Tmp2; Entry.isSigned = false; Entry.Ty = IntPtrTy;
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002238 Entry.isInReg = false; Entry.isSRet = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002239 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002240 // Extend the (previously legalized) ubyte argument to be an int value
2241 // for the call.
2242 if (Tmp3.getValueType() > MVT::i32)
2243 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2244 else
2245 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Reid Spencere63b6512006-12-31 05:55:36 +00002246 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSigned = true;
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002247 Entry.isInReg = false; Entry.isSRet = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002248 Args.push_back(Entry);
2249 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSigned = false;
2250 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002251
2252 FnName = "memset";
2253 } else if (Node->getOpcode() == ISD::MEMCPY ||
2254 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002255 Entry.Ty = IntPtrTy;
2256 Entry.isSigned = false; Entry.isInReg = false; Entry.isSRet = false;
Reid Spencer791864c2007-01-03 04:22:32 +00002257 Entry.Node = Tmp2; Args.push_back(Entry);
2258 Entry.Node = Tmp3; Args.push_back(Entry);
2259 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002260 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2261 } else {
2262 assert(0 && "Unknown op!");
2263 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002264
Chris Lattner85d70c62005-01-11 05:57:22 +00002265 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002266 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002267 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002268 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002269 break;
2270 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002271 }
2272 break;
2273 }
Chris Lattner5385db52005-05-09 20:23:03 +00002274
Chris Lattner4157c412005-04-02 04:00:59 +00002275 case ISD::SHL_PARTS:
2276 case ISD::SRA_PARTS:
2277 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002278 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002279 bool Changed = false;
2280 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2281 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2282 Changed |= Ops.back() != Node->getOperand(i);
2283 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002284 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002285 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002286
Evan Cheng870e4f82006-01-09 18:31:59 +00002287 switch (TLI.getOperationAction(Node->getOpcode(),
2288 Node->getValueType(0))) {
2289 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002290 case TargetLowering::Legal: break;
2291 case TargetLowering::Custom:
2292 Tmp1 = TLI.LowerOperation(Result, DAG);
2293 if (Tmp1.Val) {
2294 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002295 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002296 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002297 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2298 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002299 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002300 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002301 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002302 return RetVal;
2303 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002304 break;
2305 }
2306
Chris Lattner13fe99c2005-04-02 05:00:07 +00002307 // Since these produce multiple values, make sure to remember that we
2308 // legalized all of them.
2309 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2310 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2311 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002312 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002313
2314 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002315 case ISD::ADD:
2316 case ISD::SUB:
2317 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002318 case ISD::MULHS:
2319 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002320 case ISD::UDIV:
2321 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002322 case ISD::AND:
2323 case ISD::OR:
2324 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002325 case ISD::SHL:
2326 case ISD::SRL:
2327 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002328 case ISD::FADD:
2329 case ISD::FSUB:
2330 case ISD::FMUL:
2331 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002332 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002333 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2334 case Expand: assert(0 && "Not possible");
2335 case Legal:
2336 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2337 break;
2338 case Promote:
2339 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2340 break;
2341 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002342
2343 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002344
Andrew Lenharth72594262005-12-24 23:42:32 +00002345 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002346 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002347 case TargetLowering::Legal: break;
2348 case TargetLowering::Custom:
2349 Tmp1 = TLI.LowerOperation(Result, DAG);
2350 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002351 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002352 case TargetLowering::Expand: {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002353 if (Node->getValueType(0) == MVT::i32) {
2354 switch (Node->getOpcode()) {
2355 default: assert(0 && "Do not know how to expand this integer BinOp!");
2356 case ISD::UDIV:
2357 case ISD::SDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00002358 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2359 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002360 SDOperand Dummy;
Reid Spencere63b6512006-12-31 05:55:36 +00002361 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002362 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002363 };
2364 break;
2365 }
2366
Chris Lattner87f08092006-04-02 03:57:31 +00002367 assert(MVT::isVector(Node->getValueType(0)) &&
2368 "Cannot expand this binary operator!");
2369 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002370 SmallVector<SDOperand, 8> Ops;
Chris Lattner87f08092006-04-02 03:57:31 +00002371 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2372 MVT::ValueType PtrVT = TLI.getPointerTy();
2373 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2374 i != e; ++i) {
2375 SDOperand Idx = DAG.getConstant(i, PtrVT);
2376 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2377 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2378 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2379 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002380 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2381 &Ops[0], Ops.size());
Chris Lattner87f08092006-04-02 03:57:31 +00002382 break;
2383 }
Evan Cheng119266e2006-04-12 21:20:24 +00002384 case TargetLowering::Promote: {
2385 switch (Node->getOpcode()) {
2386 default: assert(0 && "Do not know how to promote this BinOp!");
2387 case ISD::AND:
2388 case ISD::OR:
2389 case ISD::XOR: {
2390 MVT::ValueType OVT = Node->getValueType(0);
2391 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2392 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2393 // Bit convert each of the values to the new type.
2394 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2395 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2396 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2397 // Bit convert the result back the original type.
2398 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2399 break;
2400 }
2401 }
2402 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002403 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002404 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002405
2406 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2407 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2408 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2409 case Expand: assert(0 && "Not possible");
2410 case Legal:
2411 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2412 break;
2413 case Promote:
2414 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2415 break;
2416 }
2417
2418 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2419
2420 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2421 default: assert(0 && "Operation not supported");
2422 case TargetLowering::Custom:
2423 Tmp1 = TLI.LowerOperation(Result, DAG);
2424 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002425 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002426 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002427 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00002428 // If this target supports fabs/fneg natively and select is cheap,
2429 // do this efficiently.
2430 if (!TLI.isSelectExpensive() &&
2431 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2432 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00002433 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00002434 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002435 // Get the sign bit of the RHS.
2436 MVT::ValueType IVT =
2437 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2438 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2439 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2440 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2441 // Get the absolute value of the result.
2442 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2443 // Select between the nabs and abs value based on the sign bit of
2444 // the input.
2445 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2446 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2447 AbsVal),
2448 AbsVal);
2449 Result = LegalizeOp(Result);
2450 break;
2451 }
2452
2453 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002454 MVT::ValueType NVT =
2455 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2456 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2457 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2458 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002459 break;
2460 }
Evan Cheng003feb02007-01-04 21:56:39 +00002461 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002462 break;
2463
Nate Begeman5965bd12006-02-17 05:43:56 +00002464 case ISD::ADDC:
2465 case ISD::SUBC:
2466 Tmp1 = LegalizeOp(Node->getOperand(0));
2467 Tmp2 = LegalizeOp(Node->getOperand(1));
2468 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2469 // Since this produces two values, make sure to remember that we legalized
2470 // both of them.
2471 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2472 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2473 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002474
Nate Begeman5965bd12006-02-17 05:43:56 +00002475 case ISD::ADDE:
2476 case ISD::SUBE:
2477 Tmp1 = LegalizeOp(Node->getOperand(0));
2478 Tmp2 = LegalizeOp(Node->getOperand(1));
2479 Tmp3 = LegalizeOp(Node->getOperand(2));
2480 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2481 // Since this produces two values, make sure to remember that we legalized
2482 // both of them.
2483 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2484 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2485 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002486
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002487 case ISD::BUILD_PAIR: {
2488 MVT::ValueType PairTy = Node->getValueType(0);
2489 // TODO: handle the case where the Lo and Hi operands are not of legal type
2490 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2491 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2492 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002493 case TargetLowering::Promote:
2494 case TargetLowering::Custom:
2495 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002496 case TargetLowering::Legal:
2497 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2498 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2499 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002500 case TargetLowering::Expand:
2501 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2502 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2503 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2504 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2505 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002506 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002507 break;
2508 }
2509 break;
2510 }
2511
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002512 case ISD::UREM:
2513 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002514 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002515 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2516 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002517
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002518 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002519 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2520 case TargetLowering::Custom:
2521 isCustom = true;
2522 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002523 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002524 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002525 if (isCustom) {
2526 Tmp1 = TLI.LowerOperation(Result, DAG);
2527 if (Tmp1.Val) Result = Tmp1;
2528 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002529 break;
Chris Lattner81914422005-08-03 20:31:37 +00002530 case TargetLowering::Expand:
Evan Cheng1fc7c362006-09-18 23:28:33 +00002531 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002532 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002533 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002534 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2535 TargetLowering::Legal) {
2536 // X % Y -> X-X/Y*Y
2537 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng1fc7c362006-09-18 23:28:33 +00002538 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002539 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2540 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2541 } else {
2542 assert(Node->getValueType(0) == MVT::i32 &&
2543 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00002544 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2545 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002546 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002547 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002548 }
Chris Lattner81914422005-08-03 20:31:37 +00002549 } else {
2550 // Floating point mod -> fmod libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002551 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2552 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner81914422005-08-03 20:31:37 +00002553 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002554 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2555 false/*sign irrelevant*/, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002556 }
2557 break;
2558 }
2559 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002560 case ISD::VAARG: {
2561 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2562 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2563
Chris Lattner364b89a2006-01-28 07:42:08 +00002564 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002565 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2566 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002567 case TargetLowering::Custom:
2568 isCustom = true;
2569 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002570 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002571 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2572 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002573 Tmp1 = Result.getValue(1);
2574
2575 if (isCustom) {
2576 Tmp2 = TLI.LowerOperation(Result, DAG);
2577 if (Tmp2.Val) {
2578 Result = LegalizeOp(Tmp2);
2579 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2580 }
2581 }
Nate Begemane74795c2006-01-25 18:21:52 +00002582 break;
2583 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002584 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00002585 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00002586 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002587 // Increment the pointer, VAList, to the next vaarg
2588 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2589 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2590 TLI.getPointerTy()));
2591 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00002592 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2593 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002594 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00002595 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002596 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002597 Result = LegalizeOp(Result);
2598 break;
2599 }
2600 }
2601 // Since VAARG produces two values, make sure to remember that we
2602 // legalized both of them.
2603 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002604 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2605 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002606 }
2607
2608 case ISD::VACOPY:
2609 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2610 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2611 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2612
2613 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2614 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002615 case TargetLowering::Custom:
2616 isCustom = true;
2617 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002618 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002619 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2620 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002621 if (isCustom) {
2622 Tmp1 = TLI.LowerOperation(Result, DAG);
2623 if (Tmp1.Val) Result = Tmp1;
2624 }
Nate Begemane74795c2006-01-25 18:21:52 +00002625 break;
2626 case TargetLowering::Expand:
2627 // This defaults to loading a pointer from the input and storing it to the
2628 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002629 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00002630 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00002631 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2632 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00002633 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2634 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002635 break;
2636 }
2637 break;
2638
2639 case ISD::VAEND:
2640 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2641 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2642
2643 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2644 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002645 case TargetLowering::Custom:
2646 isCustom = true;
2647 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002648 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002649 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002650 if (isCustom) {
2651 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2652 if (Tmp1.Val) Result = Tmp1;
2653 }
Nate Begemane74795c2006-01-25 18:21:52 +00002654 break;
2655 case TargetLowering::Expand:
2656 Result = Tmp1; // Default to a no-op, return the chain
2657 break;
2658 }
2659 break;
2660
2661 case ISD::VASTART:
2662 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2663 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2664
Chris Lattnerd02b0542006-01-28 10:58:55 +00002665 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2666
Nate Begemane74795c2006-01-25 18:21:52 +00002667 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2668 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002669 case TargetLowering::Legal: break;
2670 case TargetLowering::Custom:
2671 Tmp1 = TLI.LowerOperation(Result, DAG);
2672 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002673 break;
2674 }
2675 break;
2676
Nate Begeman1b8121b2006-01-11 21:21:00 +00002677 case ISD::ROTL:
2678 case ISD::ROTR:
2679 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2680 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002681
2682 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2683 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002684 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002685 break;
2686
Nate Begeman2fba8a32006-01-14 03:14:10 +00002687 case ISD::BSWAP:
2688 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2689 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002690 case TargetLowering::Custom:
2691 assert(0 && "Cannot custom legalize this yet!");
2692 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002693 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002694 break;
2695 case TargetLowering::Promote: {
2696 MVT::ValueType OVT = Tmp1.getValueType();
2697 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2698 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002699
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002700 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2701 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2702 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2703 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2704 break;
2705 }
2706 case TargetLowering::Expand:
2707 Result = ExpandBSWAP(Tmp1);
2708 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002709 }
2710 break;
2711
Andrew Lenharth5e177822005-05-03 17:19:30 +00002712 case ISD::CTPOP:
2713 case ISD::CTTZ:
2714 case ISD::CTLZ:
2715 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2716 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002717 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002718 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002719 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002720 break;
2721 case TargetLowering::Promote: {
2722 MVT::ValueType OVT = Tmp1.getValueType();
2723 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002724
2725 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002726 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2727 // Perform the larger operation, then subtract if needed.
2728 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002729 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002730 case ISD::CTPOP:
2731 Result = Tmp1;
2732 break;
2733 case ISD::CTTZ:
2734 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002735 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2736 DAG.getConstant(getSizeInBits(NVT), NVT),
2737 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002738 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002739 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2740 break;
2741 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002742 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002743 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2744 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002745 getSizeInBits(OVT), NVT));
2746 break;
2747 }
2748 break;
2749 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002750 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002751 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002752 break;
2753 }
2754 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002755
Chris Lattner13fe99c2005-04-02 05:00:07 +00002756 // Unary operators
2757 case ISD::FABS:
2758 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002759 case ISD::FSQRT:
2760 case ISD::FSIN:
2761 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002762 Tmp1 = LegalizeOp(Node->getOperand(0));
2763 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002764 case TargetLowering::Promote:
2765 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002766 isCustom = true;
2767 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002768 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002769 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002770 if (isCustom) {
2771 Tmp1 = TLI.LowerOperation(Result, DAG);
2772 if (Tmp1.Val) Result = Tmp1;
2773 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002774 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002775 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002776 switch (Node->getOpcode()) {
2777 default: assert(0 && "Unreachable!");
2778 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002779 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2780 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002781 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002782 break;
Chris Lattner80026402005-04-30 04:43:14 +00002783 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002784 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2785 MVT::ValueType VT = Node->getValueType(0);
2786 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002787 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002788 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2789 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002790 break;
2791 }
2792 case ISD::FSQRT:
2793 case ISD::FSIN:
2794 case ISD::FCOS: {
2795 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng31cbddf2007-01-12 02:11:51 +00002796 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00002797 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00002798 case ISD::FSQRT:
2799 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2800 break;
2801 case ISD::FSIN:
2802 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2803 break;
2804 case ISD::FCOS:
2805 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2806 break;
Chris Lattner80026402005-04-30 04:43:14 +00002807 default: assert(0 && "Unreachable!");
2808 }
Nate Begeman77558da2005-08-04 21:43:28 +00002809 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002810 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2811 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002812 break;
2813 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002814 }
2815 break;
2816 }
2817 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00002818 case ISD::FPOWI: {
2819 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002820 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2821 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattnerf0359b32006-09-09 06:03:30 +00002822 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002823 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2824 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00002825 break;
2826 }
Chris Lattner36e663d2005-12-23 00:16:34 +00002827 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002828 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002829 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002830 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002831 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2832 Node->getOperand(0).getValueType())) {
2833 default: assert(0 && "Unknown operation action!");
2834 case TargetLowering::Expand:
2835 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2836 break;
2837 case TargetLowering::Legal:
2838 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002839 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002840 break;
2841 }
2842 }
2843 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00002844 case ISD::VBIT_CONVERT: {
2845 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2846 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2847
2848 // The input has to be a vector type, we have to either scalarize it, pack
2849 // it, or convert it based on whether the input vector type is legal.
2850 SDNode *InVal = Node->getOperand(0).Val;
2851 unsigned NumElems =
2852 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2853 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2854
2855 // Figure out if there is a Packed type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002856 // type. If so, convert to the vector type.
Chris Lattnera4f68052006-03-24 02:26:29 +00002857 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2858 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2859 // Turn this into a bit convert of the packed input.
2860 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2861 PackVectorOp(Node->getOperand(0), TVT));
2862 break;
2863 } else if (NumElems == 1) {
2864 // Turn this into a bit convert of the scalar input.
2865 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2866 PackVectorOp(Node->getOperand(0), EVT));
2867 break;
2868 } else {
2869 // FIXME: UNIMP! Store then reload
2870 assert(0 && "Cast from unsupported vector type not implemented yet!");
2871 }
2872 }
2873
Chris Lattner13fe99c2005-04-02 05:00:07 +00002874 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002875 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002876 case ISD::UINT_TO_FP: {
2877 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002878 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2879 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002880 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002881 Node->getOperand(0).getValueType())) {
2882 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002883 case TargetLowering::Custom:
2884 isCustom = true;
2885 // FALLTHROUGH
2886 case TargetLowering::Legal:
2887 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002888 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002889 if (isCustom) {
2890 Tmp1 = TLI.LowerOperation(Result, DAG);
2891 if (Tmp1.Val) Result = Tmp1;
2892 }
2893 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002894 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002895 Result = ExpandLegalINT_TO_FP(isSigned,
2896 LegalizeOp(Node->getOperand(0)),
2897 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002898 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002899 case TargetLowering::Promote:
2900 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2901 Node->getValueType(0),
2902 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002903 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002904 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002905 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002906 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002907 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2908 Node->getValueType(0), Node->getOperand(0));
2909 break;
2910 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002911 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002912 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002913 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2914 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002915 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002916 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2917 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002918 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002919 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2920 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002921 break;
2922 }
2923 break;
2924 }
2925 case ISD::TRUNCATE:
2926 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2927 case Legal:
2928 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002929 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002930 break;
2931 case Expand:
2932 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2933
2934 // Since the result is legal, we should just be able to truncate the low
2935 // part of the source.
2936 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2937 break;
2938 case Promote:
2939 Result = PromoteOp(Node->getOperand(0));
2940 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2941 break;
2942 }
2943 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002944
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002945 case ISD::FP_TO_SINT:
2946 case ISD::FP_TO_UINT:
2947 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2948 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002949 Tmp1 = LegalizeOp(Node->getOperand(0));
2950
Chris Lattner44fe26f2005-07-29 00:11:56 +00002951 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2952 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002953 case TargetLowering::Custom:
2954 isCustom = true;
2955 // FALLTHROUGH
2956 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002957 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002958 if (isCustom) {
2959 Tmp1 = TLI.LowerOperation(Result, DAG);
2960 if (Tmp1.Val) Result = Tmp1;
2961 }
2962 break;
2963 case TargetLowering::Promote:
2964 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2965 Node->getOpcode() == ISD::FP_TO_SINT);
2966 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002967 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002968 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2969 SDOperand True, False;
2970 MVT::ValueType VT = Node->getOperand(0).getValueType();
2971 MVT::ValueType NVT = Node->getValueType(0);
2972 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2973 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2974 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2975 Node->getOperand(0), Tmp2, ISD::SETLT);
2976 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2977 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002978 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002979 Tmp2));
2980 False = DAG.getNode(ISD::XOR, NVT, False,
2981 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002982 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2983 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002984 } else {
2985 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2986 }
2987 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002988 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002989 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002990 case Expand: {
2991 // Convert f32 / f64 to i32 / i64.
2992 MVT::ValueType VT = Op.getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00002993 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002994 switch (Node->getOpcode()) {
2995 case ISD::FP_TO_SINT:
2996 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00002997 LC = (VT == MVT::i32)
2998 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002999 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00003000 LC = (VT == MVT::i32)
3001 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003002 break;
3003 case ISD::FP_TO_UINT:
3004 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003005 LC = (VT == MVT::i32)
3006 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003007 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00003008 LC = (VT == MVT::i32)
3009 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003010 break;
3011 default: assert(0 && "Unreachable!");
3012 }
3013 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003014 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3015 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003016 break;
3017 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003018 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003019 Tmp1 = PromoteOp(Node->getOperand(0));
3020 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3021 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003022 break;
3023 }
3024 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003025
Chris Lattner7753f172005-09-02 00:18:10 +00003026 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003027 case ISD::ZERO_EXTEND:
3028 case ISD::SIGN_EXTEND:
3029 case ISD::FP_EXTEND:
3030 case ISD::FP_ROUND:
3031 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003032 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003033 case Legal:
3034 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003035 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003036 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003037 case Promote:
3038 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003039 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003040 Tmp1 = PromoteOp(Node->getOperand(0));
3041 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003042 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003043 case ISD::ZERO_EXTEND:
3044 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003045 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003046 Result = DAG.getZeroExtendInReg(Result,
3047 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003048 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003049 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003050 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003051 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003052 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003053 Result,
3054 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003055 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003056 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003057 Result = PromoteOp(Node->getOperand(0));
3058 if (Result.getValueType() != Op.getValueType())
3059 // Dynamically dead while we have only 2 FP types.
3060 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3061 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003062 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00003063 Result = PromoteOp(Node->getOperand(0));
3064 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3065 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003066 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003067 }
3068 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003069 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003070 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003071 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003072 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003073
3074 // If this operation is not supported, convert it to a shl/shr or load/store
3075 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003076 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3077 default: assert(0 && "This action not supported for this op yet!");
3078 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003079 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003080 break;
3081 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003082 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003083 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003084 // NOTE: we could fall back on load/store here too for targets without
3085 // SAR. However, it is doubtful that any exist.
3086 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3087 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003088 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003089 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3090 Node->getOperand(0), ShiftCst);
3091 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3092 Result, ShiftCst);
3093 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003094 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003095 // EXTLOAD pair, targetting a temporary location (a stack slot).
3096
3097 // NOTE: there is a choice here between constantly creating new stack
3098 // slots and always reusing the same one. We currently always create
3099 // new ones, as reuse may inhibit scheduling.
3100 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Owen Anderson20a631f2006-05-03 01:29:57 +00003101 unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
Chris Lattner945e4372007-02-14 05:52:17 +00003102 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner99222f72005-01-15 07:15:18 +00003103 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00003104 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00003105 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
3106 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003107 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3108 StackSlot, NULL, 0, ExtraVT);
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003109 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003110 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00003111 } else {
3112 assert(0 && "Unknown op");
3113 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003114 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003115 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003116 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003117 }
Chris Lattner99222f72005-01-15 07:15:18 +00003118 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003119
Chris Lattner101ea662006-04-08 04:13:17 +00003120 assert(Result.getValueType() == Op.getValueType() &&
3121 "Bad legalization!");
3122
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003123 // Make sure that the generated code is itself legal.
3124 if (Result != Op)
3125 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003126
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003127 // Note that LegalizeOp may be reentered even from single-use nodes, which
3128 // means that we always must cache transformed nodes.
3129 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003130 return Result;
3131}
3132
Chris Lattner4d978642005-01-15 22:16:26 +00003133/// PromoteOp - Given an operation that produces a value in an invalid type,
3134/// promote it to compute the value into a larger type. The produced value will
3135/// have the correct bits for the low portion of the register, but no guarantee
3136/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003137SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3138 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003139 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003140 assert(getTypeAction(VT) == Promote &&
3141 "Caller should expand or legalize operands that are not promotable!");
3142 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3143 "Cannot promote to smaller type!");
3144
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003145 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003146 SDOperand Result;
3147 SDNode *Node = Op.Val;
3148
Chris Lattner4b0ddb22007-02-04 01:17:38 +00003149 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00003150 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003151
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003152 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003153 case ISD::CopyFromReg:
3154 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003155 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003156#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +00003157 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003158#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003159 assert(0 && "Do not know how to promote this operator!");
3160 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003161 case ISD::UNDEF:
3162 Result = DAG.getNode(ISD::UNDEF, NVT);
3163 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003164 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003165 if (VT != MVT::i1)
3166 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3167 else
3168 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003169 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3170 break;
3171 case ISD::ConstantFP:
3172 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3173 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3174 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003175
Chris Lattner2cb338d2005-01-18 02:59:52 +00003176 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003177 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003178 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3179 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003180 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003181
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003182 case ISD::TRUNCATE:
3183 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3184 case Legal:
3185 Result = LegalizeOp(Node->getOperand(0));
3186 assert(Result.getValueType() >= NVT &&
3187 "This truncation doesn't make sense!");
3188 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3189 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3190 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003191 case Promote:
3192 // The truncation is not required, because we don't guarantee anything
3193 // about high bits anyway.
3194 Result = PromoteOp(Node->getOperand(0));
3195 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003196 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003197 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3198 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003199 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003200 }
3201 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003202 case ISD::SIGN_EXTEND:
3203 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003204 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003205 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3206 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3207 case Legal:
3208 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003209 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003210 break;
3211 case Promote:
3212 // Promote the reg if it's smaller.
3213 Result = PromoteOp(Node->getOperand(0));
3214 // The high bits are not guaranteed to be anything. Insert an extend.
3215 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003216 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003217 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003218 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003219 Result = DAG.getZeroExtendInReg(Result,
3220 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003221 break;
3222 }
3223 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003224 case ISD::BIT_CONVERT:
3225 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3226 Result = PromoteOp(Result);
3227 break;
3228
Chris Lattner4d978642005-01-15 22:16:26 +00003229 case ISD::FP_EXTEND:
3230 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3231 case ISD::FP_ROUND:
3232 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3233 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3234 case Promote: assert(0 && "Unreachable with 2 FP types!");
3235 case Legal:
3236 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003237 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003238 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003239 break;
3240 }
3241 break;
3242
3243 case ISD::SINT_TO_FP:
3244 case ISD::UINT_TO_FP:
3245 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3246 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003247 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003248 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003249 break;
3250
3251 case Promote:
3252 Result = PromoteOp(Node->getOperand(0));
3253 if (Node->getOpcode() == ISD::SINT_TO_FP)
3254 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003255 Result,
3256 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003257 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003258 Result = DAG.getZeroExtendInReg(Result,
3259 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003260 // No extra round required here.
3261 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003262 break;
3263 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003264 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3265 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003266 // Round if we cannot tolerate excess precision.
3267 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003268 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3269 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003270 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003271 }
Chris Lattner4d978642005-01-15 22:16:26 +00003272 break;
3273
Chris Lattner268d4572005-12-09 17:32:47 +00003274 case ISD::SIGN_EXTEND_INREG:
3275 Result = PromoteOp(Node->getOperand(0));
3276 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3277 Node->getOperand(1));
3278 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003279 case ISD::FP_TO_SINT:
3280 case ISD::FP_TO_UINT:
3281 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3282 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003283 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003284 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003285 break;
3286 case Promote:
3287 // The input result is prerounded, so we don't have to do anything
3288 // special.
3289 Tmp1 = PromoteOp(Node->getOperand(0));
3290 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003291 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003292 // If we're promoting a UINT to a larger size, check to see if the new node
3293 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3294 // we can use that instead. This allows us to generate better code for
3295 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3296 // legal, such as PowerPC.
3297 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003298 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003299 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3300 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003301 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3302 } else {
3303 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3304 }
Chris Lattner4d978642005-01-15 22:16:26 +00003305 break;
3306
Chris Lattner13fe99c2005-04-02 05:00:07 +00003307 case ISD::FABS:
3308 case ISD::FNEG:
3309 Tmp1 = PromoteOp(Node->getOperand(0));
3310 assert(Tmp1.getValueType() == NVT);
3311 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3312 // NOTE: we do not have to do any extra rounding here for
3313 // NoExcessFPPrecision, because we know the input will have the appropriate
3314 // precision, and these operations don't modify precision at all.
3315 break;
3316
Chris Lattner9d6fa982005-04-28 21:44:33 +00003317 case ISD::FSQRT:
3318 case ISD::FSIN:
3319 case ISD::FCOS:
3320 Tmp1 = PromoteOp(Node->getOperand(0));
3321 assert(Tmp1.getValueType() == NVT);
3322 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003323 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003324 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3325 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003326 break;
3327
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003328 case ISD::AND:
3329 case ISD::OR:
3330 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003331 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003332 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003333 case ISD::MUL:
3334 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003335 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003336 // that too is okay if they are integer operations.
3337 Tmp1 = PromoteOp(Node->getOperand(0));
3338 Tmp2 = PromoteOp(Node->getOperand(1));
3339 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3340 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003341 break;
3342 case ISD::FADD:
3343 case ISD::FSUB:
3344 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003345 Tmp1 = PromoteOp(Node->getOperand(0));
3346 Tmp2 = PromoteOp(Node->getOperand(1));
3347 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3348 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3349
3350 // Floating point operations will give excess precision that we may not be
3351 // able to tolerate. If we DO allow excess precision, just leave it,
3352 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003353 // FIXME: Why would we need to round FP ops more than integer ones?
3354 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003355 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003356 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3357 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003358 break;
3359
Chris Lattner4d978642005-01-15 22:16:26 +00003360 case ISD::SDIV:
3361 case ISD::SREM:
3362 // These operators require that their input be sign extended.
3363 Tmp1 = PromoteOp(Node->getOperand(0));
3364 Tmp2 = PromoteOp(Node->getOperand(1));
3365 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003366 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3367 DAG.getValueType(VT));
3368 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3369 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003370 }
3371 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3372
3373 // Perform FP_ROUND: this is probably overly pessimistic.
3374 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003375 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3376 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003377 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003378 case ISD::FDIV:
3379 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003380 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003381 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00003382 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3383 case Legal:
3384 Tmp1 = LegalizeOp(Node->getOperand(0));
3385 break;
3386 case Promote:
3387 Tmp1 = PromoteOp(Node->getOperand(0));
3388 break;
3389 case Expand:
3390 assert(0 && "not implemented");
3391 }
3392 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3393 case Legal:
3394 Tmp2 = LegalizeOp(Node->getOperand(1));
3395 break;
3396 case Promote:
3397 Tmp2 = PromoteOp(Node->getOperand(1));
3398 break;
3399 case Expand:
3400 assert(0 && "not implemented");
3401 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00003402 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3403
3404 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003405 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003406 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3407 DAG.getValueType(VT));
3408 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003409
3410 case ISD::UDIV:
3411 case ISD::UREM:
3412 // These operators require that their input be zero extended.
3413 Tmp1 = PromoteOp(Node->getOperand(0));
3414 Tmp2 = PromoteOp(Node->getOperand(1));
3415 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003416 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3417 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003418 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3419 break;
3420
3421 case ISD::SHL:
3422 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003423 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003424 break;
3425 case ISD::SRA:
3426 // The input value must be properly sign extended.
3427 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003428 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3429 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003430 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003431 break;
3432 case ISD::SRL:
3433 // The input value must be properly zero extended.
3434 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003435 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003436 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003437 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003438
3439 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003440 Tmp1 = Node->getOperand(0); // Get the chain.
3441 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003442 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3443 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3444 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3445 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00003446 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00003447 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00003448 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003449 // Increment the pointer, VAList, to the next vaarg
3450 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3451 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3452 TLI.getPointerTy()));
3453 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00003454 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3455 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003456 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003457 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00003458 }
3459 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003460 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00003461 break;
3462
Evan Chenge71fe34d2006-10-09 20:57:25 +00003463 case ISD::LOAD: {
3464 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00003465 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3466 ? ISD::EXTLOAD : LD->getExtensionType();
3467 Result = DAG.getExtLoad(ExtType, NVT,
3468 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00003469 LD->getSrcValue(), LD->getSrcValueOffset(),
Evan Chengd35734b2006-10-11 07:10:22 +00003470 LD->getLoadedVT());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003471 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003472 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003473 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00003474 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003475 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003476 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3477 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003478 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003479 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003480 case ISD::SELECT_CC:
3481 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3482 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3483 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003484 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00003485 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003486 case ISD::BSWAP:
3487 Tmp1 = Node->getOperand(0);
3488 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3489 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3490 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3491 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3492 TLI.getShiftAmountTy()));
3493 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003494 case ISD::CTPOP:
3495 case ISD::CTTZ:
3496 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003497 // Zero extend the argument
3498 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003499 // Perform the larger operation, then subtract if needed.
3500 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003501 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003502 case ISD::CTPOP:
3503 Result = Tmp1;
3504 break;
3505 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003506 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003507 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00003508 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003509 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003510 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003511 break;
3512 case ISD::CTLZ:
3513 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003514 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3515 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003516 getSizeInBits(VT), NVT));
3517 break;
3518 }
3519 break;
Chris Lattner6f423252006-03-31 17:55:51 +00003520 case ISD::VEXTRACT_VECTOR_ELT:
3521 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3522 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00003523 case ISD::EXTRACT_VECTOR_ELT:
3524 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3525 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003526 }
3527
3528 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003529
3530 // Make sure the result is itself legal.
3531 Result = LegalizeOp(Result);
3532
3533 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003534 AddPromotedOperand(Op, Result);
3535 return Result;
3536}
Chris Lattnerdc750592005-01-07 07:47:09 +00003537
Chris Lattner6f423252006-03-31 17:55:51 +00003538/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3539/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3540/// on the vector type. The return type of this matches the element type of the
3541/// vector, which may not be legal for the target.
3542SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3543 // We know that operand #0 is the Vec vector. If the index is a constant
3544 // or if the invec is a supported hardware type, we can use it. Otherwise,
3545 // lower to a store then an indexed load.
3546 SDOperand Vec = Op.getOperand(0);
3547 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3548
3549 SDNode *InVal = Vec.Val;
3550 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3551 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3552
3553 // Figure out if there is a Packed type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00003554 // type. If so, convert to the vector type.
Chris Lattner6f423252006-03-31 17:55:51 +00003555 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3556 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3557 // Turn this into a packed extract_vector_elt operation.
3558 Vec = PackVectorOp(Vec, TVT);
3559 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3560 } else if (NumElems == 1) {
3561 // This must be an access of the only element. Return it.
3562 return PackVectorOp(Vec, EVT);
3563 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3564 SDOperand Lo, Hi;
3565 SplitVectorOp(Vec, Lo, Hi);
3566 if (CIdx->getValue() < NumElems/2) {
3567 Vec = Lo;
3568 } else {
3569 Vec = Hi;
3570 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3571 }
3572
3573 // It's now an extract from the appropriate high or low part. Recurse.
3574 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3575 return LowerVEXTRACT_VECTOR_ELT(Op);
3576 } else {
3577 // Variable index case for extract element.
3578 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3579 assert(0 && "unimp!");
3580 return SDOperand();
3581 }
3582}
3583
Chris Lattner42a5fca2006-04-02 05:06:04 +00003584/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3585/// memory traffic.
3586SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3587 SDOperand Vector = Op.getOperand(0);
3588 SDOperand Idx = Op.getOperand(1);
3589
3590 // If the target doesn't support this, store the value to a temporary
3591 // stack slot, then LOAD the scalar element back out.
3592 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
Evan Chengab51cf22006-10-13 21:14:26 +00003593 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vector, StackPtr, NULL, 0);
Chris Lattner42a5fca2006-04-02 05:06:04 +00003594
3595 // Add the offset to the index.
3596 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3597 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3598 DAG.getConstant(EltSize, Idx.getValueType()));
3599 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3600
Evan Chenge71fe34d2006-10-09 20:57:25 +00003601 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner42a5fca2006-04-02 05:06:04 +00003602}
3603
Chris Lattner6f423252006-03-31 17:55:51 +00003604
Nate Begeman7e7f4392006-02-01 07:19:44 +00003605/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3606/// with condition CC on the current target. This usually involves legalizing
3607/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3608/// there may be no choice but to create a new SetCC node to represent the
3609/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3610/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3611void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3612 SDOperand &RHS,
3613 SDOperand &CC) {
3614 SDOperand Tmp1, Tmp2, Result;
3615
3616 switch (getTypeAction(LHS.getValueType())) {
3617 case Legal:
3618 Tmp1 = LegalizeOp(LHS); // LHS
3619 Tmp2 = LegalizeOp(RHS); // RHS
3620 break;
3621 case Promote:
3622 Tmp1 = PromoteOp(LHS); // LHS
3623 Tmp2 = PromoteOp(RHS); // RHS
3624
3625 // If this is an FP compare, the operands have already been extended.
3626 if (MVT::isInteger(LHS.getValueType())) {
3627 MVT::ValueType VT = LHS.getValueType();
3628 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3629
3630 // Otherwise, we have to insert explicit sign or zero extends. Note
3631 // that we could insert sign extends for ALL conditions, but zero extend
3632 // is cheaper on many machines (an AND instead of two shifts), so prefer
3633 // it.
3634 switch (cast<CondCodeSDNode>(CC)->get()) {
3635 default: assert(0 && "Unknown integer comparison!");
3636 case ISD::SETEQ:
3637 case ISD::SETNE:
3638 case ISD::SETUGE:
3639 case ISD::SETUGT:
3640 case ISD::SETULE:
3641 case ISD::SETULT:
3642 // ALL of these operations will work if we either sign or zero extend
3643 // the operands (including the unsigned comparisons!). Zero extend is
3644 // usually a simpler/cheaper operation, so prefer it.
3645 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3646 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3647 break;
3648 case ISD::SETGE:
3649 case ISD::SETGT:
3650 case ISD::SETLT:
3651 case ISD::SETLE:
3652 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3653 DAG.getValueType(VT));
3654 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3655 DAG.getValueType(VT));
3656 break;
3657 }
3658 }
3659 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003660 case Expand: {
3661 MVT::ValueType VT = LHS.getValueType();
3662 if (VT == MVT::f32 || VT == MVT::f64) {
3663 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00003664 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003665 switch (cast<CondCodeSDNode>(CC)->get()) {
3666 case ISD::SETEQ:
3667 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003668 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003669 break;
3670 case ISD::SETNE:
3671 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003672 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003673 break;
3674 case ISD::SETGE:
3675 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003676 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003677 break;
3678 case ISD::SETLT:
3679 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003680 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003681 break;
3682 case ISD::SETLE:
3683 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003684 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003685 break;
3686 case ISD::SETGT:
3687 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003688 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003689 break;
3690 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00003691 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3692 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003693 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00003694 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003695 break;
3696 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003697 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003698 switch (cast<CondCodeSDNode>(CC)->get()) {
3699 case ISD::SETONE:
3700 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00003701 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003702 // Fallthrough
3703 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003704 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003705 break;
3706 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003707 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003708 break;
3709 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003710 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003711 break;
3712 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003713 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003714 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003715 case ISD::SETUEQ:
3716 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003717 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003718 default: assert(0 && "Unsupported FP setcc!");
3719 }
3720 }
3721
3722 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003723 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00003724 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00003725 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003726 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00003727 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00003728 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003729 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003730 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00003731 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00003732 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003733 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00003734 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003735 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3736 Tmp2 = SDOperand();
3737 }
3738 LHS = Tmp1;
3739 RHS = Tmp2;
3740 return;
3741 }
3742
Nate Begeman7e7f4392006-02-01 07:19:44 +00003743 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3744 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003745 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman7e7f4392006-02-01 07:19:44 +00003746 switch (cast<CondCodeSDNode>(CC)->get()) {
3747 case ISD::SETEQ:
3748 case ISD::SETNE:
3749 if (RHSLo == RHSHi)
3750 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3751 if (RHSCST->isAllOnesValue()) {
3752 // Comparison to -1.
3753 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3754 Tmp2 = RHSLo;
3755 break;
3756 }
3757
3758 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3759 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3760 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3761 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3762 break;
3763 default:
3764 // If this is a comparison of the sign bit, just look at the top part.
3765 // X > -1, x < 0
3766 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3767 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3768 CST->getValue() == 0) || // X < 0
3769 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3770 CST->isAllOnesValue())) { // X > -1
3771 Tmp1 = LHSHi;
3772 Tmp2 = RHSHi;
3773 break;
3774 }
3775
3776 // FIXME: This generated code sucks.
3777 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00003778 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
3779 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00003780 default: assert(0 && "Unknown integer setcc!");
3781 case ISD::SETLT:
3782 case ISD::SETULT: LowCC = ISD::SETULT; break;
3783 case ISD::SETGT:
3784 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3785 case ISD::SETLE:
3786 case ISD::SETULE: LowCC = ISD::SETULE; break;
3787 case ISD::SETGE:
3788 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3789 }
3790
3791 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3792 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3793 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3794
3795 // NOTE: on targets without efficient SELECT of bools, we can always use
3796 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00003797 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
3798 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
3799 false, DagCombineInfo);
3800 if (!Tmp1.Val)
3801 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3802 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3803 CCCode, false, DagCombineInfo);
3804 if (!Tmp2.Val)
3805 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3806
3807 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
3808 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
3809 if ((Tmp1C && Tmp1C->getValue() == 0) ||
3810 (Tmp2C && Tmp2C->getValue() == 0 &&
3811 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
3812 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
3813 (Tmp2C && Tmp2C->getValue() == 1 &&
3814 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
3815 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
3816 // low part is known false, returns high part.
3817 // For LE / GE, if high part is known false, ignore the low part.
3818 // For LT / GT, if high part is known true, ignore the low part.
3819 Tmp1 = Tmp2;
3820 Tmp2 = SDOperand();
3821 } else {
3822 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3823 ISD::SETEQ, false, DagCombineInfo);
3824 if (!Result.Val)
3825 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3826 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3827 Result, Tmp1, Tmp2));
3828 Tmp1 = Result;
3829 Tmp2 = SDOperand();
3830 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00003831 }
3832 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003833 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00003834 LHS = Tmp1;
3835 RHS = Tmp2;
3836}
3837
Chris Lattner36e663d2005-12-23 00:16:34 +00003838/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003839/// The resultant code need not be legal. Note that SrcOp is the input operand
3840/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003841SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3842 SDOperand SrcOp) {
3843 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003844 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003845
3846 // Emit a store to the stack slot.
Evan Chengab51cf22006-10-13 21:14:26 +00003847 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00003848 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003849 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00003850}
3851
Chris Lattner6be79822006-04-04 17:23:26 +00003852SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3853 // Create a vector sized/aligned stack slot, store the value to element #0,
3854 // then load the whole vector back out.
3855 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00003856 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00003857 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00003858 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00003859}
3860
3861
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003862/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3863/// support the operation, but do support the resultant packed vector type.
3864SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3865
3866 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003867 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00003868 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003869 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003870 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003871 std::map<SDOperand, std::vector<unsigned> > Values;
3872 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003873 bool isConstant = true;
3874 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3875 SplatValue.getOpcode() != ISD::UNDEF)
3876 isConstant = false;
3877
Evan Cheng1d2e9952006-03-24 01:17:21 +00003878 for (unsigned i = 1; i < NumElems; ++i) {
3879 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00003880 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003881 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003882 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003883 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00003884 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003885
3886 // If this isn't a constant element or an undef, we can't use a constant
3887 // pool load.
3888 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3889 V.getOpcode() != ISD::UNDEF)
3890 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003891 }
3892
3893 if (isOnlyLowElement) {
3894 // If the low element is an undef too, then this whole things is an undef.
3895 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3896 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3897 // Otherwise, turn this into a scalar_to_vector node.
3898 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3899 Node->getOperand(0));
3900 }
3901
Chris Lattner77e271c2006-03-24 07:29:17 +00003902 // If all elements are constants, create a load from the constant pool.
3903 if (isConstant) {
3904 MVT::ValueType VT = Node->getValueType(0);
3905 const Type *OpNTy =
3906 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3907 std::vector<Constant*> CV;
3908 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3909 if (ConstantFPSDNode *V =
3910 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3911 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3912 } else if (ConstantSDNode *V =
3913 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00003914 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00003915 } else {
3916 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3917 CV.push_back(UndefValue::get(OpNTy));
3918 }
3919 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00003920 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00003921 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00003922 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003923 }
3924
Chris Lattner21e68c82006-03-20 01:52:29 +00003925 if (SplatValue.Val) { // Splat of one value?
3926 // Build the shuffle constant vector: <0, 0, 0, 0>
3927 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00003928 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner21e68c82006-03-20 01:52:29 +00003929 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00003930 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003931 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3932 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00003933
3934 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00003935 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00003936 // Get the splatted value into the low element of a vector register.
3937 SDOperand LowValVec =
3938 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3939
3940 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3941 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3942 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3943 SplatMask);
3944 }
3945 }
3946
Evan Cheng1d2e9952006-03-24 01:17:21 +00003947 // If there are only two unique elements, we may be able to turn this into a
3948 // vector shuffle.
3949 if (Values.size() == 2) {
3950 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3951 MVT::ValueType MaskVT =
3952 MVT::getIntVectorWithNumElements(NumElems);
3953 std::vector<SDOperand> MaskVec(NumElems);
3954 unsigned i = 0;
3955 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3956 E = Values.end(); I != E; ++I) {
3957 for (std::vector<unsigned>::iterator II = I->second.begin(),
3958 EE = I->second.end(); II != EE; ++II)
3959 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3960 i += NumElems;
3961 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003962 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3963 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00003964
3965 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00003966 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3967 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003968 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003969 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3970 E = Values.end(); I != E; ++I) {
3971 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3972 I->first);
3973 Ops.push_back(Op);
3974 }
3975 Ops.push_back(ShuffleMask);
3976
3977 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003978 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
3979 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00003980 }
3981 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003982
3983 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3984 // aligned object on the stack, store each element into it, then load
3985 // the result as a vector.
3986 MVT::ValueType VT = Node->getValueType(0);
3987 // Create the stack frame object.
3988 SDOperand FIPtr = CreateStackTemporary(VT);
3989
3990 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003991 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003992 unsigned TypeByteSize =
3993 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003994 // Store (in the right endianness) the elements to memory.
3995 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3996 // Ignore undef elements.
3997 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
3998
Chris Lattner8fa445a2006-03-22 01:46:54 +00003999 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004000
4001 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4002 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4003
Evan Chengdf9ac472006-10-05 23:01:46 +00004004 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00004005 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004006 }
4007
4008 SDOperand StoreChain;
4009 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004010 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4011 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004012 else
4013 StoreChain = DAG.getEntryNode();
4014
4015 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004016 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004017}
4018
4019/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4020/// specified value type.
4021SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4022 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4023 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner50ee0e42007-01-20 22:35:55 +00004024 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattner945e4372007-02-14 05:52:17 +00004025 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004026 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004027 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4028}
4029
Chris Lattner4157c412005-04-02 04:00:59 +00004030void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4031 SDOperand Op, SDOperand Amt,
4032 SDOperand &Lo, SDOperand &Hi) {
4033 // Expand the subcomponents.
4034 SDOperand LHSL, LHSH;
4035 ExpandOp(Op, LHSL, LHSH);
4036
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004037 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00004038 MVT::ValueType VT = LHSL.getValueType();
4039 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00004040 Hi = Lo.getValue(1);
4041}
4042
4043
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004044/// ExpandShift - Try to find a clever way to expand this shift operation out to
4045/// smaller elements. If we can't find a way that is more efficient than a
4046/// libcall on this target, return false. Otherwise, return true with the
4047/// low-parts expanded into Lo and Hi.
4048bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4049 SDOperand &Lo, SDOperand &Hi) {
4050 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4051 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00004052
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004053 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00004054 SDOperand ShAmt = LegalizeOp(Amt);
4055 MVT::ValueType ShTy = ShAmt.getValueType();
4056 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4057 unsigned NVTBits = MVT::getSizeInBits(NVT);
4058
4059 // Handle the case when Amt is an immediate. Other cases are currently broken
4060 // and are disabled.
4061 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4062 unsigned Cst = CN->getValue();
4063 // Expand the incoming operand to be shifted, so that we have its parts
4064 SDOperand InL, InH;
4065 ExpandOp(Op, InL, InH);
4066 switch(Opc) {
4067 case ISD::SHL:
4068 if (Cst > VTBits) {
4069 Lo = DAG.getConstant(0, NVT);
4070 Hi = DAG.getConstant(0, NVT);
4071 } else if (Cst > NVTBits) {
4072 Lo = DAG.getConstant(0, NVT);
4073 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004074 } else if (Cst == NVTBits) {
4075 Lo = DAG.getConstant(0, NVT);
4076 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00004077 } else {
4078 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4079 Hi = DAG.getNode(ISD::OR, NVT,
4080 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4081 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4082 }
4083 return true;
4084 case ISD::SRL:
4085 if (Cst > VTBits) {
4086 Lo = DAG.getConstant(0, NVT);
4087 Hi = DAG.getConstant(0, NVT);
4088 } else if (Cst > NVTBits) {
4089 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4090 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00004091 } else if (Cst == NVTBits) {
4092 Lo = InH;
4093 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00004094 } else {
4095 Lo = DAG.getNode(ISD::OR, NVT,
4096 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4097 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4098 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4099 }
4100 return true;
4101 case ISD::SRA:
4102 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004103 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004104 DAG.getConstant(NVTBits-1, ShTy));
4105 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004106 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004107 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004108 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004109 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004110 } else if (Cst == NVTBits) {
4111 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004112 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004113 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004114 } else {
4115 Lo = DAG.getNode(ISD::OR, NVT,
4116 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4117 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4118 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4119 }
4120 return true;
4121 }
4122 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004123
4124 // Okay, the shift amount isn't constant. However, if we can tell that it is
4125 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4126 uint64_t Mask = NVTBits, KnownZero, KnownOne;
4127 TLI.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
4128
4129 // If we know that the high bit of the shift amount is one, then we can do
4130 // this as a couple of simple shifts.
4131 if (KnownOne & Mask) {
4132 // Mask out the high bit, which we know is set.
4133 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4134 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4135
4136 // Expand the incoming operand to be shifted, so that we have its parts
4137 SDOperand InL, InH;
4138 ExpandOp(Op, InL, InH);
4139 switch(Opc) {
4140 case ISD::SHL:
4141 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4142 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4143 return true;
4144 case ISD::SRL:
4145 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4146 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4147 return true;
4148 case ISD::SRA:
4149 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4150 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4151 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4152 return true;
4153 }
4154 }
4155
4156 // If we know that the high bit of the shift amount is zero, then we can do
4157 // this as a couple of simple shifts.
4158 if (KnownZero & Mask) {
4159 // Compute 32-amt.
4160 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4161 DAG.getConstant(NVTBits, Amt.getValueType()),
4162 Amt);
4163
4164 // Expand the incoming operand to be shifted, so that we have its parts
4165 SDOperand InL, InH;
4166 ExpandOp(Op, InL, InH);
4167 switch(Opc) {
4168 case ISD::SHL:
4169 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4170 Hi = DAG.getNode(ISD::OR, NVT,
4171 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4172 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4173 return true;
4174 case ISD::SRL:
4175 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4176 Lo = DAG.getNode(ISD::OR, NVT,
4177 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4178 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4179 return true;
4180 case ISD::SRA:
4181 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4182 Lo = DAG.getNode(ISD::OR, NVT,
4183 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4184 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4185 return true;
4186 }
4187 }
4188
Nate Begemanb0674922005-04-06 21:13:14 +00004189 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004190}
Chris Lattneraac464e2005-01-21 06:05:23 +00004191
Chris Lattner4add7e32005-01-23 04:42:50 +00004192
Chris Lattneraac464e2005-01-21 06:05:23 +00004193// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4194// does not fit into a register, return the lo part and set the hi part to the
4195// by-reg argument. If it does fit into a single register, return the result
4196// and leave the Hi part unset.
4197SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004198 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004199 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4200 // The input chain to this libcall is the entry node of the function.
4201 // Legalizing the call will automatically add the previous call to the
4202 // dependence.
4203 SDOperand InChain = DAG.getEntryNode();
4204
Chris Lattneraac464e2005-01-21 06:05:23 +00004205 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004206 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004207 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4208 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4209 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004210 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00004211 Entry.isSigned = isSigned; Entry.isInReg = false; Entry.isSRet = false;
Reid Spencere63b6512006-12-31 05:55:36 +00004212 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004213 }
4214 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004215
Chris Lattner06bbeb62005-05-11 19:02:11 +00004216 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004217 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004218 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004219 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004220 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004221
Chris Lattner462505f2006-02-13 09:18:02 +00004222 // Legalize the call sequence, starting with the chain. This will advance
4223 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4224 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4225 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004226 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004227 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004228 default: assert(0 && "Unknown thing");
4229 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004230 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004231 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004232 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004233 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004234 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004235 }
Chris Lattner63022662005-09-02 20:26:58 +00004236 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004237}
4238
Chris Lattner4add7e32005-01-23 04:42:50 +00004239
Chris Lattneraac464e2005-01-21 06:05:23 +00004240/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
4241/// destination type is legal.
4242SDOperand SelectionDAGLegalize::
4243ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004244 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00004245 assert(getTypeAction(Source.getValueType()) == Expand &&
4246 "This is not an expansion!");
4247 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4248
Chris Lattner06bbeb62005-05-11 19:02:11 +00004249 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004250 assert(Source.getValueType() == MVT::i64 &&
4251 "This only works for 64-bit -> FP");
4252 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4253 // incoming integer is set. To handle this, we dynamically test to see if
4254 // it is set, and, if so, add a fudge factor.
4255 SDOperand Lo, Hi;
4256 ExpandOp(Source, Lo, Hi);
4257
Chris Lattner2a4f7312005-05-13 04:45:13 +00004258 // If this is unsigned, and not supported, first perform the conversion to
4259 // signed, then adjust the result if the sign bit is set.
4260 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4261 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4262
Chris Lattnerd47675e2005-08-09 20:20:18 +00004263 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4264 DAG.getConstant(0, Hi.getValueType()),
4265 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004266 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4267 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4268 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004269 uint64_t FF = 0x5f800000ULL;
4270 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004271 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004272
Chris Lattnerc30405e2005-08-26 17:15:30 +00004273 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004274 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4275 SDOperand FudgeInReg;
4276 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004277 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004278 else {
4279 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004280 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004281 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004282 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00004283 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00004284 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00004285
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004286 // Check to see if the target has a custom way to lower this. If so, use it.
4287 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4288 default: assert(0 && "This action not implemented for this operation!");
4289 case TargetLowering::Legal:
4290 case TargetLowering::Expand:
4291 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004292 case TargetLowering::Custom: {
4293 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4294 Source), DAG);
4295 if (NV.Val)
4296 return LegalizeOp(NV);
4297 break; // The target decided this was legal after all
4298 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004299 }
4300
Chris Lattner153587e2005-05-12 07:00:44 +00004301 // Expand the source, then glue it back together for the call. We must expand
4302 // the source in case it is shared (this pass of legalize must traverse it).
4303 SDOperand SrcLo, SrcHi;
4304 ExpandOp(Source, SrcLo, SrcHi);
4305 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4306
Evan Cheng31cbddf2007-01-12 02:11:51 +00004307 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004308 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004309 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004310 else {
4311 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00004312 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004313 }
Chris Lattner462505f2006-02-13 09:18:02 +00004314
4315 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4316 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004317 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4318 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00004319}
Misha Brukman835702a2005-04-21 22:36:52 +00004320
Chris Lattner689bdcc2006-01-28 08:25:58 +00004321/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4322/// INT_TO_FP operation of the specified operand when the target requests that
4323/// we expand it. At this point, we know that the result and operand types are
4324/// legal for the target.
4325SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4326 SDOperand Op0,
4327 MVT::ValueType DestVT) {
4328 if (Op0.getValueType() == MVT::i32) {
4329 // simple 32-bit [signed|unsigned] integer to float/double expansion
4330
Chris Lattner50ee0e42007-01-20 22:35:55 +00004331 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner689bdcc2006-01-28 08:25:58 +00004332 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner50ee0e42007-01-20 22:35:55 +00004333 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4334 unsigned StackAlign =
Chris Lattner945e4372007-02-14 05:52:17 +00004335 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004336 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004337 // get address of 8 byte buffer
4338 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4339 // word offset constant for Hi/Lo address computation
4340 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4341 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00004342 SDOperand Hi = StackSlot;
4343 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4344 if (TLI.isLittleEndian())
4345 std::swap(Hi, Lo);
4346
Chris Lattner689bdcc2006-01-28 08:25:58 +00004347 // if signed map to unsigned space
4348 SDOperand Op0Mapped;
4349 if (isSigned) {
4350 // constant used to invert sign bit (signed to unsigned mapping)
4351 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4352 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4353 } else {
4354 Op0Mapped = Op0;
4355 }
4356 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00004357 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00004358 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004359 // initial hi portion of constructed double
4360 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4361 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00004362 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004363 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00004364 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004365 // FP constant to bias correct the final result
4366 SDOperand Bias = DAG.getConstantFP(isSigned ?
4367 BitsToDouble(0x4330000080000000ULL)
4368 : BitsToDouble(0x4330000000000000ULL),
4369 MVT::f64);
4370 // subtract the bias
4371 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4372 // final result
4373 SDOperand Result;
4374 // handle final rounding
4375 if (DestVT == MVT::f64) {
4376 // do nothing
4377 Result = Sub;
4378 } else {
4379 // if f32 then cast to f32
4380 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4381 }
4382 return Result;
4383 }
4384 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4385 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4386
4387 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4388 DAG.getConstant(0, Op0.getValueType()),
4389 ISD::SETLT);
4390 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4391 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4392 SignSet, Four, Zero);
4393
4394 // If the sign bit of the integer is set, the large number will be treated
4395 // as a negative number. To counteract this, the dynamic code adds an
4396 // offset depending on the data type.
4397 uint64_t FF;
4398 switch (Op0.getValueType()) {
4399 default: assert(0 && "Unsupported integer type!");
4400 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4401 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4402 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4403 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4404 }
4405 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004406 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004407
4408 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4409 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4410 SDOperand FudgeInReg;
4411 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004412 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004413 else {
4414 assert(DestVT == MVT::f64 && "Unexpected conversion");
4415 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4416 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00004417 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00004418 }
4419
4420 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4421}
4422
4423/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4424/// *INT_TO_FP operation of the specified operand when the target requests that
4425/// we promote it. At this point, we know that the result and operand types are
4426/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4427/// operation that takes a larger input.
4428SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4429 MVT::ValueType DestVT,
4430 bool isSigned) {
4431 // First step, figure out the appropriate *INT_TO_FP operation to use.
4432 MVT::ValueType NewInTy = LegalOp.getValueType();
4433
4434 unsigned OpToUse = 0;
4435
4436 // Scan for the appropriate larger type to use.
4437 while (1) {
4438 NewInTy = (MVT::ValueType)(NewInTy+1);
4439 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4440
4441 // If the target supports SINT_TO_FP of this type, use it.
4442 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4443 default: break;
4444 case TargetLowering::Legal:
4445 if (!TLI.isTypeLegal(NewInTy))
4446 break; // Can't use this datatype.
4447 // FALL THROUGH.
4448 case TargetLowering::Custom:
4449 OpToUse = ISD::SINT_TO_FP;
4450 break;
4451 }
4452 if (OpToUse) break;
4453 if (isSigned) continue;
4454
4455 // If the target supports UINT_TO_FP of this type, use it.
4456 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4457 default: break;
4458 case TargetLowering::Legal:
4459 if (!TLI.isTypeLegal(NewInTy))
4460 break; // Can't use this datatype.
4461 // FALL THROUGH.
4462 case TargetLowering::Custom:
4463 OpToUse = ISD::UINT_TO_FP;
4464 break;
4465 }
4466 if (OpToUse) break;
4467
4468 // Otherwise, try a larger type.
4469 }
4470
4471 // Okay, we found the operation and type to use. Zero extend our input to the
4472 // desired type then run the operation on it.
4473 return DAG.getNode(OpToUse, DestVT,
4474 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4475 NewInTy, LegalOp));
4476}
4477
4478/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4479/// FP_TO_*INT operation of the specified operand when the target requests that
4480/// we promote it. At this point, we know that the result and operand types are
4481/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4482/// operation that returns a larger result.
4483SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4484 MVT::ValueType DestVT,
4485 bool isSigned) {
4486 // First step, figure out the appropriate FP_TO*INT operation to use.
4487 MVT::ValueType NewOutTy = DestVT;
4488
4489 unsigned OpToUse = 0;
4490
4491 // Scan for the appropriate larger type to use.
4492 while (1) {
4493 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4494 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4495
4496 // If the target supports FP_TO_SINT returning this type, use it.
4497 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4498 default: break;
4499 case TargetLowering::Legal:
4500 if (!TLI.isTypeLegal(NewOutTy))
4501 break; // Can't use this datatype.
4502 // FALL THROUGH.
4503 case TargetLowering::Custom:
4504 OpToUse = ISD::FP_TO_SINT;
4505 break;
4506 }
4507 if (OpToUse) break;
4508
4509 // If the target supports FP_TO_UINT of this type, use it.
4510 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4511 default: break;
4512 case TargetLowering::Legal:
4513 if (!TLI.isTypeLegal(NewOutTy))
4514 break; // Can't use this datatype.
4515 // FALL THROUGH.
4516 case TargetLowering::Custom:
4517 OpToUse = ISD::FP_TO_UINT;
4518 break;
4519 }
4520 if (OpToUse) break;
4521
4522 // Otherwise, try a larger type.
4523 }
4524
4525 // Okay, we found the operation and type to use. Truncate the result of the
4526 // extended FP_TO_*INT operation to the desired size.
4527 return DAG.getNode(ISD::TRUNCATE, DestVT,
4528 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4529}
4530
4531/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4532///
4533SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4534 MVT::ValueType VT = Op.getValueType();
4535 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4536 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4537 switch (VT) {
4538 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4539 case MVT::i16:
4540 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4541 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4542 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4543 case MVT::i32:
4544 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4545 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4546 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4547 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4548 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4549 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4550 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4551 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4552 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4553 case MVT::i64:
4554 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4555 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4556 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4557 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4558 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4559 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4560 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4561 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4562 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4563 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4564 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4565 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4566 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4567 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4568 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4569 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4570 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4571 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4572 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4573 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4574 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4575 }
4576}
4577
4578/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4579///
4580SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4581 switch (Opc) {
4582 default: assert(0 && "Cannot expand this yet!");
4583 case ISD::CTPOP: {
4584 static const uint64_t mask[6] = {
4585 0x5555555555555555ULL, 0x3333333333333333ULL,
4586 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4587 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4588 };
4589 MVT::ValueType VT = Op.getValueType();
4590 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4591 unsigned len = getSizeInBits(VT);
4592 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4593 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4594 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4595 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4596 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4597 DAG.getNode(ISD::AND, VT,
4598 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4599 }
4600 return Op;
4601 }
4602 case ISD::CTLZ: {
4603 // for now, we do this:
4604 // x = x | (x >> 1);
4605 // x = x | (x >> 2);
4606 // ...
4607 // x = x | (x >>16);
4608 // x = x | (x >>32); // for 64-bit input
4609 // return popcount(~x);
4610 //
4611 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4612 MVT::ValueType VT = Op.getValueType();
4613 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4614 unsigned len = getSizeInBits(VT);
4615 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4616 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4617 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4618 }
4619 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4620 return DAG.getNode(ISD::CTPOP, VT, Op);
4621 }
4622 case ISD::CTTZ: {
4623 // for now, we use: { return popcount(~x & (x - 1)); }
4624 // unless the target has ctlz but not ctpop, in which case we use:
4625 // { return 32 - nlz(~x & (x-1)); }
4626 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4627 MVT::ValueType VT = Op.getValueType();
4628 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4629 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4630 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4631 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4632 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4633 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4634 TLI.isOperationLegal(ISD::CTLZ, VT))
4635 return DAG.getNode(ISD::SUB, VT,
4636 DAG.getConstant(getSizeInBits(VT), VT),
4637 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4638 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4639 }
4640 }
4641}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004642
Chris Lattnerdc750592005-01-07 07:47:09 +00004643/// ExpandOp - Expand the specified SDOperand into its two component pieces
4644/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4645/// LegalizeNodes map is filled in for any results that are not expanded, the
4646/// ExpandedNodes map is filled in for any results that are expanded, and the
4647/// Lo/Hi values are returned.
4648void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4649 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00004650 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00004651 SDNode *Node = Op.Val;
4652 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00004653 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
4654 VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00004655 "Cannot expand to FP value or to larger int value!");
4656
Chris Lattner1a570f12005-09-02 20:32:45 +00004657 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00004658 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00004659 = ExpandedNodes.find(Op);
4660 if (I != ExpandedNodes.end()) {
4661 Lo = I->second.first;
4662 Hi = I->second.second;
4663 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00004664 }
4665
Chris Lattnerdc750592005-01-07 07:47:09 +00004666 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00004667 case ISD::CopyFromReg:
4668 assert(0 && "CopyFromReg must be legal!");
4669 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004670#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +00004671 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004672#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00004673 assert(0 && "Do not know how to expand this operator!");
4674 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004675 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00004676 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00004677 Lo = DAG.getNode(ISD::UNDEF, NVT);
4678 Hi = DAG.getNode(ISD::UNDEF, NVT);
4679 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004680 case ISD::Constant: {
4681 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4682 Lo = DAG.getConstant(Cst, NVT);
4683 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4684 break;
4685 }
Evan Cheng47833a12006-12-12 21:32:44 +00004686 case ISD::ConstantFP: {
4687 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng3766fc602006-12-12 22:19:28 +00004688 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00004689 if (getTypeAction(Lo.getValueType()) == Expand)
4690 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00004691 break;
4692 }
Chris Lattner32e08b72005-03-28 22:03:13 +00004693 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004694 // Return the operands.
4695 Lo = Node->getOperand(0);
4696 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00004697 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004698
4699 case ISD::SIGN_EXTEND_INREG:
4700 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00004701 // sext_inreg the low part if needed.
4702 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4703
4704 // The high part gets the sign extension from the lo-part. This handles
4705 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004706 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4707 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4708 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004709 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00004710
Nate Begeman2fba8a32006-01-14 03:14:10 +00004711 case ISD::BSWAP: {
4712 ExpandOp(Node->getOperand(0), Lo, Hi);
4713 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4714 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4715 Lo = TempLo;
4716 break;
4717 }
4718
Chris Lattner55e9cde2005-05-11 04:51:16 +00004719 case ISD::CTPOP:
4720 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00004721 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4722 DAG.getNode(ISD::CTPOP, NVT, Lo),
4723 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00004724 Hi = DAG.getConstant(0, NVT);
4725 break;
4726
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004727 case ISD::CTLZ: {
4728 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004729 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004730 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4731 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004732 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4733 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004734 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4735 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4736
4737 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4738 Hi = DAG.getConstant(0, NVT);
4739 break;
4740 }
4741
4742 case ISD::CTTZ: {
4743 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004744 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004745 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4746 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004747 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4748 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004749 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4750 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4751
4752 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4753 Hi = DAG.getConstant(0, NVT);
4754 break;
4755 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00004756
Nate Begemane74795c2006-01-25 18:21:52 +00004757 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004758 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4759 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00004760 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4761 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4762
4763 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004764 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00004765 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4766 if (!TLI.isLittleEndian())
4767 std::swap(Lo, Hi);
4768 break;
4769 }
4770
Chris Lattnerdc750592005-01-07 07:47:09 +00004771 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00004772 LoadSDNode *LD = cast<LoadSDNode>(Node);
4773 SDOperand Ch = LD->getChain(); // Legalize the chain.
4774 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4775 ISD::LoadExtType ExtType = LD->getExtensionType();
Chris Lattnerdc750592005-01-07 07:47:09 +00004776
Evan Chenge71fe34d2006-10-09 20:57:25 +00004777 if (ExtType == ISD::NON_EXTLOAD) {
Chris Lattner296a83c2007-02-01 04:55:59 +00004778 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),LD->getSrcValueOffset());
Evan Cheng47833a12006-12-12 21:32:44 +00004779 if (VT == MVT::f32 || VT == MVT::f64) {
4780 // f32->i32 or f64->i64 one to one expansion.
4781 // Remember that we legalized the chain.
4782 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00004783 // Recursively expand the new load.
4784 if (getTypeAction(NVT) == Expand)
4785 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00004786 break;
4787 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00004788
Evan Chenge71fe34d2006-10-09 20:57:25 +00004789 // Increment the pointer to the other half.
4790 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4791 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4792 getIntPtrConstant(IncrementSize));
4793 // FIXME: This creates a bogus srcvalue!
Chris Lattner296a83c2007-02-01 04:55:59 +00004794 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),LD->getSrcValueOffset());
Misha Brukman835702a2005-04-21 22:36:52 +00004795
Evan Chenge71fe34d2006-10-09 20:57:25 +00004796 // Build a factor node to remember that this load is independent of the
4797 // other one.
4798 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4799 Hi.getValue(1));
4800
4801 // Remember that we legalized the chain.
4802 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4803 if (!TLI.isLittleEndian())
4804 std::swap(Lo, Hi);
4805 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00004806 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00004807
4808 if (VT == MVT::f64 && EVT == MVT::f32) {
4809 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4810 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
4811 LD->getSrcValueOffset());
4812 // Remember that we legalized the chain.
4813 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4814 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4815 break;
4816 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00004817
4818 if (EVT == NVT)
4819 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
4820 LD->getSrcValueOffset());
4821 else
4822 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
4823 LD->getSrcValueOffset(), EVT);
4824
4825 // Remember that we legalized the chain.
4826 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4827
4828 if (ExtType == ISD::SEXTLOAD) {
4829 // The high part is obtained by SRA'ing all but one of the bits of the
4830 // lo part.
4831 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4832 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4833 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4834 } else if (ExtType == ISD::ZEXTLOAD) {
4835 // The high part is just a zero.
4836 Hi = DAG.getConstant(0, NVT);
4837 } else /* if (ExtType == ISD::EXTLOAD) */ {
4838 // The high part is undefined.
4839 Hi = DAG.getNode(ISD::UNDEF, NVT);
4840 }
4841 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004842 break;
4843 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004844 case ISD::AND:
4845 case ISD::OR:
4846 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4847 SDOperand LL, LH, RL, RH;
4848 ExpandOp(Node->getOperand(0), LL, LH);
4849 ExpandOp(Node->getOperand(1), RL, RH);
4850 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4851 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4852 break;
4853 }
4854 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004855 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00004856 ExpandOp(Node->getOperand(1), LL, LH);
4857 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00004858 if (getTypeAction(NVT) == Expand)
4859 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004860 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00004861 if (VT != MVT::f32)
4862 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00004863 break;
4864 }
Nate Begemane5b86d72005-08-10 20:51:12 +00004865 case ISD::SELECT_CC: {
4866 SDOperand TL, TH, FL, FH;
4867 ExpandOp(Node->getOperand(2), TL, TH);
4868 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00004869 if (getTypeAction(NVT) == Expand)
4870 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00004871 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4872 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00004873 if (VT != MVT::f32)
4874 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4875 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004876 break;
4877 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004878 case ISD::ANY_EXTEND:
4879 // The low part is any extension of the input (which degenerates to a copy).
4880 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4881 // The high part is undefined.
4882 Hi = DAG.getNode(ISD::UNDEF, NVT);
4883 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004884 case ISD::SIGN_EXTEND: {
4885 // The low part is just a sign extension of the input (which degenerates to
4886 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004887 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004888
Chris Lattnerdc750592005-01-07 07:47:09 +00004889 // The high part is obtained by SRA'ing all but one of the bits of the lo
4890 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004891 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004892 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4893 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004894 break;
4895 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004896 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00004897 // The low part is just a zero extension of the input (which degenerates to
4898 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004899 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004900
Chris Lattnerdc750592005-01-07 07:47:09 +00004901 // The high part is just a zero.
4902 Hi = DAG.getConstant(0, NVT);
4903 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004904
Chris Lattner59b27fa372007-02-13 23:55:16 +00004905 case ISD::TRUNCATE: {
4906 // The input value must be larger than this value. Expand *it*.
4907 SDOperand NewLo;
4908 ExpandOp(Node->getOperand(0), NewLo, Hi);
4909
4910 // The low part is now either the right size, or it is closer. If not the
4911 // right size, make an illegal truncate so we recursively expand it.
4912 if (NewLo.getValueType() != Node->getValueType(0))
4913 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
4914 ExpandOp(NewLo, Lo, Hi);
4915 break;
4916 }
4917
Chris Lattner36e663d2005-12-23 00:16:34 +00004918 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004919 SDOperand Tmp;
4920 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
4921 // If the target wants to, allow it to lower this itself.
4922 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4923 case Expand: assert(0 && "cannot expand FP!");
4924 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
4925 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
4926 }
4927 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
4928 }
4929
Evan Cheng4eee7242006-12-09 02:42:38 +00004930 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00004931 if (VT == MVT::f32 || VT == MVT::f64) {
4932 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00004933 if (getTypeAction(NVT) == Expand)
4934 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00004935 break;
4936 }
4937
4938 // If source operand will be expanded to the same type as VT, i.e.
4939 // i64 <- f64, i32 <- f32, expand the source operand instead.
4940 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
4941 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
4942 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00004943 break;
4944 }
4945
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004946 // Turn this into a load/store pair by default.
4947 if (Tmp.Val == 0)
Evan Cheng3432ab92006-12-11 19:27:14 +00004948 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004949
Chris Lattner36e663d2005-12-23 00:16:34 +00004950 ExpandOp(Tmp, Lo, Hi);
4951 break;
4952 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004953
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004954 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00004955 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4956 TargetLowering::Custom &&
4957 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004958 Lo = TLI.LowerOperation(Op, DAG);
4959 assert(Lo.Val && "Node must be custom expanded!");
4960 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00004961 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004962 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004963 break;
4964
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004965 // These operators cannot be expanded directly, emit them as calls to
4966 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00004967 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00004968 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00004969 SDOperand Op;
4970 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4971 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004972 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4973 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00004974 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004975
Chris Lattner941d84a2005-07-30 01:40:57 +00004976 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4977
Chris Lattnerfe68d752005-07-29 00:33:32 +00004978 // Now that the custom expander is done, expand the result, which is still
4979 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004980 if (Op.Val) {
4981 ExpandOp(Op, Lo, Hi);
4982 break;
4983 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004984 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004985
Evan Cheng31cbddf2007-01-12 02:11:51 +00004986 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004987 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004988 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004989 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00004990 LC = RTLIB::FPTOSINT_F64_I64;
4991 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
4992 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004993 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004994 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004995
Evan Cheng31cbddf2007-01-12 02:11:51 +00004996 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00004997 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004998 SDOperand Op;
4999 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5000 case Expand: assert(0 && "cannot expand FP!");
5001 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5002 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5003 }
5004
5005 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5006
5007 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005008 if (Op.Val) {
5009 ExpandOp(Op, Lo, Hi);
5010 break;
5011 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005012 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005013
Evan Cheng31cbddf2007-01-12 02:11:51 +00005014 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005015 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005016 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005017 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005018 LC = RTLIB::FPTOUINT_F64_I64;
5019 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5020 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005021 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005022 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005023
Evan Cheng870e4f82006-01-09 18:31:59 +00005024 case ISD::SHL: {
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::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005028 SDOperand Op = DAG.getNode(ISD::SHL, 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 Lattner72b503b2006-09-13 03:50:39 +00005038 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5039 // this X << 1 as X+X.
5040 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5041 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5042 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5043 SDOperand LoOps[2], HiOps[3];
5044 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5045 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5046 LoOps[1] = LoOps[0];
5047 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5048
5049 HiOps[1] = HiOps[0];
5050 HiOps[2] = Lo.getValue(1);
5051 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5052 break;
5053 }
5054 }
5055
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005056 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005057 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005058 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005059
5060 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005061 TargetLowering::LegalizeAction Action =
5062 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5063 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5064 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005065 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005066 break;
5067 }
5068
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005069 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005070 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5071 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005072 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005073 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005074
Evan Cheng870e4f82006-01-09 18:31:59 +00005075 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005076 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005077 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005078 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005079 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005080 Op = TLI.LowerOperation(Op, DAG);
5081 if (Op.Val) {
5082 // Now that the custom expander is done, expand the result, which is
5083 // still VT.
5084 ExpandOp(Op, Lo, Hi);
5085 break;
5086 }
5087 }
5088
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005089 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005090 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005091 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005092
5093 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005094 TargetLowering::LegalizeAction Action =
5095 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5096 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5097 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005098 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005099 break;
5100 }
5101
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005102 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005103 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5104 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005105 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005106 }
5107
5108 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005109 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005110 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005111 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005112 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005113 Op = TLI.LowerOperation(Op, DAG);
5114 if (Op.Val) {
5115 // Now that the custom expander is done, expand the result, which is
5116 // still VT.
5117 ExpandOp(Op, Lo, Hi);
5118 break;
5119 }
5120 }
5121
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005122 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005123 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005124 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005125
5126 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005127 TargetLowering::LegalizeAction Action =
5128 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5129 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5130 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005131 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005132 break;
5133 }
5134
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005135 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005136 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5137 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005138 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005139 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005140
Misha Brukman835702a2005-04-21 22:36:52 +00005141 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005142 case ISD::SUB: {
5143 // If the target wants to custom expand this, let them.
5144 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5145 TargetLowering::Custom) {
5146 Op = TLI.LowerOperation(Op, DAG);
5147 if (Op.Val) {
5148 ExpandOp(Op, Lo, Hi);
5149 break;
5150 }
5151 }
5152
5153 // Expand the subcomponents.
5154 SDOperand LHSL, LHSH, RHSL, RHSH;
5155 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5156 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005157 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5158 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005159 LoOps[0] = LHSL;
5160 LoOps[1] = RHSL;
5161 HiOps[0] = LHSH;
5162 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005163 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005164 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005165 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005166 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005167 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005168 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005169 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005170 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005171 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005172 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005173 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005174 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005175 // If the target wants to custom expand this, let them.
5176 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00005177 SDOperand New = TLI.LowerOperation(Op, DAG);
5178 if (New.Val) {
5179 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005180 break;
5181 }
5182 }
5183
Evan Chenge93762d2006-09-01 18:17:58 +00005184 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5185 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Chenge93762d2006-09-01 18:17:58 +00005186 if (HasMULHS || HasMULHU) {
Nate Begemanadd0c632005-04-11 03:01:51 +00005187 SDOperand LL, LH, RL, RH;
5188 ExpandOp(Node->getOperand(0), LL, LH);
5189 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00005190 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman8e20c762006-12-11 02:23:46 +00005191 // FIXME: Move this to the dag combiner.
Nate Begeman43144a22005-08-30 02:44:00 +00005192 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5193 // extended the sign bit of the low half through the upper half, and if so
5194 // emit a MULHS instead of the alternate sequence that is valid for any
5195 // i64 x i64 multiply.
Evan Chenge93762d2006-09-01 18:17:58 +00005196 if (HasMULHS &&
Nate Begeman43144a22005-08-30 02:44:00 +00005197 // is RH an extension of the sign bit of RL?
5198 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5199 RH.getOperand(1).getOpcode() == ISD::Constant &&
5200 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5201 // is LH an extension of the sign bit of LL?
5202 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5203 LH.getOperand(1).getOpcode() == ISD::Constant &&
5204 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattner1b633912006-09-16 00:21:44 +00005205 // Low part:
5206 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5207 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005208 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattner1b633912006-09-16 00:21:44 +00005209 break;
Evan Chenge93762d2006-09-01 18:17:58 +00005210 } else if (HasMULHU) {
Chris Lattner1b633912006-09-16 00:21:44 +00005211 // Low part:
5212 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5213
5214 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005215 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5216 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5217 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5218 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5219 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00005220 break;
Nate Begeman43144a22005-08-30 02:44:00 +00005221 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005222 }
Evan Chenge93762d2006-09-01 18:17:58 +00005223
Evan Cheng31cbddf2007-01-12 02:11:51 +00005224 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5225 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00005226 break;
5227 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005228 case ISD::SDIV:
5229 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5230 break;
5231 case ISD::UDIV:
5232 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5233 break;
5234 case ISD::SREM:
5235 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5236 break;
5237 case ISD::UREM:
5238 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5239 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00005240
Evan Cheng4eee7242006-12-09 02:42:38 +00005241 case ISD::FADD:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005242 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5243 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5244 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005245 break;
5246 case ISD::FSUB:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005247 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5248 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5249 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005250 break;
5251 case ISD::FMUL:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005252 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5253 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5254 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005255 break;
5256 case ISD::FDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005257 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5258 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5259 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005260 break;
5261 case ISD::FP_EXTEND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005262 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005263 break;
5264 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005265 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005266 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005267 case ISD::FSQRT:
5268 case ISD::FSIN:
5269 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005270 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00005271 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005272 case ISD::FSQRT:
5273 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5274 break;
5275 case ISD::FSIN:
5276 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5277 break;
5278 case ISD::FCOS:
5279 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5280 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005281 default: assert(0 && "Unreachable!");
5282 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005283 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00005284 break;
5285 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00005286 case ISD::FABS: {
5287 SDOperand Mask = (VT == MVT::f64)
5288 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5289 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5290 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5291 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5292 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5293 if (getTypeAction(NVT) == Expand)
5294 ExpandOp(Lo, Lo, Hi);
5295 break;
5296 }
5297 case ISD::FNEG: {
5298 SDOperand Mask = (VT == MVT::f64)
5299 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5300 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5301 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5302 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5303 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5304 if (getTypeAction(NVT) == Expand)
5305 ExpandOp(Lo, Lo, Hi);
5306 break;
5307 }
Evan Cheng003feb02007-01-04 21:56:39 +00005308 case ISD::FCOPYSIGN: {
5309 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5310 if (getTypeAction(NVT) == Expand)
5311 ExpandOp(Lo, Lo, Hi);
5312 break;
5313 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005314 case ISD::SINT_TO_FP:
5315 case ISD::UINT_TO_FP: {
5316 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5317 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00005318 RTLIB::Libcall LC;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005319 if (Node->getOperand(0).getValueType() == MVT::i64) {
5320 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005321 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005322 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005323 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005324 } else {
5325 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005326 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005327 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005328 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005329 }
5330
5331 // Promote the operand if needed.
5332 if (getTypeAction(SrcVT) == Promote) {
5333 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5334 Tmp = isSigned
5335 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5336 DAG.getValueType(SrcVT))
5337 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5338 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5339 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005340 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005341 break;
5342 }
Evan Chengf3a80c62006-12-13 02:38:13 +00005343 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005344
Chris Lattnerac12f682005-12-21 18:02:52 +00005345 // Make sure the resultant values have been legalized themselves, unless this
5346 // is a type that requires multi-step expansion.
5347 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5348 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00005349 if (Hi.Val)
5350 // Don't legalize the high part if it is expanded to a single node.
5351 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00005352 }
Evan Cheng870e4f82006-01-09 18:31:59 +00005353
5354 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005355 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00005356 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00005357}
5358
Chris Lattner32206f52006-03-18 01:44:44 +00005359/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
5360/// two smaller values of MVT::Vector type.
5361void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5362 SDOperand &Hi) {
5363 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
5364 SDNode *Node = Op.Val;
5365 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
5366 assert(NumElements > 1 && "Cannot split a single element vector!");
5367 unsigned NewNumElts = NumElements/2;
5368 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
5369 SDOperand TypeNode = *(Node->op_end()-1);
5370
5371 // See if we already split it.
5372 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5373 = SplitNodes.find(Op);
5374 if (I != SplitNodes.end()) {
5375 Lo = I->second.first;
5376 Hi = I->second.second;
5377 return;
5378 }
5379
5380 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005381 default:
5382#ifndef NDEBUG
5383 Node->dump();
5384#endif
5385 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005386 case ISD::VBUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005387 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5388 Node->op_begin()+NewNumElts);
Chris Lattner32206f52006-03-18 01:44:44 +00005389 LoOps.push_back(NewNumEltsNode);
5390 LoOps.push_back(TypeNode);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005391 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005392
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005393 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
5394 Node->op_end()-2);
Chris Lattner32206f52006-03-18 01:44:44 +00005395 HiOps.push_back(NewNumEltsNode);
5396 HiOps.push_back(TypeNode);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005397 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005398 break;
5399 }
5400 case ISD::VADD:
5401 case ISD::VSUB:
5402 case ISD::VMUL:
5403 case ISD::VSDIV:
5404 case ISD::VUDIV:
5405 case ISD::VAND:
5406 case ISD::VOR:
5407 case ISD::VXOR: {
5408 SDOperand LL, LH, RL, RH;
5409 SplitVectorOp(Node->getOperand(0), LL, LH);
5410 SplitVectorOp(Node->getOperand(1), RL, RH);
5411
5412 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
5413 NewNumEltsNode, TypeNode);
5414 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
5415 NewNumEltsNode, TypeNode);
5416 break;
5417 }
5418 case ISD::VLOAD: {
5419 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5420 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5421 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5422
5423 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5424 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
5425 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5426 getIntPtrConstant(IncrementSize));
5427 // FIXME: This creates a bogus srcvalue!
5428 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5429
5430 // Build a factor node to remember that this load is independent of the
5431 // other one.
5432 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5433 Hi.getValue(1));
5434
5435 // Remember that we legalized the chain.
5436 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00005437 break;
5438 }
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005439 case ISD::VBIT_CONVERT: {
5440 // We know the result is a vector. The input may be either a vector or a
5441 // scalar value.
5442 if (Op.getOperand(0).getValueType() != MVT::Vector) {
5443 // Lower to a store/load. FIXME: this could be improved probably.
5444 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
5445
Evan Chengdf9ac472006-10-05 23:01:46 +00005446 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00005447 Op.getOperand(0), Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005448 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5449 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
5450 SplitVectorOp(St, Lo, Hi);
5451 } else {
5452 // If the input is a vector type, we have to either scalarize it, pack it
5453 // or convert it based on whether the input vector type is legal.
5454 SDNode *InVal = Node->getOperand(0).Val;
5455 unsigned NumElems =
5456 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5457 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5458
5459 // If the input is from a single element vector, scalarize the vector,
5460 // then treat like a scalar.
5461 if (NumElems == 1) {
5462 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
5463 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
5464 Op.getOperand(1), Op.getOperand(2));
5465 SplitVectorOp(Scalar, Lo, Hi);
5466 } else {
5467 // Split the input vector.
5468 SplitVectorOp(Op.getOperand(0), Lo, Hi);
5469
5470 // Convert each of the pieces now.
5471 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
5472 NewNumEltsNode, TypeNode);
5473 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
5474 NewNumEltsNode, TypeNode);
5475 }
5476 break;
5477 }
5478 }
Chris Lattner32206f52006-03-18 01:44:44 +00005479 }
5480
5481 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005482 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00005483 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5484 assert(isNew && "Value already expanded?!?");
5485}
5486
5487
5488/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
5489/// equivalent operation that returns a scalar (e.g. F32) or packed value
5490/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
5491/// type for the result.
5492SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
5493 MVT::ValueType NewVT) {
5494 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
5495 SDNode *Node = Op.Val;
5496
5497 // See if we already packed it.
5498 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
5499 if (I != PackedNodes.end()) return I->second;
5500
5501 SDOperand Result;
5502 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00005503 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005504#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +00005505 Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005506#endif
Chris Lattner29b23012006-03-19 01:17:20 +00005507 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattner32206f52006-03-18 01:44:44 +00005508 case ISD::VADD:
5509 case ISD::VSUB:
5510 case ISD::VMUL:
5511 case ISD::VSDIV:
5512 case ISD::VUDIV:
5513 case ISD::VAND:
5514 case ISD::VOR:
5515 case ISD::VXOR:
5516 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
5517 NewVT,
5518 PackVectorOp(Node->getOperand(0), NewVT),
5519 PackVectorOp(Node->getOperand(1), NewVT));
5520 break;
5521 case ISD::VLOAD: {
Chris Lattner93640542006-03-19 00:07:49 +00005522 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
5523 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00005524
Evan Chenge71fe34d2006-10-09 20:57:25 +00005525 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
5526 Result = DAG.getLoad(NewVT, Ch, Ptr, SV->getValue(), SV->getOffset());
Chris Lattner32206f52006-03-18 01:44:44 +00005527
5528 // Remember that we legalized the chain.
5529 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5530 break;
5531 }
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005532 case ISD::VBUILD_VECTOR:
Chris Lattner5fe1f542006-03-31 02:06:56 +00005533 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005534 // Returning a scalar?
Chris Lattner32206f52006-03-18 01:44:44 +00005535 Result = Node->getOperand(0);
5536 } else {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005537 // Returning a BUILD_VECTOR?
Chris Lattnere1401e32006-04-08 05:34:25 +00005538
5539 // If all elements of the build_vector are undefs, return an undef.
5540 bool AllUndef = true;
5541 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
5542 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
5543 AllUndef = false;
5544 break;
5545 }
5546 if (AllUndef) {
5547 Result = DAG.getNode(ISD::UNDEF, NewVT);
5548 } else {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005549 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Node->op_begin(),
5550 Node->getNumOperands()-2);
Chris Lattnere1401e32006-04-08 05:34:25 +00005551 }
Chris Lattner32206f52006-03-18 01:44:44 +00005552 }
5553 break;
Chris Lattner29b23012006-03-19 01:17:20 +00005554 case ISD::VINSERT_VECTOR_ELT:
5555 if (!MVT::isVector(NewVT)) {
5556 // Returning a scalar? Must be the inserted element.
5557 Result = Node->getOperand(1);
5558 } else {
5559 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
5560 PackVectorOp(Node->getOperand(0), NewVT),
5561 Node->getOperand(1), Node->getOperand(2));
5562 }
5563 break;
Chris Lattnerf6f94d32006-03-28 20:24:43 +00005564 case ISD::VVECTOR_SHUFFLE:
5565 if (!MVT::isVector(NewVT)) {
5566 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
5567 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5568 if (cast<ConstantSDNode>(EltNum)->getValue())
5569 Result = PackVectorOp(Node->getOperand(1), NewVT);
5570 else
5571 Result = PackVectorOp(Node->getOperand(0), NewVT);
5572 } else {
5573 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
5574 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
5575 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
5576 Node->getOperand(2).Val->op_end()-2);
5577 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005578 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT,
5579 Node->getOperand(2).Val->op_begin(),
5580 Node->getOperand(2).Val->getNumOperands()-2);
Chris Lattnerf6f94d32006-03-28 20:24:43 +00005581
5582 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
5583 PackVectorOp(Node->getOperand(0), NewVT),
5584 PackVectorOp(Node->getOperand(1), NewVT), BV);
5585 }
5586 break;
Chris Lattner2f4119a2006-03-22 20:09:35 +00005587 case ISD::VBIT_CONVERT:
5588 if (Op.getOperand(0).getValueType() != MVT::Vector)
5589 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
5590 else {
5591 // If the input is a vector type, we have to either scalarize it, pack it
5592 // or convert it based on whether the input vector type is legal.
5593 SDNode *InVal = Node->getOperand(0).Val;
5594 unsigned NumElems =
5595 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5596 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5597
5598 // Figure out if there is a Packed type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00005599 // type. If so, convert to the vector type.
Chris Lattner2f4119a2006-03-22 20:09:35 +00005600 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
5601 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
5602 // Turn this into a bit convert of the packed input.
5603 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5604 PackVectorOp(Node->getOperand(0), TVT));
5605 break;
5606 } else if (NumElems == 1) {
5607 // Turn this into a bit convert of the scalar input.
5608 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5609 PackVectorOp(Node->getOperand(0), EVT));
5610 break;
5611 } else {
5612 // FIXME: UNIMP!
5613 assert(0 && "Cast from unsupported vector type not implemented yet!");
5614 }
5615 }
Evan Chengcb73b8d2006-04-10 18:54:36 +00005616 break;
Chris Lattner02274a52006-04-08 22:22:57 +00005617 case ISD::VSELECT:
5618 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
5619 PackVectorOp(Op.getOperand(1), NewVT),
5620 PackVectorOp(Op.getOperand(2), NewVT));
5621 break;
Chris Lattner32206f52006-03-18 01:44:44 +00005622 }
5623
5624 if (TLI.isTypeLegal(NewVT))
5625 Result = LegalizeOp(Result);
5626 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
5627 assert(isNew && "Value already packed?");
5628 return Result;
5629}
5630
Chris Lattnerdc750592005-01-07 07:47:09 +00005631
5632// SelectionDAG::Legalize - This is the entry point for the file.
5633//
Chris Lattner4add7e32005-01-23 04:42:50 +00005634void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00005635 if (ViewLegalizeDAGs) viewGraph();
5636
Chris Lattnerdc750592005-01-07 07:47:09 +00005637 /// run - This is the main entry point to this class.
5638 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005639 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00005640}
5641