blob: d709169d351f74ee6a2f07848a7bdf2def43378f [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey70323a82006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Cheng631ccc62007-08-16 23:50:06 +000018#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000019#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000020#include "llvm/Target/TargetData.h"
Evan Cheng84a28d42006-10-30 08:00:44 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000022#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000023#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000024#include "llvm/Constants.h"
Reid Spencera94d3942007-01-19 21:13:56 +000025#include "llvm/DerivedTypes.h"
Chris Lattneref598052006-04-02 03:07:27 +000026#include "llvm/Support/MathExtras.h"
27#include "llvm/Support/CommandLine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattnere83030b2007-02-03 01:12:36 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattner97af9d52006-08-08 01:09:31 +000030#include "llvm/ADT/SmallVector.h"
Chris Lattnerebeb48d2007-02-04 00:27:56 +000031#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng1d2e9952006-03-24 01:17:21 +000032#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000033using namespace llvm;
34
Chris Lattneref598052006-04-02 03:07:27 +000035#ifndef NDEBUG
36static cl::opt<bool>
37ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
38 cl::desc("Pop up a window to show dags before legalize"));
39#else
40static const bool ViewLegalizeDAGs = 0;
41#endif
42
Chris Lattnerdc750592005-01-07 07:47:09 +000043//===----------------------------------------------------------------------===//
44/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
45/// hacks on it until the target machine can handle it. This involves
46/// eliminating value sizes the machine cannot handle (promoting small sizes to
47/// large sizes or splitting up large values into small values) as well as
48/// eliminating operations the machine cannot handle.
49///
50/// This code also does a small amount of optimization and recognition of idioms
51/// as part of its processing. For example, if a target does not support a
52/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
53/// will attempt merge setcc and brc instructions into brcc's.
54///
55namespace {
Chris Lattner54a34cd2006-06-28 21:58:30 +000056class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattnerdc750592005-01-07 07:47:09 +000057 TargetLowering &TLI;
58 SelectionDAG &DAG;
59
Chris Lattner462505f2006-02-13 09:18:02 +000060 // Libcall insertion helpers.
61
62 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
63 /// legalized. We use this to ensure that calls are properly serialized
64 /// against each other, including inserted libcalls.
65 SDOperand LastCALLSEQ_END;
66
67 /// IsLegalizingCall - This member is used *only* for purposes of providing
68 /// helpful assertions that a libcall isn't created while another call is
69 /// being legalized (which could lead to non-serialized call sequences).
70 bool IsLegalizingCall;
71
Chris Lattnerdc750592005-01-07 07:47:09 +000072 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000073 Legal, // The target natively supports this operation.
74 Promote, // This operation should be executed in a larger type.
Chris Lattneraa2372562006-05-24 17:04:05 +000075 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000076 };
Chris Lattner462505f2006-02-13 09:18:02 +000077
Chris Lattnerdc750592005-01-07 07:47:09 +000078 /// ValueTypeActions - This is a bitvector that contains two bits for each
79 /// value type, where the two bits correspond to the LegalizeAction enum.
80 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000081 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000082
Chris Lattnerdc750592005-01-07 07:47:09 +000083 /// LegalizedNodes - For nodes that are of legal width, and that have more
84 /// than one use, this map indicates what regularized operand to use. This
85 /// allows us to avoid legalizing the same thing more than once.
Chris Lattnered39c862007-02-04 00:50:02 +000086 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000087
Chris Lattner1f2c9d82005-01-15 05:21:40 +000088 /// PromotedNodes - For nodes that are below legal width, and that have more
89 /// than one use, this map indicates what promoted value to use. This allows
90 /// us to avoid promoting the same thing more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +000091 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner1f2c9d82005-01-15 05:21:40 +000092
Chris Lattner32206f52006-03-18 01:44:44 +000093 /// ExpandedNodes - For nodes that need to be expanded this map indicates
94 /// which which operands are the expanded version of the input. This allows
95 /// us to avoid expanding the same node more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +000096 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000097
Chris Lattner32206f52006-03-18 01:44:44 +000098 /// SplitNodes - For vector nodes that need to be split, this map indicates
99 /// which which operands are the split version of the input. This allows us
100 /// to avoid splitting the same node more than once.
101 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
102
Dan Gohmana8665142007-06-25 16:23:39 +0000103 /// ScalarizedNodes - For nodes that need to be converted from vector types to
104 /// scalar types, this contains the mapping of ones we have already
Chris Lattner32206f52006-03-18 01:44:44 +0000105 /// processed to the result.
Dan Gohmana8665142007-06-25 16:23:39 +0000106 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattner32206f52006-03-18 01:44:44 +0000107
Chris Lattnerea4ca942005-01-07 22:28:47 +0000108 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +0000109 LegalizedNodes.insert(std::make_pair(From, To));
110 // If someone requests legalization of the new node, return itself.
111 if (From != To)
112 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000113 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000114 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner4b0ddb22007-02-04 01:17:38 +0000115 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000116 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +0000117 // If someone requests legalization of the new node, return itself.
118 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000119 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000120
Chris Lattnerdc750592005-01-07 07:47:09 +0000121public:
122
Chris Lattner4add7e32005-01-23 04:42:50 +0000123 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000124
Chris Lattnerdc750592005-01-07 07:47:09 +0000125 /// getTypeAction - Return how we should legalize values of this type, either
126 /// it is already legal or we need to expand it into multiple registers of
127 /// smaller integer type, or we need to promote it to a larger type.
128 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +0000129 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +0000130 }
131
132 /// isTypeLegal - Return true if this type is legal on this target.
133 ///
134 bool isTypeLegal(MVT::ValueType VT) const {
135 return getTypeAction(VT) == Legal;
136 }
137
Chris Lattnerdc750592005-01-07 07:47:09 +0000138 void LegalizeDAG();
139
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000140private:
Dan Gohmana8665142007-06-25 16:23:39 +0000141 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000142 /// appropriate for its type.
143 void HandleOp(SDOperand Op);
144
145 /// LegalizeOp - We know that the specified value has a legal type.
146 /// Recursively ensure that the operands have legal types, then return the
147 /// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000148 SDOperand LegalizeOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000149
150 /// PromoteOp - Given an operation that produces a value in an invalid type,
151 /// promote it to compute the value into a larger type. The produced value
152 /// will have the correct bits for the low portion of the register, but no
153 /// guarantee is made about the top bits: it may be zero, sign-extended, or
154 /// garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000155 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000156
Chris Lattner32206f52006-03-18 01:44:44 +0000157 /// ExpandOp - Expand the specified SDOperand into its two component pieces
158 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
159 /// the LegalizeNodes map is filled in for any results that are not expanded,
160 /// the ExpandedNodes map is filled in for any results that are expanded, and
161 /// the Lo/Hi values are returned. This applies to integer types and Vector
162 /// types.
163 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
164
Dan Gohmana8665142007-06-25 16:23:39 +0000165 /// SplitVectorOp - Given an operand of vector type, break it down into
166 /// two smaller values.
Chris Lattner32206f52006-03-18 01:44:44 +0000167 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
168
Dan Gohmanf4e86da2007-06-27 14:06:22 +0000169 /// ScalarizeVectorOp - Given an operand of single-element vector type
170 /// (e.g. v1f32), convert it into the equivalent operation that returns a
171 /// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +0000172 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000173
Chris Lattner6be79822006-04-04 17:23:26 +0000174 /// isShuffleLegal - Return true if a vector shuffle is legal with the
175 /// specified mask and type. Targets can specify exactly which masks they
176 /// support and the code generator is tasked with not creating illegal masks.
177 ///
178 /// Note that this will also return true for shuffles that are promoted to a
179 /// different type.
180 ///
181 /// If this is a legal shuffle, this method returns the (possibly promoted)
182 /// build_vector Mask. If it's not a legal shuffle, it returns null.
183 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
184
Chris Lattner4488f0c2006-07-26 23:55:56 +0000185 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000186 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000187
Nate Begeman7e7f4392006-02-01 07:19:44 +0000188 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
189
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000190 SDOperand CreateStackTemporary(MVT::ValueType VT);
191
Reid Spencere63b6512006-12-31 05:55:36 +0000192 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattneraac464e2005-01-21 06:05:23 +0000193 SDOperand &Hi);
194 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
195 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000196
Chris Lattner36e663d2005-12-23 00:16:34 +0000197 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000198 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner6be79822006-04-04 17:23:26 +0000199 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000200 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
201 SDOperand LegalOp,
202 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000203 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
204 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000205 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
206 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000207
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000208 SDOperand ExpandBSWAP(SDOperand Op);
209 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000210 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
211 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000212 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
213 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000214
Dan Gohmana8665142007-06-25 16:23:39 +0000215 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner42a5fca2006-04-02 05:06:04 +0000216 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner6f423252006-03-31 17:55:51 +0000217
Chris Lattnerdc750592005-01-07 07:47:09 +0000218 SDOperand getIntPtrConstant(uint64_t Val) {
219 return DAG.getConstant(Val, TLI.getPointerTy());
220 }
221};
222}
223
Chris Lattner6be79822006-04-04 17:23:26 +0000224/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
225/// specified mask and type. Targets can specify exactly which masks they
226/// support and the code generator is tasked with not creating illegal masks.
227///
228/// Note that this will also return true for shuffles that are promoted to a
229/// different type.
230SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
231 SDOperand Mask) const {
232 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
233 default: return 0;
234 case TargetLowering::Legal:
235 case TargetLowering::Custom:
236 break;
237 case TargetLowering::Promote: {
238 // If this is promoted to a different type, convert the shuffle mask and
239 // ask if it is legal in the promoted type!
240 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
241
242 // If we changed # elements, change the shuffle mask.
243 unsigned NumEltsGrowth =
244 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
245 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
246 if (NumEltsGrowth > 1) {
247 // Renumber the elements.
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000248 SmallVector<SDOperand, 8> Ops;
Chris Lattner6be79822006-04-04 17:23:26 +0000249 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
250 SDOperand InOp = Mask.getOperand(i);
251 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
252 if (InOp.getOpcode() == ISD::UNDEF)
253 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
254 else {
255 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
256 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
257 }
258 }
259 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000260 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +0000261 }
262 VT = NVT;
263 break;
264 }
265 }
266 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
267}
268
Chris Lattner4add7e32005-01-23 04:42:50 +0000269SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
270 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
271 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000272 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000273 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000274}
275
Chris Lattnere31adc82007-06-18 21:28:10 +0000276/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
277/// contains all of a nodes operands before it contains the node.
278static void ComputeTopDownOrdering(SelectionDAG &DAG,
279 SmallVector<SDNode*, 64> &Order) {
280
281 DenseMap<SDNode*, unsigned> Visited;
282 std::vector<SDNode*> Worklist;
283 Worklist.reserve(128);
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000284
Chris Lattnere31adc82007-06-18 21:28:10 +0000285 // Compute ordering from all of the leaves in the graphs, those (like the
286 // entry node) that have no operands.
287 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
288 E = DAG.allnodes_end(); I != E; ++I) {
289 if (I->getNumOperands() == 0) {
290 Visited[I] = 0 - 1U;
291 Worklist.push_back(I);
292 }
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000293 }
294
Chris Lattnere31adc82007-06-18 21:28:10 +0000295 while (!Worklist.empty()) {
296 SDNode *N = Worklist.back();
297 Worklist.pop_back();
298
299 if (++Visited[N] != N->getNumOperands())
300 continue; // Haven't visited all operands yet
301
302 Order.push_back(N);
303
304 // Now that we have N in, add anything that uses it if all of their operands
305 // are now done.
306 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
307 UI != E; ++UI)
308 Worklist.push_back(*UI);
309 }
310
311 assert(Order.size() == Visited.size() &&
312 Order.size() ==
313 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
314 "Error: DAG is cyclic!");
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000315}
316
Chris Lattner44fe26f2005-07-29 00:11:56 +0000317
Chris Lattnerdc750592005-01-07 07:47:09 +0000318void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000319 LastCALLSEQ_END = DAG.getEntryNode();
320 IsLegalizingCall = false;
321
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000322 // The legalize process is inherently a bottom-up recursive process (users
323 // legalize their uses before themselves). Given infinite stack space, we
324 // could just start legalizing on the root and traverse the whole graph. In
325 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000326 // blocks. To avoid this problem, compute an ordering of the nodes where each
327 // node is only legalized after all of its operands are legalized.
Chris Lattner94c44c92007-02-04 01:20:02 +0000328 SmallVector<SDNode*, 64> Order;
Chris Lattnere31adc82007-06-18 21:28:10 +0000329 ComputeTopDownOrdering(DAG, Order);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000330
Chris Lattner32206f52006-03-18 01:44:44 +0000331 for (unsigned i = 0, e = Order.size(); i != e; ++i)
332 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000333
334 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000335 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000336 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
337 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000338
339 ExpandedNodes.clear();
340 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000341 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000342 SplitNodes.clear();
Dan Gohmana8665142007-06-25 16:23:39 +0000343 ScalarizedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000344
345 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000346 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000347}
348
Chris Lattner462505f2006-02-13 09:18:02 +0000349
350/// FindCallEndFromCallStart - Given a chained node that is part of a call
351/// sequence, find the CALLSEQ_END node that terminates the call sequence.
352static SDNode *FindCallEndFromCallStart(SDNode *Node) {
353 if (Node->getOpcode() == ISD::CALLSEQ_END)
354 return Node;
355 if (Node->use_empty())
356 return 0; // No CallSeqEnd
357
358 // The chain is usually at the end.
359 SDOperand TheChain(Node, Node->getNumValues()-1);
360 if (TheChain.getValueType() != MVT::Other) {
361 // Sometimes it's at the beginning.
362 TheChain = SDOperand(Node, 0);
363 if (TheChain.getValueType() != MVT::Other) {
364 // Otherwise, hunt for it.
365 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
366 if (Node->getValueType(i) == MVT::Other) {
367 TheChain = SDOperand(Node, i);
368 break;
369 }
370
371 // Otherwise, we walked into a node without a chain.
372 if (TheChain.getValueType() != MVT::Other)
373 return 0;
374 }
375 }
376
377 for (SDNode::use_iterator UI = Node->use_begin(),
378 E = Node->use_end(); UI != E; ++UI) {
379
380 // Make sure to only follow users of our token chain.
381 SDNode *User = *UI;
382 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
383 if (User->getOperand(i) == TheChain)
384 if (SDNode *Result = FindCallEndFromCallStart(User))
385 return Result;
386 }
387 return 0;
388}
389
390/// FindCallStartFromCallEnd - Given a chained node that is part of a call
391/// sequence, find the CALLSEQ_START node that initiates the call sequence.
392static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
393 assert(Node && "Didn't find callseq_start for a call??");
394 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
395
396 assert(Node->getOperand(0).getValueType() == MVT::Other &&
397 "Node doesn't have a token chain argument!");
398 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
399}
400
401/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
402/// see if any uses can reach Dest. If no dest operands can get to dest,
403/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000404///
405/// Keep track of the nodes we fine that actually do lead to Dest in
406/// NodesLeadingTo. This avoids retraversing them exponential number of times.
407///
408bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000409 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000410 if (N == Dest) return true; // N certainly leads to Dest :)
411
Chris Lattner4488f0c2006-07-26 23:55:56 +0000412 // If we've already processed this node and it does lead to Dest, there is no
413 // need to reprocess it.
414 if (NodesLeadingTo.count(N)) return true;
415
Chris Lattner462505f2006-02-13 09:18:02 +0000416 // If the first result of this node has been already legalized, then it cannot
417 // reach N.
418 switch (getTypeAction(N->getValueType(0))) {
419 case Legal:
420 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
421 break;
422 case Promote:
423 if (PromotedNodes.count(SDOperand(N, 0))) return false;
424 break;
425 case Expand:
426 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
427 break;
428 }
429
430 // Okay, this node has not already been legalized. Check and legalize all
431 // operands. If none lead to Dest, then we can legalize this node.
432 bool OperandsLeadToDest = false;
433 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
434 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000435 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000436
Chris Lattner4488f0c2006-07-26 23:55:56 +0000437 if (OperandsLeadToDest) {
438 NodesLeadingTo.insert(N);
439 return true;
440 }
Chris Lattner462505f2006-02-13 09:18:02 +0000441
442 // Okay, this node looks safe, legalize it and return false.
Chris Lattner916ae072006-04-17 22:10:08 +0000443 HandleOp(SDOperand(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000444 return false;
445}
446
Dan Gohmana8665142007-06-25 16:23:39 +0000447/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000448/// appropriate for its type.
449void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohmana8665142007-06-25 16:23:39 +0000450 MVT::ValueType VT = Op.getValueType();
451 switch (getTypeAction(VT)) {
Chris Lattner32206f52006-03-18 01:44:44 +0000452 default: assert(0 && "Bad type action!");
Dan Gohmana8665142007-06-25 16:23:39 +0000453 case Legal: (void)LegalizeOp(Op); break;
454 case Promote: (void)PromoteOp(Op); break;
Chris Lattner32206f52006-03-18 01:44:44 +0000455 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +0000456 if (!MVT::isVector(VT)) {
457 // If this is an illegal scalar, expand it into its two component
458 // pieces.
Chris Lattner32206f52006-03-18 01:44:44 +0000459 SDOperand X, Y;
Chris Lattner2ed652f2007-08-25 01:00:22 +0000460 if (Op.getOpcode() == ISD::TargetConstant)
461 break; // Allow illegal target nodes.
Chris Lattner32206f52006-03-18 01:44:44 +0000462 ExpandOp(Op, X, Y);
Dan Gohmana8665142007-06-25 16:23:39 +0000463 } else if (MVT::getVectorNumElements(VT) == 1) {
464 // If this is an illegal single element vector, convert it to a
465 // scalar operation.
466 (void)ScalarizeVectorOp(Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000467 } else {
Dan Gohmana8665142007-06-25 16:23:39 +0000468 // Otherwise, this is an illegal multiple element vector.
469 // Split it in half and legalize both parts.
470 SDOperand X, Y;
471 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000472 }
473 break;
474 }
475}
Chris Lattner462505f2006-02-13 09:18:02 +0000476
Evan Cheng22cf8992006-12-13 20:57:08 +0000477/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
478/// a load from the constant pool.
479static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000480 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000481 bool Extend = false;
482
483 // If a FP immediate is precise when represented as a float and if the
484 // target can do an extending load from float to double, we put it into
485 // the constant pool as a float, even if it's is statically typed as a
486 // double.
487 MVT::ValueType VT = CFP->getValueType(0);
488 bool isDouble = VT == MVT::f64;
Dale Johannesen98d3a082007-09-14 22:26:36 +0000489 ConstantFP *LLVMC = ConstantFP::get(VT==MVT::f64 ? Type::DoubleTy :
490 VT==MVT::f32 ? Type::FloatTy :
491 VT==MVT::f80 ? Type::X86_FP80Ty :
492 VT==MVT::f128 ? Type::FP128Ty :
493 VT==MVT::ppcf128 ? Type::PPC_FP128Ty :
494 Type::VoidTy, // error
495 CFP->getValueAPF());
Evan Cheng22cf8992006-12-13 20:57:08 +0000496 if (!UseCP) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000497 if (VT!=MVT::f64 && VT!=MVT::f32)
498 assert(0 && "Invalid type expansion");
Dale Johannesen028084e2007-09-12 03:30:33 +0000499 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
500 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000501 }
502
Dale Johannesend246b2c2007-08-30 00:23:21 +0000503 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng47833a12006-12-12 21:32:44 +0000504 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen98d3a082007-09-14 22:26:36 +0000505 // Do not try to be clever about long doubles (so far)
Evan Cheng47833a12006-12-12 21:32:44 +0000506 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
507 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
508 VT = MVT::f32;
509 Extend = true;
510 }
511
512 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
513 if (Extend) {
514 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
515 CPIdx, NULL, 0, MVT::f32);
516 } else {
517 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
518 }
519}
520
Chris Lattner462505f2006-02-13 09:18:02 +0000521
Evan Cheng003feb02007-01-04 21:56:39 +0000522/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
523/// operations.
524static
525SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
526 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng3b841dd2007-01-05 21:31:51 +0000527 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000528 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000529 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
530 "fcopysign expansion only supported for f32 and f64");
Evan Cheng003feb02007-01-04 21:56:39 +0000531 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000532
Evan Cheng003feb02007-01-04 21:56:39 +0000533 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000534 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000535 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
536 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000537 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000538 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000539 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000540 // Shift right or sign-extend it if the two operands have different types.
541 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
542 if (SizeDiff > 0) {
543 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
544 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
545 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
546 } else if (SizeDiff < 0)
547 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000548
549 // Clear the sign bit of first operand.
550 SDOperand Mask2 = (VT == MVT::f64)
551 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
552 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
553 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng003feb02007-01-04 21:56:39 +0000554 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000555 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
556
557 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000558 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
559 return Result;
560}
561
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000562/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
563static
564SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
565 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000566 SDOperand Chain = ST->getChain();
567 SDOperand Ptr = ST->getBasePtr();
568 SDOperand Val = ST->getValue();
569 MVT::ValueType VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000570 int Alignment = ST->getAlignment();
571 int SVOffset = ST->getSrcValueOffset();
572 if (MVT::isFloatingPoint(ST->getStoredVT())) {
573 // Expand to a bitconvert of the value to the integer type of the
574 // same size, then a (misaligned) int store.
575 MVT::ValueType intVT;
576 if (VT==MVT::f64)
577 intVT = MVT::i64;
578 else if (VT==MVT::f32)
579 intVT = MVT::i32;
580 else
581 assert(0 && "Unaligned load of unsupported floating point type");
582
583 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
584 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
585 SVOffset, ST->isVolatile(), Alignment);
586 }
587 assert(MVT::isInteger(ST->getStoredVT()) &&
588 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000589 // Get the half-size VT
590 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
591 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000592 int IncrementSize = NumBits / 8;
593
594 // Divide the stored value in two parts.
595 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
596 SDOperand Lo = Val;
597 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
598
599 // Store the two parts
600 SDOperand Store1, Store2;
601 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
602 ST->getSrcValue(), SVOffset, NewStoredVT,
603 ST->isVolatile(), Alignment);
604 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
605 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
606 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
607 ST->getSrcValue(), SVOffset + IncrementSize,
608 NewStoredVT, ST->isVolatile(), Alignment);
609
610 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
611}
612
613/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
614static
615SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
616 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000617 int SVOffset = LD->getSrcValueOffset();
618 SDOperand Chain = LD->getChain();
619 SDOperand Ptr = LD->getBasePtr();
620 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000621 MVT::ValueType LoadedVT = LD->getLoadedVT();
622 if (MVT::isFloatingPoint(VT)) {
623 // Expand to a (misaligned) integer load of the same size,
624 // then bitconvert to floating point.
625 MVT::ValueType intVT;
626 if (LoadedVT==MVT::f64)
627 intVT = MVT::i64;
628 else if (LoadedVT==MVT::f32)
629 intVT = MVT::i32;
630 else
631 assert(0 && "Unaligned load of unsupported floating point type");
632
633 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
634 SVOffset, LD->isVolatile(),
635 LD->getAlignment());
636 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
637 if (LoadedVT != VT)
638 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
639
640 SDOperand Ops[] = { Result, Chain };
641 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
642 Ops, 2);
643 }
644 assert(MVT::isInteger(LoadedVT) && "Unaligned load of unsupported type.");
645 MVT::ValueType NewLoadedVT = LoadedVT - 1;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000646 int NumBits = MVT::getSizeInBits(NewLoadedVT);
647 int Alignment = LD->getAlignment();
648 int IncrementSize = NumBits / 8;
649 ISD::LoadExtType HiExtType = LD->getExtensionType();
650
651 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
652 if (HiExtType == ISD::NON_EXTLOAD)
653 HiExtType = ISD::ZEXTLOAD;
654
655 // Load the value in two parts
656 SDOperand Lo, Hi;
657 if (TLI.isLittleEndian()) {
658 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
659 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
660 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
661 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
662 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
663 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
664 Alignment);
665 } else {
666 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
667 NewLoadedVT,LD->isVolatile(), Alignment);
668 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
669 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
670 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
671 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
672 Alignment);
673 }
674
675 // aggregate the two parts
676 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
677 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
678 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
679
680 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
681 Hi.getValue(1));
682
683 SDOperand Ops[] = { Result, TF };
684 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
685}
Evan Cheng003feb02007-01-04 21:56:39 +0000686
Dan Gohmanff727882007-07-13 20:14:11 +0000687/// LegalizeOp - We know that the specified value has a legal type, and
688/// that its operands are legal. Now ensure that the operation itself
689/// is legal, recursively ensuring that the operands' operations remain
690/// legal.
Chris Lattnerdc750592005-01-07 07:47:09 +0000691SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner2ed652f2007-08-25 01:00:22 +0000692 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
693 return Op;
694
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000695 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000696 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000697 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000698
Chris Lattnerdc750592005-01-07 07:47:09 +0000699 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000700 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000701 if (Node->getNumValues() > 1) {
702 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000703 if (getTypeAction(Node->getValueType(i)) != Legal) {
704 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000705 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000706 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000707 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000708 }
709 }
710
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000711 // Note that LegalizeOp may be reentered even from single-use nodes, which
712 // means that we always must cache transformed nodes.
Chris Lattnered39c862007-02-04 00:50:02 +0000713 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000714 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000715
Nate Begemane5b86d72005-08-10 20:51:12 +0000716 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000717 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000718 bool isCustom = false;
719
Chris Lattnerdc750592005-01-07 07:47:09 +0000720 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000721 case ISD::FrameIndex:
722 case ISD::EntryToken:
723 case ISD::Register:
724 case ISD::BasicBlock:
725 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000726 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000727 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000728 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000729 case ISD::TargetConstantPool:
730 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000731 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000732 case ISD::TargetExternalSymbol:
733 case ISD::VALUETYPE:
734 case ISD::SRCVALUE:
735 case ISD::STRING:
736 case ISD::CONDCODE:
737 // Primitives must all be legal.
738 assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) &&
739 "This must be legal!");
740 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000741 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000742 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
743 // If this is a target node, legalize it by legalizing the operands then
744 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000745 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000746 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000747 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000748
Chris Lattner97af9d52006-08-08 01:09:31 +0000749 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000750
751 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
752 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
753 return Result.getValue(Op.ResNo);
754 }
755 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000756#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000757 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000758#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000759 assert(0 && "Do not know how to legalize this operator!");
760 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000761 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000762 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000763 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000764 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000765 case ISD::ConstantPool:
766 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000767 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
768 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000769 case TargetLowering::Custom:
770 Tmp1 = TLI.LowerOperation(Op, DAG);
771 if (Tmp1.Val) Result = Tmp1;
772 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000773 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000774 break;
775 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000776 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000777 case ISD::FRAMEADDR:
778 case ISD::RETURNADDR:
779 // The only option for these nodes is to custom lower them. If the target
780 // does not custom lower them, then return zero.
781 Tmp1 = TLI.LowerOperation(Op, DAG);
782 if (Tmp1.Val)
783 Result = Tmp1;
784 else
785 Result = DAG.getConstant(0, TLI.getPointerTy());
786 break;
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000787 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000788 MVT::ValueType VT = Node->getValueType(0);
789 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
790 default: assert(0 && "This action is not supported yet!");
791 case TargetLowering::Custom:
792 Result = TLI.LowerOperation(Op, DAG);
793 if (Result.Val) break;
794 // Fall Thru
795 case TargetLowering::Legal:
796 Result = DAG.getConstant(0, VT);
797 break;
798 }
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000799 }
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000800 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000801 case ISD::EXCEPTIONADDR: {
802 Tmp1 = LegalizeOp(Node->getOperand(0));
803 MVT::ValueType VT = Node->getValueType(0);
804 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
805 default: assert(0 && "This action is not supported yet!");
806 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000807 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey7f5872c2007-02-22 15:37:19 +0000808 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
809 }
810 break;
811 case TargetLowering::Custom:
812 Result = TLI.LowerOperation(Op, DAG);
813 if (Result.Val) break;
814 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000815 case TargetLowering::Legal: {
816 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
817 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
818 Ops, 2).getValue(Op.ResNo);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000819 break;
820 }
821 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000822 }
Jim Laskey7f5872c2007-02-22 15:37:19 +0000823 break;
Jim Laskey644af6b2007-02-28 20:43:58 +0000824 case ISD::EHSELECTION: {
825 Tmp1 = LegalizeOp(Node->getOperand(0));
826 Tmp2 = LegalizeOp(Node->getOperand(1));
827 MVT::ValueType VT = Node->getValueType(0);
828 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
829 default: assert(0 && "This action is not supported yet!");
830 case TargetLowering::Expand: {
831 unsigned Reg = TLI.getExceptionSelectorRegister();
832 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
833 }
834 break;
835 case TargetLowering::Custom:
836 Result = TLI.LowerOperation(Op, DAG);
837 if (Result.Val) break;
838 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000839 case TargetLowering::Legal: {
840 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
841 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
842 Ops, 2).getValue(Op.ResNo);
Jim Laskey644af6b2007-02-28 20:43:58 +0000843 break;
844 }
845 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000846 }
Jim Laskey644af6b2007-02-28 20:43:58 +0000847 break;
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000848 case ISD::EH_RETURN: {
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000849 MVT::ValueType VT = Node->getValueType(0);
850 // The only "good" option for this node is to custom lower it.
851 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
852 default: assert(0 && "This action is not supported at all!");
853 case TargetLowering::Custom:
854 Result = TLI.LowerOperation(Op, DAG);
855 if (Result.Val) break;
856 // Fall Thru
857 case TargetLowering::Legal:
858 // Target does not know, how to lower this, lower to noop
859 Result = LegalizeOp(Node->getOperand(0));
860 break;
861 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000862 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000863 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000864 case ISD::AssertSext:
865 case ISD::AssertZext:
866 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000867 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000868 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000869 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000870 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000871 Result = Node->getOperand(Op.ResNo);
872 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000873 case ISD::CopyFromReg:
874 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000875 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000876 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000877 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000878 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000879 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000880 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000881 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000882 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
883 } else {
884 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
885 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000886 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
887 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000888 // Since CopyFromReg produces two values, make sure to remember that we
889 // legalized both of them.
890 AddLegalizedOperand(Op.getValue(0), Result);
891 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
892 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000893 case ISD::UNDEF: {
894 MVT::ValueType VT = Op.getValueType();
895 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000896 default: assert(0 && "This action is not supported yet!");
897 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000898 if (MVT::isInteger(VT))
899 Result = DAG.getConstant(0, VT);
900 else if (MVT::isFloatingPoint(VT))
901 Result = DAG.getConstantFP(0, VT);
902 else
903 assert(0 && "Unknown value type!");
904 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000905 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000906 break;
907 }
908 break;
909 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000910
Chris Lattnere55d1712006-03-28 00:40:33 +0000911 case ISD::INTRINSIC_W_CHAIN:
912 case ISD::INTRINSIC_WO_CHAIN:
913 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +0000914 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +0000915 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
916 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000917 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +0000918
919 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000920 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000921 TargetLowering::Custom) {
922 Tmp3 = TLI.LowerOperation(Result, DAG);
923 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000924 }
925
926 if (Result.Val->getNumValues() == 1) break;
927
928 // Must have return value and chain result.
929 assert(Result.Val->getNumValues() == 2 &&
930 "Cannot return more than two values!");
931
932 // Since loads produce two values, make sure to remember that we
933 // legalized both of them.
934 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
935 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
936 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000937 }
Chris Lattner435b4022005-11-29 06:21:05 +0000938
939 case ISD::LOCATION:
940 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
941 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
942
943 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
944 case TargetLowering::Promote:
945 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000946 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +0000947 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000948 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +0000949 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000950
Jim Laskeyc56315c2007-01-26 21:22:28 +0000951 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000952 const std::string &FName =
953 cast<StringSDNode>(Node->getOperand(3))->getValue();
954 const std::string &DirName =
955 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000956 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000957
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000958 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +0000959 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000960 SDOperand LineOp = Node->getOperand(1);
961 SDOperand ColOp = Node->getOperand(2);
962
963 if (useDEBUG_LOC) {
964 Ops.push_back(LineOp); // line #
965 Ops.push_back(ColOp); // col #
966 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000967 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000968 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +0000969 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
970 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000971 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000972 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskeyf9e54452007-01-26 14:34:52 +0000973 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +0000974 }
Jim Laskey9e296be2005-12-21 20:51:37 +0000975 } else {
976 Result = Tmp1; // chain
977 }
Chris Lattner435b4022005-11-29 06:21:05 +0000978 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000979 }
Chris Lattner435b4022005-11-29 06:21:05 +0000980 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000981 if (Tmp1 != Node->getOperand(0) ||
982 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +0000983 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +0000984 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000985 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
986 Ops.push_back(Node->getOperand(1)); // line # must be legal.
987 Ops.push_back(Node->getOperand(2)); // col # must be legal.
988 } else {
989 // Otherwise promote them.
990 Ops.push_back(PromoteOp(Node->getOperand(1)));
991 Ops.push_back(PromoteOp(Node->getOperand(2)));
992 }
Chris Lattner435b4022005-11-29 06:21:05 +0000993 Ops.push_back(Node->getOperand(3)); // filename must be legal.
994 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +0000995 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +0000996 }
997 break;
998 }
999 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001000
1001 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +00001002 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +00001003 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +00001004 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +00001005 case TargetLowering::Legal:
1006 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1007 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1008 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1009 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001010 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001011 break;
1012 }
1013 break;
1014
Jim Laskeyf9e54452007-01-26 14:34:52 +00001015 case ISD::LABEL:
1016 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1017 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001018 default: assert(0 && "This action is not supported yet!");
1019 case TargetLowering::Legal:
1020 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1021 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001022 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +00001023 break;
Chris Lattner567b9252007-03-03 19:21:38 +00001024 case TargetLowering::Expand:
1025 Result = LegalizeOp(Node->getOperand(0));
1026 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001027 }
Nate Begeman5965bd12006-02-17 05:43:56 +00001028 break;
Chris Lattner435b4022005-11-29 06:21:05 +00001029
Scott Michel9d09c5c2007-08-08 23:23:31 +00001030 case ISD::Constant: {
1031 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1032 unsigned opAction =
1033 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1034
Chris Lattnerdc750592005-01-07 07:47:09 +00001035 // We know we don't need to expand constants here, constants only have one
1036 // value and we check that it is fine above.
1037
Scott Michel9d09c5c2007-08-08 23:23:31 +00001038 if (opAction == TargetLowering::Custom) {
1039 Tmp1 = TLI.LowerOperation(Result, DAG);
1040 if (Tmp1.Val)
1041 Result = Tmp1;
1042 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001043 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +00001044 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001045 case ISD::ConstantFP: {
1046 // Spill FP immediates to the constant pool if the target cannot directly
1047 // codegen them. Targets often have some immediate values that can be
1048 // efficiently generated into an FP register without a load. We explicitly
1049 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +00001050 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1051
1052 // Check to see if this FP immediate is already legal.
1053 bool isLegal = false;
1054 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1055 E = TLI.legal_fpimm_end(); I != E; ++I)
1056 if (CFP->isExactlyValue(*I)) {
1057 isLegal = true;
1058 break;
1059 }
1060
Chris Lattner758b0ac2006-01-29 06:26:56 +00001061 // If this is a legal constant, turn it into a TargetConstantFP node.
1062 if (isLegal) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00001063 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1064 CFP->getValueType(0));
Chris Lattner758b0ac2006-01-29 06:26:56 +00001065 break;
1066 }
1067
1068 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1069 default: assert(0 && "This action is not supported yet!");
1070 case TargetLowering::Custom:
1071 Tmp3 = TLI.LowerOperation(Result, DAG);
1072 if (Tmp3.Val) {
1073 Result = Tmp3;
1074 break;
1075 }
1076 // FALLTHROUGH
1077 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +00001078 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001079 }
1080 break;
1081 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001082 case ISD::TokenFactor:
1083 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001084 Tmp1 = LegalizeOp(Node->getOperand(0));
1085 Tmp2 = LegalizeOp(Node->getOperand(1));
1086 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1087 } else if (Node->getNumOperands() == 3) {
1088 Tmp1 = LegalizeOp(Node->getOperand(0));
1089 Tmp2 = LegalizeOp(Node->getOperand(1));
1090 Tmp3 = LegalizeOp(Node->getOperand(2));
1091 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001092 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +00001093 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001094 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001095 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1096 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001097 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001098 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001099 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001100
1101 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001102 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001103 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001104 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1105 assert(Tmp3.Val && "Target didn't custom lower this node!");
1106 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1107 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001108
Chris Lattneraaa23d92006-05-16 22:53:20 +00001109 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001110 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +00001111 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1112 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001113 if (Op.ResNo == i)
1114 Tmp2 = Tmp1;
1115 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1116 }
1117 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001118 case ISD::EXTRACT_SUBREG: {
1119 Tmp1 = LegalizeOp(Node->getOperand(0));
1120 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1121 assert(idx && "Operand must be a constant");
1122 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1123 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1124 }
1125 break;
1126 case ISD::INSERT_SUBREG: {
1127 Tmp1 = LegalizeOp(Node->getOperand(0));
1128 Tmp2 = LegalizeOp(Node->getOperand(1));
1129 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1130 assert(idx && "Operand must be a constant");
1131 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1132 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1133 }
1134 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001135 case ISD::BUILD_VECTOR:
1136 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001137 default: assert(0 && "This action is not supported yet!");
1138 case TargetLowering::Custom:
1139 Tmp3 = TLI.LowerOperation(Result, DAG);
1140 if (Tmp3.Val) {
1141 Result = Tmp3;
1142 break;
1143 }
1144 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001145 case TargetLowering::Expand:
1146 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001147 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001148 }
Chris Lattner29b23012006-03-19 01:17:20 +00001149 break;
1150 case ISD::INSERT_VECTOR_ELT:
1151 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1152 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1153 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1154 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1155
1156 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1157 Node->getValueType(0))) {
1158 default: assert(0 && "This action is not supported yet!");
1159 case TargetLowering::Legal:
1160 break;
1161 case TargetLowering::Custom:
1162 Tmp3 = TLI.LowerOperation(Result, DAG);
1163 if (Tmp3.Val) {
1164 Result = Tmp3;
1165 break;
1166 }
1167 // FALLTHROUGH
1168 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001169 // If the insert index is a constant, codegen this as a scalar_to_vector,
1170 // then a shuffle that inserts it into the right position in the vector.
1171 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1172 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1173 Tmp1.getValueType(), Tmp2);
1174
1175 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1176 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman5c441312007-06-14 22:58:02 +00001177 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner326870b2006-04-17 19:21:01 +00001178
1179 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1180 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1181 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001182 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +00001183 for (unsigned i = 0; i != NumElts; ++i) {
1184 if (i != InsertPos->getValue())
1185 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1186 else
1187 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1188 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001189 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1190 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +00001191
1192 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1193 Tmp1, ScVec, ShufMask);
1194 Result = LegalizeOp(Result);
1195 break;
1196 }
1197
Chris Lattner29b23012006-03-19 01:17:20 +00001198 // If the target doesn't support this, we have to spill the input vector
1199 // to a temporary stack slot, update the element, then reload it. This is
1200 // badness. We could also load the value into a vector register (either
1201 // with a "move to register" or "extload into register" instruction, then
1202 // permute it into place, if the idx is a constant and if the idx is
1203 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001204 MVT::ValueType VT = Tmp1.getValueType();
1205 MVT::ValueType EltVT = Tmp2.getValueType();
1206 MVT::ValueType IdxVT = Tmp3.getValueType();
1207 MVT::ValueType PtrVT = TLI.getPointerTy();
1208 SDOperand StackPtr = CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001209 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +00001210 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001211
1212 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001213 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1214 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001215 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001216 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1217 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1218 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001219 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +00001220 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001221 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +00001222 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +00001223 break;
1224 }
1225 }
1226 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001227 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001228 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1229 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1230 break;
1231 }
1232
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001233 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1234 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1235 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1236 Node->getValueType(0))) {
1237 default: assert(0 && "This action is not supported yet!");
1238 case TargetLowering::Legal:
1239 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001240 case TargetLowering::Custom:
1241 Tmp3 = TLI.LowerOperation(Result, DAG);
1242 if (Tmp3.Val) {
1243 Result = Tmp3;
1244 break;
1245 }
1246 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001247 case TargetLowering::Expand:
1248 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001249 break;
1250 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001251 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001252 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001253 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1254 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1255 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1256
1257 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001258 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1259 default: assert(0 && "Unknown operation action!");
1260 case TargetLowering::Legal:
1261 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1262 "vector shuffle should not be created if not legal!");
1263 break;
1264 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001265 Tmp3 = TLI.LowerOperation(Result, DAG);
1266 if (Tmp3.Val) {
1267 Result = Tmp3;
1268 break;
1269 }
1270 // FALLTHROUGH
1271 case TargetLowering::Expand: {
1272 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00001273 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng9fa89592006-04-05 06:07:11 +00001274 MVT::ValueType PtrVT = TLI.getPointerTy();
1275 SDOperand Mask = Node->getOperand(2);
1276 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001277 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001278 for (unsigned i = 0; i != NumElems; ++i) {
1279 SDOperand Arg = Mask.getOperand(i);
1280 if (Arg.getOpcode() == ISD::UNDEF) {
1281 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1282 } else {
1283 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1284 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1285 if (Idx < NumElems)
1286 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1287 DAG.getConstant(Idx, PtrVT)));
1288 else
1289 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1290 DAG.getConstant(Idx - NumElems, PtrVT)));
1291 }
1292 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001293 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001294 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001295 }
Chris Lattner6be79822006-04-04 17:23:26 +00001296 case TargetLowering::Promote: {
1297 // Change base type to a different vector type.
1298 MVT::ValueType OVT = Node->getValueType(0);
1299 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1300
1301 // Cast the two input vectors.
1302 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1303 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1304
1305 // Convert the shuffle mask to the right # elements.
1306 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1307 assert(Tmp3.Val && "Shuffle not legal?");
1308 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1309 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1310 break;
1311 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001312 }
1313 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001314
1315 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001316 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001317 Tmp2 = LegalizeOp(Node->getOperand(1));
1318 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001319 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001320 break;
1321
Dan Gohmana8665142007-06-25 16:23:39 +00001322 case ISD::EXTRACT_SUBVECTOR:
1323 Tmp1 = Node->getOperand(0);
1324 Tmp2 = LegalizeOp(Node->getOperand(1));
1325 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1326 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001327 break;
1328
Chris Lattner462505f2006-02-13 09:18:02 +00001329 case ISD::CALLSEQ_START: {
1330 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1331
1332 // Recursively Legalize all of the inputs of the call end that do not lead
1333 // to this call start. This ensures that any libcalls that need be inserted
1334 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001335 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001336 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001337 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1338 NodesLeadingTo);
1339 }
Chris Lattner462505f2006-02-13 09:18:02 +00001340
1341 // Now that we legalized all of the inputs (which may have inserted
1342 // libcalls) create the new CALLSEQ_START node.
1343 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1344
1345 // Merge in the last call, to ensure that this call start after the last
1346 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001347 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001348 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1349 Tmp1 = LegalizeOp(Tmp1);
1350 }
Chris Lattner462505f2006-02-13 09:18:02 +00001351
1352 // Do not try to legalize the target-specific arguments (#1+).
1353 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001354 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001355 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001356 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001357 }
1358
1359 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001360 AddLegalizedOperand(Op.getValue(0), Result);
1361 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1362 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1363
Chris Lattner462505f2006-02-13 09:18:02 +00001364 // Now that the callseq_start and all of the non-call nodes above this call
1365 // sequence have been legalized, legalize the call itself. During this
1366 // process, no libcalls can/will be inserted, guaranteeing that no calls
1367 // can overlap.
1368 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1369 SDOperand InCallSEQ = LastCALLSEQ_END;
1370 // Note that we are selecting this call!
1371 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1372 IsLegalizingCall = true;
1373
1374 // Legalize the call, starting from the CALLSEQ_END.
1375 LegalizeOp(LastCALLSEQ_END);
1376 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1377 return Result;
1378 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001379 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001380 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1381 // will cause this node to be legalized as well as handling libcalls right.
1382 if (LastCALLSEQ_END.Val != Node) {
1383 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001384 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001385 assert(I != LegalizedNodes.end() &&
1386 "Legalizing the call start should have legalized this node!");
1387 return I->second;
1388 }
1389
1390 // Otherwise, the call start has been legalized and everything is going
1391 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001392 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001393 // Do not try to legalize the target-specific arguments (#1+), except for
1394 // an optional flag input.
1395 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1396 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001397 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001398 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001399 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001400 }
1401 } else {
1402 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1403 if (Tmp1 != Node->getOperand(0) ||
1404 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001405 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001406 Ops[0] = Tmp1;
1407 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001408 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001409 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001410 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001411 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001412 // This finishes up call legalization.
1413 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001414
1415 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1416 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1417 if (Node->getNumValues() == 2)
1418 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1419 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001420 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng631ccc62007-08-16 23:50:06 +00001421 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerec26b482005-01-09 19:03:49 +00001422 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1423 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1424 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001425 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001426
Chris Lattnerd02b0542006-01-28 10:58:55 +00001427 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001428 Tmp2 = Result.getValue(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001429 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Cheng7f4ec822006-01-11 22:14:47 +00001430 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001431 case TargetLowering::Expand: {
1432 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1433 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1434 " not tell us which reg is the stack pointer!");
1435 SDOperand Chain = Tmp1.getOperand(0);
1436 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001437 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1438 Chain = SP.getValue(1);
1439 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1440 unsigned StackAlign =
1441 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1442 if (Align > StackAlign)
Evan Chengcb6d65e2007-08-17 18:02:22 +00001443 SP = DAG.getNode(ISD::AND, VT, SP,
1444 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng631ccc62007-08-16 23:50:06 +00001445 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
1446 Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001447 Tmp1 = LegalizeOp(Tmp1);
1448 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001449 break;
1450 }
1451 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001452 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1453 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001454 Tmp1 = LegalizeOp(Tmp3);
1455 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001456 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001457 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001458 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001459 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001460 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001461 // Since this op produce two values, make sure to remember that we
1462 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001463 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1464 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001465 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001466 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001467 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001468 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001469 bool Changed = false;
1470 // Legalize all of the operands of the inline asm, in case they are nodes
1471 // that need to be expanded or something. Note we skip the asm string and
1472 // all of the TargetConstant flags.
1473 SDOperand Op = LegalizeOp(Ops[0]);
1474 Changed = Op != Ops[0];
1475 Ops[0] = Op;
1476
1477 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1478 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1479 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1480 for (++i; NumVals; ++i, --NumVals) {
1481 SDOperand Op = LegalizeOp(Ops[i]);
1482 if (Op != Ops[i]) {
1483 Changed = true;
1484 Ops[i] = Op;
1485 }
1486 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001487 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001488
1489 if (HasInFlag) {
1490 Op = LegalizeOp(Ops.back());
1491 Changed |= Op != Ops.back();
1492 Ops.back() = Op;
1493 }
1494
1495 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001496 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001497
1498 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001499 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001500 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1501 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001502 }
Chris Lattner68a12142005-01-07 22:12:08 +00001503 case ISD::BR:
1504 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001505 // Ensure that libcalls are emitted before a branch.
1506 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1507 Tmp1 = LegalizeOp(Tmp1);
1508 LastCALLSEQ_END = DAG.getEntryNode();
1509
Chris Lattnerd02b0542006-01-28 10:58:55 +00001510 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001511 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001512 case ISD::BRIND:
1513 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1514 // Ensure that libcalls are emitted before a branch.
1515 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1516 Tmp1 = LegalizeOp(Tmp1);
1517 LastCALLSEQ_END = DAG.getEntryNode();
1518
1519 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1520 default: assert(0 && "Indirect target must be legal type (pointer)!");
1521 case Legal:
1522 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1523 break;
1524 }
1525 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1526 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001527 case ISD::BR_JT:
1528 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1529 // Ensure that libcalls are emitted before a branch.
1530 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1531 Tmp1 = LegalizeOp(Tmp1);
1532 LastCALLSEQ_END = DAG.getEntryNode();
1533
1534 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1535 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1536
1537 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1538 default: assert(0 && "This action is not supported yet!");
1539 case TargetLowering::Legal: break;
1540 case TargetLowering::Custom:
1541 Tmp1 = TLI.LowerOperation(Result, DAG);
1542 if (Tmp1.Val) Result = Tmp1;
1543 break;
1544 case TargetLowering::Expand: {
1545 SDOperand Chain = Result.getOperand(0);
1546 SDOperand Table = Result.getOperand(1);
1547 SDOperand Index = Result.getOperand(2);
1548
1549 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001550 MachineFunction &MF = DAG.getMachineFunction();
1551 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001552 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1553 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001554
1555 SDOperand LD;
1556 switch (EntrySize) {
1557 default: assert(0 && "Size of jump table not supported yet."); break;
1558 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1559 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1560 }
1561
1562 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001563 // For PIC, the sequence is:
1564 // BRIND(load(Jumptable + index) + RelocBase)
1565 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1566 SDOperand Reloc;
1567 if (TLI.usesGlobalOffsetTable())
1568 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1569 else
1570 Reloc = Table;
Evan Chenge6d58472006-10-31 02:31:00 +00001571 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng84a28d42006-10-30 08:00:44 +00001572 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1573 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1574 } else {
1575 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1576 }
1577 }
1578 }
1579 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001580 case ISD::BRCOND:
1581 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001582 // Ensure that libcalls are emitted before a return.
1583 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1584 Tmp1 = LegalizeOp(Tmp1);
1585 LastCALLSEQ_END = DAG.getEntryNode();
1586
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001587 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1588 case Expand: assert(0 && "It's impossible to expand bools");
1589 case Legal:
1590 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1591 break;
1592 case Promote:
1593 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001594
1595 // The top bits of the promoted condition are not necessarily zero, ensure
1596 // that the value is properly zero extended.
Dan Gohman309d3d52007-06-22 14:59:07 +00001597 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerdb189382006-11-27 04:39:56 +00001598 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1599 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001600 break;
1601 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001602
1603 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001604 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001605
1606 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1607 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001608 case TargetLowering::Legal: break;
1609 case TargetLowering::Custom:
1610 Tmp1 = TLI.LowerOperation(Result, DAG);
1611 if (Tmp1.Val) Result = Tmp1;
1612 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001613 case TargetLowering::Expand:
1614 // Expand brcond's setcc into its constituent parts and create a BR_CC
1615 // Node.
1616 if (Tmp2.getOpcode() == ISD::SETCC) {
1617 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1618 Tmp2.getOperand(0), Tmp2.getOperand(1),
1619 Node->getOperand(2));
1620 } else {
1621 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1622 DAG.getCondCode(ISD::SETNE), Tmp2,
1623 DAG.getConstant(0, Tmp2.getValueType()),
1624 Node->getOperand(2));
1625 }
1626 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001627 }
1628 break;
1629 case ISD::BR_CC:
1630 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001631 // Ensure that libcalls are emitted before a branch.
1632 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1633 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001634 Tmp2 = Node->getOperand(2); // LHS
1635 Tmp3 = Node->getOperand(3); // RHS
1636 Tmp4 = Node->getOperand(1); // CC
1637
1638 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001639 LastCALLSEQ_END = DAG.getEntryNode();
1640
Nate Begeman7e7f4392006-02-01 07:19:44 +00001641 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1642 // the LHS is a legal SETCC itself. In this case, we need to compare
1643 // the result against zero to select between true and false values.
1644 if (Tmp3.Val == 0) {
1645 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1646 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001647 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001648
1649 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1650 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001651
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001652 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1653 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001654 case TargetLowering::Legal: break;
1655 case TargetLowering::Custom:
1656 Tmp4 = TLI.LowerOperation(Result, DAG);
1657 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001658 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001659 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001660 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001661 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001662 LoadSDNode *LD = cast<LoadSDNode>(Node);
1663 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1664 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001665
Evan Chenge71fe34d2006-10-09 20:57:25 +00001666 ISD::LoadExtType ExtType = LD->getExtensionType();
1667 if (ExtType == ISD::NON_EXTLOAD) {
1668 MVT::ValueType VT = Node->getValueType(0);
1669 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1670 Tmp3 = Result.getValue(0);
1671 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001672
Evan Chenge71fe34d2006-10-09 20:57:25 +00001673 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1674 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001675 case TargetLowering::Legal:
1676 // If this is an unaligned load and the target doesn't support it,
1677 // expand it.
1678 if (!TLI.allowsUnalignedMemoryAccesses()) {
1679 unsigned ABIAlignment = TLI.getTargetData()->
1680 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1681 if (LD->getAlignment() < ABIAlignment){
1682 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1683 TLI);
1684 Tmp3 = Result.getOperand(0);
1685 Tmp4 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001686 Tmp3 = LegalizeOp(Tmp3);
1687 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001688 }
1689 }
1690 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001691 case TargetLowering::Custom:
1692 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1693 if (Tmp1.Val) {
1694 Tmp3 = LegalizeOp(Tmp1);
1695 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001696 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001697 break;
1698 case TargetLowering::Promote: {
1699 // Only promote a load of vector type to another.
1700 assert(MVT::isVector(VT) && "Cannot promote this load!");
1701 // Change base type to a different vector type.
1702 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1703
1704 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001705 LD->getSrcValueOffset(),
1706 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001707 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1708 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001709 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001710 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001711 }
1712 // Since loads produce two values, make sure to remember that we
1713 // legalized both of them.
1714 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1715 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1716 return Op.ResNo ? Tmp4 : Tmp3;
1717 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001718 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001719 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1720 default: assert(0 && "This action is not supported yet!");
1721 case TargetLowering::Promote:
1722 assert(SrcVT == MVT::i1 &&
1723 "Can only promote extending LOAD from i1 -> i8!");
1724 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1725 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00001726 MVT::i8, LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001727 Tmp1 = Result.getValue(0);
1728 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001729 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001730 case TargetLowering::Custom:
1731 isCustom = true;
1732 // FALLTHROUGH
1733 case TargetLowering::Legal:
1734 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1735 Tmp1 = Result.getValue(0);
1736 Tmp2 = Result.getValue(1);
1737
1738 if (isCustom) {
1739 Tmp3 = TLI.LowerOperation(Result, DAG);
1740 if (Tmp3.Val) {
1741 Tmp1 = LegalizeOp(Tmp3);
1742 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1743 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001744 } else {
1745 // If this is an unaligned load and the target doesn't support it,
1746 // expand it.
1747 if (!TLI.allowsUnalignedMemoryAccesses()) {
1748 unsigned ABIAlignment = TLI.getTargetData()->
1749 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1750 if (LD->getAlignment() < ABIAlignment){
1751 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1752 TLI);
1753 Tmp1 = Result.getOperand(0);
1754 Tmp2 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001755 Tmp1 = LegalizeOp(Tmp1);
1756 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001757 }
1758 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001759 }
1760 break;
1761 case TargetLowering::Expand:
1762 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1763 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1764 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001765 LD->getSrcValueOffset(),
1766 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001767 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1768 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1769 Tmp2 = LegalizeOp(Load.getValue(1));
1770 break;
1771 }
Chris Lattner296a83c2007-02-01 04:55:59 +00001772 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001773 // Turn the unsupported load into an EXTLOAD followed by an explicit
1774 // zero/sign extend inreg.
1775 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1776 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001777 LD->getSrcValueOffset(), SrcVT,
1778 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001779 SDOperand ValRes;
1780 if (ExtType == ISD::SEXTLOAD)
1781 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1782 Result, DAG.getValueType(SrcVT));
1783 else
1784 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1785 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1786 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1787 break;
1788 }
1789 // Since loads produce two values, make sure to remember that we legalized
1790 // both of them.
1791 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1792 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1793 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001794 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001795 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001796 case ISD::EXTRACT_ELEMENT: {
1797 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1798 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001799 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001800 case Legal:
1801 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1802 // 1 -> Hi
1803 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1804 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1805 TLI.getShiftAmountTy()));
1806 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1807 } else {
1808 // 0 -> Lo
1809 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1810 Node->getOperand(0));
1811 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001812 break;
1813 case Expand:
1814 // Get both the low and high parts.
1815 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1816 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1817 Result = Tmp2; // 1 -> Hi
1818 else
1819 Result = Tmp1; // 0 -> Lo
1820 break;
1821 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001822 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001823 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001824
1825 case ISD::CopyToReg:
1826 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001827
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001828 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001829 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001830 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001831 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001832 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001833 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001834 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001835 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001836 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001837 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001838 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1839 Tmp3);
1840 } else {
1841 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001842 }
1843
1844 // Since this produces two values, make sure to remember that we legalized
1845 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001846 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001847 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001848 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001849 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001850 break;
1851
1852 case ISD::RET:
1853 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001854
1855 // Ensure that libcalls are emitted before a return.
1856 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1857 Tmp1 = LegalizeOp(Tmp1);
1858 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001859
Chris Lattnerdc750592005-01-07 07:47:09 +00001860 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001861 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001862 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001863 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001864 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001865 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001866 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001867 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001868 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +00001869 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001870 SDOperand Lo, Hi;
1871 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00001872
1873 // Big endian systems want the hi reg first.
1874 if (!TLI.isLittleEndian())
1875 std::swap(Lo, Hi);
1876
Evan Cheng3432ab92006-12-11 19:27:14 +00001877 if (Hi.Val)
1878 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1879 else
1880 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001881 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001882 } else {
1883 SDNode *InVal = Tmp2.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00001884 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1885 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001886
Dan Gohmana8665142007-06-25 16:23:39 +00001887 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001888 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001889 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00001890 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001891 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00001892 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001893 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001894 } else if (NumElems == 1) {
1895 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00001896 Tmp2 = ScalarizeVectorOp(Tmp2);
1897 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001898 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001899
1900 // FIXME: Returns of gcc generic vectors smaller than a legal type
1901 // should be returned in integer registers!
1902
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001903 // The scalarized value type may not be legal, e.g. it might require
1904 // promotion or expansion. Relegalize the return.
1905 Result = LegalizeOp(Result);
1906 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001907 // FIXME: Returns of gcc generic vectors larger than a legal vector
1908 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001909 SDOperand Lo, Hi;
1910 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00001911 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001912 Result = LegalizeOp(Result);
1913 }
1914 }
Misha Brukman835702a2005-04-21 22:36:52 +00001915 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001916 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001917 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00001918 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001919 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001920 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001921 }
1922 break;
1923 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001924 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001925 break;
1926 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00001927 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00001928 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00001929 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00001930 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1931 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001932 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00001933 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001934 break;
1935 case Expand: {
1936 SDOperand Lo, Hi;
Dan Gohman3b62d722007-06-27 16:08:04 +00001937 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001938 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001939 ExpandOp(Node->getOperand(i), Lo, Hi);
1940 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00001941 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00001942 if (Hi.Val) {
1943 NewValues.push_back(Hi);
1944 NewValues.push_back(Node->getOperand(i+1));
1945 }
Misha Brukman835702a2005-04-21 22:36:52 +00001946 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001947 }
1948 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001949 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001950 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001951
1952 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00001953 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00001954 else
Chris Lattner97af9d52006-08-08 01:09:31 +00001955 Result = DAG.getNode(ISD::RET, MVT::Other,
1956 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00001957 break;
1958 }
1959 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001960
Chris Lattner4d1ea712006-01-29 21:02:23 +00001961 if (Result.getOpcode() == ISD::RET) {
1962 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
1963 default: assert(0 && "This action is not supported yet!");
1964 case TargetLowering::Legal: break;
1965 case TargetLowering::Custom:
1966 Tmp1 = TLI.LowerOperation(Result, DAG);
1967 if (Tmp1.Val) Result = Tmp1;
1968 break;
1969 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001970 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001971 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001972 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00001973 StoreSDNode *ST = cast<StoreSDNode>(Node);
1974 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
1975 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00001976 int SVOffset = ST->getSrcValueOffset();
1977 unsigned Alignment = ST->getAlignment();
1978 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00001979
Evan Chengab51cf22006-10-13 21:14:26 +00001980 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001981 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
1982 // FIXME: We shouldn't do this for TargetConstantFP's.
1983 // FIXME: move this to the DAG Combiner! Note that we can't regress due
1984 // to phase ordering between legalized code and the dag combiner. This
1985 // probably means that we need to integrate dag combiner and legalizer
1986 // together.
Dale Johannesen98d3a082007-09-14 22:26:36 +00001987 // We generally can't do this one for long doubles.
1988 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00001989 if (CFP->getValueType(0) == MVT::f32) {
Dale Johannesen028084e2007-09-12 03:30:33 +00001990 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
1991 convertToAPInt().getZExtValue(),
Dale Johannesen245dceb2007-09-11 18:32:33 +00001992 MVT::i32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00001993 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
1994 SVOffset, isVolatile, Alignment);
1995 break;
1996 } else if (CFP->getValueType(0) == MVT::f64) {
Dale Johannesen028084e2007-09-12 03:30:33 +00001997 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
1998 getZExtValue(), MVT::i64);
Dale Johannesen98d3a082007-09-14 22:26:36 +00001999 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2000 SVOffset, isVolatile, Alignment);
2001 break;
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002002 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002003 }
2004
Evan Chengab51cf22006-10-13 21:14:26 +00002005 switch (getTypeAction(ST->getStoredVT())) {
2006 case Legal: {
2007 Tmp3 = LegalizeOp(ST->getValue());
2008 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2009 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00002010
Evan Chengab51cf22006-10-13 21:14:26 +00002011 MVT::ValueType VT = Tmp3.getValueType();
2012 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2013 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002014 case TargetLowering::Legal:
2015 // If this is an unaligned store and the target doesn't support it,
2016 // expand it.
2017 if (!TLI.allowsUnalignedMemoryAccesses()) {
2018 unsigned ABIAlignment = TLI.getTargetData()->
2019 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2020 if (ST->getAlignment() < ABIAlignment)
2021 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2022 TLI);
2023 }
2024 break;
Evan Chengab51cf22006-10-13 21:14:26 +00002025 case TargetLowering::Custom:
2026 Tmp1 = TLI.LowerOperation(Result, DAG);
2027 if (Tmp1.Val) Result = Tmp1;
2028 break;
2029 case TargetLowering::Promote:
2030 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2031 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2032 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2033 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00002034 ST->getSrcValue(), SVOffset, isVolatile,
2035 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002036 break;
2037 }
2038 break;
2039 }
2040 case Promote:
2041 // Truncate the value and store the result.
2042 Tmp3 = PromoteOp(ST->getValue());
2043 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002044 SVOffset, ST->getStoredVT(),
2045 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002046 break;
2047
2048 case Expand:
2049 unsigned IncrementSize = 0;
2050 SDOperand Lo, Hi;
2051
2052 // If this is a vector type, then we have to calculate the increment as
2053 // the product of the element size in bytes, and the number of elements
2054 // in the high half of the vector.
Dan Gohmana8665142007-06-25 16:23:39 +00002055 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002056 SDNode *InVal = ST->getValue().Val;
Dan Gohmana8665142007-06-25 16:23:39 +00002057 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
2058 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Chengab51cf22006-10-13 21:14:26 +00002059
Dan Gohmana8665142007-06-25 16:23:39 +00002060 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002061 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00002062 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002063 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002064 // Turn this into a normal store of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002065 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00002066 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002067 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002068 Result = LegalizeOp(Result);
2069 break;
2070 } else if (NumElems == 1) {
2071 // Turn this into a normal store of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002072 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00002073 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002074 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002075 // The scalarized value type may not be legal, e.g. it might require
2076 // promotion or expansion. Relegalize the scalar store.
2077 Result = LegalizeOp(Result);
2078 break;
2079 } else {
2080 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2081 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2082 }
2083 } else {
2084 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00002085 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002086
2087 if (!TLI.isLittleEndian())
2088 std::swap(Lo, Hi);
2089 }
2090
2091 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002092 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002093
2094 if (Hi.Val == NULL) {
2095 // Must be int <-> float one-to-one expansion.
2096 Result = Lo;
2097 break;
2098 }
2099
Evan Chengab51cf22006-10-13 21:14:26 +00002100 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2101 getIntPtrConstant(IncrementSize));
2102 assert(isTypeLegal(Tmp2.getValueType()) &&
2103 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002104 SVOffset += IncrementSize;
2105 if (Alignment > IncrementSize)
2106 Alignment = IncrementSize;
Evan Chengab51cf22006-10-13 21:14:26 +00002107 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002108 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002109 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2110 break;
2111 }
2112 } else {
2113 // Truncating store
2114 assert(isTypeLegal(ST->getValue().getValueType()) &&
2115 "Cannot handle illegal TRUNCSTORE yet!");
2116 Tmp3 = LegalizeOp(ST->getValue());
2117
2118 // The only promote case we handle is TRUNCSTORE:i1 X into
2119 // -> TRUNCSTORE:i8 (and X, 1)
2120 if (ST->getStoredVT() == MVT::i1 &&
2121 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2122 // Promote the bool to a mask then store.
2123 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2124 DAG.getConstant(1, Tmp3.getValueType()));
2125 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002126 SVOffset, MVT::i8,
2127 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002128 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2129 Tmp2 != ST->getBasePtr()) {
2130 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2131 ST->getOffset());
2132 }
2133
2134 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2135 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002136 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002137 case TargetLowering::Legal:
2138 // If this is an unaligned store and the target doesn't support it,
2139 // expand it.
2140 if (!TLI.allowsUnalignedMemoryAccesses()) {
2141 unsigned ABIAlignment = TLI.getTargetData()->
2142 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2143 if (ST->getAlignment() < ABIAlignment)
2144 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2145 TLI);
2146 }
2147 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002148 case TargetLowering::Custom:
2149 Tmp1 = TLI.LowerOperation(Result, DAG);
2150 if (Tmp1.Val) Result = Tmp1;
2151 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002152 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002153 }
2154 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002155 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002156 case ISD::PCMARKER:
2157 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002158 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002159 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002160 case ISD::STACKSAVE:
2161 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002162 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2163 Tmp1 = Result.getValue(0);
2164 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002165
Chris Lattnerb3266452006-01-13 02:50:02 +00002166 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2167 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002168 case TargetLowering::Legal: break;
2169 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002170 Tmp3 = TLI.LowerOperation(Result, DAG);
2171 if (Tmp3.Val) {
2172 Tmp1 = LegalizeOp(Tmp3);
2173 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002174 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002175 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002176 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002177 // Expand to CopyFromReg if the target set
2178 // StackPointerRegisterToSaveRestore.
2179 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002180 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002181 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002182 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002183 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002184 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2185 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002186 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002187 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002188 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002189
2190 // Since stacksave produce two values, make sure to remember that we
2191 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002192 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2193 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2194 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002195
Chris Lattnerb3266452006-01-13 02:50:02 +00002196 case ISD::STACKRESTORE:
2197 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2198 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002199 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002200
2201 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2202 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002203 case TargetLowering::Legal: break;
2204 case TargetLowering::Custom:
2205 Tmp1 = TLI.LowerOperation(Result, DAG);
2206 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002207 break;
2208 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002209 // Expand to CopyToReg if the target set
2210 // StackPointerRegisterToSaveRestore.
2211 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2212 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2213 } else {
2214 Result = Tmp1;
2215 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002216 break;
2217 }
2218 break;
2219
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002220 case ISD::READCYCLECOUNTER:
2221 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002222 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002223 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2224 Node->getValueType(0))) {
2225 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002226 case TargetLowering::Legal:
2227 Tmp1 = Result.getValue(0);
2228 Tmp2 = Result.getValue(1);
2229 break;
Evan Cheng69739932006-11-29 08:26:18 +00002230 case TargetLowering::Custom:
2231 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002232 Tmp1 = LegalizeOp(Result.getValue(0));
2233 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002234 break;
2235 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002236
2237 // Since rdcc produce two values, make sure to remember that we legalized
2238 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002239 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2240 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002241 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002242
Chris Lattner39c67442005-01-14 22:08:15 +00002243 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002244 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2245 case Expand: assert(0 && "It's impossible to expand bools");
2246 case Legal:
2247 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2248 break;
2249 case Promote:
2250 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002251 // Make sure the condition is either zero or one.
Dan Gohman309d3d52007-06-22 14:59:07 +00002252 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattner3abb6362006-11-28 01:03:30 +00002253 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2254 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002255 break;
2256 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002257 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002258 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002259
Chris Lattnerd02b0542006-01-28 10:58:55 +00002260 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002261
Nate Begeman987121a2005-08-23 04:29:48 +00002262 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002263 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002264 case TargetLowering::Legal: break;
2265 case TargetLowering::Custom: {
2266 Tmp1 = TLI.LowerOperation(Result, DAG);
2267 if (Tmp1.Val) Result = Tmp1;
2268 break;
2269 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002270 case TargetLowering::Expand:
2271 if (Tmp1.getOpcode() == ISD::SETCC) {
2272 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2273 Tmp2, Tmp3,
2274 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2275 } else {
2276 Result = DAG.getSelectCC(Tmp1,
2277 DAG.getConstant(0, Tmp1.getValueType()),
2278 Tmp2, Tmp3, ISD::SETNE);
2279 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002280 break;
2281 case TargetLowering::Promote: {
2282 MVT::ValueType NVT =
2283 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2284 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002285 if (MVT::isVector(Tmp2.getValueType())) {
2286 ExtOp = ISD::BIT_CONVERT;
2287 TruncOp = ISD::BIT_CONVERT;
2288 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002289 ExtOp = ISD::ANY_EXTEND;
2290 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002291 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002292 ExtOp = ISD::FP_EXTEND;
2293 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002294 }
2295 // Promote each of the values to the new type.
2296 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2297 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2298 // Perform the larger operation, then round down.
2299 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2300 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2301 break;
2302 }
2303 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002304 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002305 case ISD::SELECT_CC: {
2306 Tmp1 = Node->getOperand(0); // LHS
2307 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002308 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2309 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002310 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002311
Nate Begeman7e7f4392006-02-01 07:19:44 +00002312 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2313
2314 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2315 // the LHS is a legal SETCC itself. In this case, we need to compare
2316 // the result against zero to select between true and false values.
2317 if (Tmp2.Val == 0) {
2318 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2319 CC = DAG.getCondCode(ISD::SETNE);
2320 }
2321 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2322
2323 // Everything is legal, see if we should expand this op or something.
2324 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2325 default: assert(0 && "This action is not supported yet!");
2326 case TargetLowering::Legal: break;
2327 case TargetLowering::Custom:
2328 Tmp1 = TLI.LowerOperation(Result, DAG);
2329 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002330 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002331 }
2332 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002333 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002334 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002335 Tmp1 = Node->getOperand(0);
2336 Tmp2 = Node->getOperand(1);
2337 Tmp3 = Node->getOperand(2);
2338 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2339
2340 // If we had to Expand the SetCC operands into a SELECT node, then it may
2341 // not always be possible to return a true LHS & RHS. In this case, just
2342 // return the value we legalized, returned in the LHS
2343 if (Tmp2.Val == 0) {
2344 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002345 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002346 }
Nate Begeman987121a2005-08-23 04:29:48 +00002347
Chris Lattnerf263a232006-01-30 22:43:50 +00002348 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002349 default: assert(0 && "Cannot handle this action for SETCC yet!");
2350 case TargetLowering::Custom:
2351 isCustom = true;
2352 // FALLTHROUGH.
2353 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002354 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002355 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002356 Tmp4 = TLI.LowerOperation(Result, DAG);
2357 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002358 }
Nate Begeman987121a2005-08-23 04:29:48 +00002359 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002360 case TargetLowering::Promote: {
2361 // First step, figure out the appropriate operation to use.
2362 // Allow SETCC to not be supported for all legal data types
2363 // Mostly this targets FP
2364 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002365 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002366
2367 // Scan for the appropriate larger type to use.
2368 while (1) {
2369 NewInTy = (MVT::ValueType)(NewInTy+1);
2370
2371 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2372 "Fell off of the edge of the integer world");
2373 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2374 "Fell off of the edge of the floating point world");
2375
2376 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002377 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002378 break;
2379 }
2380 if (MVT::isInteger(NewInTy))
2381 assert(0 && "Cannot promote Legal Integer SETCC yet");
2382 else {
2383 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2384 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2385 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002386 Tmp1 = LegalizeOp(Tmp1);
2387 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002388 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002389 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002390 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002391 }
Nate Begeman987121a2005-08-23 04:29:48 +00002392 case TargetLowering::Expand:
2393 // Expand a setcc node into a select_cc of the same condition, lhs, and
2394 // rhs that selects between const 1 (true) and const 0 (false).
2395 MVT::ValueType VT = Node->getValueType(0);
2396 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2397 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002398 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002399 break;
2400 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002401 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002402 case ISD::MEMSET:
2403 case ISD::MEMCPY:
2404 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002405 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002406 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2407
2408 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2409 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2410 case Expand: assert(0 && "Cannot expand a byte!");
2411 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002412 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002413 break;
2414 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002415 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002416 break;
2417 }
2418 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002419 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002420 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002421
2422 SDOperand Tmp4;
2423 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002424 case Expand: {
2425 // Length is too big, just take the lo-part of the length.
2426 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002427 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002428 break;
2429 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002430 case Legal:
2431 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002432 break;
2433 case Promote:
2434 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002435 break;
2436 }
2437
2438 SDOperand Tmp5;
2439 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2440 case Expand: assert(0 && "Cannot expand this yet!");
2441 case Legal:
2442 Tmp5 = LegalizeOp(Node->getOperand(4));
2443 break;
2444 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002445 Tmp5 = PromoteOp(Node->getOperand(4));
2446 break;
2447 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002448
2449 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2450 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002451 case TargetLowering::Custom:
2452 isCustom = true;
2453 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00002454 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002455 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002456 if (isCustom) {
2457 Tmp1 = TLI.LowerOperation(Result, DAG);
2458 if (Tmp1.Val) Result = Tmp1;
2459 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002460 break;
2461 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002462 // Otherwise, the target does not support this operation. Lower the
2463 // operation to an explicit libcall as appropriate.
2464 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002465 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002466 TargetLowering::ArgListTy Args;
2467 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002468
Reid Spencer6dced922005-01-12 14:53:45 +00002469 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002470 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002471 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00002472 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002473 // Extend the (previously legalized) ubyte argument to be an int value
2474 // for the call.
2475 if (Tmp3.getValueType() > MVT::i32)
2476 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2477 else
2478 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002479 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencere63b6512006-12-31 05:55:36 +00002480 Args.push_back(Entry);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002481 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002482 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002483
2484 FnName = "memset";
2485 } else if (Node->getOpcode() == ISD::MEMCPY ||
2486 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002487 Entry.Ty = IntPtrTy;
Reid Spencer791864c2007-01-03 04:22:32 +00002488 Entry.Node = Tmp2; Args.push_back(Entry);
2489 Entry.Node = Tmp3; Args.push_back(Entry);
2490 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002491 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2492 } else {
2493 assert(0 && "Unknown op!");
2494 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002495
Chris Lattner85d70c62005-01-11 05:57:22 +00002496 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002497 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002498 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002499 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002500 break;
2501 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002502 }
2503 break;
2504 }
Chris Lattner5385db52005-05-09 20:23:03 +00002505
Chris Lattner4157c412005-04-02 04:00:59 +00002506 case ISD::SHL_PARTS:
2507 case ISD::SRA_PARTS:
2508 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002509 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002510 bool Changed = false;
2511 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2512 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2513 Changed |= Ops.back() != Node->getOperand(i);
2514 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002515 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002516 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002517
Evan Cheng870e4f82006-01-09 18:31:59 +00002518 switch (TLI.getOperationAction(Node->getOpcode(),
2519 Node->getValueType(0))) {
2520 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002521 case TargetLowering::Legal: break;
2522 case TargetLowering::Custom:
2523 Tmp1 = TLI.LowerOperation(Result, DAG);
2524 if (Tmp1.Val) {
2525 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002526 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002527 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002528 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2529 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002530 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002531 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002532 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002533 return RetVal;
2534 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002535 break;
2536 }
2537
Chris Lattner13fe99c2005-04-02 05:00:07 +00002538 // Since these produce multiple values, make sure to remember that we
2539 // legalized all of them.
2540 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2541 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2542 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002543 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002544
2545 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002546 case ISD::ADD:
2547 case ISD::SUB:
2548 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002549 case ISD::MULHS:
2550 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002551 case ISD::UDIV:
2552 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002553 case ISD::AND:
2554 case ISD::OR:
2555 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002556 case ISD::SHL:
2557 case ISD::SRL:
2558 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002559 case ISD::FADD:
2560 case ISD::FSUB:
2561 case ISD::FMUL:
2562 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002563 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002564 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2565 case Expand: assert(0 && "Not possible");
2566 case Legal:
2567 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2568 break;
2569 case Promote:
2570 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2571 break;
2572 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002573
2574 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002575
Andrew Lenharth72594262005-12-24 23:42:32 +00002576 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002577 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002578 case TargetLowering::Legal: break;
2579 case TargetLowering::Custom:
2580 Tmp1 = TLI.LowerOperation(Result, DAG);
2581 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002582 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002583 case TargetLowering::Expand: {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002584 if (Node->getValueType(0) == MVT::i32) {
2585 switch (Node->getOpcode()) {
2586 default: assert(0 && "Do not know how to expand this integer BinOp!");
2587 case ISD::UDIV:
2588 case ISD::SDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00002589 RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV
2590 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002591 SDOperand Dummy;
Reid Spencere63b6512006-12-31 05:55:36 +00002592 bool isSigned = Node->getOpcode() == ISD::SDIV;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002593 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002594 };
2595 break;
2596 }
2597
Chris Lattner87f08092006-04-02 03:57:31 +00002598 assert(MVT::isVector(Node->getValueType(0)) &&
2599 "Cannot expand this binary operator!");
2600 // Expand the operation into a bunch of nasty scalar code.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002601 SmallVector<SDOperand, 8> Ops;
Dan Gohman5c441312007-06-14 22:58:02 +00002602 MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0));
Chris Lattner87f08092006-04-02 03:57:31 +00002603 MVT::ValueType PtrVT = TLI.getPointerTy();
2604 for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0));
2605 i != e; ++i) {
2606 SDOperand Idx = DAG.getConstant(i, PtrVT);
2607 SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx);
2608 SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx);
2609 Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS));
2610 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002611 Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
2612 &Ops[0], Ops.size());
Chris Lattner87f08092006-04-02 03:57:31 +00002613 break;
2614 }
Evan Cheng119266e2006-04-12 21:20:24 +00002615 case TargetLowering::Promote: {
2616 switch (Node->getOpcode()) {
2617 default: assert(0 && "Do not know how to promote this BinOp!");
2618 case ISD::AND:
2619 case ISD::OR:
2620 case ISD::XOR: {
2621 MVT::ValueType OVT = Node->getValueType(0);
2622 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2623 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2624 // Bit convert each of the values to the new type.
2625 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2626 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2627 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2628 // Bit convert the result back the original type.
2629 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2630 break;
2631 }
2632 }
2633 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002634 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002635 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002636
2637 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2638 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2639 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2640 case Expand: assert(0 && "Not possible");
2641 case Legal:
2642 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2643 break;
2644 case Promote:
2645 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2646 break;
2647 }
2648
2649 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2650
2651 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2652 default: assert(0 && "Operation not supported");
2653 case TargetLowering::Custom:
2654 Tmp1 = TLI.LowerOperation(Result, DAG);
2655 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002656 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002657 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002658 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00002659 // If this target supports fabs/fneg natively and select is cheap,
2660 // do this efficiently.
2661 if (!TLI.isSelectExpensive() &&
2662 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2663 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00002664 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00002665 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002666 // Get the sign bit of the RHS.
2667 MVT::ValueType IVT =
2668 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2669 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2670 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2671 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2672 // Get the absolute value of the result.
2673 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2674 // Select between the nabs and abs value based on the sign bit of
2675 // the input.
2676 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2677 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2678 AbsVal),
2679 AbsVal);
2680 Result = LegalizeOp(Result);
2681 break;
2682 }
2683
2684 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002685 MVT::ValueType NVT =
2686 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2687 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2688 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2689 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002690 break;
2691 }
Evan Cheng003feb02007-01-04 21:56:39 +00002692 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002693 break;
2694
Nate Begeman5965bd12006-02-17 05:43:56 +00002695 case ISD::ADDC:
2696 case ISD::SUBC:
2697 Tmp1 = LegalizeOp(Node->getOperand(0));
2698 Tmp2 = LegalizeOp(Node->getOperand(1));
2699 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2700 // Since this produces two values, make sure to remember that we legalized
2701 // both of them.
2702 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2703 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2704 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002705
Nate Begeman5965bd12006-02-17 05:43:56 +00002706 case ISD::ADDE:
2707 case ISD::SUBE:
2708 Tmp1 = LegalizeOp(Node->getOperand(0));
2709 Tmp2 = LegalizeOp(Node->getOperand(1));
2710 Tmp3 = LegalizeOp(Node->getOperand(2));
2711 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2712 // Since this produces two values, make sure to remember that we legalized
2713 // both of them.
2714 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2715 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2716 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002717
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002718 case ISD::BUILD_PAIR: {
2719 MVT::ValueType PairTy = Node->getValueType(0);
2720 // TODO: handle the case where the Lo and Hi operands are not of legal type
2721 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2722 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2723 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002724 case TargetLowering::Promote:
2725 case TargetLowering::Custom:
2726 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002727 case TargetLowering::Legal:
2728 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2729 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2730 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002731 case TargetLowering::Expand:
2732 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2733 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2734 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2735 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2736 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002737 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002738 break;
2739 }
2740 break;
2741 }
2742
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002743 case ISD::UREM:
2744 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002745 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002746 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2747 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002748
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002749 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002750 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2751 case TargetLowering::Custom:
2752 isCustom = true;
2753 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002754 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002755 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002756 if (isCustom) {
2757 Tmp1 = TLI.LowerOperation(Result, DAG);
2758 if (Tmp1.Val) Result = Tmp1;
2759 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002760 break;
Chris Lattner81914422005-08-03 20:31:37 +00002761 case TargetLowering::Expand:
Evan Cheng1fc7c362006-09-18 23:28:33 +00002762 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002763 bool isSigned = DivOpc == ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00002764 if (MVT::isInteger(Node->getValueType(0))) {
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002765 if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) ==
2766 TargetLowering::Legal) {
2767 // X % Y -> X-X/Y*Y
2768 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng1fc7c362006-09-18 23:28:33 +00002769 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002770 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2771 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2772 } else {
2773 assert(Node->getValueType(0) == MVT::i32 &&
2774 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00002775 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2776 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002777 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002778 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002779 }
Chris Lattner81914422005-08-03 20:31:37 +00002780 } else {
2781 // Floating point mod -> fmod libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00002782 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
2783 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner81914422005-08-03 20:31:37 +00002784 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002785 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2786 false/*sign irrelevant*/, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002787 }
2788 break;
2789 }
2790 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002791 case ISD::VAARG: {
2792 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2793 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2794
Chris Lattner364b89a2006-01-28 07:42:08 +00002795 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002796 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2797 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002798 case TargetLowering::Custom:
2799 isCustom = true;
2800 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002801 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002802 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2803 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002804 Tmp1 = Result.getValue(1);
2805
2806 if (isCustom) {
2807 Tmp2 = TLI.LowerOperation(Result, DAG);
2808 if (Tmp2.Val) {
2809 Result = LegalizeOp(Tmp2);
2810 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2811 }
2812 }
Nate Begemane74795c2006-01-25 18:21:52 +00002813 break;
2814 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002815 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00002816 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00002817 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002818 // Increment the pointer, VAList, to the next vaarg
2819 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2820 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2821 TLI.getPointerTy()));
2822 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00002823 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2824 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002825 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00002826 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002827 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002828 Result = LegalizeOp(Result);
2829 break;
2830 }
2831 }
2832 // Since VAARG produces two values, make sure to remember that we
2833 // legalized both of them.
2834 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002835 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2836 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002837 }
2838
2839 case ISD::VACOPY:
2840 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2841 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2842 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2843
2844 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2845 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002846 case TargetLowering::Custom:
2847 isCustom = true;
2848 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002849 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002850 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
2851 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002852 if (isCustom) {
2853 Tmp1 = TLI.LowerOperation(Result, DAG);
2854 if (Tmp1.Val) Result = Tmp1;
2855 }
Nate Begemane74795c2006-01-25 18:21:52 +00002856 break;
2857 case TargetLowering::Expand:
2858 // This defaults to loading a pointer from the input and storing it to the
2859 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002860 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00002861 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00002862 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
2863 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00002864 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
2865 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002866 break;
2867 }
2868 break;
2869
2870 case ISD::VAEND:
2871 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2872 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2873
2874 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
2875 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002876 case TargetLowering::Custom:
2877 isCustom = true;
2878 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002879 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002880 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002881 if (isCustom) {
2882 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
2883 if (Tmp1.Val) Result = Tmp1;
2884 }
Nate Begemane74795c2006-01-25 18:21:52 +00002885 break;
2886 case TargetLowering::Expand:
2887 Result = Tmp1; // Default to a no-op, return the chain
2888 break;
2889 }
2890 break;
2891
2892 case ISD::VASTART:
2893 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2894 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2895
Chris Lattnerd02b0542006-01-28 10:58:55 +00002896 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2897
Nate Begemane74795c2006-01-25 18:21:52 +00002898 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
2899 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002900 case TargetLowering::Legal: break;
2901 case TargetLowering::Custom:
2902 Tmp1 = TLI.LowerOperation(Result, DAG);
2903 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00002904 break;
2905 }
2906 break;
2907
Nate Begeman1b8121b2006-01-11 21:21:00 +00002908 case ISD::ROTL:
2909 case ISD::ROTR:
2910 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2911 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00002912 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00002913 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2914 default:
2915 assert(0 && "ROTL/ROTR legalize operation not supported");
2916 break;
2917 case TargetLowering::Legal:
2918 break;
2919 case TargetLowering::Custom:
2920 Tmp1 = TLI.LowerOperation(Result, DAG);
2921 if (Tmp1.Val) Result = Tmp1;
2922 break;
2923 case TargetLowering::Promote:
2924 assert(0 && "Do not know how to promote ROTL/ROTR");
2925 break;
2926 case TargetLowering::Expand:
2927 assert(0 && "Do not know how to expand ROTL/ROTR");
2928 break;
2929 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00002930 break;
2931
Nate Begeman2fba8a32006-01-14 03:14:10 +00002932 case ISD::BSWAP:
2933 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2934 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002935 case TargetLowering::Custom:
2936 assert(0 && "Cannot custom legalize this yet!");
2937 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002938 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002939 break;
2940 case TargetLowering::Promote: {
2941 MVT::ValueType OVT = Tmp1.getValueType();
2942 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohman1796f1f2007-05-18 17:52:13 +00002943 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00002944
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002945 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2946 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2947 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2948 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
2949 break;
2950 }
2951 case TargetLowering::Expand:
2952 Result = ExpandBSWAP(Tmp1);
2953 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002954 }
2955 break;
2956
Andrew Lenharth5e177822005-05-03 17:19:30 +00002957 case ISD::CTPOP:
2958 case ISD::CTTZ:
2959 case ISD::CTLZ:
2960 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
2961 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00002962 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00002963 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002964 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00002965 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00002966 TargetLowering::Custom) {
2967 Tmp1 = TLI.LowerOperation(Result, DAG);
2968 if (Tmp1.Val) {
2969 Result = Tmp1;
2970 }
Scott Michel34e2d222007-07-30 21:00:31 +00002971 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00002972 break;
2973 case TargetLowering::Promote: {
2974 MVT::ValueType OVT = Tmp1.getValueType();
2975 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00002976
2977 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00002978 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2979 // Perform the larger operation, then subtract if needed.
2980 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002981 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00002982 case ISD::CTPOP:
2983 Result = Tmp1;
2984 break;
2985 case ISD::CTTZ:
2986 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00002987 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00002988 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00002989 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002990 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel34e2d222007-07-30 21:00:31 +00002991 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00002992 break;
2993 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002994 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002995 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00002996 DAG.getConstant(MVT::getSizeInBits(NVT) -
2997 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00002998 break;
2999 }
3000 break;
3001 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003002 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003003 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003004 break;
3005 }
3006 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003007
Chris Lattner13fe99c2005-04-02 05:00:07 +00003008 // Unary operators
3009 case ISD::FABS:
3010 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00003011 case ISD::FSQRT:
3012 case ISD::FSIN:
3013 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003014 Tmp1 = LegalizeOp(Node->getOperand(0));
3015 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003016 case TargetLowering::Promote:
3017 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00003018 isCustom = true;
3019 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00003020 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003021 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00003022 if (isCustom) {
3023 Tmp1 = TLI.LowerOperation(Result, DAG);
3024 if (Tmp1.Val) Result = Tmp1;
3025 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003026 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00003027 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003028 switch (Node->getOpcode()) {
3029 default: assert(0 && "Unreachable!");
3030 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003031 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3032 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003033 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00003034 break;
Chris Lattner80026402005-04-30 04:43:14 +00003035 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003036 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3037 MVT::ValueType VT = Node->getValueType(0);
3038 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003039 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003040 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3041 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00003042 break;
3043 }
3044 case ISD::FSQRT:
3045 case ISD::FSIN:
3046 case ISD::FCOS: {
3047 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003048 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00003049 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00003050 case ISD::FSQRT:
3051 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
3052 break;
3053 case ISD::FSIN:
3054 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3055 break;
3056 case ISD::FCOS:
3057 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3058 break;
Chris Lattner80026402005-04-30 04:43:14 +00003059 default: assert(0 && "Unreachable!");
3060 }
Nate Begeman77558da2005-08-04 21:43:28 +00003061 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003062 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3063 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00003064 break;
3065 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003066 }
3067 break;
3068 }
3069 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003070 case ISD::FPOWI: {
3071 // We always lower FPOWI into a libcall. No target support it yet.
Evan Cheng31cbddf2007-01-12 02:11:51 +00003072 RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32
3073 ? RTLIB::POWI_F32 : RTLIB::POWI_F64;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003074 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003075 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3076 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003077 break;
3078 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003079 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003080 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00003081 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohmana8665142007-06-25 16:23:39 +00003082 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3083 // The input has to be a vector type, we have to either scalarize it, pack
3084 // it, or convert it based on whether the input vector type is legal.
3085 SDNode *InVal = Node->getOperand(0).Val;
3086 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
3087 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
3088
3089 // Figure out if there is a simple type corresponding to this Vector
3090 // type. If so, convert to the vector type.
3091 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3092 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003093 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003094 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3095 LegalizeOp(Node->getOperand(0)));
3096 break;
3097 } else if (NumElems == 1) {
3098 // Turn this into a bit convert of the scalar input.
3099 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3100 ScalarizeVectorOp(Node->getOperand(0)));
3101 break;
3102 } else {
3103 // FIXME: UNIMP! Store then reload
3104 assert(0 && "Cast from unsupported vector type not implemented yet!");
3105 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003106 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003107 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3108 Node->getOperand(0).getValueType())) {
3109 default: assert(0 && "Unknown operation action!");
3110 case TargetLowering::Expand:
3111 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3112 break;
3113 case TargetLowering::Legal:
3114 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003115 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003116 break;
3117 }
3118 }
3119 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00003120
Chris Lattner13fe99c2005-04-02 05:00:07 +00003121 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00003122 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003123 case ISD::UINT_TO_FP: {
3124 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00003125 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3126 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00003127 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003128 Node->getOperand(0).getValueType())) {
3129 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003130 case TargetLowering::Custom:
3131 isCustom = true;
3132 // FALLTHROUGH
3133 case TargetLowering::Legal:
3134 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003135 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003136 if (isCustom) {
3137 Tmp1 = TLI.LowerOperation(Result, DAG);
3138 if (Tmp1.Val) Result = Tmp1;
3139 }
3140 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003141 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00003142 Result = ExpandLegalINT_TO_FP(isSigned,
3143 LegalizeOp(Node->getOperand(0)),
3144 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003145 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003146 case TargetLowering::Promote:
3147 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3148 Node->getValueType(0),
3149 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003150 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00003151 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003152 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00003153 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003154 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3155 Node->getValueType(0), Node->getOperand(0));
3156 break;
3157 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003158 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003159 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003160 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3161 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003162 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003163 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3164 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003165 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003166 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3167 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003168 break;
3169 }
3170 break;
3171 }
3172 case ISD::TRUNCATE:
3173 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3174 case Legal:
3175 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003176 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003177 break;
3178 case Expand:
3179 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3180
3181 // Since the result is legal, we should just be able to truncate the low
3182 // part of the source.
3183 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3184 break;
3185 case Promote:
3186 Result = PromoteOp(Node->getOperand(0));
3187 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3188 break;
3189 }
3190 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003191
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003192 case ISD::FP_TO_SINT:
3193 case ISD::FP_TO_UINT:
3194 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3195 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00003196 Tmp1 = LegalizeOp(Node->getOperand(0));
3197
Chris Lattner44fe26f2005-07-29 00:11:56 +00003198 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3199 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003200 case TargetLowering::Custom:
3201 isCustom = true;
3202 // FALLTHROUGH
3203 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003204 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003205 if (isCustom) {
3206 Tmp1 = TLI.LowerOperation(Result, DAG);
3207 if (Tmp1.Val) Result = Tmp1;
3208 }
3209 break;
3210 case TargetLowering::Promote:
3211 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3212 Node->getOpcode() == ISD::FP_TO_SINT);
3213 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00003214 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00003215 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3216 SDOperand True, False;
3217 MVT::ValueType VT = Node->getOperand(0).getValueType();
3218 MVT::ValueType NVT = Node->getValueType(0);
3219 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
3220 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
3221 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3222 Node->getOperand(0), Tmp2, ISD::SETLT);
3223 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3224 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00003225 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00003226 Tmp2));
3227 False = DAG.getNode(ISD::XOR, NVT, False,
3228 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003229 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3230 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00003231 } else {
3232 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3233 }
3234 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003235 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003236 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003237 case Expand: {
3238 // Convert f32 / f64 to i32 / i64.
3239 MVT::ValueType VT = Op.getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00003240 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003241 switch (Node->getOpcode()) {
3242 case ISD::FP_TO_SINT:
3243 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003244 LC = (VT == MVT::i32)
3245 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003246 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00003247 LC = (VT == MVT::i32)
3248 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003249 break;
3250 case ISD::FP_TO_UINT:
3251 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003252 LC = (VT == MVT::i32)
3253 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003254 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00003255 LC = (VT == MVT::i32)
3256 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003257 break;
3258 default: assert(0 && "Unreachable!");
3259 }
3260 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003261 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3262 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003263 break;
3264 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003265 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003266 Tmp1 = PromoteOp(Node->getOperand(0));
3267 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3268 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003269 break;
3270 }
3271 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003272
Dale Johannesenc339e452007-08-09 17:27:48 +00003273 case ISD::FP_EXTEND:
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003274 case ISD::FP_ROUND: {
3275 MVT::ValueType newVT = Op.getValueType();
3276 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3277 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesenc339e452007-08-09 17:27:48 +00003278 // The only way we can lower this is to turn it into a STORE,
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003279 // LOAD pair, targetting a temporary location (a stack slot).
3280
3281 // NOTE: there is a choice here between constantly creating new stack
3282 // slots and always reusing the same one. We currently always create
3283 // new ones, as reuse may inhibit scheduling.
Dale Johannesenc339e452007-08-09 17:27:48 +00003284 MVT::ValueType slotVT =
3285 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3286 const Type *Ty = MVT::getTypeForValueType(slotVT);
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003287 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3288 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3289 MachineFunction &MF = DAG.getMachineFunction();
3290 int SSFI =
3291 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3292 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesenc339e452007-08-09 17:27:48 +00003293 if (Node->getOpcode() == ISD::FP_EXTEND) {
3294 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3295 StackSlot, NULL, 0);
3296 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3297 Result, StackSlot, NULL, 0, oldVT);
3298 } else {
3299 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3300 StackSlot, NULL, 0, newVT);
3301 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0, newVT);
3302 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003303 break;
3304 }
Dale Johannesena2b3c172007-07-03 00:53:03 +00003305 }
3306 // FALL THROUGH
Chris Lattner7753f172005-09-02 00:18:10 +00003307 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003308 case ISD::ZERO_EXTEND:
3309 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003310 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003311 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003312 case Legal:
3313 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003314 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003315 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003316 case Promote:
3317 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003318 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003319 Tmp1 = PromoteOp(Node->getOperand(0));
3320 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003321 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003322 case ISD::ZERO_EXTEND:
3323 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003324 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003325 Result = DAG.getZeroExtendInReg(Result,
3326 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003327 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003328 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003329 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003330 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003331 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003332 Result,
3333 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003334 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003335 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003336 Result = PromoteOp(Node->getOperand(0));
3337 if (Result.getValueType() != Op.getValueType())
3338 // Dynamically dead while we have only 2 FP types.
3339 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3340 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003341 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00003342 Result = PromoteOp(Node->getOperand(0));
3343 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3344 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003345 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003346 }
3347 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003348 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003349 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003350 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003351 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003352
3353 // If this operation is not supported, convert it to a shl/shr or load/store
3354 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003355 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3356 default: assert(0 && "This action not supported for this op yet!");
3357 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003358 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003359 break;
3360 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003361 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003362 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003363 // NOTE: we could fall back on load/store here too for targets without
3364 // SAR. However, it is doubtful that any exist.
3365 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3366 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003367 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003368 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3369 Node->getOperand(0), ShiftCst);
3370 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3371 Result, ShiftCst);
3372 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003373 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003374 // EXTLOAD pair, targetting a temporary location (a stack slot).
3375
3376 // NOTE: there is a choice here between constantly creating new stack
3377 // slots and always reusing the same one. We currently always create
3378 // new ones, as reuse may inhibit scheduling.
3379 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner1deacd62007-04-28 06:42:38 +00003380 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattner945e4372007-02-14 05:52:17 +00003381 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner99222f72005-01-15 07:15:18 +00003382 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00003383 int SSFI =
Chris Lattner1deacd62007-04-28 06:42:38 +00003384 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner99222f72005-01-15 07:15:18 +00003385 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003386 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3387 StackSlot, NULL, 0, ExtraVT);
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003388 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003389 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00003390 } else {
3391 assert(0 && "Unknown op");
3392 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003393 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003394 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003395 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003396 }
Duncan Sands644f9172007-07-27 12:58:54 +00003397 case ISD::TRAMPOLINE: {
3398 SDOperand Ops[6];
3399 for (unsigned i = 0; i != 6; ++i)
3400 Ops[i] = LegalizeOp(Node->getOperand(i));
3401 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3402 // The only option for this node is to custom lower it.
3403 Result = TLI.LowerOperation(Result, DAG);
3404 assert(Result.Val && "Should always custom lower!");
Duncan Sands86e01192007-09-11 14:10:23 +00003405
3406 // Since trampoline produces two values, make sure to remember that we
3407 // legalized both of them.
3408 Tmp1 = LegalizeOp(Result.getValue(1));
3409 Result = LegalizeOp(Result);
3410 AddLegalizedOperand(SDOperand(Node, 0), Result);
3411 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3412 return Op.ResNo ? Tmp1 : Result;
Duncan Sands644f9172007-07-27 12:58:54 +00003413 }
Chris Lattner99222f72005-01-15 07:15:18 +00003414 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003415
Chris Lattner101ea662006-04-08 04:13:17 +00003416 assert(Result.getValueType() == Op.getValueType() &&
3417 "Bad legalization!");
3418
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003419 // Make sure that the generated code is itself legal.
3420 if (Result != Op)
3421 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003422
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003423 // Note that LegalizeOp may be reentered even from single-use nodes, which
3424 // means that we always must cache transformed nodes.
3425 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003426 return Result;
3427}
3428
Chris Lattner4d978642005-01-15 22:16:26 +00003429/// PromoteOp - Given an operation that produces a value in an invalid type,
3430/// promote it to compute the value into a larger type. The produced value will
3431/// have the correct bits for the low portion of the register, but no guarantee
3432/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003433SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3434 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003435 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003436 assert(getTypeAction(VT) == Promote &&
3437 "Caller should expand or legalize operands that are not promotable!");
3438 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3439 "Cannot promote to smaller type!");
3440
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003441 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003442 SDOperand Result;
3443 SDNode *Node = Op.Val;
3444
Chris Lattner4b0ddb22007-02-04 01:17:38 +00003445 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00003446 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003447
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003448 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003449 case ISD::CopyFromReg:
3450 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003451 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003452#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00003453 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003454#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003455 assert(0 && "Do not know how to promote this operator!");
3456 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003457 case ISD::UNDEF:
3458 Result = DAG.getNode(ISD::UNDEF, NVT);
3459 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003460 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003461 if (VT != MVT::i1)
3462 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3463 else
3464 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003465 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3466 break;
3467 case ISD::ConstantFP:
3468 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3469 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3470 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003471
Chris Lattner2cb338d2005-01-18 02:59:52 +00003472 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003473 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003474 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3475 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003476 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003477
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003478 case ISD::TRUNCATE:
3479 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3480 case Legal:
3481 Result = LegalizeOp(Node->getOperand(0));
3482 assert(Result.getValueType() >= NVT &&
3483 "This truncation doesn't make sense!");
3484 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3485 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3486 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003487 case Promote:
3488 // The truncation is not required, because we don't guarantee anything
3489 // about high bits anyway.
3490 Result = PromoteOp(Node->getOperand(0));
3491 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003492 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003493 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3494 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003495 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003496 }
3497 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003498 case ISD::SIGN_EXTEND:
3499 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003500 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003501 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3502 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3503 case Legal:
3504 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003505 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003506 break;
3507 case Promote:
3508 // Promote the reg if it's smaller.
3509 Result = PromoteOp(Node->getOperand(0));
3510 // The high bits are not guaranteed to be anything. Insert an extend.
3511 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003512 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003513 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003514 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003515 Result = DAG.getZeroExtendInReg(Result,
3516 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003517 break;
3518 }
3519 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003520 case ISD::BIT_CONVERT:
3521 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3522 Result = PromoteOp(Result);
3523 break;
3524
Chris Lattner4d978642005-01-15 22:16:26 +00003525 case ISD::FP_EXTEND:
3526 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3527 case ISD::FP_ROUND:
3528 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3529 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3530 case Promote: assert(0 && "Unreachable with 2 FP types!");
3531 case Legal:
3532 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003533 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003534 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003535 break;
3536 }
3537 break;
3538
3539 case ISD::SINT_TO_FP:
3540 case ISD::UINT_TO_FP:
3541 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3542 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003543 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003544 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003545 break;
3546
3547 case Promote:
3548 Result = PromoteOp(Node->getOperand(0));
3549 if (Node->getOpcode() == ISD::SINT_TO_FP)
3550 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003551 Result,
3552 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003553 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003554 Result = DAG.getZeroExtendInReg(Result,
3555 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003556 // No extra round required here.
3557 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003558 break;
3559 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003560 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3561 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003562 // Round if we cannot tolerate excess precision.
3563 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003564 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3565 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003566 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003567 }
Chris Lattner4d978642005-01-15 22:16:26 +00003568 break;
3569
Chris Lattner268d4572005-12-09 17:32:47 +00003570 case ISD::SIGN_EXTEND_INREG:
3571 Result = PromoteOp(Node->getOperand(0));
3572 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3573 Node->getOperand(1));
3574 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003575 case ISD::FP_TO_SINT:
3576 case ISD::FP_TO_UINT:
3577 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3578 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003579 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003580 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003581 break;
3582 case Promote:
3583 // The input result is prerounded, so we don't have to do anything
3584 // special.
3585 Tmp1 = PromoteOp(Node->getOperand(0));
3586 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003587 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003588 // If we're promoting a UINT to a larger size, check to see if the new node
3589 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3590 // we can use that instead. This allows us to generate better code for
3591 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3592 // legal, such as PowerPC.
3593 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003594 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003595 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3596 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003597 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3598 } else {
3599 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3600 }
Chris Lattner4d978642005-01-15 22:16:26 +00003601 break;
3602
Chris Lattner13fe99c2005-04-02 05:00:07 +00003603 case ISD::FABS:
3604 case ISD::FNEG:
3605 Tmp1 = PromoteOp(Node->getOperand(0));
3606 assert(Tmp1.getValueType() == NVT);
3607 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3608 // NOTE: we do not have to do any extra rounding here for
3609 // NoExcessFPPrecision, because we know the input will have the appropriate
3610 // precision, and these operations don't modify precision at all.
3611 break;
3612
Chris Lattner9d6fa982005-04-28 21:44:33 +00003613 case ISD::FSQRT:
3614 case ISD::FSIN:
3615 case ISD::FCOS:
3616 Tmp1 = PromoteOp(Node->getOperand(0));
3617 assert(Tmp1.getValueType() == NVT);
3618 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003619 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003620 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3621 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003622 break;
3623
Chris Lattnerca401aa2007-03-03 23:43:21 +00003624 case ISD::FPOWI: {
3625 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3626 // directly as well, which may be better.
3627 Tmp1 = PromoteOp(Node->getOperand(0));
3628 assert(Tmp1.getValueType() == NVT);
3629 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3630 if (NoExcessFPPrecision)
3631 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3632 DAG.getValueType(VT));
3633 break;
3634 }
3635
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003636 case ISD::AND:
3637 case ISD::OR:
3638 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003639 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003640 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003641 case ISD::MUL:
3642 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003643 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003644 // that too is okay if they are integer operations.
3645 Tmp1 = PromoteOp(Node->getOperand(0));
3646 Tmp2 = PromoteOp(Node->getOperand(1));
3647 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3648 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003649 break;
3650 case ISD::FADD:
3651 case ISD::FSUB:
3652 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003653 Tmp1 = PromoteOp(Node->getOperand(0));
3654 Tmp2 = PromoteOp(Node->getOperand(1));
3655 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3656 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3657
3658 // Floating point operations will give excess precision that we may not be
3659 // able to tolerate. If we DO allow excess precision, just leave it,
3660 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003661 // FIXME: Why would we need to round FP ops more than integer ones?
3662 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003663 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003664 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3665 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003666 break;
3667
Chris Lattner4d978642005-01-15 22:16:26 +00003668 case ISD::SDIV:
3669 case ISD::SREM:
3670 // These operators require that their input be sign extended.
3671 Tmp1 = PromoteOp(Node->getOperand(0));
3672 Tmp2 = PromoteOp(Node->getOperand(1));
3673 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003674 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3675 DAG.getValueType(VT));
3676 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3677 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003678 }
3679 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3680
3681 // Perform FP_ROUND: this is probably overly pessimistic.
3682 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003683 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3684 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003685 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003686 case ISD::FDIV:
3687 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003688 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003689 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00003690 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3691 case Legal:
3692 Tmp1 = LegalizeOp(Node->getOperand(0));
3693 break;
3694 case Promote:
3695 Tmp1 = PromoteOp(Node->getOperand(0));
3696 break;
3697 case Expand:
3698 assert(0 && "not implemented");
3699 }
3700 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3701 case Legal:
3702 Tmp2 = LegalizeOp(Node->getOperand(1));
3703 break;
3704 case Promote:
3705 Tmp2 = PromoteOp(Node->getOperand(1));
3706 break;
3707 case Expand:
3708 assert(0 && "not implemented");
3709 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00003710 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3711
3712 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003713 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003714 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3715 DAG.getValueType(VT));
3716 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003717
3718 case ISD::UDIV:
3719 case ISD::UREM:
3720 // These operators require that their input be zero extended.
3721 Tmp1 = PromoteOp(Node->getOperand(0));
3722 Tmp2 = PromoteOp(Node->getOperand(1));
3723 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003724 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3725 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003726 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3727 break;
3728
3729 case ISD::SHL:
3730 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003731 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003732 break;
3733 case ISD::SRA:
3734 // The input value must be properly sign extended.
3735 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003736 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3737 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003738 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003739 break;
3740 case ISD::SRL:
3741 // The input value must be properly zero extended.
3742 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003743 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003744 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003745 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003746
3747 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003748 Tmp1 = Node->getOperand(0); // Get the chain.
3749 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003750 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3751 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3752 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3753 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00003754 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00003755 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00003756 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003757 // Increment the pointer, VAList, to the next vaarg
3758 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3759 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3760 TLI.getPointerTy()));
3761 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00003762 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3763 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003764 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003765 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00003766 }
3767 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003768 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00003769 break;
3770
Evan Chenge71fe34d2006-10-09 20:57:25 +00003771 case ISD::LOAD: {
3772 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00003773 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
3774 ? ISD::EXTLOAD : LD->getExtensionType();
3775 Result = DAG.getExtLoad(ExtType, NVT,
3776 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00003777 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00003778 LD->getLoadedVT(),
3779 LD->isVolatile(),
3780 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003781 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003782 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003783 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00003784 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003785 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003786 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
3787 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003788 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003789 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00003790 case ISD::SELECT_CC:
3791 Tmp2 = PromoteOp(Node->getOperand(2)); // True
3792 Tmp3 = PromoteOp(Node->getOperand(3)); // False
3793 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003794 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00003795 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003796 case ISD::BSWAP:
3797 Tmp1 = Node->getOperand(0);
3798 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3799 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3800 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003801 DAG.getConstant(MVT::getSizeInBits(NVT) -
3802 MVT::getSizeInBits(VT),
Nate Begeman2fba8a32006-01-14 03:14:10 +00003803 TLI.getShiftAmountTy()));
3804 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003805 case ISD::CTPOP:
3806 case ISD::CTTZ:
3807 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003808 // Zero extend the argument
3809 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003810 // Perform the larger operation, then subtract if needed.
3811 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003812 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003813 case ISD::CTPOP:
3814 Result = Tmp1;
3815 break;
3816 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003817 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00003818 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003819 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
3820 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003821 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003822 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003823 break;
3824 case ISD::CTLZ:
3825 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003826 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003827 DAG.getConstant(MVT::getSizeInBits(NVT) -
3828 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00003829 break;
3830 }
3831 break;
Dan Gohmana8665142007-06-25 16:23:39 +00003832 case ISD::EXTRACT_SUBVECTOR:
3833 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00003834 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00003835 case ISD::EXTRACT_VECTOR_ELT:
3836 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
3837 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003838 }
3839
3840 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003841
3842 // Make sure the result is itself legal.
3843 Result = LegalizeOp(Result);
3844
3845 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003846 AddPromotedOperand(Op, Result);
3847 return Result;
3848}
Chris Lattnerdc750592005-01-07 07:47:09 +00003849
Dan Gohmana8665142007-06-25 16:23:39 +00003850/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
3851/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
3852/// based on the vector type. The return type of this matches the element type
3853/// of the vector, which may not be legal for the target.
3854SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00003855 // We know that operand #0 is the Vec vector. If the index is a constant
3856 // or if the invec is a supported hardware type, we can use it. Otherwise,
3857 // lower to a store then an indexed load.
3858 SDOperand Vec = Op.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00003859 SDOperand Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00003860
3861 SDNode *InVal = Vec.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00003862 MVT::ValueType TVT = InVal->getValueType(0);
3863 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner6f423252006-03-31 17:55:51 +00003864
Dan Gohmana8665142007-06-25 16:23:39 +00003865 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
3866 default: assert(0 && "This action is not supported yet!");
3867 case TargetLowering::Custom: {
3868 Vec = LegalizeOp(Vec);
3869 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
3870 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
3871 if (Tmp3.Val)
3872 return Tmp3;
3873 break;
3874 }
3875 case TargetLowering::Legal:
3876 if (isTypeLegal(TVT)) {
3877 Vec = LegalizeOp(Vec);
3878 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00003879 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00003880 }
3881 break;
3882 case TargetLowering::Expand:
3883 break;
3884 }
3885
3886 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00003887 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00003888 Op = ScalarizeVectorOp(Vec);
3889 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
3890 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner6f423252006-03-31 17:55:51 +00003891 SDOperand Lo, Hi;
3892 SplitVectorOp(Vec, Lo, Hi);
3893 if (CIdx->getValue() < NumElems/2) {
3894 Vec = Lo;
3895 } else {
3896 Vec = Hi;
Dan Gohmana8665142007-06-25 16:23:39 +00003897 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
3898 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00003899 }
Dan Gohmana8665142007-06-25 16:23:39 +00003900
Chris Lattner6f423252006-03-31 17:55:51 +00003901 // It's now an extract from the appropriate high or low part. Recurse.
3902 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00003903 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00003904 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00003905 // Store the value to a temporary stack slot, then LOAD the scalar
3906 // element back out.
3907 SDOperand StackPtr = CreateStackTemporary(Vec.getValueType());
3908 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
3909
3910 // Add the offset to the index.
3911 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
3912 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
3913 DAG.getConstant(EltSize, Idx.getValueType()));
3914 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
3915
3916 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00003917 }
Dan Gohmana8665142007-06-25 16:23:39 +00003918 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00003919}
3920
Dan Gohmana8665142007-06-25 16:23:39 +00003921/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00003922/// we assume the operation can be split if it is not already legal.
Dan Gohmana8665142007-06-25 16:23:39 +00003923SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00003924 // We know that operand #0 is the Vec vector. For now we assume the index
3925 // is a constant and that the extracted result is a supported hardware type.
3926 SDOperand Vec = Op.getOperand(0);
3927 SDOperand Idx = LegalizeOp(Op.getOperand(1));
3928
Dan Gohmana8665142007-06-25 16:23:39 +00003929 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00003930
3931 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
3932 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00003933 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00003934 }
3935
3936 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
3937 SDOperand Lo, Hi;
3938 SplitVectorOp(Vec, Lo, Hi);
3939 if (CIdx->getValue() < NumElems/2) {
3940 Vec = Lo;
3941 } else {
3942 Vec = Hi;
3943 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
3944 }
3945
3946 // It's now an extract from the appropriate high or low part. Recurse.
3947 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00003948 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00003949}
3950
Nate Begeman7e7f4392006-02-01 07:19:44 +00003951/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
3952/// with condition CC on the current target. This usually involves legalizing
3953/// or promoting the arguments. In the case where LHS and RHS must be expanded,
3954/// there may be no choice but to create a new SetCC node to represent the
3955/// legalized value of setcc lhs, rhs. In this case, the value is returned in
3956/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
3957void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
3958 SDOperand &RHS,
3959 SDOperand &CC) {
3960 SDOperand Tmp1, Tmp2, Result;
3961
3962 switch (getTypeAction(LHS.getValueType())) {
3963 case Legal:
3964 Tmp1 = LegalizeOp(LHS); // LHS
3965 Tmp2 = LegalizeOp(RHS); // RHS
3966 break;
3967 case Promote:
3968 Tmp1 = PromoteOp(LHS); // LHS
3969 Tmp2 = PromoteOp(RHS); // RHS
3970
3971 // If this is an FP compare, the operands have already been extended.
3972 if (MVT::isInteger(LHS.getValueType())) {
3973 MVT::ValueType VT = LHS.getValueType();
3974 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
3975
3976 // Otherwise, we have to insert explicit sign or zero extends. Note
3977 // that we could insert sign extends for ALL conditions, but zero extend
3978 // is cheaper on many machines (an AND instead of two shifts), so prefer
3979 // it.
3980 switch (cast<CondCodeSDNode>(CC)->get()) {
3981 default: assert(0 && "Unknown integer comparison!");
3982 case ISD::SETEQ:
3983 case ISD::SETNE:
3984 case ISD::SETUGE:
3985 case ISD::SETUGT:
3986 case ISD::SETULE:
3987 case ISD::SETULT:
3988 // ALL of these operations will work if we either sign or zero extend
3989 // the operands (including the unsigned comparisons!). Zero extend is
3990 // usually a simpler/cheaper operation, so prefer it.
3991 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3992 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
3993 break;
3994 case ISD::SETGE:
3995 case ISD::SETGT:
3996 case ISD::SETLT:
3997 case ISD::SETLE:
3998 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3999 DAG.getValueType(VT));
4000 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4001 DAG.getValueType(VT));
4002 break;
4003 }
4004 }
4005 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004006 case Expand: {
4007 MVT::ValueType VT = LHS.getValueType();
4008 if (VT == MVT::f32 || VT == MVT::f64) {
4009 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00004010 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004011 switch (cast<CondCodeSDNode>(CC)->get()) {
4012 case ISD::SETEQ:
4013 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004014 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004015 break;
4016 case ISD::SETNE:
4017 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004018 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004019 break;
4020 case ISD::SETGE:
4021 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004022 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004023 break;
4024 case ISD::SETLT:
4025 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004026 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004027 break;
4028 case ISD::SETLE:
4029 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004030 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004031 break;
4032 case ISD::SETGT:
4033 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004034 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004035 break;
4036 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00004037 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4038 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004039 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00004040 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004041 break;
4042 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004043 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004044 switch (cast<CondCodeSDNode>(CC)->get()) {
4045 case ISD::SETONE:
4046 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00004047 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004048 // Fallthrough
4049 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004050 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004051 break;
4052 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004053 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004054 break;
4055 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004056 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004057 break;
4058 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004059 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004060 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004061 case ISD::SETUEQ:
4062 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004063 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004064 default: assert(0 && "Unsupported FP setcc!");
4065 }
4066 }
4067
4068 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004069 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00004070 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004071 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004072 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00004073 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00004074 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004075 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00004076 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00004077 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004078 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004079 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00004080 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004081 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4082 Tmp2 = SDOperand();
4083 }
4084 LHS = Tmp1;
4085 RHS = Tmp2;
4086 return;
4087 }
4088
Nate Begeman7e7f4392006-02-01 07:19:44 +00004089 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4090 ExpandOp(LHS, LHSLo, LHSHi);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004091 ExpandOp(RHS, RHSLo, RHSHi);
Nate Begeman7e7f4392006-02-01 07:19:44 +00004092 switch (cast<CondCodeSDNode>(CC)->get()) {
4093 case ISD::SETEQ:
4094 case ISD::SETNE:
4095 if (RHSLo == RHSHi)
4096 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4097 if (RHSCST->isAllOnesValue()) {
4098 // Comparison to -1.
4099 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4100 Tmp2 = RHSLo;
4101 break;
4102 }
4103
4104 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4105 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4106 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4107 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4108 break;
4109 default:
4110 // If this is a comparison of the sign bit, just look at the top part.
4111 // X > -1, x < 0
4112 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4113 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4114 CST->getValue() == 0) || // X < 0
4115 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4116 CST->isAllOnesValue())) { // X > -1
4117 Tmp1 = LHSHi;
4118 Tmp2 = RHSHi;
4119 break;
4120 }
4121
4122 // FIXME: This generated code sucks.
4123 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00004124 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4125 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004126 default: assert(0 && "Unknown integer setcc!");
4127 case ISD::SETLT:
4128 case ISD::SETULT: LowCC = ISD::SETULT; break;
4129 case ISD::SETGT:
4130 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4131 case ISD::SETLE:
4132 case ISD::SETULE: LowCC = ISD::SETULE; break;
4133 case ISD::SETGE:
4134 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4135 }
4136
4137 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4138 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4139 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4140
4141 // NOTE: on targets without efficient SELECT of bools, we can always use
4142 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00004143 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4144 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4145 false, DagCombineInfo);
4146 if (!Tmp1.Val)
4147 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4148 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4149 CCCode, false, DagCombineInfo);
4150 if (!Tmp2.Val)
4151 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC);
4152
4153 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4154 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4155 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4156 (Tmp2C && Tmp2C->getValue() == 0 &&
4157 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4158 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4159 (Tmp2C && Tmp2C->getValue() == 1 &&
4160 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4161 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4162 // low part is known false, returns high part.
4163 // For LE / GE, if high part is known false, ignore the low part.
4164 // For LT / GT, if high part is known true, ignore the low part.
4165 Tmp1 = Tmp2;
4166 Tmp2 = SDOperand();
4167 } else {
4168 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4169 ISD::SETEQ, false, DagCombineInfo);
4170 if (!Result.Val)
4171 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4172 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4173 Result, Tmp1, Tmp2));
4174 Tmp1 = Result;
4175 Tmp2 = SDOperand();
4176 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004177 }
4178 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004179 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004180 LHS = Tmp1;
4181 RHS = Tmp2;
4182}
4183
Chris Lattner36e663d2005-12-23 00:16:34 +00004184/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00004185/// The resultant code need not be legal. Note that SrcOp is the input operand
4186/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00004187SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4188 SDOperand SrcOp) {
4189 // Create the stack frame object.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004190 SDOperand FIPtr = CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004191
4192 // Emit a store to the stack slot.
Evan Chengab51cf22006-10-13 21:14:26 +00004193 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004194 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004195 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004196}
4197
Chris Lattner6be79822006-04-04 17:23:26 +00004198SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4199 // Create a vector sized/aligned stack slot, store the value to element #0,
4200 // then load the whole vector back out.
4201 SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00004202 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00004203 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004204 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00004205}
4206
4207
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004208/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00004209/// support the operation, but do support the resultant vector type.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004210SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4211
4212 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00004213 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004214 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004215 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00004216 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004217 std::map<SDOperand, std::vector<unsigned> > Values;
4218 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004219 bool isConstant = true;
4220 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4221 SplatValue.getOpcode() != ISD::UNDEF)
4222 isConstant = false;
4223
Evan Cheng1d2e9952006-03-24 01:17:21 +00004224 for (unsigned i = 1; i < NumElems; ++i) {
4225 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00004226 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004227 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004228 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004229 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00004230 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004231
4232 // If this isn't a constant element or an undef, we can't use a constant
4233 // pool load.
4234 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4235 V.getOpcode() != ISD::UNDEF)
4236 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004237 }
4238
4239 if (isOnlyLowElement) {
4240 // If the low element is an undef too, then this whole things is an undef.
4241 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4242 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4243 // Otherwise, turn this into a scalar_to_vector node.
4244 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4245 Node->getOperand(0));
4246 }
4247
Chris Lattner77e271c2006-03-24 07:29:17 +00004248 // If all elements are constants, create a load from the constant pool.
4249 if (isConstant) {
4250 MVT::ValueType VT = Node->getValueType(0);
4251 const Type *OpNTy =
4252 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4253 std::vector<Constant*> CV;
4254 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4255 if (ConstantFPSDNode *V =
4256 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00004257 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004258 } else if (ConstantSDNode *V =
4259 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00004260 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004261 } else {
4262 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4263 CV.push_back(UndefValue::get(OpNTy));
4264 }
4265 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00004266 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00004267 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00004268 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004269 }
4270
Chris Lattner21e68c82006-03-20 01:52:29 +00004271 if (SplatValue.Val) { // Splat of one value?
4272 // Build the shuffle constant vector: <0, 0, 0, 0>
4273 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00004274 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00004275 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004276 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004277 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4278 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00004279
4280 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004281 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00004282 // Get the splatted value into the low element of a vector register.
4283 SDOperand LowValVec =
4284 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4285
4286 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4287 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4288 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4289 SplatMask);
4290 }
4291 }
4292
Evan Cheng1d2e9952006-03-24 01:17:21 +00004293 // If there are only two unique elements, we may be able to turn this into a
4294 // vector shuffle.
4295 if (Values.size() == 2) {
4296 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4297 MVT::ValueType MaskVT =
4298 MVT::getIntVectorWithNumElements(NumElems);
4299 std::vector<SDOperand> MaskVec(NumElems);
4300 unsigned i = 0;
4301 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4302 E = Values.end(); I != E; ++I) {
4303 for (std::vector<unsigned>::iterator II = I->second.begin(),
4304 EE = I->second.end(); II != EE; ++II)
Dan Gohman5c441312007-06-14 22:58:02 +00004305 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004306 i += NumElems;
4307 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004308 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4309 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004310
4311 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004312 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4313 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004314 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004315 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4316 E = Values.end(); I != E; ++I) {
4317 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4318 I->first);
4319 Ops.push_back(Op);
4320 }
4321 Ops.push_back(ShuffleMask);
4322
4323 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004324 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4325 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004326 }
4327 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004328
4329 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4330 // aligned object on the stack, store each element into it, then load
4331 // the result as a vector.
4332 MVT::ValueType VT = Node->getValueType(0);
4333 // Create the stack frame object.
4334 SDOperand FIPtr = CreateStackTemporary(VT);
4335
4336 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004337 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004338 unsigned TypeByteSize =
4339 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004340 // Store (in the right endianness) the elements to memory.
4341 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4342 // Ignore undef elements.
4343 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4344
Chris Lattner8fa445a2006-03-22 01:46:54 +00004345 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004346
4347 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4348 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4349
Evan Chengdf9ac472006-10-05 23:01:46 +00004350 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00004351 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004352 }
4353
4354 SDOperand StoreChain;
4355 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004356 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4357 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004358 else
4359 StoreChain = DAG.getEntryNode();
4360
4361 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004362 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004363}
4364
4365/// CreateStackTemporary - Create a stack temporary, suitable for holding the
4366/// specified value type.
4367SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) {
4368 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4369 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
Chris Lattner50ee0e42007-01-20 22:35:55 +00004370 const Type *Ty = MVT::getTypeForValueType(VT);
Chris Lattner945e4372007-02-14 05:52:17 +00004371 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004372 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004373 return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
4374}
4375
Chris Lattner4157c412005-04-02 04:00:59 +00004376void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4377 SDOperand Op, SDOperand Amt,
4378 SDOperand &Lo, SDOperand &Hi) {
4379 // Expand the subcomponents.
4380 SDOperand LHSL, LHSH;
4381 ExpandOp(Op, LHSL, LHSH);
4382
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004383 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00004384 MVT::ValueType VT = LHSL.getValueType();
4385 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00004386 Hi = Lo.getValue(1);
4387}
4388
4389
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004390/// ExpandShift - Try to find a clever way to expand this shift operation out to
4391/// smaller elements. If we can't find a way that is more efficient than a
4392/// libcall on this target, return false. Otherwise, return true with the
4393/// low-parts expanded into Lo and Hi.
4394bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4395 SDOperand &Lo, SDOperand &Hi) {
4396 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4397 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00004398
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004399 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00004400 SDOperand ShAmt = LegalizeOp(Amt);
4401 MVT::ValueType ShTy = ShAmt.getValueType();
4402 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4403 unsigned NVTBits = MVT::getSizeInBits(NVT);
4404
4405 // Handle the case when Amt is an immediate. Other cases are currently broken
4406 // and are disabled.
4407 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4408 unsigned Cst = CN->getValue();
4409 // Expand the incoming operand to be shifted, so that we have its parts
4410 SDOperand InL, InH;
4411 ExpandOp(Op, InL, InH);
4412 switch(Opc) {
4413 case ISD::SHL:
4414 if (Cst > VTBits) {
4415 Lo = DAG.getConstant(0, NVT);
4416 Hi = DAG.getConstant(0, NVT);
4417 } else if (Cst > NVTBits) {
4418 Lo = DAG.getConstant(0, NVT);
4419 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004420 } else if (Cst == NVTBits) {
4421 Lo = DAG.getConstant(0, NVT);
4422 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00004423 } else {
4424 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4425 Hi = DAG.getNode(ISD::OR, NVT,
4426 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4427 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4428 }
4429 return true;
4430 case ISD::SRL:
4431 if (Cst > VTBits) {
4432 Lo = DAG.getConstant(0, NVT);
4433 Hi = DAG.getConstant(0, NVT);
4434 } else if (Cst > NVTBits) {
4435 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4436 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00004437 } else if (Cst == NVTBits) {
4438 Lo = InH;
4439 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00004440 } else {
4441 Lo = DAG.getNode(ISD::OR, NVT,
4442 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4443 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4444 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4445 }
4446 return true;
4447 case ISD::SRA:
4448 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004449 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004450 DAG.getConstant(NVTBits-1, ShTy));
4451 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004452 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004453 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004454 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004455 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004456 } else if (Cst == NVTBits) {
4457 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004458 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004459 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004460 } else {
4461 Lo = DAG.getNode(ISD::OR, NVT,
4462 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4463 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4464 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4465 }
4466 return true;
4467 }
4468 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004469
4470 // Okay, the shift amount isn't constant. However, if we can tell that it is
4471 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4472 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00004473 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00004474
4475 // If we know that the high bit of the shift amount is one, then we can do
4476 // this as a couple of simple shifts.
4477 if (KnownOne & Mask) {
4478 // Mask out the high bit, which we know is set.
4479 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4480 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4481
4482 // Expand the incoming operand to be shifted, so that we have its parts
4483 SDOperand InL, InH;
4484 ExpandOp(Op, InL, InH);
4485 switch(Opc) {
4486 case ISD::SHL:
4487 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4488 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4489 return true;
4490 case ISD::SRL:
4491 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4492 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4493 return true;
4494 case ISD::SRA:
4495 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4496 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4497 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4498 return true;
4499 }
4500 }
4501
4502 // If we know that the high bit of the shift amount is zero, then we can do
4503 // this as a couple of simple shifts.
4504 if (KnownZero & Mask) {
4505 // Compute 32-amt.
4506 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4507 DAG.getConstant(NVTBits, Amt.getValueType()),
4508 Amt);
4509
4510 // Expand the incoming operand to be shifted, so that we have its parts
4511 SDOperand InL, InH;
4512 ExpandOp(Op, InL, InH);
4513 switch(Opc) {
4514 case ISD::SHL:
4515 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4516 Hi = DAG.getNode(ISD::OR, NVT,
4517 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4518 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4519 return true;
4520 case ISD::SRL:
4521 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4522 Lo = DAG.getNode(ISD::OR, NVT,
4523 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4524 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4525 return true;
4526 case ISD::SRA:
4527 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4528 Lo = DAG.getNode(ISD::OR, NVT,
4529 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4530 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4531 return true;
4532 }
4533 }
4534
Nate Begemanb0674922005-04-06 21:13:14 +00004535 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004536}
Chris Lattneraac464e2005-01-21 06:05:23 +00004537
Chris Lattner4add7e32005-01-23 04:42:50 +00004538
Chris Lattneraac464e2005-01-21 06:05:23 +00004539// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4540// does not fit into a register, return the lo part and set the hi part to the
4541// by-reg argument. If it does fit into a single register, return the result
4542// and leave the Hi part unset.
4543SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004544 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004545 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4546 // The input chain to this libcall is the entry node of the function.
4547 // Legalizing the call will automatically add the previous call to the
4548 // dependence.
4549 SDOperand InChain = DAG.getEntryNode();
4550
Chris Lattneraac464e2005-01-21 06:05:23 +00004551 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004552 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004553 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4554 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4555 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004556 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00004557 Entry.isSExt = isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00004558 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004559 }
4560 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004561
Chris Lattner06bbeb62005-05-11 19:02:11 +00004562 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004563 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004564 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004565 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004566 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004567
Chris Lattner462505f2006-02-13 09:18:02 +00004568 // Legalize the call sequence, starting with the chain. This will advance
4569 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4570 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4571 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004572 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004573 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004574 default: assert(0 && "Unknown thing");
4575 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004576 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004577 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004578 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004579 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004580 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004581 }
Chris Lattner63022662005-09-02 20:26:58 +00004582 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004583}
4584
Chris Lattner4add7e32005-01-23 04:42:50 +00004585
Evan Chengbf535fc2007-04-27 07:33:31 +00004586/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4587///
Chris Lattneraac464e2005-01-21 06:05:23 +00004588SDOperand SelectionDAGLegalize::
4589ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004590 assert(getTypeAction(Source.getValueType()) == Expand &&
4591 "This is not an expansion!");
4592 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4593
Chris Lattner06bbeb62005-05-11 19:02:11 +00004594 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004595 assert(Source.getValueType() == MVT::i64 &&
4596 "This only works for 64-bit -> FP");
4597 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4598 // incoming integer is set. To handle this, we dynamically test to see if
4599 // it is set, and, if so, add a fudge factor.
4600 SDOperand Lo, Hi;
4601 ExpandOp(Source, Lo, Hi);
4602
Chris Lattner2a4f7312005-05-13 04:45:13 +00004603 // If this is unsigned, and not supported, first perform the conversion to
4604 // signed, then adjust the result if the sign bit is set.
4605 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4606 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4607
Chris Lattnerd47675e2005-08-09 20:20:18 +00004608 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4609 DAG.getConstant(0, Hi.getValueType()),
4610 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004611 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4612 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4613 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004614 uint64_t FF = 0x5f800000ULL;
4615 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004616 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004617
Chris Lattnerc30405e2005-08-26 17:15:30 +00004618 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004619 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4620 SDOperand FudgeInReg;
4621 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004622 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen98d3a082007-09-14 22:26:36 +00004623 else if (DestTy == MVT::f64)
Evan Chengbf535fc2007-04-27 07:33:31 +00004624 // FIXME: Avoid the extend by construction the right constantpool?
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004625 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Evan Chenge71fe34d2006-10-09 20:57:25 +00004626 CPIdx, NULL, 0, MVT::f32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00004627 else if (DestTy == MVT::f80)
4628 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, DAG.getEntryNode(),
4629 CPIdx, NULL, 0, MVT::f32);
4630 else
4631 assert(0 && "Unexpected conversion");
4632
Evan Chengbf535fc2007-04-27 07:33:31 +00004633 MVT::ValueType SCVT = SignedConv.getValueType();
4634 if (SCVT != DestTy) {
4635 // Destination type needs to be expanded as well. The FADD now we are
4636 // constructing will be expanded into a libcall.
4637 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4638 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4639 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4640 SignedConv, SignedConv.getValue(1));
4641 }
4642 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4643 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00004644 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00004645 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00004646
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004647 // Check to see if the target has a custom way to lower this. If so, use it.
4648 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4649 default: assert(0 && "This action not implemented for this operation!");
4650 case TargetLowering::Legal:
4651 case TargetLowering::Expand:
4652 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004653 case TargetLowering::Custom: {
4654 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4655 Source), DAG);
4656 if (NV.Val)
4657 return LegalizeOp(NV);
4658 break; // The target decided this was legal after all
4659 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004660 }
4661
Chris Lattner153587e2005-05-12 07:00:44 +00004662 // Expand the source, then glue it back together for the call. We must expand
4663 // the source in case it is shared (this pass of legalize must traverse it).
4664 SDOperand SrcLo, SrcHi;
4665 ExpandOp(Source, SrcLo, SrcHi);
4666 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4667
Evan Cheng31cbddf2007-01-12 02:11:51 +00004668 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004669 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004670 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004671 else {
4672 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00004673 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004674 }
Chris Lattner462505f2006-02-13 09:18:02 +00004675
Evan Chengbf535fc2007-04-27 07:33:31 +00004676 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner462505f2006-02-13 09:18:02 +00004677 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4678 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004679 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4680 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00004681}
Misha Brukman835702a2005-04-21 22:36:52 +00004682
Chris Lattner689bdcc2006-01-28 08:25:58 +00004683/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4684/// INT_TO_FP operation of the specified operand when the target requests that
4685/// we expand it. At this point, we know that the result and operand types are
4686/// legal for the target.
4687SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4688 SDOperand Op0,
4689 MVT::ValueType DestVT) {
4690 if (Op0.getValueType() == MVT::i32) {
4691 // simple 32-bit [signed|unsigned] integer to float/double expansion
4692
Chris Lattner50ee0e42007-01-20 22:35:55 +00004693 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner689bdcc2006-01-28 08:25:58 +00004694 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner50ee0e42007-01-20 22:35:55 +00004695 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4696 unsigned StackAlign =
Chris Lattner945e4372007-02-14 05:52:17 +00004697 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004698 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004699 // get address of 8 byte buffer
4700 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4701 // word offset constant for Hi/Lo address computation
4702 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4703 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00004704 SDOperand Hi = StackSlot;
4705 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4706 if (TLI.isLittleEndian())
4707 std::swap(Hi, Lo);
4708
Chris Lattner689bdcc2006-01-28 08:25:58 +00004709 // if signed map to unsigned space
4710 SDOperand Op0Mapped;
4711 if (isSigned) {
4712 // constant used to invert sign bit (signed to unsigned mapping)
4713 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4714 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4715 } else {
4716 Op0Mapped = Op0;
4717 }
4718 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00004719 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00004720 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004721 // initial hi portion of constructed double
4722 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4723 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00004724 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004725 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00004726 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004727 // FP constant to bias correct the final result
4728 SDOperand Bias = DAG.getConstantFP(isSigned ?
4729 BitsToDouble(0x4330000080000000ULL)
4730 : BitsToDouble(0x4330000000000000ULL),
4731 MVT::f64);
4732 // subtract the bias
4733 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4734 // final result
4735 SDOperand Result;
4736 // handle final rounding
4737 if (DestVT == MVT::f64) {
4738 // do nothing
4739 Result = Sub;
Dale Johannesen98d3a082007-09-14 22:26:36 +00004740 } else if (DestVT == MVT::f32) {
Chris Lattner689bdcc2006-01-28 08:25:58 +00004741 // if f32 then cast to f32
4742 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
Dale Johannesen98d3a082007-09-14 22:26:36 +00004743 } else if (DestVT == MVT::f80) {
4744 Result = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004745 }
4746 return Result;
4747 }
4748 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4749 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4750
4751 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4752 DAG.getConstant(0, Op0.getValueType()),
4753 ISD::SETLT);
4754 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4755 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4756 SignSet, Four, Zero);
4757
4758 // If the sign bit of the integer is set, the large number will be treated
4759 // as a negative number. To counteract this, the dynamic code adds an
4760 // offset depending on the data type.
4761 uint64_t FF;
4762 switch (Op0.getValueType()) {
4763 default: assert(0 && "Unsupported integer type!");
4764 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
4765 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
4766 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
4767 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
4768 }
4769 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004770 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004771
4772 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
4773 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4774 SDOperand FudgeInReg;
4775 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004776 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004777 else {
4778 assert(DestVT == MVT::f64 && "Unexpected conversion");
4779 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
4780 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00004781 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00004782 }
4783
4784 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
4785}
4786
4787/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
4788/// *INT_TO_FP operation of the specified operand when the target requests that
4789/// we promote it. At this point, we know that the result and operand types are
4790/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
4791/// operation that takes a larger input.
4792SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
4793 MVT::ValueType DestVT,
4794 bool isSigned) {
4795 // First step, figure out the appropriate *INT_TO_FP operation to use.
4796 MVT::ValueType NewInTy = LegalOp.getValueType();
4797
4798 unsigned OpToUse = 0;
4799
4800 // Scan for the appropriate larger type to use.
4801 while (1) {
4802 NewInTy = (MVT::ValueType)(NewInTy+1);
4803 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
4804
4805 // If the target supports SINT_TO_FP of this type, use it.
4806 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
4807 default: break;
4808 case TargetLowering::Legal:
4809 if (!TLI.isTypeLegal(NewInTy))
4810 break; // Can't use this datatype.
4811 // FALL THROUGH.
4812 case TargetLowering::Custom:
4813 OpToUse = ISD::SINT_TO_FP;
4814 break;
4815 }
4816 if (OpToUse) break;
4817 if (isSigned) continue;
4818
4819 // If the target supports UINT_TO_FP of this type, use it.
4820 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
4821 default: break;
4822 case TargetLowering::Legal:
4823 if (!TLI.isTypeLegal(NewInTy))
4824 break; // Can't use this datatype.
4825 // FALL THROUGH.
4826 case TargetLowering::Custom:
4827 OpToUse = ISD::UINT_TO_FP;
4828 break;
4829 }
4830 if (OpToUse) break;
4831
4832 // Otherwise, try a larger type.
4833 }
4834
4835 // Okay, we found the operation and type to use. Zero extend our input to the
4836 // desired type then run the operation on it.
4837 return DAG.getNode(OpToUse, DestVT,
4838 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
4839 NewInTy, LegalOp));
4840}
4841
4842/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
4843/// FP_TO_*INT operation of the specified operand when the target requests that
4844/// we promote it. At this point, we know that the result and operand types are
4845/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
4846/// operation that returns a larger result.
4847SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
4848 MVT::ValueType DestVT,
4849 bool isSigned) {
4850 // First step, figure out the appropriate FP_TO*INT operation to use.
4851 MVT::ValueType NewOutTy = DestVT;
4852
4853 unsigned OpToUse = 0;
4854
4855 // Scan for the appropriate larger type to use.
4856 while (1) {
4857 NewOutTy = (MVT::ValueType)(NewOutTy+1);
4858 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
4859
4860 // If the target supports FP_TO_SINT returning this type, use it.
4861 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
4862 default: break;
4863 case TargetLowering::Legal:
4864 if (!TLI.isTypeLegal(NewOutTy))
4865 break; // Can't use this datatype.
4866 // FALL THROUGH.
4867 case TargetLowering::Custom:
4868 OpToUse = ISD::FP_TO_SINT;
4869 break;
4870 }
4871 if (OpToUse) break;
4872
4873 // If the target supports FP_TO_UINT of this type, use it.
4874 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
4875 default: break;
4876 case TargetLowering::Legal:
4877 if (!TLI.isTypeLegal(NewOutTy))
4878 break; // Can't use this datatype.
4879 // FALL THROUGH.
4880 case TargetLowering::Custom:
4881 OpToUse = ISD::FP_TO_UINT;
4882 break;
4883 }
4884 if (OpToUse) break;
4885
4886 // Otherwise, try a larger type.
4887 }
4888
4889 // Okay, we found the operation and type to use. Truncate the result of the
4890 // extended FP_TO_*INT operation to the desired size.
4891 return DAG.getNode(ISD::TRUNCATE, DestVT,
4892 DAG.getNode(OpToUse, NewOutTy, LegalOp));
4893}
4894
4895/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
4896///
4897SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
4898 MVT::ValueType VT = Op.getValueType();
4899 MVT::ValueType SHVT = TLI.getShiftAmountTy();
4900 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
4901 switch (VT) {
4902 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
4903 case MVT::i16:
4904 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4905 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4906 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
4907 case MVT::i32:
4908 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4909 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4910 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4911 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4912 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
4913 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
4914 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4915 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4916 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4917 case MVT::i64:
4918 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
4919 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
4920 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
4921 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
4922 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
4923 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
4924 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
4925 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
4926 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
4927 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
4928 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
4929 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
4930 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
4931 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
4932 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
4933 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
4934 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
4935 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
4936 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
4937 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
4938 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
4939 }
4940}
4941
4942/// ExpandBitCount - Expand the specified bitcount instruction into operations.
4943///
4944SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
4945 switch (Opc) {
4946 default: assert(0 && "Cannot expand this yet!");
4947 case ISD::CTPOP: {
4948 static const uint64_t mask[6] = {
4949 0x5555555555555555ULL, 0x3333333333333333ULL,
4950 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
4951 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
4952 };
4953 MVT::ValueType VT = Op.getValueType();
4954 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00004955 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004956 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4957 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
4958 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
4959 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4960 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
4961 DAG.getNode(ISD::AND, VT,
4962 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
4963 }
4964 return Op;
4965 }
4966 case ISD::CTLZ: {
4967 // for now, we do this:
4968 // x = x | (x >> 1);
4969 // x = x | (x >> 2);
4970 // ...
4971 // x = x | (x >>16);
4972 // x = x | (x >>32); // for 64-bit input
4973 // return popcount(~x);
4974 //
4975 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
4976 MVT::ValueType VT = Op.getValueType();
4977 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00004978 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004979 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
4980 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
4981 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
4982 }
4983 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
4984 return DAG.getNode(ISD::CTPOP, VT, Op);
4985 }
4986 case ISD::CTTZ: {
4987 // for now, we use: { return popcount(~x & (x - 1)); }
4988 // unless the target has ctlz but not ctpop, in which case we use:
4989 // { return 32 - nlz(~x & (x-1)); }
4990 // see also http://www.hackersdelight.org/HDcode/ntz.cc
4991 MVT::ValueType VT = Op.getValueType();
4992 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
4993 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
4994 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
4995 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
4996 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
4997 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
4998 TLI.isOperationLegal(ISD::CTLZ, VT))
4999 return DAG.getNode(ISD::SUB, VT,
Dan Gohman1796f1f2007-05-18 17:52:13 +00005000 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00005001 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5002 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5003 }
5004 }
5005}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005006
Chris Lattnerdc750592005-01-07 07:47:09 +00005007/// ExpandOp - Expand the specified SDOperand into its two component pieces
5008/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5009/// LegalizeNodes map is filled in for any results that are not expanded, the
5010/// ExpandedNodes map is filled in for any results that are expanded, and the
5011/// Lo/Hi values are returned.
5012void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5013 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00005014 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00005015 SDNode *Node = Op.Val;
5016 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00005017 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohmana8665142007-06-25 16:23:39 +00005018 MVT::isVector(VT)) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00005019 "Cannot expand to FP value or to larger int value!");
5020
Chris Lattner1a570f12005-09-02 20:32:45 +00005021 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005022 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00005023 = ExpandedNodes.find(Op);
5024 if (I != ExpandedNodes.end()) {
5025 Lo = I->second.first;
5026 Hi = I->second.second;
5027 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00005028 }
5029
Chris Lattnerdc750592005-01-07 07:47:09 +00005030 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00005031 case ISD::CopyFromReg:
5032 assert(0 && "CopyFromReg must be legal!");
5033 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005034#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005035 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005036#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00005037 assert(0 && "Do not know how to expand this operator!");
5038 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00005039 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00005040 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00005041 Lo = DAG.getNode(ISD::UNDEF, NVT);
5042 Hi = DAG.getNode(ISD::UNDEF, NVT);
5043 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005044 case ISD::Constant: {
5045 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5046 Lo = DAG.getConstant(Cst, NVT);
5047 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5048 break;
5049 }
Evan Cheng47833a12006-12-12 21:32:44 +00005050 case ISD::ConstantFP: {
5051 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Evan Cheng3766fc602006-12-12 22:19:28 +00005052 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00005053 if (getTypeAction(Lo.getValueType()) == Expand)
5054 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005055 break;
5056 }
Chris Lattner32e08b72005-03-28 22:03:13 +00005057 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005058 // Return the operands.
5059 Lo = Node->getOperand(0);
5060 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00005061 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005062
5063 case ISD::SIGN_EXTEND_INREG:
5064 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00005065 // sext_inreg the low part if needed.
5066 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5067
5068 // The high part gets the sign extension from the lo-part. This handles
5069 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005070 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5071 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5072 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005073 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00005074
Nate Begeman2fba8a32006-01-14 03:14:10 +00005075 case ISD::BSWAP: {
5076 ExpandOp(Node->getOperand(0), Lo, Hi);
5077 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5078 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5079 Lo = TempLo;
5080 break;
5081 }
5082
Chris Lattner55e9cde2005-05-11 04:51:16 +00005083 case ISD::CTPOP:
5084 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00005085 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5086 DAG.getNode(ISD::CTPOP, NVT, Lo),
5087 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00005088 Hi = DAG.getConstant(0, NVT);
5089 break;
5090
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005091 case ISD::CTLZ: {
5092 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005093 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005094 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5095 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005096 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5097 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005098 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5099 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5100
5101 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5102 Hi = DAG.getConstant(0, NVT);
5103 break;
5104 }
5105
5106 case ISD::CTTZ: {
5107 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005108 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005109 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5110 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005111 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5112 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005113 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5114 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5115
5116 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5117 Hi = DAG.getConstant(0, NVT);
5118 break;
5119 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00005120
Nate Begemane74795c2006-01-25 18:21:52 +00005121 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005122 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5123 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00005124 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5125 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5126
5127 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005128 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00005129 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5130 if (!TLI.isLittleEndian())
5131 std::swap(Lo, Hi);
5132 break;
5133 }
5134
Chris Lattnerdc750592005-01-07 07:47:09 +00005135 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005136 LoadSDNode *LD = cast<LoadSDNode>(Node);
5137 SDOperand Ch = LD->getChain(); // Legalize the chain.
5138 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5139 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman2af30632007-07-09 22:18:38 +00005140 int SVOffset = LD->getSrcValueOffset();
5141 unsigned Alignment = LD->getAlignment();
5142 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00005143
Evan Chenge71fe34d2006-10-09 20:57:25 +00005144 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman2af30632007-07-09 22:18:38 +00005145 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5146 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00005147 if (VT == MVT::f32 || VT == MVT::f64) {
5148 // f32->i32 or f64->i64 one to one expansion.
5149 // Remember that we legalized the chain.
5150 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00005151 // Recursively expand the new load.
5152 if (getTypeAction(NVT) == Expand)
5153 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005154 break;
5155 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00005156
Evan Chenge71fe34d2006-10-09 20:57:25 +00005157 // Increment the pointer to the other half.
5158 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5159 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5160 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005161 SVOffset += IncrementSize;
Dan Gohman2af30632007-07-09 22:18:38 +00005162 if (Alignment > IncrementSize)
5163 Alignment = IncrementSize;
5164 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5165 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00005166
Evan Chenge71fe34d2006-10-09 20:57:25 +00005167 // Build a factor node to remember that this load is independent of the
5168 // other one.
5169 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5170 Hi.getValue(1));
5171
5172 // Remember that we legalized the chain.
5173 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5174 if (!TLI.isLittleEndian())
5175 std::swap(Lo, Hi);
5176 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00005177 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00005178
5179 if (VT == MVT::f64 && EVT == MVT::f32) {
5180 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5181 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005182 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00005183 // Remember that we legalized the chain.
5184 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5185 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5186 break;
5187 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00005188
5189 if (EVT == NVT)
5190 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005191 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005192 else
5193 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005194 SVOffset, EVT, isVolatile,
5195 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005196
5197 // Remember that we legalized the chain.
5198 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5199
5200 if (ExtType == ISD::SEXTLOAD) {
5201 // The high part is obtained by SRA'ing all but one of the bits of the
5202 // lo part.
5203 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5204 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5205 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5206 } else if (ExtType == ISD::ZEXTLOAD) {
5207 // The high part is just a zero.
5208 Hi = DAG.getConstant(0, NVT);
5209 } else /* if (ExtType == ISD::EXTLOAD) */ {
5210 // The high part is undefined.
5211 Hi = DAG.getNode(ISD::UNDEF, NVT);
5212 }
5213 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005214 break;
5215 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005216 case ISD::AND:
5217 case ISD::OR:
5218 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5219 SDOperand LL, LH, RL, RH;
5220 ExpandOp(Node->getOperand(0), LL, LH);
5221 ExpandOp(Node->getOperand(1), RL, RH);
5222 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5223 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5224 break;
5225 }
5226 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005227 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00005228 ExpandOp(Node->getOperand(1), LL, LH);
5229 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00005230 if (getTypeAction(NVT) == Expand)
5231 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005232 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00005233 if (VT != MVT::f32)
5234 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00005235 break;
5236 }
Nate Begemane5b86d72005-08-10 20:51:12 +00005237 case ISD::SELECT_CC: {
5238 SDOperand TL, TH, FL, FH;
5239 ExpandOp(Node->getOperand(2), TL, TH);
5240 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00005241 if (getTypeAction(NVT) == Expand)
5242 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00005243 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5244 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00005245 if (VT != MVT::f32)
5246 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5247 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00005248 break;
5249 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005250 case ISD::ANY_EXTEND:
5251 // The low part is any extension of the input (which degenerates to a copy).
5252 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5253 // The high part is undefined.
5254 Hi = DAG.getNode(ISD::UNDEF, NVT);
5255 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005256 case ISD::SIGN_EXTEND: {
5257 // The low part is just a sign extension of the input (which degenerates to
5258 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005259 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005260
Chris Lattnerdc750592005-01-07 07:47:09 +00005261 // The high part is obtained by SRA'ing all but one of the bits of the lo
5262 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00005263 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005264 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5265 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00005266 break;
5267 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005268 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00005269 // The low part is just a zero extension of the input (which degenerates to
5270 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005271 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005272
Chris Lattnerdc750592005-01-07 07:47:09 +00005273 // The high part is just a zero.
5274 Hi = DAG.getConstant(0, NVT);
5275 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00005276
Chris Lattner59b27fa372007-02-13 23:55:16 +00005277 case ISD::TRUNCATE: {
5278 // The input value must be larger than this value. Expand *it*.
5279 SDOperand NewLo;
5280 ExpandOp(Node->getOperand(0), NewLo, Hi);
5281
5282 // The low part is now either the right size, or it is closer. If not the
5283 // right size, make an illegal truncate so we recursively expand it.
5284 if (NewLo.getValueType() != Node->getValueType(0))
5285 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5286 ExpandOp(NewLo, Lo, Hi);
5287 break;
5288 }
5289
Chris Lattner36e663d2005-12-23 00:16:34 +00005290 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005291 SDOperand Tmp;
5292 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5293 // If the target wants to, allow it to lower this itself.
5294 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5295 case Expand: assert(0 && "cannot expand FP!");
5296 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5297 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5298 }
5299 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5300 }
5301
Evan Cheng4eee7242006-12-09 02:42:38 +00005302 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00005303 if (VT == MVT::f32 || VT == MVT::f64) {
5304 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00005305 if (getTypeAction(NVT) == Expand)
5306 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00005307 break;
5308 }
5309
5310 // If source operand will be expanded to the same type as VT, i.e.
5311 // i64 <- f64, i32 <- f32, expand the source operand instead.
5312 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5313 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5314 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005315 break;
5316 }
5317
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005318 // Turn this into a load/store pair by default.
5319 if (Tmp.Val == 0)
Evan Cheng3432ab92006-12-11 19:27:14 +00005320 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005321
Chris Lattner36e663d2005-12-23 00:16:34 +00005322 ExpandOp(Tmp, Lo, Hi);
5323 break;
5324 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005325
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005326 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00005327 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5328 TargetLowering::Custom &&
5329 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005330 Lo = TLI.LowerOperation(Op, DAG);
5331 assert(Lo.Val && "Node must be custom expanded!");
5332 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00005333 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005334 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005335 break;
5336
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005337 // These operators cannot be expanded directly, emit them as calls to
5338 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005339 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005340 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00005341 SDOperand Op;
5342 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5343 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005344 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5345 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00005346 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005347
Chris Lattner941d84a2005-07-30 01:40:57 +00005348 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5349
Chris Lattnerfe68d752005-07-29 00:33:32 +00005350 // Now that the custom expander is done, expand the result, which is still
5351 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005352 if (Op.Val) {
5353 ExpandOp(Op, Lo, Hi);
5354 break;
5355 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005356 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005357
Evan Cheng31cbddf2007-01-12 02:11:51 +00005358 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005359 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005360 LC = RTLIB::FPTOSINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005361 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005362 LC = RTLIB::FPTOSINT_F64_I64;
5363 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5364 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005365 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005366 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005367
Evan Cheng31cbddf2007-01-12 02:11:51 +00005368 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005369 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005370 SDOperand Op;
5371 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5372 case Expand: assert(0 && "cannot expand FP!");
5373 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5374 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5375 }
5376
5377 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5378
5379 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005380 if (Op.Val) {
5381 ExpandOp(Op, Lo, Hi);
5382 break;
5383 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005384 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005385
Evan Cheng31cbddf2007-01-12 02:11:51 +00005386 RTLIB::Libcall LC;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005387 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005388 LC = RTLIB::FPTOUINT_F32_I64;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005389 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005390 LC = RTLIB::FPTOUINT_F64_I64;
5391 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5392 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005393 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005394 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005395
Evan Cheng870e4f82006-01-09 18:31:59 +00005396 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005397 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005398 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005399 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005400 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005401 Op = TLI.LowerOperation(Op, DAG);
5402 if (Op.Val) {
5403 // Now that the custom expander is done, expand the result, which is
5404 // still VT.
5405 ExpandOp(Op, Lo, Hi);
5406 break;
5407 }
5408 }
5409
Chris Lattner72b503b2006-09-13 03:50:39 +00005410 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5411 // this X << 1 as X+X.
5412 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5413 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5414 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5415 SDOperand LoOps[2], HiOps[3];
5416 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5417 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5418 LoOps[1] = LoOps[0];
5419 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5420
5421 HiOps[1] = HiOps[0];
5422 HiOps[2] = Lo.getValue(1);
5423 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5424 break;
5425 }
5426 }
5427
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005428 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005429 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005430 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005431
5432 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005433 TargetLowering::LegalizeAction Action =
5434 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5435 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5436 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005437 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005438 break;
5439 }
5440
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005441 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005442 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5443 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005444 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005445 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005446
Evan Cheng870e4f82006-01-09 18:31:59 +00005447 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005448 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005449 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005450 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005451 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005452 Op = TLI.LowerOperation(Op, DAG);
5453 if (Op.Val) {
5454 // Now that the custom expander is done, expand the result, which is
5455 // still VT.
5456 ExpandOp(Op, Lo, Hi);
5457 break;
5458 }
5459 }
5460
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005461 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005462 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005463 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005464
5465 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005466 TargetLowering::LegalizeAction Action =
5467 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5468 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5469 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005470 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005471 break;
5472 }
5473
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005474 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005475 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5476 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005477 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005478 }
5479
5480 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005481 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005482 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005483 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005484 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005485 Op = TLI.LowerOperation(Op, DAG);
5486 if (Op.Val) {
5487 // Now that the custom expander is done, expand the result, which is
5488 // still VT.
5489 ExpandOp(Op, Lo, Hi);
5490 break;
5491 }
5492 }
5493
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005494 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005495 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005496 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005497
5498 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005499 TargetLowering::LegalizeAction Action =
5500 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5501 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5502 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005503 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005504 break;
5505 }
5506
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005507 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005508 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5509 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005510 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005511 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005512
Misha Brukman835702a2005-04-21 22:36:52 +00005513 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005514 case ISD::SUB: {
5515 // If the target wants to custom expand this, let them.
5516 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5517 TargetLowering::Custom) {
5518 Op = TLI.LowerOperation(Op, DAG);
5519 if (Op.Val) {
5520 ExpandOp(Op, Lo, Hi);
5521 break;
5522 }
5523 }
5524
5525 // Expand the subcomponents.
5526 SDOperand LHSL, LHSH, RHSL, RHSH;
5527 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5528 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005529 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5530 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005531 LoOps[0] = LHSL;
5532 LoOps[1] = RHSL;
5533 HiOps[0] = LHSH;
5534 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005535 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005536 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005537 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005538 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005539 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005540 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005541 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005542 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005543 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005544 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005545 }
Chris Lattner2135bc02007-05-17 18:15:41 +00005546
5547 case ISD::ADDC:
5548 case ISD::SUBC: {
5549 // Expand the subcomponents.
5550 SDOperand LHSL, LHSH, RHSL, RHSH;
5551 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5552 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5553 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5554 SDOperand LoOps[2] = { LHSL, RHSL };
5555 SDOperand HiOps[3] = { LHSH, RHSH };
5556
5557 if (Node->getOpcode() == ISD::ADDC) {
5558 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5559 HiOps[2] = Lo.getValue(1);
5560 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5561 } else {
5562 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5563 HiOps[2] = Lo.getValue(1);
5564 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5565 }
5566 // Remember that we legalized the flag.
5567 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5568 break;
5569 }
5570 case ISD::ADDE:
5571 case ISD::SUBE: {
5572 // Expand the subcomponents.
5573 SDOperand LHSL, LHSH, RHSL, RHSH;
5574 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5575 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5576 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5577 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5578 SDOperand HiOps[3] = { LHSH, RHSH };
5579
5580 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5581 HiOps[2] = Lo.getValue(1);
5582 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5583
5584 // Remember that we legalized the flag.
5585 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5586 break;
5587 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005588 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005589 // If the target wants to custom expand this, let them.
5590 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00005591 SDOperand New = TLI.LowerOperation(Op, DAG);
5592 if (New.Val) {
5593 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005594 break;
5595 }
5596 }
5597
Evan Chenge93762d2006-09-01 18:17:58 +00005598 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5599 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Evan Chenge93762d2006-09-01 18:17:58 +00005600 if (HasMULHS || HasMULHU) {
Nate Begemanadd0c632005-04-11 03:01:51 +00005601 SDOperand LL, LH, RL, RH;
5602 ExpandOp(Node->getOperand(0), LL, LH);
5603 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00005604 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
Nate Begeman8e20c762006-12-11 02:23:46 +00005605 // FIXME: Move this to the dag combiner.
Nate Begeman43144a22005-08-30 02:44:00 +00005606 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
5607 // extended the sign bit of the low half through the upper half, and if so
5608 // emit a MULHS instead of the alternate sequence that is valid for any
5609 // i64 x i64 multiply.
Evan Chenge93762d2006-09-01 18:17:58 +00005610 if (HasMULHS &&
Nate Begeman43144a22005-08-30 02:44:00 +00005611 // is RH an extension of the sign bit of RL?
5612 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
5613 RH.getOperand(1).getOpcode() == ISD::Constant &&
5614 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
5615 // is LH an extension of the sign bit of LL?
5616 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
5617 LH.getOperand(1).getOpcode() == ISD::Constant &&
5618 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
Chris Lattner1b633912006-09-16 00:21:44 +00005619 // Low part:
5620 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5621 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005622 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
Chris Lattner1b633912006-09-16 00:21:44 +00005623 break;
Evan Chenge93762d2006-09-01 18:17:58 +00005624 } else if (HasMULHU) {
Chris Lattner1b633912006-09-16 00:21:44 +00005625 // Low part:
5626 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5627
5628 // High part:
Nate Begeman43144a22005-08-30 02:44:00 +00005629 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5630 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5631 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5632 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5633 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00005634 break;
Nate Begeman43144a22005-08-30 02:44:00 +00005635 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005636 }
Evan Chenge93762d2006-09-01 18:17:58 +00005637
Evan Cheng31cbddf2007-01-12 02:11:51 +00005638 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5639 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00005640 break;
5641 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005642 case ISD::SDIV:
5643 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5644 break;
5645 case ISD::UDIV:
5646 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5647 break;
5648 case ISD::SREM:
5649 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5650 break;
5651 case ISD::UREM:
5652 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5653 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00005654
Evan Cheng4eee7242006-12-09 02:42:38 +00005655 case ISD::FADD:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005656 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5657 ? RTLIB::ADD_F32 : RTLIB::ADD_F64),
5658 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005659 break;
5660 case ISD::FSUB:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005661 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5662 ? RTLIB::SUB_F32 : RTLIB::SUB_F64),
5663 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005664 break;
5665 case ISD::FMUL:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005666 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5667 ? RTLIB::MUL_F32 : RTLIB::MUL_F64),
5668 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005669 break;
5670 case ISD::FDIV:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005671 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5672 ? RTLIB::DIV_F32 : RTLIB::DIV_F64),
5673 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005674 break;
5675 case ISD::FP_EXTEND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005676 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005677 break;
5678 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005679 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005680 break;
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00005681 case ISD::FPOWI:
5682 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32)
5683 ? RTLIB::POWI_F32 : RTLIB::POWI_F64),
5684 Node, false, Hi);
5685 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005686 case ISD::FSQRT:
5687 case ISD::FSIN:
5688 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005689 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00005690 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00005691 case ISD::FSQRT:
5692 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64;
5693 break;
5694 case ISD::FSIN:
5695 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
5696 break;
5697 case ISD::FCOS:
5698 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
5699 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00005700 default: assert(0 && "Unreachable!");
5701 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005702 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00005703 break;
5704 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00005705 case ISD::FABS: {
5706 SDOperand Mask = (VT == MVT::f64)
5707 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
5708 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
5709 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5710 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5711 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
5712 if (getTypeAction(NVT) == Expand)
5713 ExpandOp(Lo, Lo, Hi);
5714 break;
5715 }
5716 case ISD::FNEG: {
5717 SDOperand Mask = (VT == MVT::f64)
5718 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
5719 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
5720 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
5721 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
5722 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
5723 if (getTypeAction(NVT) == Expand)
5724 ExpandOp(Lo, Lo, Hi);
5725 break;
5726 }
Evan Cheng003feb02007-01-04 21:56:39 +00005727 case ISD::FCOPYSIGN: {
5728 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
5729 if (getTypeAction(NVT) == Expand)
5730 ExpandOp(Lo, Lo, Hi);
5731 break;
5732 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005733 case ISD::SINT_TO_FP:
5734 case ISD::UINT_TO_FP: {
5735 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
5736 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Evan Cheng31cbddf2007-01-12 02:11:51 +00005737 RTLIB::Libcall LC;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005738 if (Node->getOperand(0).getValueType() == MVT::i64) {
5739 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005740 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005741 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005742 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005743 } else {
5744 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005745 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005746 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00005747 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005748 }
5749
5750 // Promote the operand if needed.
5751 if (getTypeAction(SrcVT) == Promote) {
5752 SDOperand Tmp = PromoteOp(Node->getOperand(0));
5753 Tmp = isSigned
5754 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
5755 DAG.getValueType(SrcVT))
5756 : DAG.getZeroExtendInReg(Tmp, SrcVT);
5757 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
5758 }
Evan Chengbf535fc2007-04-27 07:33:31 +00005759
5760 const char *LibCall = TLI.getLibcallName(LC);
5761 if (LibCall)
5762 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
5763 else {
5764 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
5765 Node->getOperand(0));
5766 if (getTypeAction(Lo.getValueType()) == Expand)
5767 ExpandOp(Lo, Lo, Hi);
5768 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00005769 break;
5770 }
Evan Chengf3a80c62006-12-13 02:38:13 +00005771 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005772
Chris Lattnerac12f682005-12-21 18:02:52 +00005773 // Make sure the resultant values have been legalized themselves, unless this
5774 // is a type that requires multi-step expansion.
5775 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
5776 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00005777 if (Hi.Val)
5778 // Don't legalize the high part if it is expanded to a single node.
5779 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00005780 }
Evan Cheng870e4f82006-01-09 18:31:59 +00005781
5782 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005783 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00005784 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00005785}
5786
Dan Gohmana8665142007-06-25 16:23:39 +00005787/// SplitVectorOp - Given an operand of vector type, break it down into
5788/// two smaller values, still of vector type.
Chris Lattner32206f52006-03-18 01:44:44 +00005789void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
5790 SDOperand &Hi) {
Dan Gohmana8665142007-06-25 16:23:39 +00005791 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00005792 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00005793 unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0));
Chris Lattner32206f52006-03-18 01:44:44 +00005794 assert(NumElements > 1 && "Cannot split a single element vector!");
5795 unsigned NewNumElts = NumElements/2;
Dan Gohmana8665142007-06-25 16:23:39 +00005796 MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0));
5797 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattner32206f52006-03-18 01:44:44 +00005798
5799 // See if we already split it.
5800 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
5801 = SplitNodes.find(Op);
5802 if (I != SplitNodes.end()) {
5803 Lo = I->second.first;
5804 Hi = I->second.second;
5805 return;
5806 }
5807
5808 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005809 default:
5810#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005811 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005812#endif
5813 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohmana8665142007-06-25 16:23:39 +00005814 case ISD::BUILD_PAIR:
5815 Lo = Node->getOperand(0);
5816 Hi = Node->getOperand(1);
5817 break;
5818 case ISD::BUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005819 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5820 Node->op_begin()+NewNumElts);
Dan Gohmana8665142007-06-25 16:23:39 +00005821 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005822
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005823 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohmana8665142007-06-25 16:23:39 +00005824 Node->op_end());
5825 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00005826 break;
5827 }
Dan Gohmana8665142007-06-25 16:23:39 +00005828 case ISD::CONCAT_VECTORS: {
5829 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
5830 if (NewNumSubvectors == 1) {
5831 Lo = Node->getOperand(0);
5832 Hi = Node->getOperand(1);
5833 } else {
5834 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
5835 Node->op_begin()+NewNumSubvectors);
5836 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00005837
Dan Gohmana8665142007-06-25 16:23:39 +00005838 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
5839 Node->op_end());
5840 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
5841 }
Dan Gohman26455c42007-06-13 15:12:02 +00005842 break;
5843 }
Dan Gohmana8665142007-06-25 16:23:39 +00005844 case ISD::ADD:
5845 case ISD::SUB:
5846 case ISD::MUL:
5847 case ISD::FADD:
5848 case ISD::FSUB:
5849 case ISD::FMUL:
5850 case ISD::SDIV:
5851 case ISD::UDIV:
5852 case ISD::FDIV:
5853 case ISD::AND:
5854 case ISD::OR:
5855 case ISD::XOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00005856 SDOperand LL, LH, RL, RH;
5857 SplitVectorOp(Node->getOperand(0), LL, LH);
5858 SplitVectorOp(Node->getOperand(1), RL, RH);
5859
Dan Gohmana8665142007-06-25 16:23:39 +00005860 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
5861 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00005862 break;
5863 }
Dan Gohmana8665142007-06-25 16:23:39 +00005864 case ISD::LOAD: {
5865 LoadSDNode *LD = cast<LoadSDNode>(Node);
5866 SDOperand Ch = LD->getChain();
5867 SDOperand Ptr = LD->getBasePtr();
5868 const Value *SV = LD->getSrcValue();
5869 int SVOffset = LD->getSrcValueOffset();
5870 unsigned Alignment = LD->getAlignment();
5871 bool isVolatile = LD->isVolatile();
5872
5873 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
5874 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattner32206f52006-03-18 01:44:44 +00005875 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5876 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005877 SVOffset += IncrementSize;
5878 if (Alignment > IncrementSize)
5879 Alignment = IncrementSize;
5880 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00005881
5882 // Build a factor node to remember that this load is independent of the
5883 // other one.
5884 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5885 Hi.getValue(1));
5886
5887 // Remember that we legalized the chain.
5888 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00005889 break;
5890 }
Dan Gohmana8665142007-06-25 16:23:39 +00005891 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005892 // We know the result is a vector. The input may be either a vector or a
5893 // scalar value.
Dan Gohman0de76942007-06-29 00:09:08 +00005894 SDOperand InOp = Node->getOperand(0);
5895 if (!MVT::isVector(InOp.getValueType()) ||
5896 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
5897 // The input is a scalar or single-element vector.
5898 // Lower to a store/load so that it can be split.
5899 // FIXME: this could be improved probably.
5900 SDOperand Ptr = CreateStackTemporary(InOp.getValueType());
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005901
Evan Chengdf9ac472006-10-05 23:01:46 +00005902 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman0de76942007-06-29 00:09:08 +00005903 InOp, Ptr, NULL, 0);
5904 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005905 }
Dan Gohman0de76942007-06-29 00:09:08 +00005906 // Split the vector and convert each of the pieces now.
5907 SplitVectorOp(InOp, Lo, Hi);
5908 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
5909 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
5910 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00005911 }
Chris Lattner32206f52006-03-18 01:44:44 +00005912 }
5913
5914 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005915 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00005916 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00005917 assert(isNew && "Value already split?!?");
Chris Lattner32206f52006-03-18 01:44:44 +00005918}
5919
5920
Dan Gohmanf4e86da2007-06-27 14:06:22 +00005921/// ScalarizeVectorOp - Given an operand of single-element vector type
5922/// (e.g. v1f32), convert it into the equivalent operation that returns a
5923/// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +00005924SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
5925 assert(MVT::isVector(Op.getValueType()) &&
5926 "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00005927 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00005928 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
5929 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00005930
Dan Gohmana8665142007-06-25 16:23:39 +00005931 // See if we already scalarized it.
5932 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
5933 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00005934
5935 SDOperand Result;
5936 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00005937 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005938#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005939 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005940#endif
Dan Gohmana8665142007-06-25 16:23:39 +00005941 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
5942 case ISD::ADD:
5943 case ISD::FADD:
5944 case ISD::SUB:
5945 case ISD::FSUB:
5946 case ISD::MUL:
5947 case ISD::FMUL:
5948 case ISD::SDIV:
5949 case ISD::UDIV:
5950 case ISD::FDIV:
5951 case ISD::SREM:
5952 case ISD::UREM:
5953 case ISD::FREM:
5954 case ISD::AND:
5955 case ISD::OR:
5956 case ISD::XOR:
5957 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00005958 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00005959 ScalarizeVectorOp(Node->getOperand(0)),
5960 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00005961 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005962 case ISD::FNEG:
5963 case ISD::FABS:
5964 case ISD::FSQRT:
5965 case ISD::FSIN:
5966 case ISD::FCOS:
5967 Result = DAG.getNode(Node->getOpcode(),
5968 NewVT,
5969 ScalarizeVectorOp(Node->getOperand(0)));
5970 break;
5971 case ISD::LOAD: {
5972 LoadSDNode *LD = cast<LoadSDNode>(Node);
5973 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
5974 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00005975
Dan Gohmana8665142007-06-25 16:23:39 +00005976 const Value *SV = LD->getSrcValue();
5977 int SVOffset = LD->getSrcValueOffset();
5978 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
5979 LD->isVolatile(), LD->getAlignment());
5980
Chris Lattner32206f52006-03-18 01:44:44 +00005981 // Remember that we legalized the chain.
5982 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
5983 break;
5984 }
Dan Gohmana8665142007-06-25 16:23:39 +00005985 case ISD::BUILD_VECTOR:
5986 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00005987 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005988 case ISD::INSERT_VECTOR_ELT:
5989 // Returning the inserted scalar element.
5990 Result = Node->getOperand(1);
5991 break;
5992 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00005993 assert(Node->getOperand(0).getValueType() == NewVT &&
5994 "Concat of non-legal vectors not yet supported!");
5995 Result = Node->getOperand(0);
5996 break;
Dan Gohmana8665142007-06-25 16:23:39 +00005997 case ISD::VECTOR_SHUFFLE: {
5998 // Figure out if the scalar is the LHS or RHS and return it.
5999 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6000 if (cast<ConstantSDNode>(EltNum)->getValue())
6001 Result = ScalarizeVectorOp(Node->getOperand(1));
6002 else
6003 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00006004 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006005 }
6006 case ISD::EXTRACT_SUBVECTOR:
6007 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00006008 assert(Result.getValueType() == NewVT);
6009 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006010 case ISD::BIT_CONVERT:
6011 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattnerf6f94d32006-03-28 20:24:43 +00006012 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006013 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00006014 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00006015 ScalarizeVectorOp(Op.getOperand(1)),
6016 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00006017 break;
Chris Lattner32206f52006-03-18 01:44:44 +00006018 }
6019
6020 if (TLI.isTypeLegal(NewVT))
6021 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00006022 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6023 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00006024 return Result;
6025}
6026
Chris Lattnerdc750592005-01-07 07:47:09 +00006027
6028// SelectionDAG::Legalize - This is the entry point for the file.
6029//
Chris Lattner4add7e32005-01-23 04:42:50 +00006030void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00006031 if (ViewLegalizeDAGs) viewGraph();
6032
Chris Lattnerdc750592005-01-07 07:47:09 +00006033 /// run - This is the main entry point to this class.
6034 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00006035 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00006036}
6037