blob: 357e2cd9f15c6192217e21dcbecfbcee110247c3 [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;
Chris Lattner567b9252007-03-03 19:21:38 +0000882 case TargetLowering::Expand:
883 Result = LegalizeOp(Node->getOperand(0));
884 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000885 }
Nate Begeman5965bd12006-02-17 05:43:56 +0000886 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000887
Chris Lattnerdc750592005-01-07 07:47:09 +0000888 case ISD::Constant:
889 // We know we don't need to expand constants here, constants only have one
890 // value and we check that it is fine above.
891
892 // FIXME: Maybe we should handle things like targets that don't support full
893 // 32-bit immediates?
894 break;
895 case ISD::ConstantFP: {
896 // Spill FP immediates to the constant pool if the target cannot directly
897 // codegen them. Targets often have some immediate values that can be
898 // efficiently generated into an FP register without a load. We explicitly
899 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +0000900 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
901
902 // Check to see if this FP immediate is already legal.
903 bool isLegal = false;
904 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
905 E = TLI.legal_fpimm_end(); I != E; ++I)
906 if (CFP->isExactlyValue(*I)) {
907 isLegal = true;
908 break;
909 }
910
Chris Lattner758b0ac2006-01-29 06:26:56 +0000911 // If this is a legal constant, turn it into a TargetConstantFP node.
912 if (isLegal) {
913 Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
914 break;
915 }
916
917 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
918 default: assert(0 && "This action is not supported yet!");
919 case TargetLowering::Custom:
920 Tmp3 = TLI.LowerOperation(Result, DAG);
921 if (Tmp3.Val) {
922 Result = Tmp3;
923 break;
924 }
925 // FALLTHROUGH
926 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +0000927 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +0000928 }
929 break;
930 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000931 case ISD::TokenFactor:
932 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000933 Tmp1 = LegalizeOp(Node->getOperand(0));
934 Tmp2 = LegalizeOp(Node->getOperand(1));
935 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
936 } else if (Node->getNumOperands() == 3) {
937 Tmp1 = LegalizeOp(Node->getOperand(0));
938 Tmp2 = LegalizeOp(Node->getOperand(1));
939 Tmp3 = LegalizeOp(Node->getOperand(2));
940 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000941 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +0000942 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000943 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000944 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
945 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000946 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +0000947 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000948 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000949
950 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +0000951 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000952 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +0000953 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
954 assert(Tmp3.Val && "Target didn't custom lower this node!");
955 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
956 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000957
Chris Lattneraaa23d92006-05-16 22:53:20 +0000958 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000959 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +0000960 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
961 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +0000962 if (Op.ResNo == i)
963 Tmp2 = Tmp1;
964 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
965 }
966 return Tmp2;
Chris Lattnerd3b504a2006-04-12 16:20:43 +0000967
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000968 case ISD::BUILD_VECTOR:
969 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +0000970 default: assert(0 && "This action is not supported yet!");
971 case TargetLowering::Custom:
972 Tmp3 = TLI.LowerOperation(Result, DAG);
973 if (Tmp3.Val) {
974 Result = Tmp3;
975 break;
976 }
977 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000978 case TargetLowering::Expand:
979 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000980 break;
Chris Lattner29b23012006-03-19 01:17:20 +0000981 }
Chris Lattner29b23012006-03-19 01:17:20 +0000982 break;
983 case ISD::INSERT_VECTOR_ELT:
984 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
985 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
986 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
987 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
988
989 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
990 Node->getValueType(0))) {
991 default: assert(0 && "This action is not supported yet!");
992 case TargetLowering::Legal:
993 break;
994 case TargetLowering::Custom:
995 Tmp3 = TLI.LowerOperation(Result, DAG);
996 if (Tmp3.Val) {
997 Result = Tmp3;
998 break;
999 }
1000 // FALLTHROUGH
1001 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001002 // If the insert index is a constant, codegen this as a scalar_to_vector,
1003 // then a shuffle that inserts it into the right position in the vector.
1004 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1005 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1006 Tmp1.getValueType(), Tmp2);
1007
1008 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1009 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1010 MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT);
1011
1012 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1013 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1014 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001015 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +00001016 for (unsigned i = 0; i != NumElts; ++i) {
1017 if (i != InsertPos->getValue())
1018 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1019 else
1020 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1021 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001022 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1023 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +00001024
1025 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1026 Tmp1, ScVec, ShufMask);
1027 Result = LegalizeOp(Result);
1028 break;
1029 }
1030
Chris Lattner29b23012006-03-19 01:17:20 +00001031 // If the target doesn't support this, we have to spill the input vector
1032 // to a temporary stack slot, update the element, then reload it. This is
1033 // badness. We could also load the value into a vector register (either
1034 // with a "move to register" or "extload into register" instruction, then
1035 // permute it into place, if the idx is a constant and if the idx is
1036 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001037 MVT::ValueType VT = Tmp1.getValueType();
1038 MVT::ValueType EltVT = Tmp2.getValueType();
1039 MVT::ValueType IdxVT = Tmp3.getValueType();
1040 MVT::ValueType PtrVT = TLI.getPointerTy();
1041 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001042 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +00001043 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001044
1045 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001046 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1047 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001048 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001049 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1050 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1051 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001052 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +00001053 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001054 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +00001055 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +00001056 break;
1057 }
1058 }
1059 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001060 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001061 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1062 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1063 break;
1064 }
1065
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001066 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1067 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1068 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1069 Node->getValueType(0))) {
1070 default: assert(0 && "This action is not supported yet!");
1071 case TargetLowering::Legal:
1072 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001073 case TargetLowering::Custom:
1074 Tmp3 = TLI.LowerOperation(Result, DAG);
1075 if (Tmp3.Val) {
1076 Result = Tmp3;
1077 break;
1078 }
1079 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001080 case TargetLowering::Expand:
1081 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001082 break;
1083 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001084 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001085 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001086 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1087 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1088 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1089
1090 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001091 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1092 default: assert(0 && "Unknown operation action!");
1093 case TargetLowering::Legal:
1094 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1095 "vector shuffle should not be created if not legal!");
1096 break;
1097 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001098 Tmp3 = TLI.LowerOperation(Result, DAG);
1099 if (Tmp3.Val) {
1100 Result = Tmp3;
1101 break;
1102 }
1103 // FALLTHROUGH
1104 case TargetLowering::Expand: {
1105 MVT::ValueType VT = Node->getValueType(0);
1106 MVT::ValueType EltVT = MVT::getVectorBaseType(VT);
1107 MVT::ValueType PtrVT = TLI.getPointerTy();
1108 SDOperand Mask = Node->getOperand(2);
1109 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001110 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001111 for (unsigned i = 0; i != NumElems; ++i) {
1112 SDOperand Arg = Mask.getOperand(i);
1113 if (Arg.getOpcode() == ISD::UNDEF) {
1114 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1115 } else {
1116 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1117 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1118 if (Idx < NumElems)
1119 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1120 DAG.getConstant(Idx, PtrVT)));
1121 else
1122 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1123 DAG.getConstant(Idx - NumElems, PtrVT)));
1124 }
1125 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001126 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001127 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001128 }
Chris Lattner6be79822006-04-04 17:23:26 +00001129 case TargetLowering::Promote: {
1130 // Change base type to a different vector type.
1131 MVT::ValueType OVT = Node->getValueType(0);
1132 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1133
1134 // Cast the two input vectors.
1135 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1136 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1137
1138 // Convert the shuffle mask to the right # elements.
1139 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1140 assert(Tmp3.Val && "Shuffle not legal?");
1141 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1142 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1143 break;
1144 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001145 }
1146 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001147
1148 case ISD::EXTRACT_VECTOR_ELT:
1149 Tmp1 = LegalizeOp(Node->getOperand(0));
1150 Tmp2 = LegalizeOp(Node->getOperand(1));
1151 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner340a6b52006-03-21 21:02:03 +00001152
1153 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT,
1154 Tmp1.getValueType())) {
1155 default: assert(0 && "This action is not supported yet!");
1156 case TargetLowering::Legal:
1157 break;
1158 case TargetLowering::Custom:
1159 Tmp3 = TLI.LowerOperation(Result, DAG);
1160 if (Tmp3.Val) {
1161 Result = Tmp3;
1162 break;
1163 }
1164 // FALLTHROUGH
Chris Lattner42a5fca2006-04-02 05:06:04 +00001165 case TargetLowering::Expand:
1166 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner340a6b52006-03-21 21:02:03 +00001167 break;
1168 }
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001169 break;
1170
Chris Lattner6f423252006-03-31 17:55:51 +00001171 case ISD::VEXTRACT_VECTOR_ELT:
1172 Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op));
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001173 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001174
Chris Lattner462505f2006-02-13 09:18:02 +00001175 case ISD::CALLSEQ_START: {
1176 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1177
1178 // Recursively Legalize all of the inputs of the call end that do not lead
1179 // to this call start. This ensures that any libcalls that need be inserted
1180 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001181 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001182 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001183 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1184 NodesLeadingTo);
1185 }
Chris Lattner462505f2006-02-13 09:18:02 +00001186
1187 // Now that we legalized all of the inputs (which may have inserted
1188 // libcalls) create the new CALLSEQ_START node.
1189 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1190
1191 // Merge in the last call, to ensure that this call start after the last
1192 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001193 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001194 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1195 Tmp1 = LegalizeOp(Tmp1);
1196 }
Chris Lattner462505f2006-02-13 09:18:02 +00001197
1198 // Do not try to legalize the target-specific arguments (#1+).
1199 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001200 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001201 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001202 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001203 }
1204
1205 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001206 AddLegalizedOperand(Op.getValue(0), Result);
1207 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1208 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1209
Chris Lattner462505f2006-02-13 09:18:02 +00001210 // Now that the callseq_start and all of the non-call nodes above this call
1211 // sequence have been legalized, legalize the call itself. During this
1212 // process, no libcalls can/will be inserted, guaranteeing that no calls
1213 // can overlap.
1214 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1215 SDOperand InCallSEQ = LastCALLSEQ_END;
1216 // Note that we are selecting this call!
1217 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1218 IsLegalizingCall = true;
1219
1220 // Legalize the call, starting from the CALLSEQ_END.
1221 LegalizeOp(LastCALLSEQ_END);
1222 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1223 return Result;
1224 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001225 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001226 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1227 // will cause this node to be legalized as well as handling libcalls right.
1228 if (LastCALLSEQ_END.Val != Node) {
1229 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001230 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001231 assert(I != LegalizedNodes.end() &&
1232 "Legalizing the call start should have legalized this node!");
1233 return I->second;
1234 }
1235
1236 // Otherwise, the call start has been legalized and everything is going
1237 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001238 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001239 // Do not try to legalize the target-specific arguments (#1+), except for
1240 // an optional flag input.
1241 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1242 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001243 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001244 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001245 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001246 }
1247 } else {
1248 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1249 if (Tmp1 != Node->getOperand(0) ||
1250 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001251 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001252 Ops[0] = Tmp1;
1253 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001254 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001255 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001256 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001257 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001258 // This finishes up call legalization.
1259 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001260
1261 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1262 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1263 if (Node->getNumValues() == 2)
1264 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1265 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001266 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +00001267 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1268 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1269 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001270 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001271
Chris Lattnerd02b0542006-01-28 10:58:55 +00001272 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001273 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001274 switch (TLI.getOperationAction(Node->getOpcode(),
1275 Node->getValueType(0))) {
1276 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001277 case TargetLowering::Expand: {
1278 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1279 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1280 " not tell us which reg is the stack pointer!");
1281 SDOperand Chain = Tmp1.getOperand(0);
1282 SDOperand Size = Tmp2.getOperand(1);
1283 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
1284 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
1285 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001286 Tmp1 = LegalizeOp(Tmp1);
1287 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001288 break;
1289 }
1290 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001291 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1292 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001293 Tmp1 = LegalizeOp(Tmp3);
1294 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001295 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001296 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001297 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001298 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001299 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001300 // Since this op produce two values, make sure to remember that we
1301 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001302 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1303 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001304 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001305 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001306 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001307 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001308 bool Changed = false;
1309 // Legalize all of the operands of the inline asm, in case they are nodes
1310 // that need to be expanded or something. Note we skip the asm string and
1311 // all of the TargetConstant flags.
1312 SDOperand Op = LegalizeOp(Ops[0]);
1313 Changed = Op != Ops[0];
1314 Ops[0] = Op;
1315
1316 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1317 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1318 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1319 for (++i; NumVals; ++i, --NumVals) {
1320 SDOperand Op = LegalizeOp(Ops[i]);
1321 if (Op != Ops[i]) {
1322 Changed = true;
1323 Ops[i] = Op;
1324 }
1325 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001326 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001327
1328 if (HasInFlag) {
1329 Op = LegalizeOp(Ops.back());
1330 Changed |= Op != Ops.back();
1331 Ops.back() = Op;
1332 }
1333
1334 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001335 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001336
1337 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001338 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001339 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1340 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001341 }
Chris Lattner68a12142005-01-07 22:12:08 +00001342 case ISD::BR:
1343 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001344 // Ensure that libcalls are emitted before a branch.
1345 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1346 Tmp1 = LegalizeOp(Tmp1);
1347 LastCALLSEQ_END = DAG.getEntryNode();
1348
Chris Lattnerd02b0542006-01-28 10:58:55 +00001349 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001350 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001351 case ISD::BRIND:
1352 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1353 // Ensure that libcalls are emitted before a branch.
1354 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1355 Tmp1 = LegalizeOp(Tmp1);
1356 LastCALLSEQ_END = DAG.getEntryNode();
1357
1358 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1359 default: assert(0 && "Indirect target must be legal type (pointer)!");
1360 case Legal:
1361 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1362 break;
1363 }
1364 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1365 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001366 case ISD::BR_JT:
1367 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1368 // Ensure that libcalls are emitted before a branch.
1369 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1370 Tmp1 = LegalizeOp(Tmp1);
1371 LastCALLSEQ_END = DAG.getEntryNode();
1372
1373 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1374 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1375
1376 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1377 default: assert(0 && "This action is not supported yet!");
1378 case TargetLowering::Legal: break;
1379 case TargetLowering::Custom:
1380 Tmp1 = TLI.LowerOperation(Result, DAG);
1381 if (Tmp1.Val) Result = Tmp1;
1382 break;
1383 case TargetLowering::Expand: {
1384 SDOperand Chain = Result.getOperand(0);
1385 SDOperand Table = Result.getOperand(1);
1386 SDOperand Index = Result.getOperand(2);
1387
1388 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001389 MachineFunction &MF = DAG.getMachineFunction();
1390 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001391 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1392 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001393
1394 SDOperand LD;
1395 switch (EntrySize) {
1396 default: assert(0 && "Size of jump table not supported yet."); break;
1397 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1398 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1399 }
1400
1401 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001402 // For PIC, the sequence is:
1403 // BRIND(load(Jumptable + index) + RelocBase)
1404 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1405 SDOperand Reloc;
1406 if (TLI.usesGlobalOffsetTable())
1407 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1408 else
1409 Reloc = Table;
Evan Chenge6d58472006-10-31 02:31:00 +00001410 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng84a28d42006-10-30 08:00:44 +00001411 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1412 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1413 } else {
1414 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1415 }
1416 }
1417 }
1418 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001419 case ISD::BRCOND:
1420 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001421 // Ensure that libcalls are emitted before a return.
1422 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1423 Tmp1 = LegalizeOp(Tmp1);
1424 LastCALLSEQ_END = DAG.getEntryNode();
1425
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001426 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1427 case Expand: assert(0 && "It's impossible to expand bools");
1428 case Legal:
1429 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1430 break;
1431 case Promote:
1432 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001433
1434 // The top bits of the promoted condition are not necessarily zero, ensure
1435 // that the value is properly zero extended.
1436 if (!TLI.MaskedValueIsZero(Tmp2,
1437 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1438 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001439 break;
1440 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001441
1442 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001443 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001444
1445 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1446 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001447 case TargetLowering::Legal: break;
1448 case TargetLowering::Custom:
1449 Tmp1 = TLI.LowerOperation(Result, DAG);
1450 if (Tmp1.Val) Result = Tmp1;
1451 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001452 case TargetLowering::Expand:
1453 // Expand brcond's setcc into its constituent parts and create a BR_CC
1454 // Node.
1455 if (Tmp2.getOpcode() == ISD::SETCC) {
1456 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1457 Tmp2.getOperand(0), Tmp2.getOperand(1),
1458 Node->getOperand(2));
1459 } else {
1460 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1461 DAG.getCondCode(ISD::SETNE), Tmp2,
1462 DAG.getConstant(0, Tmp2.getValueType()),
1463 Node->getOperand(2));
1464 }
1465 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001466 }
1467 break;
1468 case ISD::BR_CC:
1469 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001470 // Ensure that libcalls are emitted before a branch.
1471 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1472 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001473 Tmp2 = Node->getOperand(2); // LHS
1474 Tmp3 = Node->getOperand(3); // RHS
1475 Tmp4 = Node->getOperand(1); // CC
1476
1477 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001478 LastCALLSEQ_END = DAG.getEntryNode();
1479
Nate Begeman7e7f4392006-02-01 07:19:44 +00001480 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1481 // the LHS is a legal SETCC itself. In this case, we need to compare
1482 // the result against zero to select between true and false values.
1483 if (Tmp3.Val == 0) {
1484 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1485 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001486 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001487
1488 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1489 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001490
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001491 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1492 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001493 case TargetLowering::Legal: break;
1494 case TargetLowering::Custom:
1495 Tmp4 = TLI.LowerOperation(Result, DAG);
1496 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001497 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001498 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001499 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001500 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001501 LoadSDNode *LD = cast<LoadSDNode>(Node);
1502 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1503 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001504
Evan Chenge71fe34d2006-10-09 20:57:25 +00001505 ISD::LoadExtType ExtType = LD->getExtensionType();
1506 if (ExtType == ISD::NON_EXTLOAD) {
1507 MVT::ValueType VT = Node->getValueType(0);
1508 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1509 Tmp3 = Result.getValue(0);
1510 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001511
Evan Chenge71fe34d2006-10-09 20:57:25 +00001512 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1513 default: assert(0 && "This action is not supported yet!");
1514 case TargetLowering::Legal: break;
1515 case TargetLowering::Custom:
1516 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1517 if (Tmp1.Val) {
1518 Tmp3 = LegalizeOp(Tmp1);
1519 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001520 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001521 break;
1522 case TargetLowering::Promote: {
1523 // Only promote a load of vector type to another.
1524 assert(MVT::isVector(VT) && "Cannot promote this load!");
1525 // Change base type to a different vector type.
1526 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1527
1528 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
1529 LD->getSrcValueOffset());
1530 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1531 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001532 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001533 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001534 }
1535 // Since loads produce two values, make sure to remember that we
1536 // legalized both of them.
1537 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1538 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1539 return Op.ResNo ? Tmp4 : Tmp3;
1540 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001541 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001542 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1543 default: assert(0 && "This action is not supported yet!");
1544 case TargetLowering::Promote:
1545 assert(SrcVT == MVT::i1 &&
1546 "Can only promote extending LOAD from i1 -> i8!");
1547 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1548 LD->getSrcValue(), LD->getSrcValueOffset(),
1549 MVT::i8);
1550 Tmp1 = Result.getValue(0);
1551 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001552 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001553 case TargetLowering::Custom:
1554 isCustom = true;
1555 // FALLTHROUGH
1556 case TargetLowering::Legal:
1557 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1558 Tmp1 = Result.getValue(0);
1559 Tmp2 = Result.getValue(1);
1560
1561 if (isCustom) {
1562 Tmp3 = TLI.LowerOperation(Result, DAG);
1563 if (Tmp3.Val) {
1564 Tmp1 = LegalizeOp(Tmp3);
1565 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1566 }
1567 }
1568 break;
1569 case TargetLowering::Expand:
1570 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1571 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1572 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
1573 LD->getSrcValueOffset());
1574 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1575 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1576 Tmp2 = LegalizeOp(Load.getValue(1));
1577 break;
1578 }
Chris Lattner296a83c2007-02-01 04:55:59 +00001579 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001580 // Turn the unsupported load into an EXTLOAD followed by an explicit
1581 // zero/sign extend inreg.
1582 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1583 Tmp1, Tmp2, LD->getSrcValue(),
1584 LD->getSrcValueOffset(), SrcVT);
1585 SDOperand ValRes;
1586 if (ExtType == ISD::SEXTLOAD)
1587 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1588 Result, DAG.getValueType(SrcVT));
1589 else
1590 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1591 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1592 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1593 break;
1594 }
1595 // Since loads produce two values, make sure to remember that we legalized
1596 // both of them.
1597 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1598 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1599 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001600 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001601 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001602 case ISD::EXTRACT_ELEMENT: {
1603 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1604 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001605 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001606 case Legal:
1607 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1608 // 1 -> Hi
1609 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1610 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1611 TLI.getShiftAmountTy()));
1612 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1613 } else {
1614 // 0 -> Lo
1615 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1616 Node->getOperand(0));
1617 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001618 break;
1619 case Expand:
1620 // Get both the low and high parts.
1621 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1622 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1623 Result = Tmp2; // 1 -> Hi
1624 else
1625 Result = Tmp1; // 0 -> Lo
1626 break;
1627 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001628 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001629 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001630
1631 case ISD::CopyToReg:
1632 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001633
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001634 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001635 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001636 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001637 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001638 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001639 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001640 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001641 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001642 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001643 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001644 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1645 Tmp3);
1646 } else {
1647 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001648 }
1649
1650 // Since this produces two values, make sure to remember that we legalized
1651 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001652 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001653 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001654 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001655 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001656 break;
1657
1658 case ISD::RET:
1659 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001660
1661 // Ensure that libcalls are emitted before a return.
1662 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1663 Tmp1 = LegalizeOp(Tmp1);
1664 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001665
Chris Lattnerdc750592005-01-07 07:47:09 +00001666 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001667 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001668 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001669 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001670 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001671 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001672 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001673 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001674 case Expand:
1675 if (Tmp2.getValueType() != MVT::Vector) {
1676 SDOperand Lo, Hi;
1677 ExpandOp(Tmp2, Lo, Hi);
Evan Cheng3432ab92006-12-11 19:27:14 +00001678 if (Hi.Val)
1679 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1680 else
1681 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001682 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001683 } else {
1684 SDNode *InVal = Tmp2.Val;
1685 unsigned NumElems =
1686 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1687 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1688
1689 // Figure out if there is a Packed type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001690 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001691 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1692 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001693 // Turn this into a return of the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001694 Tmp2 = PackVectorOp(Tmp2, TVT);
Evan Chenga2e99532006-05-26 23:09:09 +00001695 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001696 } else if (NumElems == 1) {
1697 // Turn this into a return of the scalar type.
1698 Tmp2 = PackVectorOp(Tmp2, EVT);
Evan Chenga2e99532006-05-26 23:09:09 +00001699 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001700
1701 // FIXME: Returns of gcc generic vectors smaller than a legal type
1702 // should be returned in integer registers!
1703
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001704 // The scalarized value type may not be legal, e.g. it might require
1705 // promotion or expansion. Relegalize the return.
1706 Result = LegalizeOp(Result);
1707 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001708 // FIXME: Returns of gcc generic vectors larger than a legal vector
1709 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001710 SDOperand Lo, Hi;
1711 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00001712 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001713 Result = LegalizeOp(Result);
1714 }
1715 }
Misha Brukman835702a2005-04-21 22:36:52 +00001716 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001717 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001718 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00001719 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001720 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001721 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001722 }
1723 break;
1724 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001725 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001726 break;
1727 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00001728 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00001729 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00001730 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00001731 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1732 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001733 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00001734 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001735 break;
1736 case Expand: {
1737 SDOperand Lo, Hi;
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001738 assert(Node->getOperand(i).getValueType() != MVT::Vector &&
1739 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001740 ExpandOp(Node->getOperand(i), Lo, Hi);
1741 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00001742 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00001743 if (Hi.Val) {
1744 NewValues.push_back(Hi);
1745 NewValues.push_back(Node->getOperand(i+1));
1746 }
Misha Brukman835702a2005-04-21 22:36:52 +00001747 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001748 }
1749 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001750 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001751 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001752
1753 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00001754 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00001755 else
Chris Lattner97af9d52006-08-08 01:09:31 +00001756 Result = DAG.getNode(ISD::RET, MVT::Other,
1757 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00001758 break;
1759 }
1760 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001761
Chris Lattner4d1ea712006-01-29 21:02:23 +00001762 if (Result.getOpcode() == ISD::RET) {
1763 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1764 default: assert(0 && "This action is not supported yet!");
1765 case TargetLowering::Legal: break;
1766 case TargetLowering::Custom:
1767 Tmp1 = TLI.LowerOperation(Result, DAG);
1768 if (Tmp1.Val) Result = Tmp1;
1769 break;
1770 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001771 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001772 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001773 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00001774 StoreSDNode *ST = cast<StoreSDNode>(Node);
1775 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1776 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Chris Lattnerdc750592005-01-07 07:47:09 +00001777
Evan Chengab51cf22006-10-13 21:14:26 +00001778 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001779 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1780 // FIXME: We shouldn't do this for TargetConstantFP's.
1781 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1782 // to phase ordering between legalized code and the dag combiner. This
1783 // probably means that we need to integrate dag combiner and legalizer
1784 // together.
1785 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
1786 if (CFP->getValueType(0) == MVT::f32) {
1787 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
1788 } else {
1789 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1790 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
1791 }
1792 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1793 ST->getSrcValueOffset());
1794 break;
1795 }
1796
Evan Chengab51cf22006-10-13 21:14:26 +00001797 switch (getTypeAction(ST->getStoredVT())) {
1798 case Legal: {
1799 Tmp3 = LegalizeOp(ST->getValue());
1800 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1801 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00001802
Evan Chengab51cf22006-10-13 21:14:26 +00001803 MVT::ValueType VT = Tmp3.getValueType();
1804 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1805 default: assert(0 && "This action is not supported yet!");
1806 case TargetLowering::Legal: break;
1807 case TargetLowering::Custom:
1808 Tmp1 = TLI.LowerOperation(Result, DAG);
1809 if (Tmp1.Val) Result = Tmp1;
1810 break;
1811 case TargetLowering::Promote:
1812 assert(MVT::isVector(VT) && "Unknown legal promote case!");
1813 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
1814 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
1815 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
1816 ST->getSrcValue(), ST->getSrcValueOffset());
1817 break;
1818 }
1819 break;
1820 }
1821 case Promote:
1822 // Truncate the value and store the result.
1823 Tmp3 = PromoteOp(ST->getValue());
1824 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1825 ST->getSrcValueOffset(), ST->getStoredVT());
1826 break;
1827
1828 case Expand:
1829 unsigned IncrementSize = 0;
1830 SDOperand Lo, Hi;
1831
1832 // If this is a vector type, then we have to calculate the increment as
1833 // the product of the element size in bytes, and the number of elements
1834 // in the high half of the vector.
1835 if (ST->getValue().getValueType() == MVT::Vector) {
1836 SDNode *InVal = ST->getValue().Val;
1837 unsigned NumElems =
1838 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
1839 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
1840
1841 // Figure out if there is a Packed type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001842 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00001843 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
1844 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001845 // Turn this into a normal store of the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00001846 Tmp3 = PackVectorOp(Node->getOperand(1), TVT);
1847 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1848 ST->getSrcValueOffset());
1849 Result = LegalizeOp(Result);
1850 break;
1851 } else if (NumElems == 1) {
1852 // Turn this into a normal store of the scalar type.
1853 Tmp3 = PackVectorOp(Node->getOperand(1), EVT);
1854 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1855 ST->getSrcValueOffset());
1856 // The scalarized value type may not be legal, e.g. it might require
1857 // promotion or expansion. Relegalize the scalar store.
1858 Result = LegalizeOp(Result);
1859 break;
1860 } else {
1861 SplitVectorOp(Node->getOperand(1), Lo, Hi);
1862 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
1863 }
1864 } else {
1865 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00001866 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00001867
1868 if (!TLI.isLittleEndian())
1869 std::swap(Lo, Hi);
1870 }
1871
1872 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
1873 ST->getSrcValueOffset());
Evan Cheng0076ca02006-12-12 19:53:13 +00001874
1875 if (Hi.Val == NULL) {
1876 // Must be int <-> float one-to-one expansion.
1877 Result = Lo;
1878 break;
1879 }
1880
Evan Chengab51cf22006-10-13 21:14:26 +00001881 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1882 getIntPtrConstant(IncrementSize));
1883 assert(isTypeLegal(Tmp2.getValueType()) &&
1884 "Pointers must be legal!");
1885 // FIXME: This sets the srcvalue of both halves to be the same, which is
1886 // wrong.
1887 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
1888 ST->getSrcValueOffset());
1889 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1890 break;
1891 }
1892 } else {
1893 // Truncating store
1894 assert(isTypeLegal(ST->getValue().getValueType()) &&
1895 "Cannot handle illegal TRUNCSTORE yet!");
1896 Tmp3 = LegalizeOp(ST->getValue());
1897
1898 // The only promote case we handle is TRUNCSTORE:i1 X into
1899 // -> TRUNCSTORE:i8 (and X, 1)
1900 if (ST->getStoredVT() == MVT::i1 &&
1901 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
1902 // Promote the bool to a mask then store.
1903 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
1904 DAG.getConstant(1, Tmp3.getValueType()));
1905 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1906 ST->getSrcValueOffset(), MVT::i8);
1907 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
1908 Tmp2 != ST->getBasePtr()) {
1909 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1910 ST->getOffset());
1911 }
1912
1913 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
1914 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001915 default: assert(0 && "This action is not supported yet!");
Evan Chengab51cf22006-10-13 21:14:26 +00001916 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001917 case TargetLowering::Custom:
1918 Tmp1 = TLI.LowerOperation(Result, DAG);
1919 if (Tmp1.Val) Result = Tmp1;
1920 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001921 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001922 }
1923 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001924 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001925 case ISD::PCMARKER:
1926 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001927 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001928 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001929 case ISD::STACKSAVE:
1930 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001931 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1932 Tmp1 = Result.getValue(0);
1933 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001934
Chris Lattnerb3266452006-01-13 02:50:02 +00001935 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1936 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001937 case TargetLowering::Legal: break;
1938 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001939 Tmp3 = TLI.LowerOperation(Result, DAG);
1940 if (Tmp3.Val) {
1941 Tmp1 = LegalizeOp(Tmp3);
1942 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001943 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001944 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001945 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001946 // Expand to CopyFromReg if the target set
1947 // StackPointerRegisterToSaveRestore.
1948 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001949 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001950 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001951 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001952 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001953 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1954 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001955 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001956 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001957 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001958
1959 // Since stacksave produce two values, make sure to remember that we
1960 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001961 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1962 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1963 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001964
Chris Lattnerb3266452006-01-13 02:50:02 +00001965 case ISD::STACKRESTORE:
1966 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1967 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001968 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001969
1970 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1971 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001972 case TargetLowering::Legal: break;
1973 case TargetLowering::Custom:
1974 Tmp1 = TLI.LowerOperation(Result, DAG);
1975 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001976 break;
1977 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001978 // Expand to CopyToReg if the target set
1979 // StackPointerRegisterToSaveRestore.
1980 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1981 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1982 } else {
1983 Result = Tmp1;
1984 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001985 break;
1986 }
1987 break;
1988
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001989 case ISD::READCYCLECOUNTER:
1990 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001991 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00001992 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
1993 Node->getValueType(0))) {
1994 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00001995 case TargetLowering::Legal:
1996 Tmp1 = Result.getValue(0);
1997 Tmp2 = Result.getValue(1);
1998 break;
Evan Cheng69739932006-11-29 08:26:18 +00001999 case TargetLowering::Custom:
2000 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002001 Tmp1 = LegalizeOp(Result.getValue(0));
2002 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002003 break;
2004 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002005
2006 // Since rdcc produce two values, make sure to remember that we legalized
2007 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002008 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2009 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002010 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002011
Chris Lattner39c67442005-01-14 22:08:15 +00002012 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002013 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2014 case Expand: assert(0 && "It's impossible to expand bools");
2015 case Legal:
2016 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2017 break;
2018 case Promote:
2019 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002020 // Make sure the condition is either zero or one.
2021 if (!TLI.MaskedValueIsZero(Tmp1,
2022 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2023 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002024 break;
2025 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002026 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002027 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002028
Chris Lattnerd02b0542006-01-28 10:58:55 +00002029 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002030
Nate Begeman987121a2005-08-23 04:29:48 +00002031 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002032 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002033 case TargetLowering::Legal: break;
2034 case TargetLowering::Custom: {
2035 Tmp1 = TLI.LowerOperation(Result, DAG);
2036 if (Tmp1.Val) Result = Tmp1;
2037 break;
2038 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002039 case TargetLowering::Expand:
2040 if (Tmp1.getOpcode() == ISD::SETCC) {
2041 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2042 Tmp2, Tmp3,
2043 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2044 } else {
2045 Result = DAG.getSelectCC(Tmp1,
2046 DAG.getConstant(0, Tmp1.getValueType()),
2047 Tmp2, Tmp3, ISD::SETNE);
2048 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002049 break;
2050 case TargetLowering::Promote: {
2051 MVT::ValueType NVT =
2052 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2053 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002054 if (MVT::isVector(Tmp2.getValueType())) {
2055 ExtOp = ISD::BIT_CONVERT;
2056 TruncOp = ISD::BIT_CONVERT;
2057 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002058 ExtOp = ISD::ANY_EXTEND;
2059 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002060 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002061 ExtOp = ISD::FP_EXTEND;
2062 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002063 }
2064 // Promote each of the values to the new type.
2065 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2066 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2067 // Perform the larger operation, then round down.
2068 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2069 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2070 break;
2071 }
2072 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002073 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002074 case ISD::SELECT_CC: {
2075 Tmp1 = Node->getOperand(0); // LHS
2076 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002077 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2078 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002079 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002080
Nate Begeman7e7f4392006-02-01 07:19:44 +00002081 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2082
2083 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2084 // the LHS is a legal SETCC itself. In this case, we need to compare
2085 // the result against zero to select between true and false values.
2086 if (Tmp2.Val == 0) {
2087 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2088 CC = DAG.getCondCode(ISD::SETNE);
2089 }
2090 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2091
2092 // Everything is legal, see if we should expand this op or something.
2093 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2094 default: assert(0 && "This action is not supported yet!");
2095 case TargetLowering::Legal: break;
2096 case TargetLowering::Custom:
2097 Tmp1 = TLI.LowerOperation(Result, DAG);
2098 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002099 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002100 }
2101 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002102 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002103 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002104 Tmp1 = Node->getOperand(0);
2105 Tmp2 = Node->getOperand(1);
2106 Tmp3 = Node->getOperand(2);
2107 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2108
2109 // If we had to Expand the SetCC operands into a SELECT node, then it may
2110 // not always be possible to return a true LHS & RHS. In this case, just
2111 // return the value we legalized, returned in the LHS
2112 if (Tmp2.Val == 0) {
2113 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002114 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002115 }
Nate Begeman987121a2005-08-23 04:29:48 +00002116
Chris Lattnerf263a232006-01-30 22:43:50 +00002117 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002118 default: assert(0 && "Cannot handle this action for SETCC yet!");
2119 case TargetLowering::Custom:
2120 isCustom = true;
2121 // FALLTHROUGH.
2122 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002123 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002124 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002125 Tmp4 = TLI.LowerOperation(Result, DAG);
2126 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002127 }
Nate Begeman987121a2005-08-23 04:29:48 +00002128 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002129 case TargetLowering::Promote: {
2130 // First step, figure out the appropriate operation to use.
2131 // Allow SETCC to not be supported for all legal data types
2132 // Mostly this targets FP
2133 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002134 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002135
2136 // Scan for the appropriate larger type to use.
2137 while (1) {
2138 NewInTy = (MVT::ValueType)(NewInTy+1);
2139
2140 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2141 "Fell off of the edge of the integer world");
2142 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2143 "Fell off of the edge of the floating point world");
2144
2145 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002146 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002147 break;
2148 }
2149 if (MVT::isInteger(NewInTy))
2150 assert(0 && "Cannot promote Legal Integer SETCC yet");
2151 else {
2152 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2153 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2154 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002155 Tmp1 = LegalizeOp(Tmp1);
2156 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002157 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002158 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002159 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002160 }
Nate Begeman987121a2005-08-23 04:29:48 +00002161 case TargetLowering::Expand:
2162 // Expand a setcc node into a select_cc of the same condition, lhs, and
2163 // rhs that selects between const 1 (true) and const 0 (false).
2164 MVT::ValueType VT = Node->getValueType(0);
2165 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2166 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002167 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002168 break;
2169 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002170 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002171 case ISD::MEMSET:
2172 case ISD::MEMCPY:
2173 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002174 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002175 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2176
2177 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2178 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2179 case Expand: assert(0 && "Cannot expand a byte!");
2180 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002181 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002182 break;
2183 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002184 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002185 break;
2186 }
2187 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002188 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002189 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002190
2191 SDOperand Tmp4;
2192 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002193 case Expand: {
2194 // Length is too big, just take the lo-part of the length.
2195 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002196 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002197 break;
2198 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002199 case Legal:
2200 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002201 break;
2202 case Promote:
2203 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002204 break;
2205 }
2206
2207 SDOperand Tmp5;
2208 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2209 case Expand: assert(0 && "Cannot expand this yet!");
2210 case Legal:
2211 Tmp5 = LegalizeOp(Node->getOperand(4));
2212 break;
2213 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002214 Tmp5 = PromoteOp(Node->getOperand(4));
2215 break;
2216 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002217
2218 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2219 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002220 case TargetLowering::Custom:
2221 isCustom = true;
2222 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00002223 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002224 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002225 if (isCustom) {
2226 Tmp1 = TLI.LowerOperation(Result, DAG);
2227 if (Tmp1.Val) Result = Tmp1;
2228 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002229 break;
2230 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002231 // Otherwise, the target does not support this operation. Lower the
2232 // operation to an explicit libcall as appropriate.
2233 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002234 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002235 TargetLowering::ArgListTy Args;
2236 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002237
Reid Spencer6dced922005-01-12 14:53:45 +00002238 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002239 if (Node->getOpcode() == ISD::MEMSET) {
Reid Spencer791864c2007-01-03 04:22:32 +00002240 Entry.Node = Tmp2; Entry.isSigned = false; Entry.Ty = IntPtrTy;
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002241 Entry.isInReg = false; Entry.isSRet = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002242 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002243 // Extend the (previously legalized) ubyte argument to be an int value
2244 // for the call.
2245 if (Tmp3.getValueType() > MVT::i32)
2246 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2247 else
2248 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Reid Spencere63b6512006-12-31 05:55:36 +00002249 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSigned = true;
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002250 Entry.isInReg = false; Entry.isSRet = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002251 Args.push_back(Entry);
2252 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSigned = false;
2253 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002254
2255 FnName = "memset";
2256 } else if (Node->getOpcode() == ISD::MEMCPY ||
2257 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002258 Entry.Ty = IntPtrTy;
2259 Entry.isSigned = false; Entry.isInReg = false; Entry.isSRet = false;
Reid Spencer791864c2007-01-03 04:22:32 +00002260 Entry.Node = Tmp2; Args.push_back(Entry);
2261 Entry.Node = Tmp3; Args.push_back(Entry);
2262 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002263 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2264 } else {
2265 assert(0 && "Unknown op!");
2266 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002267
Chris Lattner85d70c62005-01-11 05:57:22 +00002268 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002269 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002270 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002271 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002272 break;
2273 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002274 }
2275 break;
2276 }
Chris Lattner5385db52005-05-09 20:23:03 +00002277
Chris Lattner4157c412005-04-02 04:00:59 +00002278 case ISD::SHL_PARTS:
2279 case ISD::SRA_PARTS:
2280 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002281 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002282 bool Changed = false;
2283 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2284 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2285 Changed |= Ops.back() != Node->getOperand(i);
2286 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002287 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002288 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002289
Evan Cheng870e4f82006-01-09 18:31:59 +00002290 switch (TLI.getOperationAction(Node->getOpcode(),
2291 Node->getValueType(0))) {
2292 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002293 case TargetLowering::Legal: break;
2294 case TargetLowering::Custom:
2295 Tmp1 = TLI.LowerOperation(Result, DAG);
2296 if (Tmp1.Val) {
2297 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002298 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002299 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002300 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2301 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002302 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002303 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002304 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002305 return RetVal;
2306 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002307 break;
2308 }
2309
Chris Lattner13fe99c2005-04-02 05:00:07 +00002310 // Since these produce multiple values, make sure to remember that we
2311 // legalized all of them.
2312 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2313 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2314 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002315 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002316
2317 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002318 case ISD::ADD:
2319 case ISD::SUB:
2320 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002321 case ISD::MULHS:
2322 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002323 case ISD::UDIV:
2324 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002325 case ISD::AND:
2326 case ISD::OR:
2327 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002328 case ISD::SHL:
2329 case ISD::SRL:
2330 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002331 case ISD::FADD:
2332 case ISD::FSUB:
2333 case ISD::FMUL:
2334 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002335 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002336 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2337 case Expand: assert(0 && "Not possible");
2338 case Legal:
2339 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2340 break;
2341 case Promote:
2342 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2343 break;
2344 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002345
2346 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002347
Andrew Lenharth72594262005-12-24 23:42:32 +00002348 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002349 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002350 case TargetLowering::Legal: break;
2351 case TargetLowering::Custom:
2352 Tmp1 = TLI.LowerOperation(Result, DAG);
2353 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002354 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002355 case TargetLowering::Expand: {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002356 if (Node->getValueType(0) == MVT::i32) {
2357 switch (Node->getOpcode()) {
2358 default: assert(0 && "Do not know how to expand this integer BinOp!");
2359 case ISD::UDIV:
2360 case ISD::SDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00002361 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2362 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002363 SDOperand Dummy;
Reid Spencere63b6512006-12-31 05:55:36 +00002364 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002365 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002366 };
2367 break;
2368 }
2369
Chris Lattner87f08092006-04-02 03:57:31 +00002370 assert(MVT::isVector(Node->getValueType(0)) &&
2371 "Cannot expand this binary operator!");
2372 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002373 SmallVector<SDOperand, 8> Ops;
Chris Lattner87f08092006-04-02 03:57:31 +00002374 MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0));
2375 MVT::ValueType PtrVT = TLI.getPointerTy();
2376 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2377 i != e; ++i) {
2378 SDOperand Idx = DAG.getConstant(i, PtrVT);
2379 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2380 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2381 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2382 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002383 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2384 &Ops[0], Ops.size());
Chris Lattner87f08092006-04-02 03:57:31 +00002385 break;
2386 }
Evan Cheng119266e2006-04-12 21:20:24 +00002387 case TargetLowering::Promote: {
2388 switch (Node->getOpcode()) {
2389 default: assert(0 && "Do not know how to promote this BinOp!");
2390 case ISD::AND:
2391 case ISD::OR:
2392 case ISD::XOR: {
2393 MVT::ValueType OVT = Node->getValueType(0);
2394 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2395 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2396 // Bit convert each of the values to the new type.
2397 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2398 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2399 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2400 // Bit convert the result back the original type.
2401 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2402 break;
2403 }
2404 }
2405 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002406 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002407 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002408
2409 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2410 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2411 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2412 case Expand: assert(0 && "Not possible");
2413 case Legal:
2414 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2415 break;
2416 case Promote:
2417 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2418 break;
2419 }
2420
2421 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2422
2423 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2424 default: assert(0 && "Operation not supported");
2425 case TargetLowering::Custom:
2426 Tmp1 = TLI.LowerOperation(Result, DAG);
2427 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002428 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002429 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002430 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00002431 // If this target supports fabs/fneg natively and select is cheap,
2432 // do this efficiently.
2433 if (!TLI.isSelectExpensive() &&
2434 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2435 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00002436 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00002437 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002438 // Get the sign bit of the RHS.
2439 MVT::ValueType IVT =
2440 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2441 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2442 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2443 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2444 // Get the absolute value of the result.
2445 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2446 // Select between the nabs and abs value based on the sign bit of
2447 // the input.
2448 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2449 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2450 AbsVal),
2451 AbsVal);
2452 Result = LegalizeOp(Result);
2453 break;
2454 }
2455
2456 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002457 MVT::ValueType NVT =
2458 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2459 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2460 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2461 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002462 break;
2463 }
Evan Cheng003feb02007-01-04 21:56:39 +00002464 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002465 break;
2466
Nate Begeman5965bd12006-02-17 05:43:56 +00002467 case ISD::ADDC:
2468 case ISD::SUBC:
2469 Tmp1 = LegalizeOp(Node->getOperand(0));
2470 Tmp2 = LegalizeOp(Node->getOperand(1));
2471 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2472 // Since this produces two values, make sure to remember that we legalized
2473 // both of them.
2474 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2475 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2476 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002477
Nate Begeman5965bd12006-02-17 05:43:56 +00002478 case ISD::ADDE:
2479 case ISD::SUBE:
2480 Tmp1 = LegalizeOp(Node->getOperand(0));
2481 Tmp2 = LegalizeOp(Node->getOperand(1));
2482 Tmp3 = LegalizeOp(Node->getOperand(2));
2483 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2484 // Since this produces two values, make sure to remember that we legalized
2485 // both of them.
2486 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2487 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2488 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002489
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002490 case ISD::BUILD_PAIR: {
2491 MVT::ValueType PairTy = Node->getValueType(0);
2492 // TODO: handle the case where the Lo and Hi operands are not of legal type
2493 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2494 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2495 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002496 case TargetLowering::Promote:
2497 case TargetLowering::Custom:
2498 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002499 case TargetLowering::Legal:
2500 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2501 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2502 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002503 case TargetLowering::Expand:
2504 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2505 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2506 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2507 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2508 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002509 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002510 break;
2511 }
2512 break;
2513 }
2514
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002515 case ISD::UREM:
2516 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002517 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002518 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2519 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002520
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002521 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002522 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2523 case TargetLowering::Custom:
2524 isCustom = true;
2525 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002526 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002527 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002528 if (isCustom) {
2529 Tmp1 = TLI.LowerOperation(Result, DAG);
2530 if (Tmp1.Val) Result = Tmp1;
2531 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002532 break;
Chris Lattner81914422005-08-03 20:31:37 +00002533 case TargetLowering::Expand:
Evan Cheng1fc7c362006-09-18 23:28:33 +00002534 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002535 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002536 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002537 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2538 TargetLowering::Legal) {
2539 // X % Y -> X-X/Y*Y
2540 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng1fc7c362006-09-18 23:28:33 +00002541 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002542 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2543 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2544 } else {
2545 assert(Node->getValueType(0) == MVT::i32 &&
2546 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00002547 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2548 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002549 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002550 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002551 }
Chris Lattner81914422005-08-03 20:31:37 +00002552 } else {
2553 // Floating point mod -> fmod libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002554 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2555 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner81914422005-08-03 20:31:37 +00002556 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002557 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2558 false/*sign irrelevant*/, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002559 }
2560 break;
2561 }
2562 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002563 case ISD::VAARG: {
2564 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2565 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2566
Chris Lattner364b89a2006-01-28 07:42:08 +00002567 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002568 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2569 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002570 case TargetLowering::Custom:
2571 isCustom = true;
2572 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002573 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002574 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2575 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002576 Tmp1 = Result.getValue(1);
2577
2578 if (isCustom) {
2579 Tmp2 = TLI.LowerOperation(Result, DAG);
2580 if (Tmp2.Val) {
2581 Result = LegalizeOp(Tmp2);
2582 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2583 }
2584 }
Nate Begemane74795c2006-01-25 18:21:52 +00002585 break;
2586 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002587 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00002588 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00002589 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002590 // Increment the pointer, VAList, to the next vaarg
2591 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2592 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2593 TLI.getPointerTy()));
2594 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00002595 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2596 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002597 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00002598 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002599 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002600 Result = LegalizeOp(Result);
2601 break;
2602 }
2603 }
2604 // Since VAARG produces two values, make sure to remember that we
2605 // legalized both of them.
2606 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002607 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2608 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002609 }
2610
2611 case ISD::VACOPY:
2612 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2613 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2614 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2615
2616 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2617 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002618 case TargetLowering::Custom:
2619 isCustom = true;
2620 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002621 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002622 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2623 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002624 if (isCustom) {
2625 Tmp1 = TLI.LowerOperation(Result, DAG);
2626 if (Tmp1.Val) Result = Tmp1;
2627 }
Nate Begemane74795c2006-01-25 18:21:52 +00002628 break;
2629 case TargetLowering::Expand:
2630 // This defaults to loading a pointer from the input and storing it to the
2631 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002632 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00002633 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00002634 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2635 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00002636 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2637 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002638 break;
2639 }
2640 break;
2641
2642 case ISD::VAEND:
2643 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2644 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2645
2646 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2647 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002648 case TargetLowering::Custom:
2649 isCustom = true;
2650 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002651 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002652 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002653 if (isCustom) {
2654 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2655 if (Tmp1.Val) Result = Tmp1;
2656 }
Nate Begemane74795c2006-01-25 18:21:52 +00002657 break;
2658 case TargetLowering::Expand:
2659 Result = Tmp1; // Default to a no-op, return the chain
2660 break;
2661 }
2662 break;
2663
2664 case ISD::VASTART:
2665 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2666 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2667
Chris Lattnerd02b0542006-01-28 10:58:55 +00002668 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2669
Nate Begemane74795c2006-01-25 18:21:52 +00002670 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2671 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002672 case TargetLowering::Legal: break;
2673 case TargetLowering::Custom:
2674 Tmp1 = TLI.LowerOperation(Result, DAG);
2675 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002676 break;
2677 }
2678 break;
2679
Nate Begeman1b8121b2006-01-11 21:21:00 +00002680 case ISD::ROTL:
2681 case ISD::ROTR:
2682 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2683 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002684
2685 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
2686 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002687 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00002688 break;
2689
Nate Begeman2fba8a32006-01-14 03:14:10 +00002690 case ISD::BSWAP:
2691 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2692 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002693 case TargetLowering::Custom:
2694 assert(0 && "Cannot custom legalize this yet!");
2695 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002696 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002697 break;
2698 case TargetLowering::Promote: {
2699 MVT::ValueType OVT = Tmp1.getValueType();
2700 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2701 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002702
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002703 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2704 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2705 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2706 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2707 break;
2708 }
2709 case TargetLowering::Expand:
2710 Result = ExpandBSWAP(Tmp1);
2711 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002712 }
2713 break;
2714
Andrew Lenharth5e177822005-05-03 17:19:30 +00002715 case ISD::CTPOP:
2716 case ISD::CTTZ:
2717 case ISD::CTLZ:
2718 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2719 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002720 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00002721 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002722 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002723 break;
2724 case TargetLowering::Promote: {
2725 MVT::ValueType OVT = Tmp1.getValueType();
2726 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002727
2728 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002729 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2730 // Perform the larger operation, then subtract if needed.
2731 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002732 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002733 case ISD::CTPOP:
2734 Result = Tmp1;
2735 break;
2736 case ISD::CTTZ:
2737 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002738 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
2739 DAG.getConstant(getSizeInBits(NVT), NVT),
2740 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002741 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00002742 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
2743 break;
2744 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002745 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002746 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2747 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00002748 getSizeInBits(OVT), NVT));
2749 break;
2750 }
2751 break;
2752 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002753 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002754 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002755 break;
2756 }
2757 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002758
Chris Lattner13fe99c2005-04-02 05:00:07 +00002759 // Unary operators
2760 case ISD::FABS:
2761 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002762 case ISD::FSQRT:
2763 case ISD::FSIN:
2764 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002765 Tmp1 = LegalizeOp(Node->getOperand(0));
2766 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002767 case TargetLowering::Promote:
2768 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00002769 isCustom = true;
2770 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00002771 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002772 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00002773 if (isCustom) {
2774 Tmp1 = TLI.LowerOperation(Result, DAG);
2775 if (Tmp1.Val) Result = Tmp1;
2776 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002777 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002778 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002779 switch (Node->getOpcode()) {
2780 default: assert(0 && "Unreachable!");
2781 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002782 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2783 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002784 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002785 break;
Chris Lattner80026402005-04-30 04:43:14 +00002786 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002787 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2788 MVT::ValueType VT = Node->getValueType(0);
2789 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002790 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002791 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2792 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002793 break;
2794 }
2795 case ISD::FSQRT:
2796 case ISD::FSIN:
2797 case ISD::FCOS: {
2798 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng31cbddf2007-01-12 02:11:51 +00002799 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00002800 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00002801 case ISD::FSQRT:
2802 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
2803 break;
2804 case ISD::FSIN:
2805 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
2806 break;
2807 case ISD::FCOS:
2808 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
2809 break;
Chris Lattner80026402005-04-30 04:43:14 +00002810 default: assert(0 && "Unreachable!");
2811 }
Nate Begeman77558da2005-08-04 21:43:28 +00002812 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002813 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2814 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002815 break;
2816 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002817 }
2818 break;
2819 }
2820 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00002821 case ISD::FPOWI: {
2822 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002823 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2824 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattnerf0359b32006-09-09 06:03:30 +00002825 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002826 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2827 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00002828 break;
2829 }
Chris Lattner36e663d2005-12-23 00:16:34 +00002830 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002831 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002832 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002833 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002834 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2835 Node->getOperand(0).getValueType())) {
2836 default: assert(0 && "Unknown operation action!");
2837 case TargetLowering::Expand:
2838 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2839 break;
2840 case TargetLowering::Legal:
2841 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002842 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002843 break;
2844 }
2845 }
2846 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00002847 case ISD::VBIT_CONVERT: {
2848 assert(Op.getOperand(0).getValueType() == MVT::Vector &&
2849 "Can only have VBIT_CONVERT where input or output is MVT::Vector!");
2850
2851 // The input has to be a vector type, we have to either scalarize it, pack
2852 // it, or convert it based on whether the input vector type is legal.
2853 SDNode *InVal = Node->getOperand(0).Val;
2854 unsigned NumElems =
2855 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
2856 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
2857
2858 // Figure out if there is a Packed type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002859 // type. If so, convert to the vector type.
Chris Lattnera4f68052006-03-24 02:26:29 +00002860 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
2861 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
2862 // Turn this into a bit convert of the packed input.
2863 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2864 PackVectorOp(Node->getOperand(0), TVT));
2865 break;
2866 } else if (NumElems == 1) {
2867 // Turn this into a bit convert of the scalar input.
2868 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
2869 PackVectorOp(Node->getOperand(0), EVT));
2870 break;
2871 } else {
2872 // FIXME: UNIMP! Store then reload
2873 assert(0 && "Cast from unsupported vector type not implemented yet!");
2874 }
2875 }
2876
Chris Lattner13fe99c2005-04-02 05:00:07 +00002877 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002878 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002879 case ISD::UINT_TO_FP: {
2880 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002881 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2882 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002883 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002884 Node->getOperand(0).getValueType())) {
2885 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002886 case TargetLowering::Custom:
2887 isCustom = true;
2888 // FALLTHROUGH
2889 case TargetLowering::Legal:
2890 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002891 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002892 if (isCustom) {
2893 Tmp1 = TLI.LowerOperation(Result, DAG);
2894 if (Tmp1.Val) Result = Tmp1;
2895 }
2896 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002897 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002898 Result = ExpandLegalINT_TO_FP(isSigned,
2899 LegalizeOp(Node->getOperand(0)),
2900 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002901 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002902 case TargetLowering::Promote:
2903 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2904 Node->getValueType(0),
2905 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002906 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002907 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002908 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002909 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002910 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2911 Node->getValueType(0), Node->getOperand(0));
2912 break;
2913 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002914 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002915 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002916 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2917 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002918 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002919 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2920 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002921 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002922 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2923 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002924 break;
2925 }
2926 break;
2927 }
2928 case ISD::TRUNCATE:
2929 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2930 case Legal:
2931 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002932 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002933 break;
2934 case Expand:
2935 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2936
2937 // Since the result is legal, we should just be able to truncate the low
2938 // part of the source.
2939 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2940 break;
2941 case Promote:
2942 Result = PromoteOp(Node->getOperand(0));
2943 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2944 break;
2945 }
2946 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002947
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002948 case ISD::FP_TO_SINT:
2949 case ISD::FP_TO_UINT:
2950 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2951 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002952 Tmp1 = LegalizeOp(Node->getOperand(0));
2953
Chris Lattner44fe26f2005-07-29 00:11:56 +00002954 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2955 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002956 case TargetLowering::Custom:
2957 isCustom = true;
2958 // FALLTHROUGH
2959 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002960 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002961 if (isCustom) {
2962 Tmp1 = TLI.LowerOperation(Result, DAG);
2963 if (Tmp1.Val) Result = Tmp1;
2964 }
2965 break;
2966 case TargetLowering::Promote:
2967 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2968 Node->getOpcode() == ISD::FP_TO_SINT);
2969 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002970 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002971 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2972 SDOperand True, False;
2973 MVT::ValueType VT = Node->getOperand(0).getValueType();
2974 MVT::ValueType NVT = Node->getValueType(0);
2975 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2976 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2977 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2978 Node->getOperand(0), Tmp2, ISD::SETLT);
2979 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2980 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002981 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002982 Tmp2));
2983 False = DAG.getNode(ISD::XOR, NVT, False,
2984 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002985 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2986 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002987 } else {
2988 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2989 }
2990 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002991 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002992 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002993 case Expand: {
2994 // Convert f32 / f64 to i32 / i64.
2995 MVT::ValueType VT = Op.getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00002996 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00002997 switch (Node->getOpcode()) {
2998 case ISD::FP_TO_SINT:
2999 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003000 LC = (VT == MVT::i32)
3001 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003002 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00003003 LC = (VT == MVT::i32)
3004 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003005 break;
3006 case ISD::FP_TO_UINT:
3007 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003008 LC = (VT == MVT::i32)
3009 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003010 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00003011 LC = (VT == MVT::i32)
3012 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003013 break;
3014 default: assert(0 && "Unreachable!");
3015 }
3016 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003017 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3018 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003019 break;
3020 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003021 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003022 Tmp1 = PromoteOp(Node->getOperand(0));
3023 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3024 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003025 break;
3026 }
3027 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003028
Chris Lattner7753f172005-09-02 00:18:10 +00003029 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003030 case ISD::ZERO_EXTEND:
3031 case ISD::SIGN_EXTEND:
3032 case ISD::FP_EXTEND:
3033 case ISD::FP_ROUND:
3034 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003035 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003036 case Legal:
3037 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003038 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003039 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003040 case Promote:
3041 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003042 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003043 Tmp1 = PromoteOp(Node->getOperand(0));
3044 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003045 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003046 case ISD::ZERO_EXTEND:
3047 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003048 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003049 Result = DAG.getZeroExtendInReg(Result,
3050 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003051 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003052 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003053 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003054 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003055 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003056 Result,
3057 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003058 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003059 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003060 Result = PromoteOp(Node->getOperand(0));
3061 if (Result.getValueType() != Op.getValueType())
3062 // Dynamically dead while we have only 2 FP types.
3063 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3064 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003065 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00003066 Result = PromoteOp(Node->getOperand(0));
3067 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3068 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003069 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003070 }
3071 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003072 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003073 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003074 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003075 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003076
3077 // If this operation is not supported, convert it to a shl/shr or load/store
3078 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003079 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3080 default: assert(0 && "This action not supported for this op yet!");
3081 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003082 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003083 break;
3084 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003085 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003086 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003087 // NOTE: we could fall back on load/store here too for targets without
3088 // SAR. However, it is doubtful that any exist.
3089 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3090 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003091 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003092 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3093 Node->getOperand(0), ShiftCst);
3094 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3095 Result, ShiftCst);
3096 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003097 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003098 // EXTLOAD pair, targetting a temporary location (a stack slot).
3099
3100 // NOTE: there is a choice here between constantly creating new stack
3101 // slots and always reusing the same one. We currently always create
3102 // new ones, as reuse may inhibit scheduling.
3103 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Owen Anderson20a631f2006-05-03 01:29:57 +00003104 unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty);
Chris Lattner945e4372007-02-14 05:52:17 +00003105 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner99222f72005-01-15 07:15:18 +00003106 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00003107 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00003108 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
3109 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003110 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3111 StackSlot, NULL, 0, ExtraVT);
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003112 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003113 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00003114 } else {
3115 assert(0 && "Unknown op");
3116 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003117 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003118 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003119 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003120 }
Chris Lattner99222f72005-01-15 07:15:18 +00003121 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003122
Chris Lattner101ea662006-04-08 04:13:17 +00003123 assert(Result.getValueType() == Op.getValueType() &&
3124 "Bad legalization!");
3125
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003126 // Make sure that the generated code is itself legal.
3127 if (Result != Op)
3128 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003129
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003130 // Note that LegalizeOp may be reentered even from single-use nodes, which
3131 // means that we always must cache transformed nodes.
3132 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003133 return Result;
3134}
3135
Chris Lattner4d978642005-01-15 22:16:26 +00003136/// PromoteOp - Given an operation that produces a value in an invalid type,
3137/// promote it to compute the value into a larger type. The produced value will
3138/// have the correct bits for the low portion of the register, but no guarantee
3139/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003140SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3141 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003142 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003143 assert(getTypeAction(VT) == Promote &&
3144 "Caller should expand or legalize operands that are not promotable!");
3145 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3146 "Cannot promote to smaller type!");
3147
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003148 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003149 SDOperand Result;
3150 SDNode *Node = Op.Val;
3151
Chris Lattner4b0ddb22007-02-04 01:17:38 +00003152 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00003153 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003154
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003155 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003156 case ISD::CopyFromReg:
3157 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003158 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003159#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +00003160 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003161#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003162 assert(0 && "Do not know how to promote this operator!");
3163 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003164 case ISD::UNDEF:
3165 Result = DAG.getNode(ISD::UNDEF, NVT);
3166 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003167 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003168 if (VT != MVT::i1)
3169 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3170 else
3171 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003172 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3173 break;
3174 case ISD::ConstantFP:
3175 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3176 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3177 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003178
Chris Lattner2cb338d2005-01-18 02:59:52 +00003179 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003180 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003181 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3182 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003183 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003184
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003185 case ISD::TRUNCATE:
3186 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3187 case Legal:
3188 Result = LegalizeOp(Node->getOperand(0));
3189 assert(Result.getValueType() >= NVT &&
3190 "This truncation doesn't make sense!");
3191 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3192 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3193 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003194 case Promote:
3195 // The truncation is not required, because we don't guarantee anything
3196 // about high bits anyway.
3197 Result = PromoteOp(Node->getOperand(0));
3198 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003199 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003200 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3201 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003202 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003203 }
3204 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003205 case ISD::SIGN_EXTEND:
3206 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003207 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003208 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3209 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3210 case Legal:
3211 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003212 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003213 break;
3214 case Promote:
3215 // Promote the reg if it's smaller.
3216 Result = PromoteOp(Node->getOperand(0));
3217 // The high bits are not guaranteed to be anything. Insert an extend.
3218 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003219 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003220 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003221 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003222 Result = DAG.getZeroExtendInReg(Result,
3223 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003224 break;
3225 }
3226 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003227 case ISD::BIT_CONVERT:
3228 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3229 Result = PromoteOp(Result);
3230 break;
3231
Chris Lattner4d978642005-01-15 22:16:26 +00003232 case ISD::FP_EXTEND:
3233 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3234 case ISD::FP_ROUND:
3235 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3236 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3237 case Promote: assert(0 && "Unreachable with 2 FP types!");
3238 case Legal:
3239 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003240 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003241 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003242 break;
3243 }
3244 break;
3245
3246 case ISD::SINT_TO_FP:
3247 case ISD::UINT_TO_FP:
3248 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3249 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003250 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003251 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003252 break;
3253
3254 case Promote:
3255 Result = PromoteOp(Node->getOperand(0));
3256 if (Node->getOpcode() == ISD::SINT_TO_FP)
3257 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003258 Result,
3259 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003260 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003261 Result = DAG.getZeroExtendInReg(Result,
3262 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003263 // No extra round required here.
3264 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003265 break;
3266 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003267 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3268 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003269 // Round if we cannot tolerate excess precision.
3270 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003271 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3272 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003273 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003274 }
Chris Lattner4d978642005-01-15 22:16:26 +00003275 break;
3276
Chris Lattner268d4572005-12-09 17:32:47 +00003277 case ISD::SIGN_EXTEND_INREG:
3278 Result = PromoteOp(Node->getOperand(0));
3279 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3280 Node->getOperand(1));
3281 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003282 case ISD::FP_TO_SINT:
3283 case ISD::FP_TO_UINT:
3284 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3285 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003286 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003287 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003288 break;
3289 case Promote:
3290 // The input result is prerounded, so we don't have to do anything
3291 // special.
3292 Tmp1 = PromoteOp(Node->getOperand(0));
3293 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003294 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003295 // If we're promoting a UINT to a larger size, check to see if the new node
3296 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3297 // we can use that instead. This allows us to generate better code for
3298 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3299 // legal, such as PowerPC.
3300 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003301 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003302 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3303 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003304 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3305 } else {
3306 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3307 }
Chris Lattner4d978642005-01-15 22:16:26 +00003308 break;
3309
Chris Lattner13fe99c2005-04-02 05:00:07 +00003310 case ISD::FABS:
3311 case ISD::FNEG:
3312 Tmp1 = PromoteOp(Node->getOperand(0));
3313 assert(Tmp1.getValueType() == NVT);
3314 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3315 // NOTE: we do not have to do any extra rounding here for
3316 // NoExcessFPPrecision, because we know the input will have the appropriate
3317 // precision, and these operations don't modify precision at all.
3318 break;
3319
Chris Lattner9d6fa982005-04-28 21:44:33 +00003320 case ISD::FSQRT:
3321 case ISD::FSIN:
3322 case ISD::FCOS:
3323 Tmp1 = PromoteOp(Node->getOperand(0));
3324 assert(Tmp1.getValueType() == NVT);
3325 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003326 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003327 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3328 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003329 break;
3330
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003331 case ISD::AND:
3332 case ISD::OR:
3333 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003334 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003335 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003336 case ISD::MUL:
3337 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003338 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003339 // that too is okay if they are integer operations.
3340 Tmp1 = PromoteOp(Node->getOperand(0));
3341 Tmp2 = PromoteOp(Node->getOperand(1));
3342 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3343 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003344 break;
3345 case ISD::FADD:
3346 case ISD::FSUB:
3347 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003348 Tmp1 = PromoteOp(Node->getOperand(0));
3349 Tmp2 = PromoteOp(Node->getOperand(1));
3350 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3351 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3352
3353 // Floating point operations will give excess precision that we may not be
3354 // able to tolerate. If we DO allow excess precision, just leave it,
3355 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003356 // FIXME: Why would we need to round FP ops more than integer ones?
3357 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003358 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003359 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3360 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003361 break;
3362
Chris Lattner4d978642005-01-15 22:16:26 +00003363 case ISD::SDIV:
3364 case ISD::SREM:
3365 // These operators require that their input be sign extended.
3366 Tmp1 = PromoteOp(Node->getOperand(0));
3367 Tmp2 = PromoteOp(Node->getOperand(1));
3368 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003369 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3370 DAG.getValueType(VT));
3371 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3372 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003373 }
3374 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3375
3376 // Perform FP_ROUND: this is probably overly pessimistic.
3377 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003378 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3379 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003380 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003381 case ISD::FDIV:
3382 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003383 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003384 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00003385 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3386 case Legal:
3387 Tmp1 = LegalizeOp(Node->getOperand(0));
3388 break;
3389 case Promote:
3390 Tmp1 = PromoteOp(Node->getOperand(0));
3391 break;
3392 case Expand:
3393 assert(0 && "not implemented");
3394 }
3395 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3396 case Legal:
3397 Tmp2 = LegalizeOp(Node->getOperand(1));
3398 break;
3399 case Promote:
3400 Tmp2 = PromoteOp(Node->getOperand(1));
3401 break;
3402 case Expand:
3403 assert(0 && "not implemented");
3404 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00003405 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3406
3407 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003408 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003409 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3410 DAG.getValueType(VT));
3411 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003412
3413 case ISD::UDIV:
3414 case ISD::UREM:
3415 // These operators require that their input be zero extended.
3416 Tmp1 = PromoteOp(Node->getOperand(0));
3417 Tmp2 = PromoteOp(Node->getOperand(1));
3418 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003419 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3420 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003421 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3422 break;
3423
3424 case ISD::SHL:
3425 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003426 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003427 break;
3428 case ISD::SRA:
3429 // The input value must be properly sign extended.
3430 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003431 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3432 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003433 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003434 break;
3435 case ISD::SRL:
3436 // The input value must be properly zero extended.
3437 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003438 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003439 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003440 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003441
3442 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003443 Tmp1 = Node->getOperand(0); // Get the chain.
3444 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003445 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3446 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3447 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3448 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00003449 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00003450 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00003451 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003452 // Increment the pointer, VAList, to the next vaarg
3453 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3454 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3455 TLI.getPointerTy()));
3456 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00003457 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3458 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003459 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003460 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00003461 }
3462 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003463 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00003464 break;
3465
Evan Chenge71fe34d2006-10-09 20:57:25 +00003466 case ISD::LOAD: {
3467 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00003468 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3469 ? ISD::EXTLOAD : LD->getExtensionType();
3470 Result = DAG.getExtLoad(ExtType, NVT,
3471 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00003472 LD->getSrcValue(), LD->getSrcValueOffset(),
Evan Chengd35734b2006-10-11 07:10:22 +00003473 LD->getLoadedVT());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003474 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003475 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003476 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00003477 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003478 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003479 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3480 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003481 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003482 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003483 case ISD::SELECT_CC:
3484 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3485 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3486 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003487 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00003488 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003489 case ISD::BSWAP:
3490 Tmp1 = Node->getOperand(0);
3491 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3492 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3493 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3494 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
3495 TLI.getShiftAmountTy()));
3496 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003497 case ISD::CTPOP:
3498 case ISD::CTTZ:
3499 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003500 // Zero extend the argument
3501 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003502 // Perform the larger operation, then subtract if needed.
3503 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003504 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003505 case ISD::CTPOP:
3506 Result = Tmp1;
3507 break;
3508 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003509 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003510 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00003511 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003512 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003513 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003514 break;
3515 case ISD::CTLZ:
3516 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003517 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
3518 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003519 getSizeInBits(VT), NVT));
3520 break;
3521 }
3522 break;
Chris Lattner6f423252006-03-31 17:55:51 +00003523 case ISD::VEXTRACT_VECTOR_ELT:
3524 Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op));
3525 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00003526 case ISD::EXTRACT_VECTOR_ELT:
3527 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3528 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003529 }
3530
3531 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003532
3533 // Make sure the result is itself legal.
3534 Result = LegalizeOp(Result);
3535
3536 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003537 AddPromotedOperand(Op, Result);
3538 return Result;
3539}
Chris Lattnerdc750592005-01-07 07:47:09 +00003540
Chris Lattner6f423252006-03-31 17:55:51 +00003541/// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a
3542/// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based
3543/// on the vector type. The return type of this matches the element type of the
3544/// vector, which may not be legal for the target.
3545SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) {
3546 // We know that operand #0 is the Vec vector. If the index is a constant
3547 // or if the invec is a supported hardware type, we can use it. Otherwise,
3548 // lower to a store then an indexed load.
3549 SDOperand Vec = Op.getOperand(0);
3550 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3551
3552 SDNode *InVal = Vec.Val;
3553 unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
3554 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
3555
3556 // Figure out if there is a Packed type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00003557 // type. If so, convert to the vector type.
Chris Lattner6f423252006-03-31 17:55:51 +00003558 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3559 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
3560 // Turn this into a packed extract_vector_elt operation.
3561 Vec = PackVectorOp(Vec, TVT);
3562 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx);
3563 } else if (NumElems == 1) {
3564 // This must be an access of the only element. Return it.
3565 return PackVectorOp(Vec, EVT);
3566 } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) {
3567 SDOperand Lo, Hi;
3568 SplitVectorOp(Vec, Lo, Hi);
3569 if (CIdx->getValue() < NumElems/2) {
3570 Vec = Lo;
3571 } else {
3572 Vec = Hi;
3573 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3574 }
3575
3576 // It's now an extract from the appropriate high or low part. Recurse.
3577 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3578 return LowerVEXTRACT_VECTOR_ELT(Op);
3579 } else {
3580 // Variable index case for extract element.
3581 // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!!
3582 assert(0 && "unimp!");
3583 return SDOperand();
3584 }
3585}
3586
Chris Lattner42a5fca2006-04-02 05:06:04 +00003587/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3588/// memory traffic.
3589SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
3590 SDOperand Vector = Op.getOperand(0);
3591 SDOperand Idx = Op.getOperand(1);
3592
3593 // If the target doesn't support this, store the value to a temporary
3594 // stack slot, then LOAD the scalar element back out.
3595 SDOperand StackPtr = CreateStackTemporary(Vector.getValueType());
Evan Chengab51cf22006-10-13 21:14:26 +00003596 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vector, StackPtr, NULL, 0);
Chris Lattner42a5fca2006-04-02 05:06:04 +00003597
3598 // Add the offset to the index.
3599 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3600 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3601 DAG.getConstant(EltSize, Idx.getValueType()));
3602 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3603
Evan Chenge71fe34d2006-10-09 20:57:25 +00003604 return DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner42a5fca2006-04-02 05:06:04 +00003605}
3606
Chris Lattner6f423252006-03-31 17:55:51 +00003607
Nate Begeman7e7f4392006-02-01 07:19:44 +00003608/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3609/// with condition CC on the current target. This usually involves legalizing
3610/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3611/// there may be no choice but to create a new SetCC node to represent the
3612/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3613/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3614void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3615 SDOperand &RHS,
3616 SDOperand &CC) {
3617 SDOperand Tmp1, Tmp2, Result;
3618
3619 switch (getTypeAction(LHS.getValueType())) {
3620 case Legal:
3621 Tmp1 = LegalizeOp(LHS); // LHS
3622 Tmp2 = LegalizeOp(RHS); // RHS
3623 break;
3624 case Promote:
3625 Tmp1 = PromoteOp(LHS); // LHS
3626 Tmp2 = PromoteOp(RHS); // RHS
3627
3628 // If this is an FP compare, the operands have already been extended.
3629 if (MVT::isInteger(LHS.getValueType())) {
3630 MVT::ValueType VT = LHS.getValueType();
3631 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3632
3633 // Otherwise, we have to insert explicit sign or zero extends. Note
3634 // that we could insert sign extends for ALL conditions, but zero extend
3635 // is cheaper on many machines (an AND instead of two shifts), so prefer
3636 // it.
3637 switch (cast<CondCodeSDNode>(CC)->get()) {
3638 default: assert(0 && "Unknown integer comparison!");
3639 case ISD::SETEQ:
3640 case ISD::SETNE:
3641 case ISD::SETUGE:
3642 case ISD::SETUGT:
3643 case ISD::SETULE:
3644 case ISD::SETULT:
3645 // ALL of these operations will work if we either sign or zero extend
3646 // the operands (including the unsigned comparisons!). Zero extend is
3647 // usually a simpler/cheaper operation, so prefer it.
3648 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3649 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3650 break;
3651 case ISD::SETGE:
3652 case ISD::SETGT:
3653 case ISD::SETLT:
3654 case ISD::SETLE:
3655 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3656 DAG.getValueType(VT));
3657 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3658 DAG.getValueType(VT));
3659 break;
3660 }
3661 }
3662 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003663 case Expand: {
3664 MVT::ValueType VT = LHS.getValueType();
3665 if (VT == MVT::f32 || VT == MVT::f64) {
3666 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00003667 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003668 switch (cast<CondCodeSDNode>(CC)->get()) {
3669 case ISD::SETEQ:
3670 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003671 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003672 break;
3673 case ISD::SETNE:
3674 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003675 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003676 break;
3677 case ISD::SETGE:
3678 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003679 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003680 break;
3681 case ISD::SETLT:
3682 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003683 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003684 break;
3685 case ISD::SETLE:
3686 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003687 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003688 break;
3689 case ISD::SETGT:
3690 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003691 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003692 break;
3693 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00003694 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
3695 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003696 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00003697 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003698 break;
3699 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003700 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003701 switch (cast<CondCodeSDNode>(CC)->get()) {
3702 case ISD::SETONE:
3703 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00003704 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003705 // Fallthrough
3706 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003707 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003708 break;
3709 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003710 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003711 break;
3712 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003713 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003714 break;
3715 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00003716 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003717 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003718 case ISD::SETUEQ:
3719 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003720 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003721 default: assert(0 && "Unsupported FP setcc!");
3722 }
3723 }
3724
3725 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003726 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00003727 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00003728 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003729 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00003730 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00003731 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003732 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003733 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00003734 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00003735 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003736 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00003737 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003738 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3739 Tmp2 = SDOperand();
3740 }
3741 LHS = Tmp1;
3742 RHS = Tmp2;
3743 return;
3744 }
3745
Nate Begeman7e7f4392006-02-01 07:19:44 +00003746 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
3747 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003748 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman7e7f4392006-02-01 07:19:44 +00003749 switch (cast<CondCodeSDNode>(CC)->get()) {
3750 case ISD::SETEQ:
3751 case ISD::SETNE:
3752 if (RHSLo == RHSHi)
3753 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
3754 if (RHSCST->isAllOnesValue()) {
3755 // Comparison to -1.
3756 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
3757 Tmp2 = RHSLo;
3758 break;
3759 }
3760
3761 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
3762 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
3763 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
3764 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3765 break;
3766 default:
3767 // If this is a comparison of the sign bit, just look at the top part.
3768 // X > -1, x < 0
3769 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
3770 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
3771 CST->getValue() == 0) || // X < 0
3772 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
3773 CST->isAllOnesValue())) { // X > -1
3774 Tmp1 = LHSHi;
3775 Tmp2 = RHSHi;
3776 break;
3777 }
3778
3779 // FIXME: This generated code sucks.
3780 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00003781 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
3782 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00003783 default: assert(0 && "Unknown integer setcc!");
3784 case ISD::SETLT:
3785 case ISD::SETULT: LowCC = ISD::SETULT; break;
3786 case ISD::SETGT:
3787 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3788 case ISD::SETLE:
3789 case ISD::SETULE: LowCC = ISD::SETULE; break;
3790 case ISD::SETGE:
3791 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3792 }
3793
3794 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
3795 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
3796 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
3797
3798 // NOTE: on targets without efficient SELECT of bools, we can always use
3799 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00003800 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
3801 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
3802 false, DagCombineInfo);
3803 if (!Tmp1.Val)
3804 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
3805 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3806 CCCode, false, DagCombineInfo);
3807 if (!Tmp2.Val)
3808 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
3809
3810 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
3811 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
3812 if ((Tmp1C && Tmp1C->getValue() == 0) ||
3813 (Tmp2C && Tmp2C->getValue() == 0 &&
3814 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
3815 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
3816 (Tmp2C && Tmp2C->getValue() == 1 &&
3817 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
3818 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
3819 // low part is known false, returns high part.
3820 // For LE / GE, if high part is known false, ignore the low part.
3821 // For LT / GT, if high part is known true, ignore the low part.
3822 Tmp1 = Tmp2;
3823 Tmp2 = SDOperand();
3824 } else {
3825 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
3826 ISD::SETEQ, false, DagCombineInfo);
3827 if (!Result.Val)
3828 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
3829 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
3830 Result, Tmp1, Tmp2));
3831 Tmp1 = Result;
3832 Tmp2 = SDOperand();
3833 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00003834 }
3835 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00003836 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00003837 LHS = Tmp1;
3838 RHS = Tmp2;
3839}
3840
Chris Lattner36e663d2005-12-23 00:16:34 +00003841/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00003842/// The resultant code need not be legal. Note that SrcOp is the input operand
3843/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00003844SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
3845 SDOperand SrcOp) {
3846 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003847 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00003848
3849 // Emit a store to the stack slot.
Evan Chengab51cf22006-10-13 21:14:26 +00003850 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00003851 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003852 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00003853}
3854
Chris Lattner6be79822006-04-04 17:23:26 +00003855SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
3856 // Create a vector sized/aligned stack slot, store the value to element #0,
3857 // then load the whole vector back out.
3858 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00003859 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00003860 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00003861 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00003862}
3863
3864
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003865/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
3866/// support the operation, but do support the resultant packed vector type.
3867SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
3868
3869 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00003870 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00003871 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003872 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00003873 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003874 std::map<SDOperand, std::vector<unsigned> > Values;
3875 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003876 bool isConstant = true;
3877 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
3878 SplatValue.getOpcode() != ISD::UNDEF)
3879 isConstant = false;
3880
Evan Cheng1d2e9952006-03-24 01:17:21 +00003881 for (unsigned i = 1; i < NumElems; ++i) {
3882 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00003883 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00003884 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003885 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003886 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00003887 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003888
3889 // If this isn't a constant element or an undef, we can't use a constant
3890 // pool load.
3891 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
3892 V.getOpcode() != ISD::UNDEF)
3893 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003894 }
3895
3896 if (isOnlyLowElement) {
3897 // If the low element is an undef too, then this whole things is an undef.
3898 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
3899 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
3900 // Otherwise, turn this into a scalar_to_vector node.
3901 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3902 Node->getOperand(0));
3903 }
3904
Chris Lattner77e271c2006-03-24 07:29:17 +00003905 // If all elements are constants, create a load from the constant pool.
3906 if (isConstant) {
3907 MVT::ValueType VT = Node->getValueType(0);
3908 const Type *OpNTy =
3909 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
3910 std::vector<Constant*> CV;
3911 for (unsigned i = 0, e = NumElems; i != e; ++i) {
3912 if (ConstantFPSDNode *V =
3913 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
3914 CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
3915 } else if (ConstantSDNode *V =
3916 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00003917 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00003918 } else {
3919 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
3920 CV.push_back(UndefValue::get(OpNTy));
3921 }
3922 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00003923 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00003924 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00003925 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00003926 }
3927
Chris Lattner21e68c82006-03-20 01:52:29 +00003928 if (SplatValue.Val) { // Splat of one value?
3929 // Build the shuffle constant vector: <0, 0, 0, 0>
3930 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00003931 MVT::getIntVectorWithNumElements(NumElems);
Chris Lattner21e68c82006-03-20 01:52:29 +00003932 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00003933 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003934 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3935 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00003936
3937 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00003938 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00003939 // Get the splatted value into the low element of a vector register.
3940 SDOperand LowValVec =
3941 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
3942
3943 // Return shuffle(LowValVec, undef, <0,0,0,0>)
3944 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
3945 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
3946 SplatMask);
3947 }
3948 }
3949
Evan Cheng1d2e9952006-03-24 01:17:21 +00003950 // If there are only two unique elements, we may be able to turn this into a
3951 // vector shuffle.
3952 if (Values.size() == 2) {
3953 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
3954 MVT::ValueType MaskVT =
3955 MVT::getIntVectorWithNumElements(NumElems);
3956 std::vector<SDOperand> MaskVec(NumElems);
3957 unsigned i = 0;
3958 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3959 E = Values.end(); I != E; ++I) {
3960 for (std::vector<unsigned>::iterator II = I->second.begin(),
3961 EE = I->second.end(); II != EE; ++II)
3962 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT));
3963 i += NumElems;
3964 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003965 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3966 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00003967
3968 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00003969 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
3970 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003971 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00003972 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
3973 E = Values.end(); I != E; ++I) {
3974 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
3975 I->first);
3976 Ops.push_back(Op);
3977 }
3978 Ops.push_back(ShuffleMask);
3979
3980 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003981 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
3982 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00003983 }
3984 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003985
3986 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
3987 // aligned object on the stack, store each element into it, then load
3988 // the result as a vector.
3989 MVT::ValueType VT = Node->getValueType(0);
3990 // Create the stack frame object.
3991 SDOperand FIPtr = CreateStackTemporary(VT);
3992
3993 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003994 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003995 unsigned TypeByteSize =
3996 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00003997 // Store (in the right endianness) the elements to memory.
3998 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3999 // Ignore undef elements.
4000 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4001
Chris Lattner8fa445a2006-03-22 01:46:54 +00004002 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004003
4004 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4005 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4006
Evan Chengdf9ac472006-10-05 23:01:46 +00004007 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00004008 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004009 }
4010
4011 SDOperand StoreChain;
4012 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004013 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4014 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004015 else
4016 StoreChain = DAG.getEntryNode();
4017
4018 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004019 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004020}
4021
4022/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4023/// specified value type.
4024SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4025 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4026 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner50ee0e42007-01-20 22:35:55 +00004027 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattner945e4372007-02-14 05:52:17 +00004028 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004029 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004030 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4031}
4032
Chris Lattner4157c412005-04-02 04:00:59 +00004033void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4034 SDOperand Op, SDOperand Amt,
4035 SDOperand &Lo, SDOperand &Hi) {
4036 // Expand the subcomponents.
4037 SDOperand LHSL, LHSH;
4038 ExpandOp(Op, LHSL, LHSH);
4039
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004040 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00004041 MVT::ValueType VT = LHSL.getValueType();
4042 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00004043 Hi = Lo.getValue(1);
4044}
4045
4046
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004047/// ExpandShift - Try to find a clever way to expand this shift operation out to
4048/// smaller elements. If we can't find a way that is more efficient than a
4049/// libcall on this target, return false. Otherwise, return true with the
4050/// low-parts expanded into Lo and Hi.
4051bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4052 SDOperand &Lo, SDOperand &Hi) {
4053 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4054 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00004055
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004056 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00004057 SDOperand ShAmt = LegalizeOp(Amt);
4058 MVT::ValueType ShTy = ShAmt.getValueType();
4059 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4060 unsigned NVTBits = MVT::getSizeInBits(NVT);
4061
4062 // Handle the case when Amt is an immediate. Other cases are currently broken
4063 // and are disabled.
4064 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4065 unsigned Cst = CN->getValue();
4066 // Expand the incoming operand to be shifted, so that we have its parts
4067 SDOperand InL, InH;
4068 ExpandOp(Op, InL, InH);
4069 switch(Opc) {
4070 case ISD::SHL:
4071 if (Cst > VTBits) {
4072 Lo = DAG.getConstant(0, NVT);
4073 Hi = DAG.getConstant(0, NVT);
4074 } else if (Cst > NVTBits) {
4075 Lo = DAG.getConstant(0, NVT);
4076 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004077 } else if (Cst == NVTBits) {
4078 Lo = DAG.getConstant(0, NVT);
4079 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00004080 } else {
4081 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4082 Hi = DAG.getNode(ISD::OR, NVT,
4083 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4084 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4085 }
4086 return true;
4087 case ISD::SRL:
4088 if (Cst > VTBits) {
4089 Lo = DAG.getConstant(0, NVT);
4090 Hi = DAG.getConstant(0, NVT);
4091 } else if (Cst > NVTBits) {
4092 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4093 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00004094 } else if (Cst == NVTBits) {
4095 Lo = InH;
4096 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00004097 } else {
4098 Lo = DAG.getNode(ISD::OR, NVT,
4099 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4100 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4101 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4102 }
4103 return true;
4104 case ISD::SRA:
4105 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004106 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004107 DAG.getConstant(NVTBits-1, ShTy));
4108 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004109 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004110 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004111 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004112 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004113 } else if (Cst == NVTBits) {
4114 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004115 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004116 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004117 } else {
4118 Lo = DAG.getNode(ISD::OR, NVT,
4119 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4120 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4121 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4122 }
4123 return true;
4124 }
4125 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004126
4127 // Okay, the shift amount isn't constant. However, if we can tell that it is
4128 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4129 uint64_t Mask = NVTBits, KnownZero, KnownOne;
4130 TLI.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
4131
4132 // If we know that the high bit of the shift amount is one, then we can do
4133 // this as a couple of simple shifts.
4134 if (KnownOne & Mask) {
4135 // Mask out the high bit, which we know is set.
4136 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4137 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4138
4139 // Expand the incoming operand to be shifted, so that we have its parts
4140 SDOperand InL, InH;
4141 ExpandOp(Op, InL, InH);
4142 switch(Opc) {
4143 case ISD::SHL:
4144 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4145 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4146 return true;
4147 case ISD::SRL:
4148 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4149 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4150 return true;
4151 case ISD::SRA:
4152 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4153 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4154 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4155 return true;
4156 }
4157 }
4158
4159 // If we know that the high bit of the shift amount is zero, then we can do
4160 // this as a couple of simple shifts.
4161 if (KnownZero & Mask) {
4162 // Compute 32-amt.
4163 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4164 DAG.getConstant(NVTBits, Amt.getValueType()),
4165 Amt);
4166
4167 // Expand the incoming operand to be shifted, so that we have its parts
4168 SDOperand InL, InH;
4169 ExpandOp(Op, InL, InH);
4170 switch(Opc) {
4171 case ISD::SHL:
4172 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4173 Hi = DAG.getNode(ISD::OR, NVT,
4174 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4175 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4176 return true;
4177 case ISD::SRL:
4178 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4179 Lo = DAG.getNode(ISD::OR, NVT,
4180 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4181 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4182 return true;
4183 case ISD::SRA:
4184 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4185 Lo = DAG.getNode(ISD::OR, NVT,
4186 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4187 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4188 return true;
4189 }
4190 }
4191
Nate Begemanb0674922005-04-06 21:13:14 +00004192 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004193}
Chris Lattneraac464e2005-01-21 06:05:23 +00004194
Chris Lattner4add7e32005-01-23 04:42:50 +00004195
Chris Lattneraac464e2005-01-21 06:05:23 +00004196// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4197// does not fit into a register, return the lo part and set the hi part to the
4198// by-reg argument. If it does fit into a single register, return the result
4199// and leave the Hi part unset.
4200SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004201 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004202 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4203 // The input chain to this libcall is the entry node of the function.
4204 // Legalizing the call will automatically add the previous call to the
4205 // dependence.
4206 SDOperand InChain = DAG.getEntryNode();
4207
Chris Lattneraac464e2005-01-21 06:05:23 +00004208 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004209 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004210 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4211 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4212 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004213 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00004214 Entry.isSigned = isSigned; Entry.isInReg = false; Entry.isSRet = false;
Reid Spencere63b6512006-12-31 05:55:36 +00004215 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004216 }
4217 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004218
Chris Lattner06bbeb62005-05-11 19:02:11 +00004219 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004220 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004221 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004222 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004223 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004224
Chris Lattner462505f2006-02-13 09:18:02 +00004225 // Legalize the call sequence, starting with the chain. This will advance
4226 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4227 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4228 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004229 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004230 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004231 default: assert(0 && "Unknown thing");
4232 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004233 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004234 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004235 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004236 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004237 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004238 }
Chris Lattner63022662005-09-02 20:26:58 +00004239 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004240}
4241
Chris Lattner4add7e32005-01-23 04:42:50 +00004242
Chris Lattneraac464e2005-01-21 06:05:23 +00004243/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
4244/// destination type is legal.
4245SDOperand SelectionDAGLegalize::
4246ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004247 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00004248 assert(getTypeAction(Source.getValueType()) == Expand &&
4249 "This is not an expansion!");
4250 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4251
Chris Lattner06bbeb62005-05-11 19:02:11 +00004252 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004253 assert(Source.getValueType() == MVT::i64 &&
4254 "This only works for 64-bit -> FP");
4255 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4256 // incoming integer is set. To handle this, we dynamically test to see if
4257 // it is set, and, if so, add a fudge factor.
4258 SDOperand Lo, Hi;
4259 ExpandOp(Source, Lo, Hi);
4260
Chris Lattner2a4f7312005-05-13 04:45:13 +00004261 // If this is unsigned, and not supported, first perform the conversion to
4262 // signed, then adjust the result if the sign bit is set.
4263 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4264 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4265
Chris Lattnerd47675e2005-08-09 20:20:18 +00004266 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4267 DAG.getConstant(0, Hi.getValueType()),
4268 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004269 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4270 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4271 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004272 uint64_t FF = 0x5f800000ULL;
4273 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004274 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004275
Chris Lattnerc30405e2005-08-26 17:15:30 +00004276 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004277 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4278 SDOperand FudgeInReg;
4279 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004280 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004281 else {
4282 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004283 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004284 CPIdx, NULL, 0, MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004285 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00004286 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00004287 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00004288
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004289 // Check to see if the target has a custom way to lower this. If so, use it.
4290 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4291 default: assert(0 && "This action not implemented for this operation!");
4292 case TargetLowering::Legal:
4293 case TargetLowering::Expand:
4294 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004295 case TargetLowering::Custom: {
4296 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4297 Source), DAG);
4298 if (NV.Val)
4299 return LegalizeOp(NV);
4300 break; // The target decided this was legal after all
4301 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004302 }
4303
Chris Lattner153587e2005-05-12 07:00:44 +00004304 // Expand the source, then glue it back together for the call. We must expand
4305 // the source in case it is shared (this pass of legalize must traverse it).
4306 SDOperand SrcLo, SrcHi;
4307 ExpandOp(Source, SrcLo, SrcHi);
4308 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4309
Evan Cheng31cbddf2007-01-12 02:11:51 +00004310 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004311 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004312 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004313 else {
4314 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00004315 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004316 }
Chris Lattner462505f2006-02-13 09:18:02 +00004317
4318 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4319 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004320 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4321 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00004322}
Misha Brukman835702a2005-04-21 22:36:52 +00004323
Chris Lattner689bdcc2006-01-28 08:25:58 +00004324/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4325/// INT_TO_FP operation of the specified operand when the target requests that
4326/// we expand it. At this point, we know that the result and operand types are
4327/// legal for the target.
4328SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4329 SDOperand Op0,
4330 MVT::ValueType DestVT) {
4331 if (Op0.getValueType() == MVT::i32) {
4332 // simple 32-bit [signed|unsigned] integer to float/double expansion
4333
Chris Lattner50ee0e42007-01-20 22:35:55 +00004334 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner689bdcc2006-01-28 08:25:58 +00004335 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner50ee0e42007-01-20 22:35:55 +00004336 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4337 unsigned StackAlign =
Chris Lattner945e4372007-02-14 05:52:17 +00004338 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004339 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004340 // get address of 8 byte buffer
4341 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4342 // word offset constant for Hi/Lo address computation
4343 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4344 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00004345 SDOperand Hi = StackSlot;
4346 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4347 if (TLI.isLittleEndian())
4348 std::swap(Hi, Lo);
4349
Chris Lattner689bdcc2006-01-28 08:25:58 +00004350 // if signed map to unsigned space
4351 SDOperand Op0Mapped;
4352 if (isSigned) {
4353 // constant used to invert sign bit (signed to unsigned mapping)
4354 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4355 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4356 } else {
4357 Op0Mapped = Op0;
4358 }
4359 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00004360 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00004361 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004362 // initial hi portion of constructed double
4363 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4364 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00004365 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004366 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00004367 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004368 // FP constant to bias correct the final result
4369 SDOperand Bias = DAG.getConstantFP(isSigned ?
4370 BitsToDouble(0x4330000080000000ULL)
4371 : BitsToDouble(0x4330000000000000ULL),
4372 MVT::f64);
4373 // subtract the bias
4374 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4375 // final result
4376 SDOperand Result;
4377 // handle final rounding
4378 if (DestVT == MVT::f64) {
4379 // do nothing
4380 Result = Sub;
4381 } else {
4382 // if f32 then cast to f32
4383 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
4384 }
4385 return Result;
4386 }
4387 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4388 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4389
4390 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4391 DAG.getConstant(0, Op0.getValueType()),
4392 ISD::SETLT);
4393 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4394 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4395 SignSet, Four, Zero);
4396
4397 // If the sign bit of the integer is set, the large number will be treated
4398 // as a negative number. To counteract this, the dynamic code adds an
4399 // offset depending on the data type.
4400 uint64_t FF;
4401 switch (Op0.getValueType()) {
4402 default: assert(0 && "Unsupported integer type!");
4403 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4404 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4405 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4406 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4407 }
4408 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004409 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004410
4411 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4412 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4413 SDOperand FudgeInReg;
4414 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004415 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004416 else {
4417 assert(DestVT == MVT::f64 && "Unexpected conversion");
4418 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4419 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00004420 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00004421 }
4422
4423 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4424}
4425
4426/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4427/// *INT_TO_FP operation of the specified operand when the target requests that
4428/// we promote it. At this point, we know that the result and operand types are
4429/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4430/// operation that takes a larger input.
4431SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4432 MVT::ValueType DestVT,
4433 bool isSigned) {
4434 // First step, figure out the appropriate *INT_TO_FP operation to use.
4435 MVT::ValueType NewInTy = LegalOp.getValueType();
4436
4437 unsigned OpToUse = 0;
4438
4439 // Scan for the appropriate larger type to use.
4440 while (1) {
4441 NewInTy = (MVT::ValueType)(NewInTy+1);
4442 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4443
4444 // If the target supports SINT_TO_FP of this type, use it.
4445 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4446 default: break;
4447 case TargetLowering::Legal:
4448 if (!TLI.isTypeLegal(NewInTy))
4449 break; // Can't use this datatype.
4450 // FALL THROUGH.
4451 case TargetLowering::Custom:
4452 OpToUse = ISD::SINT_TO_FP;
4453 break;
4454 }
4455 if (OpToUse) break;
4456 if (isSigned) continue;
4457
4458 // If the target supports UINT_TO_FP of this type, use it.
4459 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4460 default: break;
4461 case TargetLowering::Legal:
4462 if (!TLI.isTypeLegal(NewInTy))
4463 break; // Can't use this datatype.
4464 // FALL THROUGH.
4465 case TargetLowering::Custom:
4466 OpToUse = ISD::UINT_TO_FP;
4467 break;
4468 }
4469 if (OpToUse) break;
4470
4471 // Otherwise, try a larger type.
4472 }
4473
4474 // Okay, we found the operation and type to use. Zero extend our input to the
4475 // desired type then run the operation on it.
4476 return DAG.getNode(OpToUse, DestVT,
4477 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4478 NewInTy, LegalOp));
4479}
4480
4481/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4482/// FP_TO_*INT operation of the specified operand when the target requests that
4483/// we promote it. At this point, we know that the result and operand types are
4484/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4485/// operation that returns a larger result.
4486SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4487 MVT::ValueType DestVT,
4488 bool isSigned) {
4489 // First step, figure out the appropriate FP_TO*INT operation to use.
4490 MVT::ValueType NewOutTy = DestVT;
4491
4492 unsigned OpToUse = 0;
4493
4494 // Scan for the appropriate larger type to use.
4495 while (1) {
4496 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4497 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4498
4499 // If the target supports FP_TO_SINT returning this type, use it.
4500 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4501 default: break;
4502 case TargetLowering::Legal:
4503 if (!TLI.isTypeLegal(NewOutTy))
4504 break; // Can't use this datatype.
4505 // FALL THROUGH.
4506 case TargetLowering::Custom:
4507 OpToUse = ISD::FP_TO_SINT;
4508 break;
4509 }
4510 if (OpToUse) break;
4511
4512 // If the target supports FP_TO_UINT of this type, use it.
4513 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4514 default: break;
4515 case TargetLowering::Legal:
4516 if (!TLI.isTypeLegal(NewOutTy))
4517 break; // Can't use this datatype.
4518 // FALL THROUGH.
4519 case TargetLowering::Custom:
4520 OpToUse = ISD::FP_TO_UINT;
4521 break;
4522 }
4523 if (OpToUse) break;
4524
4525 // Otherwise, try a larger type.
4526 }
4527
4528 // Okay, we found the operation and type to use. Truncate the result of the
4529 // extended FP_TO_*INT operation to the desired size.
4530 return DAG.getNode(ISD::TRUNCATE, DestVT,
4531 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4532}
4533
4534/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4535///
4536SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4537 MVT::ValueType VT = Op.getValueType();
4538 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4539 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4540 switch (VT) {
4541 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4542 case MVT::i16:
4543 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4544 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4545 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4546 case MVT::i32:
4547 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4548 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4549 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4550 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4551 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4552 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4553 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4554 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4555 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4556 case MVT::i64:
4557 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4558 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4559 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4560 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4561 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4562 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4563 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4564 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4565 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4566 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4567 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4568 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4569 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4570 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4571 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4572 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4573 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4574 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4575 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4576 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4577 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4578 }
4579}
4580
4581/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4582///
4583SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4584 switch (Opc) {
4585 default: assert(0 && "Cannot expand this yet!");
4586 case ISD::CTPOP: {
4587 static const uint64_t mask[6] = {
4588 0x5555555555555555ULL, 0x3333333333333333ULL,
4589 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4590 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4591 };
4592 MVT::ValueType VT = Op.getValueType();
4593 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4594 unsigned len = getSizeInBits(VT);
4595 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4596 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4597 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4598 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4599 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4600 DAG.getNode(ISD::AND, VT,
4601 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4602 }
4603 return Op;
4604 }
4605 case ISD::CTLZ: {
4606 // for now, we do this:
4607 // x = x | (x >> 1);
4608 // x = x | (x >> 2);
4609 // ...
4610 // x = x | (x >>16);
4611 // x = x | (x >>32); // for 64-bit input
4612 // return popcount(~x);
4613 //
4614 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4615 MVT::ValueType VT = Op.getValueType();
4616 MVT::ValueType ShVT = TLI.getShiftAmountTy();
4617 unsigned len = getSizeInBits(VT);
4618 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4619 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4620 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4621 }
4622 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4623 return DAG.getNode(ISD::CTPOP, VT, Op);
4624 }
4625 case ISD::CTTZ: {
4626 // for now, we use: { return popcount(~x & (x - 1)); }
4627 // unless the target has ctlz but not ctpop, in which case we use:
4628 // { return 32 - nlz(~x & (x-1)); }
4629 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4630 MVT::ValueType VT = Op.getValueType();
4631 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4632 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4633 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4634 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4635 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4636 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4637 TLI.isOperationLegal(ISD::CTLZ, VT))
4638 return DAG.getNode(ISD::SUB, VT,
4639 DAG.getConstant(getSizeInBits(VT), VT),
4640 DAG.getNode(ISD::CTLZ, VT, Tmp3));
4641 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
4642 }
4643 }
4644}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004645
Chris Lattnerdc750592005-01-07 07:47:09 +00004646/// ExpandOp - Expand the specified SDOperand into its two component pieces
4647/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
4648/// LegalizeNodes map is filled in for any results that are not expanded, the
4649/// ExpandedNodes map is filled in for any results that are expanded, and the
4650/// Lo/Hi values are returned.
4651void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
4652 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00004653 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00004654 SDNode *Node = Op.Val;
4655 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00004656 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
4657 VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00004658 "Cannot expand to FP value or to larger int value!");
4659
Chris Lattner1a570f12005-09-02 20:32:45 +00004660 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00004661 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00004662 = ExpandedNodes.find(Op);
4663 if (I != ExpandedNodes.end()) {
4664 Lo = I->second.first;
4665 Hi = I->second.second;
4666 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00004667 }
4668
Chris Lattnerdc750592005-01-07 07:47:09 +00004669 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00004670 case ISD::CopyFromReg:
4671 assert(0 && "CopyFromReg must be legal!");
4672 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004673#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +00004674 cerr << "NODE: "; Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004675#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00004676 assert(0 && "Do not know how to expand this operator!");
4677 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004678 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00004679 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00004680 Lo = DAG.getNode(ISD::UNDEF, NVT);
4681 Hi = DAG.getNode(ISD::UNDEF, NVT);
4682 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004683 case ISD::Constant: {
4684 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
4685 Lo = DAG.getConstant(Cst, NVT);
4686 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
4687 break;
4688 }
Evan Cheng47833a12006-12-12 21:32:44 +00004689 case ISD::ConstantFP: {
4690 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng3766fc602006-12-12 22:19:28 +00004691 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00004692 if (getTypeAction(Lo.getValueType()) == Expand)
4693 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00004694 break;
4695 }
Chris Lattner32e08b72005-03-28 22:03:13 +00004696 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004697 // Return the operands.
4698 Lo = Node->getOperand(0);
4699 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00004700 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004701
4702 case ISD::SIGN_EXTEND_INREG:
4703 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00004704 // sext_inreg the low part if needed.
4705 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
4706
4707 // The high part gets the sign extension from the lo-part. This handles
4708 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004709 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4710 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
4711 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00004712 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00004713
Nate Begeman2fba8a32006-01-14 03:14:10 +00004714 case ISD::BSWAP: {
4715 ExpandOp(Node->getOperand(0), Lo, Hi);
4716 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
4717 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
4718 Lo = TempLo;
4719 break;
4720 }
4721
Chris Lattner55e9cde2005-05-11 04:51:16 +00004722 case ISD::CTPOP:
4723 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00004724 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
4725 DAG.getNode(ISD::CTPOP, NVT, Lo),
4726 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00004727 Hi = DAG.getConstant(0, NVT);
4728 break;
4729
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004730 case ISD::CTLZ: {
4731 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004732 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004733 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4734 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004735 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
4736 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004737 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
4738 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
4739
4740 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
4741 Hi = DAG.getConstant(0, NVT);
4742 break;
4743 }
4744
4745 case ISD::CTTZ: {
4746 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00004747 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004748 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
4749 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00004750 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
4751 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00004752 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
4753 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
4754
4755 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
4756 Hi = DAG.getConstant(0, NVT);
4757 break;
4758 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00004759
Nate Begemane74795c2006-01-25 18:21:52 +00004760 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004761 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
4762 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00004763 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
4764 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
4765
4766 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004767 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00004768 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
4769 if (!TLI.isLittleEndian())
4770 std::swap(Lo, Hi);
4771 break;
4772 }
4773
Chris Lattnerdc750592005-01-07 07:47:09 +00004774 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00004775 LoadSDNode *LD = cast<LoadSDNode>(Node);
4776 SDOperand Ch = LD->getChain(); // Legalize the chain.
4777 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
4778 ISD::LoadExtType ExtType = LD->getExtensionType();
Chris Lattnerdc750592005-01-07 07:47:09 +00004779
Evan Chenge71fe34d2006-10-09 20:57:25 +00004780 if (ExtType == ISD::NON_EXTLOAD) {
Chris Lattner296a83c2007-02-01 04:55:59 +00004781 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),LD->getSrcValueOffset());
Evan Cheng47833a12006-12-12 21:32:44 +00004782 if (VT == MVT::f32 || VT == MVT::f64) {
4783 // f32->i32 or f64->i64 one to one expansion.
4784 // Remember that we legalized the chain.
4785 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00004786 // Recursively expand the new load.
4787 if (getTypeAction(NVT) == Expand)
4788 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00004789 break;
4790 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00004791
Evan Chenge71fe34d2006-10-09 20:57:25 +00004792 // Increment the pointer to the other half.
4793 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
4794 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4795 getIntPtrConstant(IncrementSize));
4796 // FIXME: This creates a bogus srcvalue!
Chris Lattner296a83c2007-02-01 04:55:59 +00004797 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),LD->getSrcValueOffset());
Misha Brukman835702a2005-04-21 22:36:52 +00004798
Evan Chenge71fe34d2006-10-09 20:57:25 +00004799 // Build a factor node to remember that this load is independent of the
4800 // other one.
4801 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
4802 Hi.getValue(1));
4803
4804 // Remember that we legalized the chain.
4805 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
4806 if (!TLI.isLittleEndian())
4807 std::swap(Lo, Hi);
4808 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00004809 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00004810
4811 if (VT == MVT::f64 && EVT == MVT::f32) {
4812 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
4813 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
4814 LD->getSrcValueOffset());
4815 // Remember that we legalized the chain.
4816 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
4817 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
4818 break;
4819 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00004820
4821 if (EVT == NVT)
4822 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
4823 LD->getSrcValueOffset());
4824 else
4825 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
4826 LD->getSrcValueOffset(), EVT);
4827
4828 // Remember that we legalized the chain.
4829 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
4830
4831 if (ExtType == ISD::SEXTLOAD) {
4832 // The high part is obtained by SRA'ing all but one of the bits of the
4833 // lo part.
4834 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
4835 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4836 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
4837 } else if (ExtType == ISD::ZEXTLOAD) {
4838 // The high part is just a zero.
4839 Hi = DAG.getConstant(0, NVT);
4840 } else /* if (ExtType == ISD::EXTLOAD) */ {
4841 // The high part is undefined.
4842 Hi = DAG.getNode(ISD::UNDEF, NVT);
4843 }
4844 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004845 break;
4846 }
Chris Lattnerdc750592005-01-07 07:47:09 +00004847 case ISD::AND:
4848 case ISD::OR:
4849 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
4850 SDOperand LL, LH, RL, RH;
4851 ExpandOp(Node->getOperand(0), LL, LH);
4852 ExpandOp(Node->getOperand(1), RL, RH);
4853 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
4854 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
4855 break;
4856 }
4857 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004858 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00004859 ExpandOp(Node->getOperand(1), LL, LH);
4860 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00004861 if (getTypeAction(NVT) == Expand)
4862 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004863 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00004864 if (VT != MVT::f32)
4865 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00004866 break;
4867 }
Nate Begemane5b86d72005-08-10 20:51:12 +00004868 case ISD::SELECT_CC: {
4869 SDOperand TL, TH, FL, FH;
4870 ExpandOp(Node->getOperand(2), TL, TH);
4871 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00004872 if (getTypeAction(NVT) == Expand)
4873 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00004874 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4875 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00004876 if (VT != MVT::f32)
4877 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
4878 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004879 break;
4880 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004881 case ISD::ANY_EXTEND:
4882 // The low part is any extension of the input (which degenerates to a copy).
4883 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
4884 // The high part is undefined.
4885 Hi = DAG.getNode(ISD::UNDEF, NVT);
4886 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004887 case ISD::SIGN_EXTEND: {
4888 // The low part is just a sign extension of the input (which degenerates to
4889 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004890 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004891
Chris Lattnerdc750592005-01-07 07:47:09 +00004892 // The high part is obtained by SRA'ing all but one of the bits of the lo
4893 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00004894 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004895 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
4896 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00004897 break;
4898 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004899 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00004900 // The low part is just a zero extension of the input (which degenerates to
4901 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004902 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00004903
Chris Lattnerdc750592005-01-07 07:47:09 +00004904 // The high part is just a zero.
4905 Hi = DAG.getConstant(0, NVT);
4906 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004907
Chris Lattner59b27fa372007-02-13 23:55:16 +00004908 case ISD::TRUNCATE: {
4909 // The input value must be larger than this value. Expand *it*.
4910 SDOperand NewLo;
4911 ExpandOp(Node->getOperand(0), NewLo, Hi);
4912
4913 // The low part is now either the right size, or it is closer. If not the
4914 // right size, make an illegal truncate so we recursively expand it.
4915 if (NewLo.getValueType() != Node->getValueType(0))
4916 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
4917 ExpandOp(NewLo, Lo, Hi);
4918 break;
4919 }
4920
Chris Lattner36e663d2005-12-23 00:16:34 +00004921 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004922 SDOperand Tmp;
4923 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
4924 // If the target wants to, allow it to lower this itself.
4925 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4926 case Expand: assert(0 && "cannot expand FP!");
4927 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
4928 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
4929 }
4930 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
4931 }
4932
Evan Cheng4eee7242006-12-09 02:42:38 +00004933 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00004934 if (VT == MVT::f32 || VT == MVT::f64) {
4935 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00004936 if (getTypeAction(NVT) == Expand)
4937 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00004938 break;
4939 }
4940
4941 // If source operand will be expanded to the same type as VT, i.e.
4942 // i64 <- f64, i32 <- f32, expand the source operand instead.
4943 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
4944 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
4945 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00004946 break;
4947 }
4948
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004949 // Turn this into a load/store pair by default.
4950 if (Tmp.Val == 0)
Evan Cheng3432ab92006-12-11 19:27:14 +00004951 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00004952
Chris Lattner36e663d2005-12-23 00:16:34 +00004953 ExpandOp(Tmp, Lo, Hi);
4954 break;
4955 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004956
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004957 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00004958 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
4959 TargetLowering::Custom &&
4960 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004961 Lo = TLI.LowerOperation(Op, DAG);
4962 assert(Lo.Val && "Node must be custom expanded!");
4963 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00004964 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004965 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00004966 break;
4967
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004968 // These operators cannot be expanded directly, emit them as calls to
4969 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00004970 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00004971 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00004972 SDOperand Op;
4973 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4974 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00004975 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
4976 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00004977 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004978
Chris Lattner941d84a2005-07-30 01:40:57 +00004979 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
4980
Chris Lattnerfe68d752005-07-29 00:33:32 +00004981 // Now that the custom expander is done, expand the result, which is still
4982 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004983 if (Op.Val) {
4984 ExpandOp(Op, Lo, Hi);
4985 break;
4986 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00004987 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004988
Evan Cheng31cbddf2007-01-12 02:11:51 +00004989 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004990 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004991 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004992 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00004993 LC = RTLIB::FPTOSINT_F64_I64;
4994 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
4995 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00004996 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004997 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004998
Evan Cheng31cbddf2007-01-12 02:11:51 +00004999 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005000 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005001 SDOperand Op;
5002 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5003 case Expand: assert(0 && "cannot expand FP!");
5004 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5005 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5006 }
5007
5008 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5009
5010 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005011 if (Op.Val) {
5012 ExpandOp(Op, Lo, Hi);
5013 break;
5014 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005015 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005016
Evan Cheng31cbddf2007-01-12 02:11:51 +00005017 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005018 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005019 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005020 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005021 LC = RTLIB::FPTOUINT_F64_I64;
5022 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5023 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005024 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005025 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005026
Evan Cheng870e4f82006-01-09 18:31:59 +00005027 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005028 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005029 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005030 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005031 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005032 Op = TLI.LowerOperation(Op, DAG);
5033 if (Op.Val) {
5034 // Now that the custom expander is done, expand the result, which is
5035 // still VT.
5036 ExpandOp(Op, Lo, Hi);
5037 break;
5038 }
5039 }
5040
Chris Lattner72b503b2006-09-13 03:50:39 +00005041 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5042 // this X << 1 as X+X.
5043 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5044 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5045 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5046 SDOperand LoOps[2], HiOps[3];
5047 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5048 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5049 LoOps[1] = LoOps[0];
5050 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5051
5052 HiOps[1] = HiOps[0];
5053 HiOps[2] = Lo.getValue(1);
5054 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5055 break;
5056 }
5057 }
5058
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005059 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005060 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005061 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005062
5063 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005064 TargetLowering::LegalizeAction Action =
5065 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5066 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5067 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005068 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005069 break;
5070 }
5071
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005072 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005073 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5074 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005075 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005076 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005077
Evan Cheng870e4f82006-01-09 18:31:59 +00005078 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005079 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005080 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005081 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005082 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005083 Op = TLI.LowerOperation(Op, DAG);
5084 if (Op.Val) {
5085 // Now that the custom expander is done, expand the result, which is
5086 // still VT.
5087 ExpandOp(Op, Lo, Hi);
5088 break;
5089 }
5090 }
5091
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005092 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005093 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005094 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005095
5096 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005097 TargetLowering::LegalizeAction Action =
5098 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5099 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5100 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005101 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005102 break;
5103 }
5104
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005105 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005106 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5107 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005108 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005109 }
5110
5111 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005112 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005113 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005114 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005115 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005116 Op = TLI.LowerOperation(Op, DAG);
5117 if (Op.Val) {
5118 // Now that the custom expander is done, expand the result, which is
5119 // still VT.
5120 ExpandOp(Op, Lo, Hi);
5121 break;
5122 }
5123 }
5124
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005125 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005126 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005127 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005128
5129 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005130 TargetLowering::LegalizeAction Action =
5131 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5132 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5133 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005134 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005135 break;
5136 }
5137
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005138 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005139 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5140 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005141 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005142 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005143
Misha Brukman835702a2005-04-21 22:36:52 +00005144 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005145 case ISD::SUB: {
5146 // If the target wants to custom expand this, let them.
5147 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5148 TargetLowering::Custom) {
5149 Op = TLI.LowerOperation(Op, DAG);
5150 if (Op.Val) {
5151 ExpandOp(Op, Lo, Hi);
5152 break;
5153 }
5154 }
5155
5156 // Expand the subcomponents.
5157 SDOperand LHSL, LHSH, RHSL, RHSH;
5158 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5159 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005160 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5161 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005162 LoOps[0] = LHSL;
5163 LoOps[1] = RHSL;
5164 HiOps[0] = LHSH;
5165 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005166 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005167 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005168 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005169 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005170 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005171 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005172 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005173 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005174 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005175 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005176 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005177 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005178 // If the target wants to custom expand this, let them.
5179 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00005180 SDOperand New = TLI.LowerOperation(Op, DAG);
5181 if (New.Val) {
5182 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005183 break;
5184 }
5185 }
5186
Evan Chenge93762d2006-09-01 18:17:58 +00005187 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5188 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Chenge93762d2006-09-01 18:17:58 +00005189 if (HasMULHS || HasMULHU) {
Nate Begemanadd0c632005-04-11 03:01:51 +00005190 SDOperand LL, LH, RL, RH;
5191 ExpandOp(Node->getOperand(0), LL, LH);
5192 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00005193 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman8e20c762006-12-11 02:23:46 +00005194 // FIXME: Move this to the dag combiner.
Nate Begeman43144a22005-08-30 02:44:00 +00005195 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5196 // extended the sign bit of the low half through the upper half, and if so
5197 // emit a MULHS instead of the alternate sequence that is valid for any
5198 // i64 x i64 multiply.
Evan Chenge93762d2006-09-01 18:17:58 +00005199 if (HasMULHS &&
Nate Begeman43144a22005-08-30 02:44:00 +00005200 // is RH an extension of the sign bit of RL?
5201 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5202 RH.getOperand(1).getOpcode() == ISD::Constant &&
5203 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5204 // is LH an extension of the sign bit of LL?
5205 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5206 LH.getOperand(1).getOpcode() == ISD::Constant &&
5207 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattner1b633912006-09-16 00:21:44 +00005208 // Low part:
5209 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5210 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005211 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattner1b633912006-09-16 00:21:44 +00005212 break;
Evan Chenge93762d2006-09-01 18:17:58 +00005213 } else if (HasMULHU) {
Chris Lattner1b633912006-09-16 00:21:44 +00005214 // Low part:
5215 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5216
5217 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005218 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5219 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5220 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5221 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5222 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00005223 break;
Nate Begeman43144a22005-08-30 02:44:00 +00005224 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005225 }
Evan Chenge93762d2006-09-01 18:17:58 +00005226
Evan Cheng31cbddf2007-01-12 02:11:51 +00005227 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5228 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00005229 break;
5230 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005231 case ISD::SDIV:
5232 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5233 break;
5234 case ISD::UDIV:
5235 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5236 break;
5237 case ISD::SREM:
5238 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5239 break;
5240 case ISD::UREM:
5241 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5242 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00005243
Evan Cheng4eee7242006-12-09 02:42:38 +00005244 case ISD::FADD:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005245 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5246 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5247 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005248 break;
5249 case ISD::FSUB:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005250 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5251 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5252 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005253 break;
5254 case ISD::FMUL:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005255 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5256 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5257 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005258 break;
5259 case ISD::FDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005260 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5261 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5262 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005263 break;
5264 case ISD::FP_EXTEND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005265 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005266 break;
5267 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005268 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005269 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005270 case ISD::FSQRT:
5271 case ISD::FSIN:
5272 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005273 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00005274 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005275 case ISD::FSQRT:
5276 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5277 break;
5278 case ISD::FSIN:
5279 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5280 break;
5281 case ISD::FCOS:
5282 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5283 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005284 default: assert(0 && "Unreachable!");
5285 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005286 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00005287 break;
5288 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00005289 case ISD::FABS: {
5290 SDOperand Mask = (VT == MVT::f64)
5291 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5292 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5293 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5294 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5295 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5296 if (getTypeAction(NVT) == Expand)
5297 ExpandOp(Lo, Lo, Hi);
5298 break;
5299 }
5300 case ISD::FNEG: {
5301 SDOperand Mask = (VT == MVT::f64)
5302 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5303 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5304 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5305 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5306 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5307 if (getTypeAction(NVT) == Expand)
5308 ExpandOp(Lo, Lo, Hi);
5309 break;
5310 }
Evan Cheng003feb02007-01-04 21:56:39 +00005311 case ISD::FCOPYSIGN: {
5312 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5313 if (getTypeAction(NVT) == Expand)
5314 ExpandOp(Lo, Lo, Hi);
5315 break;
5316 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005317 case ISD::SINT_TO_FP:
5318 case ISD::UINT_TO_FP: {
5319 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5320 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00005321 RTLIB::Libcall LC;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005322 if (Node->getOperand(0).getValueType() == MVT::i64) {
5323 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005324 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005325 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005326 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005327 } else {
5328 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005329 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005330 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005331 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005332 }
5333
5334 // Promote the operand if needed.
5335 if (getTypeAction(SrcVT) == Promote) {
5336 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5337 Tmp = isSigned
5338 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5339 DAG.getValueType(SrcVT))
5340 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5341 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5342 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005343 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005344 break;
5345 }
Evan Chengf3a80c62006-12-13 02:38:13 +00005346 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005347
Chris Lattnerac12f682005-12-21 18:02:52 +00005348 // Make sure the resultant values have been legalized themselves, unless this
5349 // is a type that requires multi-step expansion.
5350 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5351 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00005352 if (Hi.Val)
5353 // Don't legalize the high part if it is expanded to a single node.
5354 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00005355 }
Evan Cheng870e4f82006-01-09 18:31:59 +00005356
5357 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005358 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00005359 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00005360}
5361
Chris Lattner32206f52006-03-18 01:44:44 +00005362/// SplitVectorOp - Given an operand of MVT::Vector type, break it down into
5363/// two smaller values of MVT::Vector type.
5364void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5365 SDOperand &Hi) {
5366 assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!");
5367 SDNode *Node = Op.Val;
5368 unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue();
5369 assert(NumElements > 1 && "Cannot split a single element vector!");
5370 unsigned NewNumElts = NumElements/2;
5371 SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32);
5372 SDOperand TypeNode = *(Node->op_end()-1);
5373
5374 // See if we already split it.
5375 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5376 = SplitNodes.find(Op);
5377 if (I != SplitNodes.end()) {
5378 Lo = I->second.first;
5379 Hi = I->second.second;
5380 return;
5381 }
5382
5383 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005384 default:
5385#ifndef NDEBUG
5386 Node->dump();
5387#endif
5388 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005389 case ISD::VBUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005390 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5391 Node->op_begin()+NewNumElts);
Chris Lattner32206f52006-03-18 01:44:44 +00005392 LoOps.push_back(NewNumEltsNode);
5393 LoOps.push_back(TypeNode);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005394 Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005395
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005396 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
5397 Node->op_end()-2);
Chris Lattner32206f52006-03-18 01:44:44 +00005398 HiOps.push_back(NewNumEltsNode);
5399 HiOps.push_back(TypeNode);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005400 Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005401 break;
5402 }
5403 case ISD::VADD:
5404 case ISD::VSUB:
5405 case ISD::VMUL:
5406 case ISD::VSDIV:
5407 case ISD::VUDIV:
5408 case ISD::VAND:
5409 case ISD::VOR:
5410 case ISD::VXOR: {
5411 SDOperand LL, LH, RL, RH;
5412 SplitVectorOp(Node->getOperand(0), LL, LH);
5413 SplitVectorOp(Node->getOperand(1), RL, RH);
5414
5415 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL,
5416 NewNumEltsNode, TypeNode);
5417 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH,
5418 NewNumEltsNode, TypeNode);
5419 break;
5420 }
5421 case ISD::VLOAD: {
5422 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5423 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
5424 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5425
5426 Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5427 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8;
5428 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5429 getIntPtrConstant(IncrementSize));
5430 // FIXME: This creates a bogus srcvalue!
5431 Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2));
5432
5433 // Build a factor node to remember that this load is independent of the
5434 // other one.
5435 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5436 Hi.getValue(1));
5437
5438 // Remember that we legalized the chain.
5439 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00005440 break;
5441 }
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005442 case ISD::VBIT_CONVERT: {
5443 // We know the result is a vector. The input may be either a vector or a
5444 // scalar value.
5445 if (Op.getOperand(0).getValueType() != MVT::Vector) {
5446 // Lower to a store/load. FIXME: this could be improved probably.
5447 SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType());
5448
Evan Chengdf9ac472006-10-05 23:01:46 +00005449 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00005450 Op.getOperand(0), Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005451 MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT();
5452 St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0));
5453 SplitVectorOp(St, Lo, Hi);
5454 } else {
5455 // If the input is a vector type, we have to either scalarize it, pack it
5456 // or convert it based on whether the input vector type is legal.
5457 SDNode *InVal = Node->getOperand(0).Val;
5458 unsigned NumElems =
5459 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5460 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5461
5462 // If the input is from a single element vector, scalarize the vector,
5463 // then treat like a scalar.
5464 if (NumElems == 1) {
5465 SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT);
5466 Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar,
5467 Op.getOperand(1), Op.getOperand(2));
5468 SplitVectorOp(Scalar, Lo, Hi);
5469 } else {
5470 // Split the input vector.
5471 SplitVectorOp(Op.getOperand(0), Lo, Hi);
5472
5473 // Convert each of the pieces now.
5474 Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo,
5475 NewNumEltsNode, TypeNode);
5476 Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi,
5477 NewNumEltsNode, TypeNode);
5478 }
5479 break;
5480 }
5481 }
Chris Lattner32206f52006-03-18 01:44:44 +00005482 }
5483
5484 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005485 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00005486 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
5487 assert(isNew && "Value already expanded?!?");
5488}
5489
5490
5491/// PackVectorOp - Given an operand of MVT::Vector type, convert it into the
5492/// equivalent operation that returns a scalar (e.g. F32) or packed value
5493/// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right
5494/// type for the result.
5495SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op,
5496 MVT::ValueType NewVT) {
5497 assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!");
5498 SDNode *Node = Op.Val;
5499
5500 // See if we already packed it.
5501 std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op);
5502 if (I != PackedNodes.end()) return I->second;
5503
5504 SDOperand Result;
5505 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00005506 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005507#ifndef NDEBUG
Bill Wendling22e978a2006-12-07 20:04:42 +00005508 Node->dump(); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005509#endif
Chris Lattner29b23012006-03-19 01:17:20 +00005510 assert(0 && "Unknown vector operation in PackVectorOp!");
Chris Lattner32206f52006-03-18 01:44:44 +00005511 case ISD::VADD:
5512 case ISD::VSUB:
5513 case ISD::VMUL:
5514 case ISD::VSDIV:
5515 case ISD::VUDIV:
5516 case ISD::VAND:
5517 case ISD::VOR:
5518 case ISD::VXOR:
5519 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT),
5520 NewVT,
5521 PackVectorOp(Node->getOperand(0), NewVT),
5522 PackVectorOp(Node->getOperand(1), NewVT));
5523 break;
5524 case ISD::VLOAD: {
Chris Lattner93640542006-03-19 00:07:49 +00005525 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
5526 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00005527
Evan Chenge71fe34d2006-10-09 20:57:25 +00005528 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
5529 Result = DAG.getLoad(NewVT, Ch, Ptr, SV->getValue(), SV->getOffset());
Chris Lattner32206f52006-03-18 01:44:44 +00005530
5531 // Remember that we legalized the chain.
5532 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5533 break;
5534 }
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005535 case ISD::VBUILD_VECTOR:
Chris Lattner5fe1f542006-03-31 02:06:56 +00005536 if (Node->getOperand(0).getValueType() == NewVT) {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005537 // Returning a scalar?
Chris Lattner32206f52006-03-18 01:44:44 +00005538 Result = Node->getOperand(0);
5539 } else {
Chris Lattnerf4e1a532006-03-19 00:52:58 +00005540 // Returning a BUILD_VECTOR?
Chris Lattnere1401e32006-04-08 05:34:25 +00005541
5542 // If all elements of the build_vector are undefs, return an undef.
5543 bool AllUndef = true;
5544 for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i)
5545 if (Node->getOperand(i).getOpcode() != ISD::UNDEF) {
5546 AllUndef = false;
5547 break;
5548 }
5549 if (AllUndef) {
5550 Result = DAG.getNode(ISD::UNDEF, NewVT);
5551 } else {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005552 Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Node->op_begin(),
5553 Node->getNumOperands()-2);
Chris Lattnere1401e32006-04-08 05:34:25 +00005554 }
Chris Lattner32206f52006-03-18 01:44:44 +00005555 }
5556 break;
Chris Lattner29b23012006-03-19 01:17:20 +00005557 case ISD::VINSERT_VECTOR_ELT:
5558 if (!MVT::isVector(NewVT)) {
5559 // Returning a scalar? Must be the inserted element.
5560 Result = Node->getOperand(1);
5561 } else {
5562 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT,
5563 PackVectorOp(Node->getOperand(0), NewVT),
5564 Node->getOperand(1), Node->getOperand(2));
5565 }
5566 break;
Chris Lattnerf6f94d32006-03-28 20:24:43 +00005567 case ISD::VVECTOR_SHUFFLE:
5568 if (!MVT::isVector(NewVT)) {
5569 // Returning a scalar? Figure out if it is the LHS or RHS and return it.
5570 SDOperand EltNum = Node->getOperand(2).getOperand(0);
5571 if (cast<ConstantSDNode>(EltNum)->getValue())
5572 Result = PackVectorOp(Node->getOperand(1), NewVT);
5573 else
5574 Result = PackVectorOp(Node->getOperand(0), NewVT);
5575 } else {
5576 // Otherwise, return a VECTOR_SHUFFLE node. First convert the index
5577 // vector from a VBUILD_VECTOR to a BUILD_VECTOR.
5578 std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(),
5579 Node->getOperand(2).Val->op_end()-2);
5580 MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size());
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005581 SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT,
5582 Node->getOperand(2).Val->op_begin(),
5583 Node->getOperand(2).Val->getNumOperands()-2);
Chris Lattnerf6f94d32006-03-28 20:24:43 +00005584
5585 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT,
5586 PackVectorOp(Node->getOperand(0), NewVT),
5587 PackVectorOp(Node->getOperand(1), NewVT), BV);
5588 }
5589 break;
Chris Lattner2f4119a2006-03-22 20:09:35 +00005590 case ISD::VBIT_CONVERT:
5591 if (Op.getOperand(0).getValueType() != MVT::Vector)
5592 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
5593 else {
5594 // If the input is a vector type, we have to either scalarize it, pack it
5595 // or convert it based on whether the input vector type is legal.
5596 SDNode *InVal = Node->getOperand(0).Val;
5597 unsigned NumElems =
5598 cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue();
5599 MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT();
5600
5601 // Figure out if there is a Packed type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00005602 // type. If so, convert to the vector type.
Chris Lattner2f4119a2006-03-22 20:09:35 +00005603 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
5604 if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
5605 // Turn this into a bit convert of the packed input.
5606 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5607 PackVectorOp(Node->getOperand(0), TVT));
5608 break;
5609 } else if (NumElems == 1) {
5610 // Turn this into a bit convert of the scalar input.
5611 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT,
5612 PackVectorOp(Node->getOperand(0), EVT));
5613 break;
5614 } else {
5615 // FIXME: UNIMP!
5616 assert(0 && "Cast from unsupported vector type not implemented yet!");
5617 }
5618 }
Evan Chengcb73b8d2006-04-10 18:54:36 +00005619 break;
Chris Lattner02274a52006-04-08 22:22:57 +00005620 case ISD::VSELECT:
5621 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
5622 PackVectorOp(Op.getOperand(1), NewVT),
5623 PackVectorOp(Op.getOperand(2), NewVT));
5624 break;
Chris Lattner32206f52006-03-18 01:44:44 +00005625 }
5626
5627 if (TLI.isTypeLegal(NewVT))
5628 Result = LegalizeOp(Result);
5629 bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second;
5630 assert(isNew && "Value already packed?");
5631 return Result;
5632}
5633
Chris Lattnerdc750592005-01-07 07:47:09 +00005634
5635// SelectionDAG::Legalize - This is the entry point for the file.
5636//
Chris Lattner4add7e32005-01-23 04:42:50 +00005637void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00005638 if (ViewLegalizeDAGs) viewGraph();
5639
Chris Lattnerdc750592005-01-07 07:47:09 +00005640 /// run - This is the main entry point to this class.
5641 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005642 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00005643}
5644