blob: 9c0c845932f7ac8edc3d58274b2b3aad21443614 [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/CommandLine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Duncan Sands1826ded2007-10-28 12:59:45 +000028#include "llvm/Support/MathExtras.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
Dan Gohman2a7de412007-10-11 23:57:53 +0000150 /// UnrollVectorOp - We know that the given vector has a legal type, however
151 /// the operation it performs is not legal and is an operation that we have
152 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
153 /// operating on each element individually.
154 SDOperand UnrollVectorOp(SDOperand O);
155
Chris Lattner32206f52006-03-18 01:44:44 +0000156 /// PromoteOp - Given an operation that produces a value in an invalid type,
157 /// promote it to compute the value into a larger type. The produced value
158 /// will have the correct bits for the low portion of the register, but no
159 /// guarantee is made about the top bits: it may be zero, sign-extended, or
160 /// garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000161 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000162
Chris Lattner32206f52006-03-18 01:44:44 +0000163 /// ExpandOp - Expand the specified SDOperand into its two component pieces
164 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
165 /// the LegalizeNodes map is filled in for any results that are not expanded,
166 /// the ExpandedNodes map is filled in for any results that are expanded, and
167 /// the Lo/Hi values are returned. This applies to integer types and Vector
168 /// types.
169 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
170
Dan Gohmana8665142007-06-25 16:23:39 +0000171 /// SplitVectorOp - Given an operand of vector type, break it down into
172 /// two smaller values.
Chris Lattner32206f52006-03-18 01:44:44 +0000173 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
174
Dan Gohmanf4e86da2007-06-27 14:06:22 +0000175 /// ScalarizeVectorOp - Given an operand of single-element vector type
176 /// (e.g. v1f32), convert it into the equivalent operation that returns a
177 /// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +0000178 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000179
Chris Lattner6be79822006-04-04 17:23:26 +0000180 /// isShuffleLegal - Return true if a vector shuffle is legal with the
181 /// specified mask and type. Targets can specify exactly which masks they
182 /// support and the code generator is tasked with not creating illegal masks.
183 ///
184 /// Note that this will also return true for shuffles that are promoted to a
185 /// different type.
186 ///
187 /// If this is a legal shuffle, this method returns the (possibly promoted)
188 /// build_vector Mask. If it's not a legal shuffle, it returns null.
189 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
190
Chris Lattner4488f0c2006-07-26 23:55:56 +0000191 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000192 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000193
Nate Begeman7e7f4392006-02-01 07:19:44 +0000194 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
195
Reid Spencere63b6512006-12-31 05:55:36 +0000196 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattneraac464e2005-01-21 06:05:23 +0000197 SDOperand &Hi);
198 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
199 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000200
Chris Lattner36e663d2005-12-23 00:16:34 +0000201 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000202 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner6be79822006-04-04 17:23:26 +0000203 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000204 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
205 SDOperand LegalOp,
206 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000207 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
208 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000209 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
210 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000211
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000212 SDOperand ExpandBSWAP(SDOperand Op);
213 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000214 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
215 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000216 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
217 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000218
Dan Gohmana8665142007-06-25 16:23:39 +0000219 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner42a5fca2006-04-02 05:06:04 +0000220 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner6f423252006-03-31 17:55:51 +0000221
Chris Lattnerdc750592005-01-07 07:47:09 +0000222 SDOperand getIntPtrConstant(uint64_t Val) {
223 return DAG.getConstant(Val, TLI.getPointerTy());
224 }
225};
226}
227
Chris Lattner6be79822006-04-04 17:23:26 +0000228/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
229/// specified mask and type. Targets can specify exactly which masks they
230/// support and the code generator is tasked with not creating illegal masks.
231///
232/// Note that this will also return true for shuffles that are promoted to a
233/// different type.
234SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
235 SDOperand Mask) const {
236 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
237 default: return 0;
238 case TargetLowering::Legal:
239 case TargetLowering::Custom:
240 break;
241 case TargetLowering::Promote: {
242 // If this is promoted to a different type, convert the shuffle mask and
243 // ask if it is legal in the promoted type!
244 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
245
246 // If we changed # elements, change the shuffle mask.
247 unsigned NumEltsGrowth =
248 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
249 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
250 if (NumEltsGrowth > 1) {
251 // Renumber the elements.
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000252 SmallVector<SDOperand, 8> Ops;
Chris Lattner6be79822006-04-04 17:23:26 +0000253 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
254 SDOperand InOp = Mask.getOperand(i);
255 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
256 if (InOp.getOpcode() == ISD::UNDEF)
257 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
258 else {
259 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
260 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
261 }
262 }
263 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000264 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +0000265 }
266 VT = NVT;
267 break;
268 }
269 }
270 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
271}
272
Chris Lattner4add7e32005-01-23 04:42:50 +0000273SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
274 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
275 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000276 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000277 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000278}
279
Chris Lattnere31adc82007-06-18 21:28:10 +0000280/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
281/// contains all of a nodes operands before it contains the node.
282static void ComputeTopDownOrdering(SelectionDAG &DAG,
283 SmallVector<SDNode*, 64> &Order) {
284
285 DenseMap<SDNode*, unsigned> Visited;
286 std::vector<SDNode*> Worklist;
287 Worklist.reserve(128);
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000288
Chris Lattnere31adc82007-06-18 21:28:10 +0000289 // Compute ordering from all of the leaves in the graphs, those (like the
290 // entry node) that have no operands.
291 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
292 E = DAG.allnodes_end(); I != E; ++I) {
293 if (I->getNumOperands() == 0) {
294 Visited[I] = 0 - 1U;
295 Worklist.push_back(I);
296 }
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000297 }
298
Chris Lattnere31adc82007-06-18 21:28:10 +0000299 while (!Worklist.empty()) {
300 SDNode *N = Worklist.back();
301 Worklist.pop_back();
302
303 if (++Visited[N] != N->getNumOperands())
304 continue; // Haven't visited all operands yet
305
306 Order.push_back(N);
307
308 // Now that we have N in, add anything that uses it if all of their operands
309 // are now done.
310 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
311 UI != E; ++UI)
312 Worklist.push_back(*UI);
313 }
314
315 assert(Order.size() == Visited.size() &&
316 Order.size() ==
317 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
318 "Error: DAG is cyclic!");
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000319}
320
Chris Lattner44fe26f2005-07-29 00:11:56 +0000321
Chris Lattnerdc750592005-01-07 07:47:09 +0000322void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000323 LastCALLSEQ_END = DAG.getEntryNode();
324 IsLegalizingCall = false;
325
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000326 // The legalize process is inherently a bottom-up recursive process (users
327 // legalize their uses before themselves). Given infinite stack space, we
328 // could just start legalizing on the root and traverse the whole graph. In
329 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000330 // blocks. To avoid this problem, compute an ordering of the nodes where each
331 // node is only legalized after all of its operands are legalized.
Chris Lattner94c44c92007-02-04 01:20:02 +0000332 SmallVector<SDNode*, 64> Order;
Chris Lattnere31adc82007-06-18 21:28:10 +0000333 ComputeTopDownOrdering(DAG, Order);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000334
Chris Lattner32206f52006-03-18 01:44:44 +0000335 for (unsigned i = 0, e = Order.size(); i != e; ++i)
336 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000337
338 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000339 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000340 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
341 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000342
343 ExpandedNodes.clear();
344 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000345 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000346 SplitNodes.clear();
Dan Gohmana8665142007-06-25 16:23:39 +0000347 ScalarizedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000348
349 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000350 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000351}
352
Chris Lattner462505f2006-02-13 09:18:02 +0000353
354/// FindCallEndFromCallStart - Given a chained node that is part of a call
355/// sequence, find the CALLSEQ_END node that terminates the call sequence.
356static SDNode *FindCallEndFromCallStart(SDNode *Node) {
357 if (Node->getOpcode() == ISD::CALLSEQ_END)
358 return Node;
359 if (Node->use_empty())
360 return 0; // No CallSeqEnd
361
362 // The chain is usually at the end.
363 SDOperand TheChain(Node, Node->getNumValues()-1);
364 if (TheChain.getValueType() != MVT::Other) {
365 // Sometimes it's at the beginning.
366 TheChain = SDOperand(Node, 0);
367 if (TheChain.getValueType() != MVT::Other) {
368 // Otherwise, hunt for it.
369 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
370 if (Node->getValueType(i) == MVT::Other) {
371 TheChain = SDOperand(Node, i);
372 break;
373 }
374
375 // Otherwise, we walked into a node without a chain.
376 if (TheChain.getValueType() != MVT::Other)
377 return 0;
378 }
379 }
380
381 for (SDNode::use_iterator UI = Node->use_begin(),
382 E = Node->use_end(); UI != E; ++UI) {
383
384 // Make sure to only follow users of our token chain.
385 SDNode *User = *UI;
386 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
387 if (User->getOperand(i) == TheChain)
388 if (SDNode *Result = FindCallEndFromCallStart(User))
389 return Result;
390 }
391 return 0;
392}
393
394/// FindCallStartFromCallEnd - Given a chained node that is part of a call
395/// sequence, find the CALLSEQ_START node that initiates the call sequence.
396static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
397 assert(Node && "Didn't find callseq_start for a call??");
398 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
399
400 assert(Node->getOperand(0).getValueType() == MVT::Other &&
401 "Node doesn't have a token chain argument!");
402 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
403}
404
405/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
406/// see if any uses can reach Dest. If no dest operands can get to dest,
407/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000408///
409/// Keep track of the nodes we fine that actually do lead to Dest in
410/// NodesLeadingTo. This avoids retraversing them exponential number of times.
411///
412bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000413 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000414 if (N == Dest) return true; // N certainly leads to Dest :)
415
Chris Lattner4488f0c2006-07-26 23:55:56 +0000416 // If we've already processed this node and it does lead to Dest, there is no
417 // need to reprocess it.
418 if (NodesLeadingTo.count(N)) return true;
419
Chris Lattner462505f2006-02-13 09:18:02 +0000420 // If the first result of this node has been already legalized, then it cannot
421 // reach N.
422 switch (getTypeAction(N->getValueType(0))) {
423 case Legal:
424 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
425 break;
426 case Promote:
427 if (PromotedNodes.count(SDOperand(N, 0))) return false;
428 break;
429 case Expand:
430 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
431 break;
432 }
433
434 // Okay, this node has not already been legalized. Check and legalize all
435 // operands. If none lead to Dest, then we can legalize this node.
436 bool OperandsLeadToDest = false;
437 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
438 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000439 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000440
Chris Lattner4488f0c2006-07-26 23:55:56 +0000441 if (OperandsLeadToDest) {
442 NodesLeadingTo.insert(N);
443 return true;
444 }
Chris Lattner462505f2006-02-13 09:18:02 +0000445
446 // Okay, this node looks safe, legalize it and return false.
Chris Lattner916ae072006-04-17 22:10:08 +0000447 HandleOp(SDOperand(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000448 return false;
449}
450
Dan Gohmana8665142007-06-25 16:23:39 +0000451/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000452/// appropriate for its type.
453void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohmana8665142007-06-25 16:23:39 +0000454 MVT::ValueType VT = Op.getValueType();
455 switch (getTypeAction(VT)) {
Chris Lattner32206f52006-03-18 01:44:44 +0000456 default: assert(0 && "Bad type action!");
Dan Gohmana8665142007-06-25 16:23:39 +0000457 case Legal: (void)LegalizeOp(Op); break;
458 case Promote: (void)PromoteOp(Op); break;
Chris Lattner32206f52006-03-18 01:44:44 +0000459 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +0000460 if (!MVT::isVector(VT)) {
461 // If this is an illegal scalar, expand it into its two component
462 // pieces.
Chris Lattner32206f52006-03-18 01:44:44 +0000463 SDOperand X, Y;
Chris Lattner2ed652f2007-08-25 01:00:22 +0000464 if (Op.getOpcode() == ISD::TargetConstant)
465 break; // Allow illegal target nodes.
Chris Lattner32206f52006-03-18 01:44:44 +0000466 ExpandOp(Op, X, Y);
Dan Gohmana8665142007-06-25 16:23:39 +0000467 } else if (MVT::getVectorNumElements(VT) == 1) {
468 // If this is an illegal single element vector, convert it to a
469 // scalar operation.
470 (void)ScalarizeVectorOp(Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000471 } else {
Dan Gohmana8665142007-06-25 16:23:39 +0000472 // Otherwise, this is an illegal multiple element vector.
473 // Split it in half and legalize both parts.
474 SDOperand X, Y;
475 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000476 }
477 break;
478 }
479}
Chris Lattner462505f2006-02-13 09:18:02 +0000480
Evan Cheng22cf8992006-12-13 20:57:08 +0000481/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
482/// a load from the constant pool.
483static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000484 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000485 bool Extend = false;
486
487 // If a FP immediate is precise when represented as a float and if the
488 // target can do an extending load from float to double, we put it into
489 // the constant pool as a float, even if it's is statically typed as a
490 // double.
491 MVT::ValueType VT = CFP->getValueType(0);
492 bool isDouble = VT == MVT::f64;
Dale Johannesen7f724e92007-09-16 16:51:49 +0000493 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen98d3a082007-09-14 22:26:36 +0000494 CFP->getValueAPF());
Evan Cheng22cf8992006-12-13 20:57:08 +0000495 if (!UseCP) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000496 if (VT!=MVT::f64 && VT!=MVT::f32)
497 assert(0 && "Invalid type expansion");
Dale Johannesen028084e2007-09-12 03:30:33 +0000498 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
499 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000500 }
501
Dale Johannesend246b2c2007-08-30 00:23:21 +0000502 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng47833a12006-12-12 21:32:44 +0000503 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen98d3a082007-09-14 22:26:36 +0000504 // Do not try to be clever about long doubles (so far)
Evan Cheng47833a12006-12-12 21:32:44 +0000505 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
506 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
507 VT = MVT::f32;
508 Extend = true;
509 }
510
511 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
512 if (Extend) {
513 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
514 CPIdx, NULL, 0, MVT::f32);
515 } else {
516 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
517 }
518}
519
Chris Lattner462505f2006-02-13 09:18:02 +0000520
Evan Cheng003feb02007-01-04 21:56:39 +0000521/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
522/// operations.
523static
524SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
525 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng3b841dd2007-01-05 21:31:51 +0000526 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000527 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000528 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
529 "fcopysign expansion only supported for f32 and f64");
Evan Cheng003feb02007-01-04 21:56:39 +0000530 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000531
Evan Cheng003feb02007-01-04 21:56:39 +0000532 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000533 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000534 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
535 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000536 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000537 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000538 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000539 // Shift right or sign-extend it if the two operands have different types.
540 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
541 if (SizeDiff > 0) {
542 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
543 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
544 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
545 } else if (SizeDiff < 0)
546 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000547
548 // Clear the sign bit of first operand.
549 SDOperand Mask2 = (VT == MVT::f64)
550 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
551 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
552 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng003feb02007-01-04 21:56:39 +0000553 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000554 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
555
556 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000557 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
558 return Result;
559}
560
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000561/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
562static
563SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
564 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000565 SDOperand Chain = ST->getChain();
566 SDOperand Ptr = ST->getBasePtr();
567 SDOperand Val = ST->getValue();
568 MVT::ValueType VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000569 int Alignment = ST->getAlignment();
570 int SVOffset = ST->getSrcValueOffset();
571 if (MVT::isFloatingPoint(ST->getStoredVT())) {
572 // Expand to a bitconvert of the value to the integer type of the
573 // same size, then a (misaligned) int store.
574 MVT::ValueType intVT;
575 if (VT==MVT::f64)
576 intVT = MVT::i64;
577 else if (VT==MVT::f32)
578 intVT = MVT::i32;
579 else
580 assert(0 && "Unaligned load of unsupported floating point type");
581
582 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
583 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
584 SVOffset, ST->isVolatile(), Alignment);
585 }
586 assert(MVT::isInteger(ST->getStoredVT()) &&
587 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000588 // Get the half-size VT
589 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
590 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000591 int IncrementSize = NumBits / 8;
592
593 // Divide the stored value in two parts.
594 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
595 SDOperand Lo = Val;
596 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
597
598 // Store the two parts
599 SDOperand Store1, Store2;
600 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
601 ST->getSrcValue(), SVOffset, NewStoredVT,
602 ST->isVolatile(), Alignment);
603 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
604 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sands1826ded2007-10-28 12:59:45 +0000605 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000606 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(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000664 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000665 } 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(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000672 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000673 }
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 Gohman2a7de412007-10-11 23:57:53 +0000687/// UnrollVectorOp - We know that the given vector has a legal type, however
688/// the operation it performs is not legal and is an operation that we have
689/// no way of lowering. "Unroll" the vector, splitting out the scalars and
690/// operating on each element individually.
691SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
692 MVT::ValueType VT = Op.getValueType();
693 assert(isTypeLegal(VT) &&
694 "Caller should expand or promote operands that are not legal!");
695 assert(Op.Val->getNumValues() == 1 &&
696 "Can't unroll a vector with multiple results!");
697 unsigned NE = MVT::getVectorNumElements(VT);
698 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
699
700 SmallVector<SDOperand, 8> Scalars;
701 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
702 for (unsigned i = 0; i != NE; ++i) {
703 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
704 SDOperand Operand = Op.getOperand(j);
705 MVT::ValueType OperandVT = Operand.getValueType();
706 if (MVT::isVector(OperandVT)) {
707 // A vector operand; extract a single element.
708 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
709 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
710 OperandEltVT,
711 Operand,
712 DAG.getConstant(i, MVT::i32));
713 } else {
714 // A scalar operand; just use it as is.
715 Operands[j] = Operand;
716 }
717 }
718 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
719 &Operands[0], Operands.size()));
720 }
721
722 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
723}
724
Dan Gohmanff727882007-07-13 20:14:11 +0000725/// LegalizeOp - We know that the specified value has a legal type, and
726/// that its operands are legal. Now ensure that the operation itself
727/// is legal, recursively ensuring that the operands' operations remain
728/// legal.
Chris Lattnerdc750592005-01-07 07:47:09 +0000729SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner2ed652f2007-08-25 01:00:22 +0000730 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
731 return Op;
732
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000733 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000734 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000735 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000736
Chris Lattnerdc750592005-01-07 07:47:09 +0000737 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000738 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000739 if (Node->getNumValues() > 1) {
740 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000741 if (getTypeAction(Node->getValueType(i)) != Legal) {
742 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000743 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000744 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000745 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000746 }
747 }
748
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000749 // Note that LegalizeOp may be reentered even from single-use nodes, which
750 // means that we always must cache transformed nodes.
Chris Lattnered39c862007-02-04 00:50:02 +0000751 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000752 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000753
Nate Begemane5b86d72005-08-10 20:51:12 +0000754 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000755 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000756 bool isCustom = false;
757
Chris Lattnerdc750592005-01-07 07:47:09 +0000758 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000759 case ISD::FrameIndex:
760 case ISD::EntryToken:
761 case ISD::Register:
762 case ISD::BasicBlock:
763 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000764 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000765 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000766 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000767 case ISD::TargetConstantPool:
768 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000769 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000770 case ISD::TargetExternalSymbol:
771 case ISD::VALUETYPE:
772 case ISD::SRCVALUE:
773 case ISD::STRING:
774 case ISD::CONDCODE:
775 // Primitives must all be legal.
Duncan Sands052c8432007-10-16 09:07:20 +0000776 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattnereb637512006-01-28 08:31:04 +0000777 "This must be legal!");
778 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000779 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000780 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
781 // If this is a target node, legalize it by legalizing the operands then
782 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000783 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000784 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000785 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000786
Chris Lattner97af9d52006-08-08 01:09:31 +0000787 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000788
789 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
790 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
791 return Result.getValue(Op.ResNo);
792 }
793 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000794#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000795 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000796#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000797 assert(0 && "Do not know how to legalize this operator!");
798 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000799 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000800 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000801 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000802 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000803 case ISD::ConstantPool:
804 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000805 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
806 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000807 case TargetLowering::Custom:
808 Tmp1 = TLI.LowerOperation(Op, DAG);
809 if (Tmp1.Val) Result = Tmp1;
810 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000811 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000812 break;
813 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000814 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000815 case ISD::FRAMEADDR:
816 case ISD::RETURNADDR:
817 // The only option for these nodes is to custom lower them. If the target
818 // does not custom lower them, then return zero.
819 Tmp1 = TLI.LowerOperation(Op, DAG);
820 if (Tmp1.Val)
821 Result = Tmp1;
822 else
823 Result = DAG.getConstant(0, TLI.getPointerTy());
824 break;
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000825 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000826 MVT::ValueType VT = Node->getValueType(0);
827 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
828 default: assert(0 && "This action is not supported yet!");
829 case TargetLowering::Custom:
830 Result = TLI.LowerOperation(Op, DAG);
831 if (Result.Val) break;
832 // Fall Thru
833 case TargetLowering::Legal:
834 Result = DAG.getConstant(0, VT);
835 break;
836 }
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000837 }
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000838 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000839 case ISD::EXCEPTIONADDR: {
840 Tmp1 = LegalizeOp(Node->getOperand(0));
841 MVT::ValueType VT = Node->getValueType(0);
842 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
843 default: assert(0 && "This action is not supported yet!");
844 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000845 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey7f5872c2007-02-22 15:37:19 +0000846 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
847 }
848 break;
849 case TargetLowering::Custom:
850 Result = TLI.LowerOperation(Op, DAG);
851 if (Result.Val) break;
852 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000853 case TargetLowering::Legal: {
854 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
855 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
856 Ops, 2).getValue(Op.ResNo);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000857 break;
858 }
859 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000860 }
Jim Laskey7f5872c2007-02-22 15:37:19 +0000861 break;
Jim Laskey644af6b2007-02-28 20:43:58 +0000862 case ISD::EHSELECTION: {
863 Tmp1 = LegalizeOp(Node->getOperand(0));
864 Tmp2 = LegalizeOp(Node->getOperand(1));
865 MVT::ValueType VT = Node->getValueType(0);
866 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
867 default: assert(0 && "This action is not supported yet!");
868 case TargetLowering::Expand: {
869 unsigned Reg = TLI.getExceptionSelectorRegister();
870 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
871 }
872 break;
873 case TargetLowering::Custom:
874 Result = TLI.LowerOperation(Op, DAG);
875 if (Result.Val) break;
876 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000877 case TargetLowering::Legal: {
878 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
879 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
880 Ops, 2).getValue(Op.ResNo);
Jim Laskey644af6b2007-02-28 20:43:58 +0000881 break;
882 }
883 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000884 }
Jim Laskey644af6b2007-02-28 20:43:58 +0000885 break;
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000886 case ISD::EH_RETURN: {
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000887 MVT::ValueType VT = Node->getValueType(0);
888 // The only "good" option for this node is to custom lower it.
889 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
890 default: assert(0 && "This action is not supported at all!");
891 case TargetLowering::Custom:
892 Result = TLI.LowerOperation(Op, DAG);
893 if (Result.Val) break;
894 // Fall Thru
895 case TargetLowering::Legal:
896 // Target does not know, how to lower this, lower to noop
897 Result = LegalizeOp(Node->getOperand(0));
898 break;
899 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000900 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000901 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000902 case ISD::AssertSext:
903 case ISD::AssertZext:
904 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000905 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000906 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000907 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000908 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000909 Result = Node->getOperand(Op.ResNo);
910 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000911 case ISD::CopyFromReg:
912 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000913 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000914 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000915 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000916 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000917 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000918 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000919 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000920 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
921 } else {
922 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
923 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000924 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
925 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000926 // Since CopyFromReg produces two values, make sure to remember that we
927 // legalized both of them.
928 AddLegalizedOperand(Op.getValue(0), Result);
929 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
930 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000931 case ISD::UNDEF: {
932 MVT::ValueType VT = Op.getValueType();
933 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000934 default: assert(0 && "This action is not supported yet!");
935 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000936 if (MVT::isInteger(VT))
937 Result = DAG.getConstant(0, VT);
938 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf04d37d2007-09-26 17:26:49 +0000939 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
940 VT);
Nate Begemancda9aa72005-04-01 22:34:39 +0000941 else
942 assert(0 && "Unknown value type!");
943 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000944 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000945 break;
946 }
947 break;
948 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000949
Chris Lattnere55d1712006-03-28 00:40:33 +0000950 case ISD::INTRINSIC_W_CHAIN:
951 case ISD::INTRINSIC_WO_CHAIN:
952 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +0000953 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +0000954 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
955 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000956 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +0000957
958 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000959 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000960 TargetLowering::Custom) {
961 Tmp3 = TLI.LowerOperation(Result, DAG);
962 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000963 }
964
965 if (Result.Val->getNumValues() == 1) break;
966
967 // Must have return value and chain result.
968 assert(Result.Val->getNumValues() == 2 &&
969 "Cannot return more than two values!");
970
971 // Since loads produce two values, make sure to remember that we
972 // legalized both of them.
973 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
974 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
975 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000976 }
Chris Lattner435b4022005-11-29 06:21:05 +0000977
978 case ISD::LOCATION:
979 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
980 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
981
982 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
983 case TargetLowering::Promote:
984 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000985 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +0000986 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000987 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +0000988 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000989
Jim Laskeyc56315c2007-01-26 21:22:28 +0000990 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000991 const std::string &FName =
992 cast<StringSDNode>(Node->getOperand(3))->getValue();
993 const std::string &DirName =
994 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000995 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000996
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000997 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +0000998 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000999 SDOperand LineOp = Node->getOperand(1);
1000 SDOperand ColOp = Node->getOperand(2);
1001
1002 if (useDEBUG_LOC) {
1003 Ops.push_back(LineOp); // line #
1004 Ops.push_back(ColOp); // col #
1005 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001006 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001007 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +00001008 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1009 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +00001010 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001011 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskeyf9e54452007-01-26 14:34:52 +00001012 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001013 }
Jim Laskey9e296be2005-12-21 20:51:37 +00001014 } else {
1015 Result = Tmp1; // chain
1016 }
Chris Lattner435b4022005-11-29 06:21:05 +00001017 break;
Chris Lattnerc06da622005-12-18 23:54:29 +00001018 }
Chris Lattner435b4022005-11-29 06:21:05 +00001019 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +00001020 if (Tmp1 != Node->getOperand(0) ||
1021 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001022 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +00001023 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +00001024 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1025 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1026 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1027 } else {
1028 // Otherwise promote them.
1029 Ops.push_back(PromoteOp(Node->getOperand(1)));
1030 Ops.push_back(PromoteOp(Node->getOperand(2)));
1031 }
Chris Lattner435b4022005-11-29 06:21:05 +00001032 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1033 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +00001034 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +00001035 }
1036 break;
1037 }
1038 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001039
1040 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +00001041 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +00001042 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +00001043 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +00001044 case TargetLowering::Legal:
1045 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1046 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1047 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1048 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001049 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001050 break;
1051 }
1052 break;
1053
Jim Laskeyf9e54452007-01-26 14:34:52 +00001054 case ISD::LABEL:
1055 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1056 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001057 default: assert(0 && "This action is not supported yet!");
1058 case TargetLowering::Legal:
1059 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1060 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001061 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +00001062 break;
Chris Lattner567b9252007-03-03 19:21:38 +00001063 case TargetLowering::Expand:
1064 Result = LegalizeOp(Node->getOperand(0));
1065 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001066 }
Nate Begeman5965bd12006-02-17 05:43:56 +00001067 break;
Chris Lattner435b4022005-11-29 06:21:05 +00001068
Scott Michel9d09c5c2007-08-08 23:23:31 +00001069 case ISD::Constant: {
1070 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1071 unsigned opAction =
1072 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1073
Chris Lattnerdc750592005-01-07 07:47:09 +00001074 // We know we don't need to expand constants here, constants only have one
1075 // value and we check that it is fine above.
1076
Scott Michel9d09c5c2007-08-08 23:23:31 +00001077 if (opAction == TargetLowering::Custom) {
1078 Tmp1 = TLI.LowerOperation(Result, DAG);
1079 if (Tmp1.Val)
1080 Result = Tmp1;
1081 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001082 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +00001083 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001084 case ISD::ConstantFP: {
1085 // Spill FP immediates to the constant pool if the target cannot directly
1086 // codegen them. Targets often have some immediate values that can be
1087 // efficiently generated into an FP register without a load. We explicitly
1088 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +00001089 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1090
1091 // Check to see if this FP immediate is already legal.
1092 bool isLegal = false;
1093 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1094 E = TLI.legal_fpimm_end(); I != E; ++I)
1095 if (CFP->isExactlyValue(*I)) {
1096 isLegal = true;
1097 break;
1098 }
1099
Chris Lattner758b0ac2006-01-29 06:26:56 +00001100 // If this is a legal constant, turn it into a TargetConstantFP node.
1101 if (isLegal) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00001102 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1103 CFP->getValueType(0));
Chris Lattner758b0ac2006-01-29 06:26:56 +00001104 break;
1105 }
1106
1107 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1108 default: assert(0 && "This action is not supported yet!");
1109 case TargetLowering::Custom:
1110 Tmp3 = TLI.LowerOperation(Result, DAG);
1111 if (Tmp3.Val) {
1112 Result = Tmp3;
1113 break;
1114 }
1115 // FALLTHROUGH
1116 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +00001117 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001118 }
1119 break;
1120 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001121 case ISD::TokenFactor:
1122 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001123 Tmp1 = LegalizeOp(Node->getOperand(0));
1124 Tmp2 = LegalizeOp(Node->getOperand(1));
1125 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1126 } else if (Node->getNumOperands() == 3) {
1127 Tmp1 = LegalizeOp(Node->getOperand(0));
1128 Tmp2 = LegalizeOp(Node->getOperand(1));
1129 Tmp3 = LegalizeOp(Node->getOperand(2));
1130 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001131 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +00001132 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001133 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001134 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1135 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001136 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001137 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001138 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001139
1140 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001141 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001142 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001143 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1144 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendlingf359fed2007-11-13 00:44:25 +00001145
1146 // The number of incoming and outgoing values should match; unless the final
1147 // outgoing value is a flag.
1148 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1149 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1150 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1151 MVT::Flag)) &&
Chris Lattnera1cec012006-05-17 17:55:45 +00001152 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001153
Chris Lattneraaa23d92006-05-16 22:53:20 +00001154 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001155 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +00001156 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendlingf359fed2007-11-13 00:44:25 +00001157 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1158 continue;
Chris Lattnera1cec012006-05-17 17:55:45 +00001159 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001160 if (Op.ResNo == i)
1161 Tmp2 = Tmp1;
1162 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1163 }
1164 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001165 case ISD::EXTRACT_SUBREG: {
1166 Tmp1 = LegalizeOp(Node->getOperand(0));
1167 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1168 assert(idx && "Operand must be a constant");
1169 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1170 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1171 }
1172 break;
1173 case ISD::INSERT_SUBREG: {
1174 Tmp1 = LegalizeOp(Node->getOperand(0));
1175 Tmp2 = LegalizeOp(Node->getOperand(1));
1176 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1177 assert(idx && "Operand must be a constant");
1178 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1179 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1180 }
1181 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001182 case ISD::BUILD_VECTOR:
1183 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001184 default: assert(0 && "This action is not supported yet!");
1185 case TargetLowering::Custom:
1186 Tmp3 = TLI.LowerOperation(Result, DAG);
1187 if (Tmp3.Val) {
1188 Result = Tmp3;
1189 break;
1190 }
1191 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001192 case TargetLowering::Expand:
1193 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001194 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001195 }
Chris Lattner29b23012006-03-19 01:17:20 +00001196 break;
1197 case ISD::INSERT_VECTOR_ELT:
1198 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1199 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1200 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1201 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1202
1203 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1204 Node->getValueType(0))) {
1205 default: assert(0 && "This action is not supported yet!");
1206 case TargetLowering::Legal:
1207 break;
1208 case TargetLowering::Custom:
1209 Tmp3 = TLI.LowerOperation(Result, DAG);
1210 if (Tmp3.Val) {
1211 Result = Tmp3;
1212 break;
1213 }
1214 // FALLTHROUGH
1215 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001216 // If the insert index is a constant, codegen this as a scalar_to_vector,
1217 // then a shuffle that inserts it into the right position in the vector.
1218 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1219 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1220 Tmp1.getValueType(), Tmp2);
1221
1222 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1223 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman5c441312007-06-14 22:58:02 +00001224 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner326870b2006-04-17 19:21:01 +00001225
1226 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1227 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1228 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001229 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +00001230 for (unsigned i = 0; i != NumElts; ++i) {
1231 if (i != InsertPos->getValue())
1232 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1233 else
1234 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1235 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001236 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1237 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +00001238
1239 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1240 Tmp1, ScVec, ShufMask);
1241 Result = LegalizeOp(Result);
1242 break;
1243 }
1244
Chris Lattner29b23012006-03-19 01:17:20 +00001245 // If the target doesn't support this, we have to spill the input vector
1246 // to a temporary stack slot, update the element, then reload it. This is
1247 // badness. We could also load the value into a vector register (either
1248 // with a "move to register" or "extload into register" instruction, then
1249 // permute it into place, if the idx is a constant and if the idx is
1250 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001251 MVT::ValueType VT = Tmp1.getValueType();
1252 MVT::ValueType EltVT = Tmp2.getValueType();
1253 MVT::ValueType IdxVT = Tmp3.getValueType();
1254 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattnerd6f7d442007-10-15 17:48:57 +00001255 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001256 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +00001257 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001258
1259 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001260 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1261 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001262 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001263 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1264 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1265 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001266 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +00001267 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001268 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +00001269 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +00001270 break;
1271 }
1272 }
1273 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001274 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001275 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1276 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1277 break;
1278 }
1279
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001280 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1281 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1282 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1283 Node->getValueType(0))) {
1284 default: assert(0 && "This action is not supported yet!");
1285 case TargetLowering::Legal:
1286 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001287 case TargetLowering::Custom:
1288 Tmp3 = TLI.LowerOperation(Result, DAG);
1289 if (Tmp3.Val) {
1290 Result = Tmp3;
1291 break;
1292 }
1293 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001294 case TargetLowering::Expand:
1295 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001296 break;
1297 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001298 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001299 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001300 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1301 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1302 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1303
1304 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001305 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1306 default: assert(0 && "Unknown operation action!");
1307 case TargetLowering::Legal:
1308 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1309 "vector shuffle should not be created if not legal!");
1310 break;
1311 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001312 Tmp3 = TLI.LowerOperation(Result, DAG);
1313 if (Tmp3.Val) {
1314 Result = Tmp3;
1315 break;
1316 }
1317 // FALLTHROUGH
1318 case TargetLowering::Expand: {
1319 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00001320 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng9fa89592006-04-05 06:07:11 +00001321 MVT::ValueType PtrVT = TLI.getPointerTy();
1322 SDOperand Mask = Node->getOperand(2);
1323 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001324 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001325 for (unsigned i = 0; i != NumElems; ++i) {
1326 SDOperand Arg = Mask.getOperand(i);
1327 if (Arg.getOpcode() == ISD::UNDEF) {
1328 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1329 } else {
1330 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1331 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1332 if (Idx < NumElems)
1333 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1334 DAG.getConstant(Idx, PtrVT)));
1335 else
1336 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1337 DAG.getConstant(Idx - NumElems, PtrVT)));
1338 }
1339 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001340 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001341 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001342 }
Chris Lattner6be79822006-04-04 17:23:26 +00001343 case TargetLowering::Promote: {
1344 // Change base type to a different vector type.
1345 MVT::ValueType OVT = Node->getValueType(0);
1346 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1347
1348 // Cast the two input vectors.
1349 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1350 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1351
1352 // Convert the shuffle mask to the right # elements.
1353 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1354 assert(Tmp3.Val && "Shuffle not legal?");
1355 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1356 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1357 break;
1358 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001359 }
1360 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001361
1362 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001363 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001364 Tmp2 = LegalizeOp(Node->getOperand(1));
1365 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001366 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001367 break;
1368
Dan Gohmana8665142007-06-25 16:23:39 +00001369 case ISD::EXTRACT_SUBVECTOR:
1370 Tmp1 = Node->getOperand(0);
1371 Tmp2 = LegalizeOp(Node->getOperand(1));
1372 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1373 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001374 break;
1375
Chris Lattner462505f2006-02-13 09:18:02 +00001376 case ISD::CALLSEQ_START: {
1377 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1378
1379 // Recursively Legalize all of the inputs of the call end that do not lead
1380 // to this call start. This ensures that any libcalls that need be inserted
1381 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001382 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001383 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001384 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1385 NodesLeadingTo);
1386 }
Chris Lattner462505f2006-02-13 09:18:02 +00001387
1388 // Now that we legalized all of the inputs (which may have inserted
1389 // libcalls) create the new CALLSEQ_START node.
1390 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1391
1392 // Merge in the last call, to ensure that this call start after the last
1393 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001394 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001395 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1396 Tmp1 = LegalizeOp(Tmp1);
1397 }
Chris Lattner462505f2006-02-13 09:18:02 +00001398
1399 // Do not try to legalize the target-specific arguments (#1+).
1400 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001401 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001402 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001403 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001404 }
1405
1406 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001407 AddLegalizedOperand(Op.getValue(0), Result);
1408 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1409 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1410
Chris Lattner462505f2006-02-13 09:18:02 +00001411 // Now that the callseq_start and all of the non-call nodes above this call
1412 // sequence have been legalized, legalize the call itself. During this
1413 // process, no libcalls can/will be inserted, guaranteeing that no calls
1414 // can overlap.
1415 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1416 SDOperand InCallSEQ = LastCALLSEQ_END;
1417 // Note that we are selecting this call!
1418 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1419 IsLegalizingCall = true;
1420
1421 // Legalize the call, starting from the CALLSEQ_END.
1422 LegalizeOp(LastCALLSEQ_END);
1423 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1424 return Result;
1425 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001426 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001427 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1428 // will cause this node to be legalized as well as handling libcalls right.
1429 if (LastCALLSEQ_END.Val != Node) {
1430 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001431 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001432 assert(I != LegalizedNodes.end() &&
1433 "Legalizing the call start should have legalized this node!");
1434 return I->second;
1435 }
1436
1437 // Otherwise, the call start has been legalized and everything is going
1438 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001439 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001440 // Do not try to legalize the target-specific arguments (#1+), except for
1441 // an optional flag input.
1442 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1443 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001444 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001445 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001446 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001447 }
1448 } else {
1449 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1450 if (Tmp1 != Node->getOperand(0) ||
1451 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001452 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001453 Ops[0] = Tmp1;
1454 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001455 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001456 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001457 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001458 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001459 // This finishes up call legalization.
1460 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001461
1462 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1463 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1464 if (Node->getNumValues() == 2)
1465 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1466 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001467 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng631ccc62007-08-16 23:50:06 +00001468 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerec26b482005-01-09 19:03:49 +00001469 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1470 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1471 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001472 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001473
Chris Lattnerd02b0542006-01-28 10:58:55 +00001474 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001475 Tmp2 = Result.getValue(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001476 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Cheng7f4ec822006-01-11 22:14:47 +00001477 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001478 case TargetLowering::Expand: {
1479 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1480 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1481 " not tell us which reg is the stack pointer!");
1482 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendlingf359fed2007-11-13 00:44:25 +00001483
1484 // Chain the dynamic stack allocation so that it doesn't modify the stack
1485 // pointer when other instructions are using the stack.
1486 Chain = DAG.getCALLSEQ_START(Chain,
1487 DAG.getConstant(0, TLI.getPointerTy()));
1488
Chris Lattner59b82f92006-01-15 08:54:32 +00001489 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001490 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1491 Chain = SP.getValue(1);
1492 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1493 unsigned StackAlign =
1494 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1495 if (Align > StackAlign)
Evan Chengcb6d65e2007-08-17 18:02:22 +00001496 SP = DAG.getNode(ISD::AND, VT, SP,
1497 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng631ccc62007-08-16 23:50:06 +00001498 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendlingf359fed2007-11-13 00:44:25 +00001499 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1500
1501 Tmp2 =
1502 DAG.getCALLSEQ_END(Chain,
1503 DAG.getConstant(0, TLI.getPointerTy()),
1504 DAG.getConstant(0, TLI.getPointerTy()),
1505 SDOperand());
1506
Chris Lattnerd02b0542006-01-28 10:58:55 +00001507 Tmp1 = LegalizeOp(Tmp1);
1508 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001509 break;
1510 }
1511 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001512 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1513 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001514 Tmp1 = LegalizeOp(Tmp3);
1515 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001516 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001517 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001518 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001519 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001520 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001521 // Since this op produce two values, make sure to remember that we
1522 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001523 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1524 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001525 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001526 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001527 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001528 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001529 bool Changed = false;
1530 // Legalize all of the operands of the inline asm, in case they are nodes
1531 // that need to be expanded or something. Note we skip the asm string and
1532 // all of the TargetConstant flags.
1533 SDOperand Op = LegalizeOp(Ops[0]);
1534 Changed = Op != Ops[0];
1535 Ops[0] = Op;
1536
1537 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1538 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1539 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1540 for (++i; NumVals; ++i, --NumVals) {
1541 SDOperand Op = LegalizeOp(Ops[i]);
1542 if (Op != Ops[i]) {
1543 Changed = true;
1544 Ops[i] = Op;
1545 }
1546 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001547 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001548
1549 if (HasInFlag) {
1550 Op = LegalizeOp(Ops.back());
1551 Changed |= Op != Ops.back();
1552 Ops.back() = Op;
1553 }
1554
1555 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001556 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001557
1558 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001559 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001560 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1561 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001562 }
Chris Lattner68a12142005-01-07 22:12:08 +00001563 case ISD::BR:
1564 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001565 // Ensure that libcalls are emitted before a branch.
1566 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1567 Tmp1 = LegalizeOp(Tmp1);
1568 LastCALLSEQ_END = DAG.getEntryNode();
1569
Chris Lattnerd02b0542006-01-28 10:58:55 +00001570 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001571 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001572 case ISD::BRIND:
1573 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1574 // Ensure that libcalls are emitted before a branch.
1575 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1576 Tmp1 = LegalizeOp(Tmp1);
1577 LastCALLSEQ_END = DAG.getEntryNode();
1578
1579 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1580 default: assert(0 && "Indirect target must be legal type (pointer)!");
1581 case Legal:
1582 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1583 break;
1584 }
1585 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1586 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001587 case ISD::BR_JT:
1588 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1589 // Ensure that libcalls are emitted before a branch.
1590 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1591 Tmp1 = LegalizeOp(Tmp1);
1592 LastCALLSEQ_END = DAG.getEntryNode();
1593
1594 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1595 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1596
1597 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1598 default: assert(0 && "This action is not supported yet!");
1599 case TargetLowering::Legal: break;
1600 case TargetLowering::Custom:
1601 Tmp1 = TLI.LowerOperation(Result, DAG);
1602 if (Tmp1.Val) Result = Tmp1;
1603 break;
1604 case TargetLowering::Expand: {
1605 SDOperand Chain = Result.getOperand(0);
1606 SDOperand Table = Result.getOperand(1);
1607 SDOperand Index = Result.getOperand(2);
1608
1609 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001610 MachineFunction &MF = DAG.getMachineFunction();
1611 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001612 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1613 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001614
1615 SDOperand LD;
1616 switch (EntrySize) {
1617 default: assert(0 && "Size of jump table not supported yet."); break;
1618 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1619 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1620 }
1621
Evan Cheng797d56f2007-11-09 01:32:10 +00001622 Addr = LD;
Jim Laskey70323a82006-12-14 19:17:33 +00001623 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001624 // For PIC, the sequence is:
1625 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng797d56f2007-11-09 01:32:10 +00001626 // RelocBase can be JumpTable, GOT or some sort of global base.
1627 if (PTy != MVT::i32)
1628 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1629 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1630 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng84a28d42006-10-30 08:00:44 +00001631 }
Evan Cheng797d56f2007-11-09 01:32:10 +00001632 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng84a28d42006-10-30 08:00:44 +00001633 }
1634 }
1635 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001636 case ISD::BRCOND:
1637 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001638 // Ensure that libcalls are emitted before a return.
1639 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1640 Tmp1 = LegalizeOp(Tmp1);
1641 LastCALLSEQ_END = DAG.getEntryNode();
1642
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001643 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1644 case Expand: assert(0 && "It's impossible to expand bools");
1645 case Legal:
1646 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1647 break;
1648 case Promote:
1649 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001650
1651 // The top bits of the promoted condition are not necessarily zero, ensure
1652 // that the value is properly zero extended.
Dan Gohman309d3d52007-06-22 14:59:07 +00001653 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerdb189382006-11-27 04:39:56 +00001654 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1655 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001656 break;
1657 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001658
1659 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001660 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001661
1662 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1663 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001664 case TargetLowering::Legal: break;
1665 case TargetLowering::Custom:
1666 Tmp1 = TLI.LowerOperation(Result, DAG);
1667 if (Tmp1.Val) Result = Tmp1;
1668 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001669 case TargetLowering::Expand:
1670 // Expand brcond's setcc into its constituent parts and create a BR_CC
1671 // Node.
1672 if (Tmp2.getOpcode() == ISD::SETCC) {
1673 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1674 Tmp2.getOperand(0), Tmp2.getOperand(1),
1675 Node->getOperand(2));
1676 } else {
1677 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1678 DAG.getCondCode(ISD::SETNE), Tmp2,
1679 DAG.getConstant(0, Tmp2.getValueType()),
1680 Node->getOperand(2));
1681 }
1682 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001683 }
1684 break;
1685 case ISD::BR_CC:
1686 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001687 // Ensure that libcalls are emitted before a branch.
1688 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1689 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001690 Tmp2 = Node->getOperand(2); // LHS
1691 Tmp3 = Node->getOperand(3); // RHS
1692 Tmp4 = Node->getOperand(1); // CC
1693
1694 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001695 LastCALLSEQ_END = DAG.getEntryNode();
1696
Nate Begeman7e7f4392006-02-01 07:19:44 +00001697 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1698 // the LHS is a legal SETCC itself. In this case, we need to compare
1699 // the result against zero to select between true and false values.
1700 if (Tmp3.Val == 0) {
1701 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1702 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001703 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001704
1705 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1706 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001707
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001708 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1709 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001710 case TargetLowering::Legal: break;
1711 case TargetLowering::Custom:
1712 Tmp4 = TLI.LowerOperation(Result, DAG);
1713 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001714 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001715 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001716 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001717 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001718 LoadSDNode *LD = cast<LoadSDNode>(Node);
1719 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1720 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001721
Evan Chenge71fe34d2006-10-09 20:57:25 +00001722 ISD::LoadExtType ExtType = LD->getExtensionType();
1723 if (ExtType == ISD::NON_EXTLOAD) {
1724 MVT::ValueType VT = Node->getValueType(0);
1725 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1726 Tmp3 = Result.getValue(0);
1727 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001728
Evan Chenge71fe34d2006-10-09 20:57:25 +00001729 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1730 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001731 case TargetLowering::Legal:
1732 // If this is an unaligned load and the target doesn't support it,
1733 // expand it.
1734 if (!TLI.allowsUnalignedMemoryAccesses()) {
1735 unsigned ABIAlignment = TLI.getTargetData()->
1736 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1737 if (LD->getAlignment() < ABIAlignment){
1738 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1739 TLI);
1740 Tmp3 = Result.getOperand(0);
1741 Tmp4 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001742 Tmp3 = LegalizeOp(Tmp3);
1743 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001744 }
1745 }
1746 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001747 case TargetLowering::Custom:
1748 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1749 if (Tmp1.Val) {
1750 Tmp3 = LegalizeOp(Tmp1);
1751 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001752 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001753 break;
1754 case TargetLowering::Promote: {
1755 // Only promote a load of vector type to another.
1756 assert(MVT::isVector(VT) && "Cannot promote this load!");
1757 // Change base type to a different vector type.
1758 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1759
1760 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001761 LD->getSrcValueOffset(),
1762 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001763 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1764 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001765 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001766 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001767 }
1768 // Since loads produce two values, make sure to remember that we
1769 // legalized both of them.
1770 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1771 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1772 return Op.ResNo ? Tmp4 : Tmp3;
1773 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001774 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001775 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1776 default: assert(0 && "This action is not supported yet!");
1777 case TargetLowering::Promote:
1778 assert(SrcVT == MVT::i1 &&
1779 "Can only promote extending LOAD from i1 -> i8!");
1780 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1781 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00001782 MVT::i8, LD->isVolatile(), LD->getAlignment());
Duncan Sandsd42c8122007-10-17 13:49:58 +00001783 Tmp1 = Result.getValue(0);
1784 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001785 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001786 case TargetLowering::Custom:
1787 isCustom = true;
1788 // FALLTHROUGH
1789 case TargetLowering::Legal:
1790 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1791 Tmp1 = Result.getValue(0);
1792 Tmp2 = Result.getValue(1);
1793
1794 if (isCustom) {
1795 Tmp3 = TLI.LowerOperation(Result, DAG);
1796 if (Tmp3.Val) {
1797 Tmp1 = LegalizeOp(Tmp3);
1798 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1799 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001800 } else {
1801 // If this is an unaligned load and the target doesn't support it,
1802 // expand it.
1803 if (!TLI.allowsUnalignedMemoryAccesses()) {
1804 unsigned ABIAlignment = TLI.getTargetData()->
1805 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1806 if (LD->getAlignment() < ABIAlignment){
1807 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1808 TLI);
1809 Tmp1 = Result.getOperand(0);
1810 Tmp2 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001811 Tmp1 = LegalizeOp(Tmp1);
1812 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001813 }
1814 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001815 }
1816 break;
1817 case TargetLowering::Expand:
1818 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1819 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1820 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001821 LD->getSrcValueOffset(),
1822 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001823 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1824 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1825 Tmp2 = LegalizeOp(Load.getValue(1));
1826 break;
1827 }
Chris Lattner296a83c2007-02-01 04:55:59 +00001828 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001829 // Turn the unsupported load into an EXTLOAD followed by an explicit
1830 // zero/sign extend inreg.
1831 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1832 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001833 LD->getSrcValueOffset(), SrcVT,
1834 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001835 SDOperand ValRes;
1836 if (ExtType == ISD::SEXTLOAD)
1837 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1838 Result, DAG.getValueType(SrcVT));
1839 else
1840 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1841 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1842 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1843 break;
1844 }
1845 // Since loads produce two values, make sure to remember that we legalized
1846 // both of them.
1847 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1848 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1849 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001850 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001851 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001852 case ISD::EXTRACT_ELEMENT: {
1853 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1854 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001855 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001856 case Legal:
1857 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1858 // 1 -> Hi
1859 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1860 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1861 TLI.getShiftAmountTy()));
1862 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1863 } else {
1864 // 0 -> Lo
1865 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1866 Node->getOperand(0));
1867 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001868 break;
1869 case Expand:
1870 // Get both the low and high parts.
1871 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1872 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1873 Result = Tmp2; // 1 -> Hi
1874 else
1875 Result = Tmp1; // 0 -> Lo
1876 break;
1877 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001878 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001879 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001880
1881 case ISD::CopyToReg:
1882 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001883
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001884 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001885 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001886 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001887 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001888 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001889 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001890 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001891 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001892 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001893 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001894 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1895 Tmp3);
1896 } else {
1897 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001898 }
1899
1900 // Since this produces two values, make sure to remember that we legalized
1901 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001902 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001903 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001904 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001905 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001906 break;
1907
1908 case ISD::RET:
1909 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001910
1911 // Ensure that libcalls are emitted before a return.
1912 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1913 Tmp1 = LegalizeOp(Tmp1);
1914 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001915
Chris Lattnerdc750592005-01-07 07:47:09 +00001916 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001917 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001918 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001919 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001920 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001921 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001922 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001923 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001924 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +00001925 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001926 SDOperand Lo, Hi;
1927 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00001928
1929 // Big endian systems want the hi reg first.
1930 if (!TLI.isLittleEndian())
1931 std::swap(Lo, Hi);
1932
Evan Cheng3432ab92006-12-11 19:27:14 +00001933 if (Hi.Val)
1934 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1935 else
1936 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001937 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001938 } else {
1939 SDNode *InVal = Tmp2.Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00001940 int InIx = Tmp2.ResNo;
1941 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
1942 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001943
Dan Gohmana8665142007-06-25 16:23:39 +00001944 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001945 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001946 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00001947 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001948 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00001949 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001950 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001951 } else if (NumElems == 1) {
1952 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00001953 Tmp2 = ScalarizeVectorOp(Tmp2);
1954 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001955 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001956
1957 // FIXME: Returns of gcc generic vectors smaller than a legal type
1958 // should be returned in integer registers!
1959
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001960 // The scalarized value type may not be legal, e.g. it might require
1961 // promotion or expansion. Relegalize the return.
1962 Result = LegalizeOp(Result);
1963 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001964 // FIXME: Returns of gcc generic vectors larger than a legal vector
1965 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001966 SDOperand Lo, Hi;
1967 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00001968 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001969 Result = LegalizeOp(Result);
1970 }
1971 }
Misha Brukman835702a2005-04-21 22:36:52 +00001972 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001973 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001974 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00001975 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001976 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001977 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001978 }
1979 break;
1980 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001981 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001982 break;
1983 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00001984 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00001985 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00001986 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00001987 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1988 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001989 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00001990 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001991 break;
1992 case Expand: {
1993 SDOperand Lo, Hi;
Dan Gohman3b62d722007-06-27 16:08:04 +00001994 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001995 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001996 ExpandOp(Node->getOperand(i), Lo, Hi);
1997 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00001998 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00001999 if (Hi.Val) {
2000 NewValues.push_back(Hi);
2001 NewValues.push_back(Node->getOperand(i+1));
2002 }
Misha Brukman835702a2005-04-21 22:36:52 +00002003 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002004 }
2005 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002006 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002007 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002008
2009 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00002010 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00002011 else
Chris Lattner97af9d52006-08-08 01:09:31 +00002012 Result = DAG.getNode(ISD::RET, MVT::Other,
2013 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00002014 break;
2015 }
2016 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002017
Chris Lattner4d1ea712006-01-29 21:02:23 +00002018 if (Result.getOpcode() == ISD::RET) {
2019 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2020 default: assert(0 && "This action is not supported yet!");
2021 case TargetLowering::Legal: break;
2022 case TargetLowering::Custom:
2023 Tmp1 = TLI.LowerOperation(Result, DAG);
2024 if (Tmp1.Val) Result = Tmp1;
2025 break;
2026 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002027 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002028 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002029 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00002030 StoreSDNode *ST = cast<StoreSDNode>(Node);
2031 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2032 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00002033 int SVOffset = ST->getSrcValueOffset();
2034 unsigned Alignment = ST->getAlignment();
2035 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00002036
Evan Chengab51cf22006-10-13 21:14:26 +00002037 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002038 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2039 // FIXME: We shouldn't do this for TargetConstantFP's.
2040 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2041 // to phase ordering between legalized code and the dag combiner. This
2042 // probably means that we need to integrate dag combiner and legalizer
2043 // together.
Dale Johannesen98d3a082007-09-14 22:26:36 +00002044 // We generally can't do this one for long doubles.
Chris Lattner5e6fe052007-10-13 06:35:54 +00002045 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002046 if (CFP->getValueType(0) == MVT::f32 &&
2047 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen028084e2007-09-12 03:30:33 +00002048 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2049 convertToAPInt().getZExtValue(),
Dale Johannesen245dceb2007-09-11 18:32:33 +00002050 MVT::i32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00002051 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2052 SVOffset, isVolatile, Alignment);
2053 break;
2054 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002055 // If this target supports 64-bit registers, do a single 64-bit store.
2056 if (getTypeAction(MVT::i64) == Legal) {
2057 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2058 getZExtValue(), MVT::i64);
2059 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2060 SVOffset, isVolatile, Alignment);
2061 break;
2062 } else if (getTypeAction(MVT::i32) == Legal) {
2063 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2064 // stores. If the target supports neither 32- nor 64-bits, this
2065 // xform is certainly not worth it.
2066 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2067 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2068 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2069 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2070
2071 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2072 SVOffset, isVolatile, Alignment);
2073 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2074 getIntPtrConstant(4));
2075 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sands1826ded2007-10-28 12:59:45 +00002076 isVolatile, MinAlign(Alignment, 4U));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002077
2078 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2079 break;
2080 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002081 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002082 }
2083
Evan Chengab51cf22006-10-13 21:14:26 +00002084 switch (getTypeAction(ST->getStoredVT())) {
2085 case Legal: {
2086 Tmp3 = LegalizeOp(ST->getValue());
2087 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2088 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00002089
Evan Chengab51cf22006-10-13 21:14:26 +00002090 MVT::ValueType VT = Tmp3.getValueType();
2091 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2092 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002093 case TargetLowering::Legal:
2094 // If this is an unaligned store and the target doesn't support it,
2095 // expand it.
2096 if (!TLI.allowsUnalignedMemoryAccesses()) {
2097 unsigned ABIAlignment = TLI.getTargetData()->
2098 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2099 if (ST->getAlignment() < ABIAlignment)
2100 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2101 TLI);
2102 }
2103 break;
Evan Chengab51cf22006-10-13 21:14:26 +00002104 case TargetLowering::Custom:
2105 Tmp1 = TLI.LowerOperation(Result, DAG);
2106 if (Tmp1.Val) Result = Tmp1;
2107 break;
2108 case TargetLowering::Promote:
2109 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2110 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2111 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2112 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00002113 ST->getSrcValue(), SVOffset, isVolatile,
2114 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002115 break;
2116 }
2117 break;
2118 }
2119 case Promote:
2120 // Truncate the value and store the result.
2121 Tmp3 = PromoteOp(ST->getValue());
2122 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002123 SVOffset, ST->getStoredVT(),
2124 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002125 break;
2126
2127 case Expand:
2128 unsigned IncrementSize = 0;
2129 SDOperand Lo, Hi;
2130
2131 // If this is a vector type, then we have to calculate the increment as
2132 // the product of the element size in bytes, and the number of elements
2133 // in the high half of the vector.
Dan Gohmana8665142007-06-25 16:23:39 +00002134 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002135 SDNode *InVal = ST->getValue().Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00002136 int InIx = ST->getValue().ResNo;
2137 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2138 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Evan Chengab51cf22006-10-13 21:14:26 +00002139
Dan Gohmana8665142007-06-25 16:23:39 +00002140 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002141 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00002142 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002143 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002144 // Turn this into a normal store of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002145 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00002146 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002147 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002148 Result = LegalizeOp(Result);
2149 break;
2150 } else if (NumElems == 1) {
2151 // Turn this into a normal store of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002152 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00002153 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002154 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002155 // The scalarized value type may not be legal, e.g. it might require
2156 // promotion or expansion. Relegalize the scalar store.
2157 Result = LegalizeOp(Result);
2158 break;
2159 } else {
2160 SplitVectorOp(Node->getOperand(1), Lo, Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00002161 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2162 MVT::getSizeInBits(EVT)/8;
Evan Chengab51cf22006-10-13 21:14:26 +00002163 }
2164 } else {
2165 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00002166 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002167
2168 if (!TLI.isLittleEndian())
2169 std::swap(Lo, Hi);
2170 }
2171
2172 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002173 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002174
2175 if (Hi.Val == NULL) {
2176 // Must be int <-> float one-to-one expansion.
2177 Result = Lo;
2178 break;
2179 }
2180
Evan Chengab51cf22006-10-13 21:14:26 +00002181 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2182 getIntPtrConstant(IncrementSize));
2183 assert(isTypeLegal(Tmp2.getValueType()) &&
2184 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002185 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00002186 Alignment = MinAlign(Alignment, IncrementSize);
Evan Chengab51cf22006-10-13 21:14:26 +00002187 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002188 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002189 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2190 break;
2191 }
2192 } else {
2193 // Truncating store
2194 assert(isTypeLegal(ST->getValue().getValueType()) &&
2195 "Cannot handle illegal TRUNCSTORE yet!");
2196 Tmp3 = LegalizeOp(ST->getValue());
2197
2198 // The only promote case we handle is TRUNCSTORE:i1 X into
2199 // -> TRUNCSTORE:i8 (and X, 1)
2200 if (ST->getStoredVT() == MVT::i1 &&
2201 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2202 // Promote the bool to a mask then store.
2203 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2204 DAG.getConstant(1, Tmp3.getValueType()));
2205 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002206 SVOffset, MVT::i8,
2207 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002208 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2209 Tmp2 != ST->getBasePtr()) {
2210 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2211 ST->getOffset());
2212 }
2213
2214 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2215 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002216 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002217 case TargetLowering::Legal:
2218 // If this is an unaligned store and the target doesn't support it,
2219 // expand it.
2220 if (!TLI.allowsUnalignedMemoryAccesses()) {
2221 unsigned ABIAlignment = TLI.getTargetData()->
2222 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2223 if (ST->getAlignment() < ABIAlignment)
2224 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2225 TLI);
2226 }
2227 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002228 case TargetLowering::Custom:
2229 Tmp1 = TLI.LowerOperation(Result, DAG);
2230 if (Tmp1.Val) Result = Tmp1;
2231 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002232 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002233 }
2234 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002235 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002236 case ISD::PCMARKER:
2237 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002238 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002239 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002240 case ISD::STACKSAVE:
2241 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002242 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2243 Tmp1 = Result.getValue(0);
2244 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002245
Chris Lattnerb3266452006-01-13 02:50:02 +00002246 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2247 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002248 case TargetLowering::Legal: break;
2249 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002250 Tmp3 = TLI.LowerOperation(Result, DAG);
2251 if (Tmp3.Val) {
2252 Tmp1 = LegalizeOp(Tmp3);
2253 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002254 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002255 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002256 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002257 // Expand to CopyFromReg if the target set
2258 // StackPointerRegisterToSaveRestore.
2259 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002260 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002261 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002262 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002263 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002264 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2265 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002266 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002267 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002268 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002269
2270 // Since stacksave produce two values, make sure to remember that we
2271 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002272 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2273 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2274 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002275
Chris Lattnerb3266452006-01-13 02:50:02 +00002276 case ISD::STACKRESTORE:
2277 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2278 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002279 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002280
2281 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2282 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002283 case TargetLowering::Legal: break;
2284 case TargetLowering::Custom:
2285 Tmp1 = TLI.LowerOperation(Result, DAG);
2286 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002287 break;
2288 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002289 // Expand to CopyToReg if the target set
2290 // StackPointerRegisterToSaveRestore.
2291 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2292 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2293 } else {
2294 Result = Tmp1;
2295 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002296 break;
2297 }
2298 break;
2299
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002300 case ISD::READCYCLECOUNTER:
2301 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002302 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002303 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2304 Node->getValueType(0))) {
2305 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002306 case TargetLowering::Legal:
2307 Tmp1 = Result.getValue(0);
2308 Tmp2 = Result.getValue(1);
2309 break;
Evan Cheng69739932006-11-29 08:26:18 +00002310 case TargetLowering::Custom:
2311 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002312 Tmp1 = LegalizeOp(Result.getValue(0));
2313 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002314 break;
2315 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002316
2317 // Since rdcc produce two values, make sure to remember that we legalized
2318 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002319 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2320 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002321 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002322
Chris Lattner39c67442005-01-14 22:08:15 +00002323 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002324 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2325 case Expand: assert(0 && "It's impossible to expand bools");
2326 case Legal:
2327 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2328 break;
2329 case Promote:
2330 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002331 // Make sure the condition is either zero or one.
Dan Gohman309d3d52007-06-22 14:59:07 +00002332 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattner3abb6362006-11-28 01:03:30 +00002333 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2334 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002335 break;
2336 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002337 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002338 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002339
Chris Lattnerd02b0542006-01-28 10:58:55 +00002340 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002341
Nate Begeman987121a2005-08-23 04:29:48 +00002342 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002343 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002344 case TargetLowering::Legal: break;
2345 case TargetLowering::Custom: {
2346 Tmp1 = TLI.LowerOperation(Result, DAG);
2347 if (Tmp1.Val) Result = Tmp1;
2348 break;
2349 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002350 case TargetLowering::Expand:
2351 if (Tmp1.getOpcode() == ISD::SETCC) {
2352 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2353 Tmp2, Tmp3,
2354 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2355 } else {
2356 Result = DAG.getSelectCC(Tmp1,
2357 DAG.getConstant(0, Tmp1.getValueType()),
2358 Tmp2, Tmp3, ISD::SETNE);
2359 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002360 break;
2361 case TargetLowering::Promote: {
2362 MVT::ValueType NVT =
2363 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2364 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002365 if (MVT::isVector(Tmp2.getValueType())) {
2366 ExtOp = ISD::BIT_CONVERT;
2367 TruncOp = ISD::BIT_CONVERT;
2368 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002369 ExtOp = ISD::ANY_EXTEND;
2370 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002371 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002372 ExtOp = ISD::FP_EXTEND;
2373 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002374 }
2375 // Promote each of the values to the new type.
2376 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2377 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2378 // Perform the larger operation, then round down.
2379 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2380 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2381 break;
2382 }
2383 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002384 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002385 case ISD::SELECT_CC: {
2386 Tmp1 = Node->getOperand(0); // LHS
2387 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002388 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2389 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002390 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002391
Nate Begeman7e7f4392006-02-01 07:19:44 +00002392 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2393
2394 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2395 // the LHS is a legal SETCC itself. In this case, we need to compare
2396 // the result against zero to select between true and false values.
2397 if (Tmp2.Val == 0) {
2398 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2399 CC = DAG.getCondCode(ISD::SETNE);
2400 }
2401 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2402
2403 // Everything is legal, see if we should expand this op or something.
2404 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2405 default: assert(0 && "This action is not supported yet!");
2406 case TargetLowering::Legal: break;
2407 case TargetLowering::Custom:
2408 Tmp1 = TLI.LowerOperation(Result, DAG);
2409 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002410 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002411 }
2412 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002413 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002414 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002415 Tmp1 = Node->getOperand(0);
2416 Tmp2 = Node->getOperand(1);
2417 Tmp3 = Node->getOperand(2);
2418 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2419
2420 // If we had to Expand the SetCC operands into a SELECT node, then it may
2421 // not always be possible to return a true LHS & RHS. In this case, just
2422 // return the value we legalized, returned in the LHS
2423 if (Tmp2.Val == 0) {
2424 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002425 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002426 }
Nate Begeman987121a2005-08-23 04:29:48 +00002427
Chris Lattnerf263a232006-01-30 22:43:50 +00002428 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002429 default: assert(0 && "Cannot handle this action for SETCC yet!");
2430 case TargetLowering::Custom:
2431 isCustom = true;
2432 // FALLTHROUGH.
2433 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002434 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002435 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002436 Tmp4 = TLI.LowerOperation(Result, DAG);
2437 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002438 }
Nate Begeman987121a2005-08-23 04:29:48 +00002439 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002440 case TargetLowering::Promote: {
2441 // First step, figure out the appropriate operation to use.
2442 // Allow SETCC to not be supported for all legal data types
2443 // Mostly this targets FP
2444 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002445 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002446
2447 // Scan for the appropriate larger type to use.
2448 while (1) {
2449 NewInTy = (MVT::ValueType)(NewInTy+1);
2450
2451 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2452 "Fell off of the edge of the integer world");
2453 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2454 "Fell off of the edge of the floating point world");
2455
2456 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002457 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002458 break;
2459 }
2460 if (MVT::isInteger(NewInTy))
2461 assert(0 && "Cannot promote Legal Integer SETCC yet");
2462 else {
2463 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2464 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2465 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002466 Tmp1 = LegalizeOp(Tmp1);
2467 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002468 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002469 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002470 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002471 }
Nate Begeman987121a2005-08-23 04:29:48 +00002472 case TargetLowering::Expand:
2473 // Expand a setcc node into a select_cc of the same condition, lhs, and
2474 // rhs that selects between const 1 (true) and const 0 (false).
2475 MVT::ValueType VT = Node->getValueType(0);
2476 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2477 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002478 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002479 break;
2480 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002481 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002482 case ISD::MEMSET:
2483 case ISD::MEMCPY:
2484 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002485 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002486 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2487
2488 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2489 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2490 case Expand: assert(0 && "Cannot expand a byte!");
2491 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002492 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002493 break;
2494 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002495 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002496 break;
2497 }
2498 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002499 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002500 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002501
2502 SDOperand Tmp4;
2503 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002504 case Expand: {
2505 // Length is too big, just take the lo-part of the length.
2506 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002507 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002508 break;
2509 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002510 case Legal:
2511 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002512 break;
2513 case Promote:
2514 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002515 break;
2516 }
2517
2518 SDOperand Tmp5;
2519 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2520 case Expand: assert(0 && "Cannot expand this yet!");
2521 case Legal:
2522 Tmp5 = LegalizeOp(Node->getOperand(4));
2523 break;
2524 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002525 Tmp5 = PromoteOp(Node->getOperand(4));
2526 break;
2527 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002528
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002529 SDOperand Tmp6;
2530 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2531 case Expand: assert(0 && "Cannot expand this yet!");
2532 case Legal:
2533 Tmp6 = LegalizeOp(Node->getOperand(5));
2534 break;
2535 case Promote:
2536 Tmp6 = PromoteOp(Node->getOperand(5));
2537 break;
2538 }
2539
Chris Lattner3c0dd462005-01-16 07:29:19 +00002540 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2541 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002542 case TargetLowering::Custom:
2543 isCustom = true;
2544 // FALLTHROUGH
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002545 case TargetLowering::Legal: {
2546 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2547 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002548 if (isCustom) {
2549 Tmp1 = TLI.LowerOperation(Result, DAG);
2550 if (Tmp1.Val) Result = Tmp1;
2551 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002552 break;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002553 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002554 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002555 // Otherwise, the target does not support this operation. Lower the
2556 // operation to an explicit libcall as appropriate.
2557 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002558 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002559 TargetLowering::ArgListTy Args;
2560 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002561
Reid Spencer6dced922005-01-12 14:53:45 +00002562 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002563 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002564 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00002565 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002566 // Extend the (previously legalized) ubyte argument to be an int value
2567 // for the call.
2568 if (Tmp3.getValueType() > MVT::i32)
2569 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2570 else
2571 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002572 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencere63b6512006-12-31 05:55:36 +00002573 Args.push_back(Entry);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002574 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002575 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002576
2577 FnName = "memset";
2578 } else if (Node->getOpcode() == ISD::MEMCPY ||
2579 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002580 Entry.Ty = IntPtrTy;
Reid Spencer791864c2007-01-03 04:22:32 +00002581 Entry.Node = Tmp2; Args.push_back(Entry);
2582 Entry.Node = Tmp3; Args.push_back(Entry);
2583 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002584 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2585 } else {
2586 assert(0 && "Unknown op!");
2587 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002588
Chris Lattner85d70c62005-01-11 05:57:22 +00002589 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002590 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002591 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002592 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002593 break;
2594 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002595 }
2596 break;
2597 }
Chris Lattner5385db52005-05-09 20:23:03 +00002598
Chris Lattner4157c412005-04-02 04:00:59 +00002599 case ISD::SHL_PARTS:
2600 case ISD::SRA_PARTS:
2601 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002602 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002603 bool Changed = false;
2604 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2605 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2606 Changed |= Ops.back() != Node->getOperand(i);
2607 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002608 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002609 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002610
Evan Cheng870e4f82006-01-09 18:31:59 +00002611 switch (TLI.getOperationAction(Node->getOpcode(),
2612 Node->getValueType(0))) {
2613 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002614 case TargetLowering::Legal: break;
2615 case TargetLowering::Custom:
2616 Tmp1 = TLI.LowerOperation(Result, DAG);
2617 if (Tmp1.Val) {
2618 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002619 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002620 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002621 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2622 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002623 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002624 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002625 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002626 return RetVal;
2627 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002628 break;
2629 }
2630
Chris Lattner13fe99c2005-04-02 05:00:07 +00002631 // Since these produce multiple values, make sure to remember that we
2632 // legalized all of them.
2633 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2634 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2635 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002636 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002637
2638 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002639 case ISD::ADD:
2640 case ISD::SUB:
2641 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002642 case ISD::MULHS:
2643 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002644 case ISD::UDIV:
2645 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002646 case ISD::AND:
2647 case ISD::OR:
2648 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002649 case ISD::SHL:
2650 case ISD::SRL:
2651 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002652 case ISD::FADD:
2653 case ISD::FSUB:
2654 case ISD::FMUL:
2655 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00002656 case ISD::FPOW:
Chris Lattnerdc750592005-01-07 07:47:09 +00002657 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002658 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2659 case Expand: assert(0 && "Not possible");
2660 case Legal:
2661 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2662 break;
2663 case Promote:
2664 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2665 break;
2666 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002667
2668 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002669
Andrew Lenharth72594262005-12-24 23:42:32 +00002670 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002671 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002672 case TargetLowering::Legal: break;
2673 case TargetLowering::Custom:
2674 Tmp1 = TLI.LowerOperation(Result, DAG);
2675 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002676 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002677 case TargetLowering::Expand: {
Dan Gohmana1603612007-10-08 18:33:35 +00002678 MVT::ValueType VT = Op.getValueType();
2679
2680 // See if multiply or divide can be lowered using two-result operations.
2681 SDVTList VTs = DAG.getVTList(VT, VT);
2682 if (Node->getOpcode() == ISD::MUL) {
2683 // We just need the low half of the multiply; try both the signed
2684 // and unsigned forms. If the target supports both SMUL_LOHI and
2685 // UMUL_LOHI, form a preference by checking which forms of plain
2686 // MULH it supports.
2687 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2688 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2689 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2690 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2691 unsigned OpToUse = 0;
2692 if (HasSMUL_LOHI && !HasMULHS) {
2693 OpToUse = ISD::SMUL_LOHI;
2694 } else if (HasUMUL_LOHI && !HasMULHU) {
2695 OpToUse = ISD::UMUL_LOHI;
2696 } else if (HasSMUL_LOHI) {
2697 OpToUse = ISD::SMUL_LOHI;
2698 } else if (HasUMUL_LOHI) {
2699 OpToUse = ISD::UMUL_LOHI;
2700 }
2701 if (OpToUse) {
2702 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2703 break;
2704 }
2705 }
2706 if (Node->getOpcode() == ISD::MULHS &&
2707 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2708 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2709 break;
2710 }
2711 if (Node->getOpcode() == ISD::MULHU &&
2712 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2713 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2714 break;
2715 }
2716 if (Node->getOpcode() == ISD::SDIV &&
2717 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2718 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2719 break;
2720 }
2721 if (Node->getOpcode() == ISD::UDIV &&
2722 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2723 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2724 break;
2725 }
2726
Dan Gohman2a7de412007-10-11 23:57:53 +00002727 // Check to see if we have a libcall for this operator.
2728 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2729 bool isSigned = false;
2730 switch (Node->getOpcode()) {
2731 case ISD::UDIV:
2732 case ISD::SDIV:
2733 if (VT == MVT::i32) {
2734 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng31cbddf2007-01-12 02:11:51 +00002735 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman2a7de412007-10-11 23:57:53 +00002736 isSigned = Node->getOpcode() == ISD::SDIV;
2737 }
2738 break;
2739 case ISD::FPOW:
2740 LC = VT == MVT::f32 ? RTLIB::POW_F32 :
2741 VT == MVT::f64 ? RTLIB::POW_F64 :
2742 VT == MVT::f80 ? RTLIB::POW_F80 :
2743 VT == MVT::ppcf128 ? RTLIB::POW_PPCF128 :
2744 RTLIB::UNKNOWN_LIBCALL;
2745 break;
2746 default: break;
2747 }
2748 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2749 SDOperand Dummy;
2750 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002751 break;
2752 }
2753
Chris Lattner87f08092006-04-02 03:57:31 +00002754 assert(MVT::isVector(Node->getValueType(0)) &&
2755 "Cannot expand this binary operator!");
2756 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman2a7de412007-10-11 23:57:53 +00002757 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattner87f08092006-04-02 03:57:31 +00002758 break;
2759 }
Evan Cheng119266e2006-04-12 21:20:24 +00002760 case TargetLowering::Promote: {
2761 switch (Node->getOpcode()) {
2762 default: assert(0 && "Do not know how to promote this BinOp!");
2763 case ISD::AND:
2764 case ISD::OR:
2765 case ISD::XOR: {
2766 MVT::ValueType OVT = Node->getValueType(0);
2767 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2768 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2769 // Bit convert each of the values to the new type.
2770 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2771 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2772 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2773 // Bit convert the result back the original type.
2774 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2775 break;
2776 }
2777 }
2778 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002779 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002780 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002781
Dan Gohman12334ac2007-10-05 14:17:22 +00002782 case ISD::SMUL_LOHI:
2783 case ISD::UMUL_LOHI:
2784 case ISD::SDIVREM:
2785 case ISD::UDIVREM:
2786 // These nodes will only be produced by target-specific lowering, so
2787 // they shouldn't be here if they aren't legal.
Duncan Sands052c8432007-10-16 09:07:20 +00002788 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman12334ac2007-10-05 14:17:22 +00002789 "This must be legal!");
Dan Gohmana1603612007-10-08 18:33:35 +00002790
2791 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2792 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2793 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman12334ac2007-10-05 14:17:22 +00002794 break;
2795
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002796 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2797 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2798 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2799 case Expand: assert(0 && "Not possible");
2800 case Legal:
2801 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2802 break;
2803 case Promote:
2804 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2805 break;
2806 }
2807
2808 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2809
2810 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2811 default: assert(0 && "Operation not supported");
2812 case TargetLowering::Custom:
2813 Tmp1 = TLI.LowerOperation(Result, DAG);
2814 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002815 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002816 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002817 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00002818 // If this target supports fabs/fneg natively and select is cheap,
2819 // do this efficiently.
2820 if (!TLI.isSelectExpensive() &&
2821 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2822 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00002823 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00002824 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002825 // Get the sign bit of the RHS.
2826 MVT::ValueType IVT =
2827 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2828 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2829 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2830 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2831 // Get the absolute value of the result.
2832 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2833 // Select between the nabs and abs value based on the sign bit of
2834 // the input.
2835 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2836 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2837 AbsVal),
2838 AbsVal);
2839 Result = LegalizeOp(Result);
2840 break;
2841 }
2842
2843 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002844 MVT::ValueType NVT =
2845 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2846 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2847 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2848 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002849 break;
2850 }
Evan Cheng003feb02007-01-04 21:56:39 +00002851 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002852 break;
2853
Nate Begeman5965bd12006-02-17 05:43:56 +00002854 case ISD::ADDC:
2855 case ISD::SUBC:
2856 Tmp1 = LegalizeOp(Node->getOperand(0));
2857 Tmp2 = LegalizeOp(Node->getOperand(1));
2858 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2859 // Since this produces two values, make sure to remember that we legalized
2860 // both of them.
2861 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2862 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2863 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002864
Nate Begeman5965bd12006-02-17 05:43:56 +00002865 case ISD::ADDE:
2866 case ISD::SUBE:
2867 Tmp1 = LegalizeOp(Node->getOperand(0));
2868 Tmp2 = LegalizeOp(Node->getOperand(1));
2869 Tmp3 = LegalizeOp(Node->getOperand(2));
2870 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2871 // Since this produces two values, make sure to remember that we legalized
2872 // both of them.
2873 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2874 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2875 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002876
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002877 case ISD::BUILD_PAIR: {
2878 MVT::ValueType PairTy = Node->getValueType(0);
2879 // TODO: handle the case where the Lo and Hi operands are not of legal type
2880 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2881 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2882 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002883 case TargetLowering::Promote:
2884 case TargetLowering::Custom:
2885 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002886 case TargetLowering::Legal:
2887 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2888 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2889 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002890 case TargetLowering::Expand:
2891 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2892 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2893 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2894 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2895 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002896 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002897 break;
2898 }
2899 break;
2900 }
2901
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002902 case ISD::UREM:
2903 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002904 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002905 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2906 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002907
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002908 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002909 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2910 case TargetLowering::Custom:
2911 isCustom = true;
2912 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002913 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002914 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002915 if (isCustom) {
2916 Tmp1 = TLI.LowerOperation(Result, DAG);
2917 if (Tmp1.Val) Result = Tmp1;
2918 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002919 break;
Dan Gohmana1603612007-10-08 18:33:35 +00002920 case TargetLowering::Expand: {
Evan Cheng1fc7c362006-09-18 23:28:33 +00002921 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002922 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohmana1603612007-10-08 18:33:35 +00002923 MVT::ValueType VT = Node->getValueType(0);
2924
2925 // See if remainder can be lowered using two-result operations.
2926 SDVTList VTs = DAG.getVTList(VT, VT);
2927 if (Node->getOpcode() == ISD::SREM &&
2928 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2929 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2930 break;
2931 }
2932 if (Node->getOpcode() == ISD::UREM &&
2933 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2934 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2935 break;
2936 }
2937
2938 if (MVT::isInteger(VT)) {
2939 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002940 TargetLowering::Legal) {
2941 // X % Y -> X-X/Y*Y
Evan Cheng1fc7c362006-09-18 23:28:33 +00002942 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002943 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2944 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman08143e32007-11-05 23:35:22 +00002945 } else if (MVT::isVector(VT)) {
2946 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002947 } else {
Dan Gohmana1603612007-10-08 18:33:35 +00002948 assert(VT == MVT::i32 &&
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002949 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00002950 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2951 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002952 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002953 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002954 }
Dan Gohmanccfc0282007-11-06 22:11:54 +00002955 } else {
2956 assert(MVT::isFloatingPoint(VT) &&
2957 "remainder op must have integer or floating-point type");
Dan Gohman08143e32007-11-05 23:35:22 +00002958 if (MVT::isVector(VT)) {
2959 Result = LegalizeOp(UnrollVectorOp(Op));
2960 } else {
2961 // Floating point mod -> fmod libcall.
2962 RTLIB::Libcall LC = VT == MVT::f32
2963 ? RTLIB::REM_F32 : RTLIB::REM_F64;
2964 SDOperand Dummy;
2965 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2966 false/*sign irrelevant*/, Dummy);
2967 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002968 }
2969 break;
2970 }
Dan Gohmana1603612007-10-08 18:33:35 +00002971 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002972 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002973 case ISD::VAARG: {
2974 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2975 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2976
Chris Lattner364b89a2006-01-28 07:42:08 +00002977 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002978 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2979 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002980 case TargetLowering::Custom:
2981 isCustom = true;
2982 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002983 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002984 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2985 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002986 Tmp1 = Result.getValue(1);
2987
2988 if (isCustom) {
2989 Tmp2 = TLI.LowerOperation(Result, DAG);
2990 if (Tmp2.Val) {
2991 Result = LegalizeOp(Tmp2);
2992 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2993 }
2994 }
Nate Begemane74795c2006-01-25 18:21:52 +00002995 break;
2996 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002997 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00002998 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00002999 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00003000 // Increment the pointer, VAList, to the next vaarg
3001 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3002 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3003 TLI.getPointerTy()));
3004 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00003005 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3006 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00003007 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003008 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003009 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00003010 Result = LegalizeOp(Result);
3011 break;
3012 }
3013 }
3014 // Since VAARG produces two values, make sure to remember that we
3015 // legalized both of them.
3016 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003017 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3018 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00003019 }
3020
3021 case ISD::VACOPY:
3022 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3023 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3024 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3025
3026 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3027 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003028 case TargetLowering::Custom:
3029 isCustom = true;
3030 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003031 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003032 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3033 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003034 if (isCustom) {
3035 Tmp1 = TLI.LowerOperation(Result, DAG);
3036 if (Tmp1.Val) Result = Tmp1;
3037 }
Nate Begemane74795c2006-01-25 18:21:52 +00003038 break;
3039 case TargetLowering::Expand:
3040 // This defaults to loading a pointer from the input and storing it to the
3041 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003042 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00003043 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00003044 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
3045 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00003046 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
3047 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00003048 break;
3049 }
3050 break;
3051
3052 case ISD::VAEND:
3053 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3054 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3055
3056 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3057 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003058 case TargetLowering::Custom:
3059 isCustom = true;
3060 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003061 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003062 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003063 if (isCustom) {
3064 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3065 if (Tmp1.Val) Result = Tmp1;
3066 }
Nate Begemane74795c2006-01-25 18:21:52 +00003067 break;
3068 case TargetLowering::Expand:
3069 Result = Tmp1; // Default to a no-op, return the chain
3070 break;
3071 }
3072 break;
3073
3074 case ISD::VASTART:
3075 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3076 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3077
Chris Lattnerd02b0542006-01-28 10:58:55 +00003078 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3079
Nate Begemane74795c2006-01-25 18:21:52 +00003080 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3081 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003082 case TargetLowering::Legal: break;
3083 case TargetLowering::Custom:
3084 Tmp1 = TLI.LowerOperation(Result, DAG);
3085 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00003086 break;
3087 }
3088 break;
3089
Nate Begeman1b8121b2006-01-11 21:21:00 +00003090 case ISD::ROTL:
3091 case ISD::ROTR:
3092 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3093 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00003094 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00003095 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3096 default:
3097 assert(0 && "ROTL/ROTR legalize operation not supported");
3098 break;
3099 case TargetLowering::Legal:
3100 break;
3101 case TargetLowering::Custom:
3102 Tmp1 = TLI.LowerOperation(Result, DAG);
3103 if (Tmp1.Val) Result = Tmp1;
3104 break;
3105 case TargetLowering::Promote:
3106 assert(0 && "Do not know how to promote ROTL/ROTR");
3107 break;
3108 case TargetLowering::Expand:
3109 assert(0 && "Do not know how to expand ROTL/ROTR");
3110 break;
3111 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00003112 break;
3113
Nate Begeman2fba8a32006-01-14 03:14:10 +00003114 case ISD::BSWAP:
3115 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3116 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003117 case TargetLowering::Custom:
3118 assert(0 && "Cannot custom legalize this yet!");
3119 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003120 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003121 break;
3122 case TargetLowering::Promote: {
3123 MVT::ValueType OVT = Tmp1.getValueType();
3124 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohman1796f1f2007-05-18 17:52:13 +00003125 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00003126
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003127 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3128 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3129 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3130 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3131 break;
3132 }
3133 case TargetLowering::Expand:
3134 Result = ExpandBSWAP(Tmp1);
3135 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003136 }
3137 break;
3138
Andrew Lenharth5e177822005-05-03 17:19:30 +00003139 case ISD::CTPOP:
3140 case ISD::CTTZ:
3141 case ISD::CTLZ:
3142 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3143 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00003144 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00003145 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003146 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00003147 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00003148 TargetLowering::Custom) {
3149 Tmp1 = TLI.LowerOperation(Result, DAG);
3150 if (Tmp1.Val) {
3151 Result = Tmp1;
3152 }
Scott Michel34e2d222007-07-30 21:00:31 +00003153 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003154 break;
3155 case TargetLowering::Promote: {
3156 MVT::ValueType OVT = Tmp1.getValueType();
3157 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00003158
3159 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00003160 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3161 // Perform the larger operation, then subtract if needed.
3162 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003163 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00003164 case ISD::CTPOP:
3165 Result = Tmp1;
3166 break;
3167 case ISD::CTTZ:
3168 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00003169 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003170 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00003171 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003172 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel34e2d222007-07-30 21:00:31 +00003173 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003174 break;
3175 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003176 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003177 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003178 DAG.getConstant(MVT::getSizeInBits(NVT) -
3179 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00003180 break;
3181 }
3182 break;
3183 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003184 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003185 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003186 break;
3187 }
3188 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003189
Chris Lattner13fe99c2005-04-02 05:00:07 +00003190 // Unary operators
3191 case ISD::FABS:
3192 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00003193 case ISD::FSQRT:
3194 case ISD::FSIN:
3195 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003196 Tmp1 = LegalizeOp(Node->getOperand(0));
3197 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003198 case TargetLowering::Promote:
3199 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00003200 isCustom = true;
3201 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00003202 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003203 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00003204 if (isCustom) {
3205 Tmp1 = TLI.LowerOperation(Result, DAG);
3206 if (Tmp1.Val) Result = Tmp1;
3207 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003208 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00003209 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003210 switch (Node->getOpcode()) {
3211 default: assert(0 && "Unreachable!");
3212 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003213 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3214 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003215 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00003216 break;
Chris Lattner80026402005-04-30 04:43:14 +00003217 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003218 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3219 MVT::ValueType VT = Node->getValueType(0);
3220 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003221 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003222 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3223 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00003224 break;
3225 }
3226 case ISD::FSQRT:
3227 case ISD::FSIN:
3228 case ISD::FCOS: {
3229 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman2a7de412007-10-11 23:57:53 +00003230
3231 // Expand unsupported unary vector operators by unrolling them.
3232 if (MVT::isVector(VT)) {
3233 Result = LegalizeOp(UnrollVectorOp(Op));
3234 break;
3235 }
3236
Evan Cheng31cbddf2007-01-12 02:11:51 +00003237 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00003238 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00003239 case ISD::FSQRT:
Dale Johannesen25a00a62007-09-28 01:08:20 +00003240 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00003241 VT == MVT::f64 ? RTLIB::SQRT_F64 :
3242 VT == MVT::f80 ? RTLIB::SQRT_F80 :
3243 VT == MVT::ppcf128 ? RTLIB::SQRT_PPCF128 :
3244 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003245 break;
3246 case ISD::FSIN:
3247 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3248 break;
3249 case ISD::FCOS:
3250 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3251 break;
Chris Lattner80026402005-04-30 04:43:14 +00003252 default: assert(0 && "Unreachable!");
3253 }
Nate Begeman77558da2005-08-04 21:43:28 +00003254 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003255 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3256 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00003257 break;
3258 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003259 }
3260 break;
3261 }
3262 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003263 case ISD::FPOWI: {
Dan Gohman2a7de412007-10-11 23:57:53 +00003264 MVT::ValueType VT = Node->getValueType(0);
3265
3266 // Expand unsupported unary vector operators by unrolling them.
3267 if (MVT::isVector(VT)) {
3268 Result = LegalizeOp(UnrollVectorOp(Op));
3269 break;
3270 }
3271
3272 // We always lower FPOWI into a libcall. No target support for it yet.
Dale Johannesen25a00a62007-09-28 01:08:20 +00003273 RTLIB::Libcall LC =
Dan Gohman2a7de412007-10-11 23:57:53 +00003274 VT == MVT::f32 ? RTLIB::POWI_F32 :
3275 VT == MVT::f64 ? RTLIB::POWI_F64 :
3276 VT == MVT::f80 ? RTLIB::POWI_F80 :
3277 VT == MVT::ppcf128 ? RTLIB::POWI_PPCF128 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00003278 RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003279 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003280 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3281 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003282 break;
3283 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003284 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003285 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00003286 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohmana8665142007-06-25 16:23:39 +00003287 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3288 // The input has to be a vector type, we have to either scalarize it, pack
3289 // it, or convert it based on whether the input vector type is legal.
3290 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00003291 int InIx = Node->getOperand(0).ResNo;
3292 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3293 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmana8665142007-06-25 16:23:39 +00003294
3295 // Figure out if there is a simple type corresponding to this Vector
3296 // type. If so, convert to the vector type.
3297 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3298 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003299 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003300 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3301 LegalizeOp(Node->getOperand(0)));
3302 break;
3303 } else if (NumElems == 1) {
3304 // Turn this into a bit convert of the scalar input.
3305 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3306 ScalarizeVectorOp(Node->getOperand(0)));
3307 break;
3308 } else {
3309 // FIXME: UNIMP! Store then reload
3310 assert(0 && "Cast from unsupported vector type not implemented yet!");
3311 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003312 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003313 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3314 Node->getOperand(0).getValueType())) {
3315 default: assert(0 && "Unknown operation action!");
3316 case TargetLowering::Expand:
3317 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3318 break;
3319 case TargetLowering::Legal:
3320 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003321 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003322 break;
3323 }
3324 }
3325 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00003326
Chris Lattner13fe99c2005-04-02 05:00:07 +00003327 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00003328 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003329 case ISD::UINT_TO_FP: {
3330 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00003331 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3332 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00003333 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003334 Node->getOperand(0).getValueType())) {
3335 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003336 case TargetLowering::Custom:
3337 isCustom = true;
3338 // FALLTHROUGH
3339 case TargetLowering::Legal:
3340 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003341 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003342 if (isCustom) {
3343 Tmp1 = TLI.LowerOperation(Result, DAG);
3344 if (Tmp1.Val) Result = Tmp1;
3345 }
3346 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003347 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00003348 Result = ExpandLegalINT_TO_FP(isSigned,
3349 LegalizeOp(Node->getOperand(0)),
3350 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003351 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003352 case TargetLowering::Promote:
3353 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3354 Node->getValueType(0),
3355 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003356 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00003357 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003358 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00003359 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003360 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3361 Node->getValueType(0), Node->getOperand(0));
3362 break;
3363 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003364 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003365 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003366 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3367 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003368 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003369 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3370 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003371 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003372 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3373 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003374 break;
3375 }
3376 break;
3377 }
3378 case ISD::TRUNCATE:
3379 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3380 case Legal:
3381 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003382 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003383 break;
3384 case Expand:
3385 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3386
3387 // Since the result is legal, we should just be able to truncate the low
3388 // part of the source.
3389 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3390 break;
3391 case Promote:
3392 Result = PromoteOp(Node->getOperand(0));
3393 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3394 break;
3395 }
3396 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003397
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003398 case ISD::FP_TO_SINT:
3399 case ISD::FP_TO_UINT:
3400 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3401 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00003402 Tmp1 = LegalizeOp(Node->getOperand(0));
3403
Chris Lattner44fe26f2005-07-29 00:11:56 +00003404 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3405 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003406 case TargetLowering::Custom:
3407 isCustom = true;
3408 // FALLTHROUGH
3409 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003410 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003411 if (isCustom) {
3412 Tmp1 = TLI.LowerOperation(Result, DAG);
3413 if (Tmp1.Val) Result = Tmp1;
3414 }
3415 break;
3416 case TargetLowering::Promote:
3417 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3418 Node->getOpcode() == ISD::FP_TO_SINT);
3419 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00003420 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00003421 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3422 SDOperand True, False;
3423 MVT::ValueType VT = Node->getOperand(0).getValueType();
3424 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenb59d25f2007-09-19 17:53:26 +00003425 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003426 const uint64_t zero[] = {0, 0};
3427 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3428 uint64_t x = 1ULL << ShiftAmt;
Neil Booth5f009732007-10-07 11:45:55 +00003429 (void)apf.convertFromZeroExtendedInteger
3430 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003431 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begeman36853ee2005-08-14 01:20:53 +00003432 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3433 Node->getOperand(0), Tmp2, ISD::SETLT);
3434 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3435 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00003436 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00003437 Tmp2));
3438 False = DAG.getNode(ISD::XOR, NVT, False,
3439 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003440 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3441 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00003442 } else {
3443 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3444 }
3445 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003446 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003447 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003448 case Expand: {
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003449 MVT::ValueType VT = Op.getValueType();
Dale Johannesen666323e2007-10-10 01:01:31 +00003450 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesen6472eb62007-10-11 23:32:15 +00003451 // Convert ppcf128 to i32
Dale Johannesen666323e2007-10-10 01:01:31 +00003452 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Dale Johannesen6472eb62007-10-11 23:32:15 +00003453 if (Node->getOpcode()==ISD::FP_TO_SINT)
3454 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
Dale Johannesen666323e2007-10-10 01:01:31 +00003455 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3456 (DAG.getNode(ISD::FP_ROUND_INREG,
3457 MVT::ppcf128, Node->getOperand(0),
3458 DAG.getValueType(MVT::f64)))));
Dale Johannesen6472eb62007-10-11 23:32:15 +00003459 else {
3460 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3461 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3462 Tmp2 = DAG.getConstantFP(apf, OVT);
3463 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3464 // FIXME: generated code sucks.
3465 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3466 DAG.getNode(ISD::ADD, MVT::i32,
3467 DAG.getNode(ISD::FP_TO_SINT, VT,
3468 DAG.getNode(ISD::FSUB, OVT,
3469 Node->getOperand(0), Tmp2)),
3470 DAG.getConstant(0x80000000, MVT::i32)),
3471 DAG.getNode(ISD::FP_TO_SINT, VT,
3472 Node->getOperand(0)),
3473 DAG.getCondCode(ISD::SETGE));
3474 }
Dale Johannesen666323e2007-10-10 01:01:31 +00003475 break;
3476 }
Dale Johannesen6472eb62007-10-11 23:32:15 +00003477 // Convert f32 / f64 to i32 / i64.
Evan Cheng31cbddf2007-01-12 02:11:51 +00003478 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003479 switch (Node->getOpcode()) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003480 case ISD::FP_TO_SINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003481 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003482 LC = (VT == MVT::i32)
3483 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003484 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003485 LC = (VT == MVT::i32)
3486 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003487 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003488 assert(VT == MVT::i64);
Dale Johannesenc0154c02007-10-05 20:04:43 +00003489 LC = RTLIB::FPTOSINT_F80_I64;
3490 }
3491 else if (OVT == MVT::ppcf128) {
3492 assert(VT == MVT::i64);
3493 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003494 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003495 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003496 }
3497 case ISD::FP_TO_UINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003498 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003499 LC = (VT == MVT::i32)
3500 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003501 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003502 LC = (VT == MVT::i32)
3503 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003504 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003505 LC = (VT == MVT::i32)
Dale Johannesenc0154c02007-10-05 20:04:43 +00003506 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3507 }
3508 else if (OVT == MVT::ppcf128) {
3509 assert(VT == MVT::i64);
3510 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003511 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003512 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003513 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003514 default: assert(0 && "Unreachable!");
3515 }
3516 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003517 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3518 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003519 break;
3520 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003521 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003522 Tmp1 = PromoteOp(Node->getOperand(0));
3523 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3524 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003525 break;
3526 }
3527 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003528
Dale Johannesenc339e452007-08-09 17:27:48 +00003529 case ISD::FP_EXTEND:
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003530 case ISD::FP_ROUND: {
3531 MVT::ValueType newVT = Op.getValueType();
3532 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3533 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesenf864ac92007-10-06 01:24:11 +00003534 if (Node->getOpcode() == ISD::FP_ROUND && oldVT == MVT::ppcf128) {
3535 SDOperand Lo, Hi;
3536 ExpandOp(Node->getOperand(0), Lo, Hi);
3537 if (newVT == MVT::f64)
3538 Result = Hi;
3539 else
3540 Result = DAG.getNode(ISD::FP_ROUND, newVT, Hi);
3541 break;
Dale Johannesenc339e452007-08-09 17:27:48 +00003542 } else {
Dale Johannesenf864ac92007-10-06 01:24:11 +00003543 // The only other way we can lower this is to turn it into a STORE,
3544 // LOAD pair, targetting a temporary location (a stack slot).
3545
3546 // NOTE: there is a choice here between constantly creating new stack
3547 // slots and always reusing the same one. We currently always create
3548 // new ones, as reuse may inhibit scheduling.
3549 MVT::ValueType slotVT =
3550 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3551 const Type *Ty = MVT::getTypeForValueType(slotVT);
Duncan Sands44b87212007-11-01 20:53:16 +00003552 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Dale Johannesenf864ac92007-10-06 01:24:11 +00003553 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3554 MachineFunction &MF = DAG.getMachineFunction();
3555 int SSFI =
3556 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3557 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3558 if (Node->getOpcode() == ISD::FP_EXTEND) {
3559 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3560 StackSlot, NULL, 0);
3561 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3562 Result, StackSlot, NULL, 0, oldVT);
3563 } else {
3564 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3565 StackSlot, NULL, 0, newVT);
Duncan Sands052c8432007-10-16 09:07:20 +00003566 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0);
Dale Johannesenf864ac92007-10-06 01:24:11 +00003567 }
3568 break;
Dale Johannesenc339e452007-08-09 17:27:48 +00003569 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003570 }
Dale Johannesena2b3c172007-07-03 00:53:03 +00003571 }
3572 // FALL THROUGH
Chris Lattner7753f172005-09-02 00:18:10 +00003573 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003574 case ISD::ZERO_EXTEND:
3575 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003576 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003577 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003578 case Legal:
3579 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003580 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003581 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003582 case Promote:
3583 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003584 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003585 Tmp1 = PromoteOp(Node->getOperand(0));
3586 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003587 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003588 case ISD::ZERO_EXTEND:
3589 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003590 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003591 Result = DAG.getZeroExtendInReg(Result,
3592 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003593 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003594 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003595 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003596 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003597 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003598 Result,
3599 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003600 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003601 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003602 Result = PromoteOp(Node->getOperand(0));
3603 if (Result.getValueType() != Op.getValueType())
3604 // Dynamically dead while we have only 2 FP types.
3605 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3606 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003607 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00003608 Result = PromoteOp(Node->getOperand(0));
3609 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3610 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003611 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003612 }
3613 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003614 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003615 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003616 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003617 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003618
3619 // If this operation is not supported, convert it to a shl/shr or load/store
3620 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003621 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3622 default: assert(0 && "This action not supported for this op yet!");
3623 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003624 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003625 break;
3626 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003627 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003628 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003629 // NOTE: we could fall back on load/store here too for targets without
3630 // SAR. However, it is doubtful that any exist.
3631 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3632 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003633 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003634 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3635 Node->getOperand(0), ShiftCst);
3636 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3637 Result, ShiftCst);
3638 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003639 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003640 // EXTLOAD pair, targetting a temporary location (a stack slot).
3641
3642 // NOTE: there is a choice here between constantly creating new stack
3643 // slots and always reusing the same one. We currently always create
3644 // new ones, as reuse may inhibit scheduling.
3645 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Duncan Sands44b87212007-11-01 20:53:16 +00003646 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Chris Lattner945e4372007-02-14 05:52:17 +00003647 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner99222f72005-01-15 07:15:18 +00003648 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00003649 int SSFI =
Chris Lattner1deacd62007-04-28 06:42:38 +00003650 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner99222f72005-01-15 07:15:18 +00003651 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003652 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3653 StackSlot, NULL, 0, ExtraVT);
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003654 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003655 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00003656 } else {
3657 assert(0 && "Unknown op");
3658 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003659 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003660 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003661 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003662 }
Duncan Sands644f9172007-07-27 12:58:54 +00003663 case ISD::TRAMPOLINE: {
3664 SDOperand Ops[6];
3665 for (unsigned i = 0; i != 6; ++i)
3666 Ops[i] = LegalizeOp(Node->getOperand(i));
3667 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3668 // The only option for this node is to custom lower it.
3669 Result = TLI.LowerOperation(Result, DAG);
3670 assert(Result.Val && "Should always custom lower!");
Duncan Sands86e01192007-09-11 14:10:23 +00003671
3672 // Since trampoline produces two values, make sure to remember that we
3673 // legalized both of them.
3674 Tmp1 = LegalizeOp(Result.getValue(1));
3675 Result = LegalizeOp(Result);
3676 AddLegalizedOperand(SDOperand(Node, 0), Result);
3677 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3678 return Op.ResNo ? Tmp1 : Result;
Duncan Sands644f9172007-07-27 12:58:54 +00003679 }
Chris Lattner99222f72005-01-15 07:15:18 +00003680 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003681
Chris Lattner101ea662006-04-08 04:13:17 +00003682 assert(Result.getValueType() == Op.getValueType() &&
3683 "Bad legalization!");
3684
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003685 // Make sure that the generated code is itself legal.
3686 if (Result != Op)
3687 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003688
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003689 // Note that LegalizeOp may be reentered even from single-use nodes, which
3690 // means that we always must cache transformed nodes.
3691 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003692 return Result;
3693}
3694
Chris Lattner4d978642005-01-15 22:16:26 +00003695/// PromoteOp - Given an operation that produces a value in an invalid type,
3696/// promote it to compute the value into a larger type. The produced value will
3697/// have the correct bits for the low portion of the register, but no guarantee
3698/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003699SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3700 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003701 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003702 assert(getTypeAction(VT) == Promote &&
3703 "Caller should expand or legalize operands that are not promotable!");
3704 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3705 "Cannot promote to smaller type!");
3706
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003707 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003708 SDOperand Result;
3709 SDNode *Node = Op.Val;
3710
Chris Lattner4b0ddb22007-02-04 01:17:38 +00003711 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00003712 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003713
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003714 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003715 case ISD::CopyFromReg:
3716 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003717 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003718#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00003719 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003720#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003721 assert(0 && "Do not know how to promote this operator!");
3722 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003723 case ISD::UNDEF:
3724 Result = DAG.getNode(ISD::UNDEF, NVT);
3725 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003726 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003727 if (VT != MVT::i1)
3728 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3729 else
3730 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003731 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3732 break;
3733 case ISD::ConstantFP:
3734 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3735 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3736 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003737
Chris Lattner2cb338d2005-01-18 02:59:52 +00003738 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003739 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003740 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3741 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003742 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003743
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003744 case ISD::TRUNCATE:
3745 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3746 case Legal:
3747 Result = LegalizeOp(Node->getOperand(0));
3748 assert(Result.getValueType() >= NVT &&
3749 "This truncation doesn't make sense!");
3750 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3751 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3752 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003753 case Promote:
3754 // The truncation is not required, because we don't guarantee anything
3755 // about high bits anyway.
3756 Result = PromoteOp(Node->getOperand(0));
3757 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003758 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003759 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3760 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003761 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003762 }
3763 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003764 case ISD::SIGN_EXTEND:
3765 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003766 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003767 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3768 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3769 case Legal:
3770 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003771 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003772 break;
3773 case Promote:
3774 // Promote the reg if it's smaller.
3775 Result = PromoteOp(Node->getOperand(0));
3776 // The high bits are not guaranteed to be anything. Insert an extend.
3777 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003778 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003779 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003780 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003781 Result = DAG.getZeroExtendInReg(Result,
3782 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003783 break;
3784 }
3785 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003786 case ISD::BIT_CONVERT:
3787 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3788 Result = PromoteOp(Result);
3789 break;
3790
Chris Lattner4d978642005-01-15 22:16:26 +00003791 case ISD::FP_EXTEND:
3792 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3793 case ISD::FP_ROUND:
3794 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3795 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3796 case Promote: assert(0 && "Unreachable with 2 FP types!");
3797 case Legal:
3798 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003799 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003800 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003801 break;
3802 }
3803 break;
3804
3805 case ISD::SINT_TO_FP:
3806 case ISD::UINT_TO_FP:
3807 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3808 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003809 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003810 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003811 break;
3812
3813 case Promote:
3814 Result = PromoteOp(Node->getOperand(0));
3815 if (Node->getOpcode() == ISD::SINT_TO_FP)
3816 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003817 Result,
3818 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003819 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003820 Result = DAG.getZeroExtendInReg(Result,
3821 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003822 // No extra round required here.
3823 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003824 break;
3825 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003826 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3827 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003828 // Round if we cannot tolerate excess precision.
3829 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003830 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3831 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003832 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003833 }
Chris Lattner4d978642005-01-15 22:16:26 +00003834 break;
3835
Chris Lattner268d4572005-12-09 17:32:47 +00003836 case ISD::SIGN_EXTEND_INREG:
3837 Result = PromoteOp(Node->getOperand(0));
3838 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3839 Node->getOperand(1));
3840 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003841 case ISD::FP_TO_SINT:
3842 case ISD::FP_TO_UINT:
3843 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3844 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003845 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003846 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003847 break;
3848 case Promote:
3849 // The input result is prerounded, so we don't have to do anything
3850 // special.
3851 Tmp1 = PromoteOp(Node->getOperand(0));
3852 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003853 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003854 // If we're promoting a UINT to a larger size, check to see if the new node
3855 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3856 // we can use that instead. This allows us to generate better code for
3857 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3858 // legal, such as PowerPC.
3859 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003860 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003861 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3862 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003863 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3864 } else {
3865 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3866 }
Chris Lattner4d978642005-01-15 22:16:26 +00003867 break;
3868
Chris Lattner13fe99c2005-04-02 05:00:07 +00003869 case ISD::FABS:
3870 case ISD::FNEG:
3871 Tmp1 = PromoteOp(Node->getOperand(0));
3872 assert(Tmp1.getValueType() == NVT);
3873 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3874 // NOTE: we do not have to do any extra rounding here for
3875 // NoExcessFPPrecision, because we know the input will have the appropriate
3876 // precision, and these operations don't modify precision at all.
3877 break;
3878
Chris Lattner9d6fa982005-04-28 21:44:33 +00003879 case ISD::FSQRT:
3880 case ISD::FSIN:
3881 case ISD::FCOS:
3882 Tmp1 = PromoteOp(Node->getOperand(0));
3883 assert(Tmp1.getValueType() == NVT);
3884 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003885 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003886 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3887 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003888 break;
3889
Chris Lattnerca401aa2007-03-03 23:43:21 +00003890 case ISD::FPOWI: {
3891 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3892 // directly as well, which may be better.
3893 Tmp1 = PromoteOp(Node->getOperand(0));
3894 assert(Tmp1.getValueType() == NVT);
3895 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3896 if (NoExcessFPPrecision)
3897 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3898 DAG.getValueType(VT));
3899 break;
3900 }
3901
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003902 case ISD::AND:
3903 case ISD::OR:
3904 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003905 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003906 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003907 case ISD::MUL:
3908 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003909 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003910 // that too is okay if they are integer operations.
3911 Tmp1 = PromoteOp(Node->getOperand(0));
3912 Tmp2 = PromoteOp(Node->getOperand(1));
3913 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3914 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003915 break;
3916 case ISD::FADD:
3917 case ISD::FSUB:
3918 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003919 Tmp1 = PromoteOp(Node->getOperand(0));
3920 Tmp2 = PromoteOp(Node->getOperand(1));
3921 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3922 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3923
3924 // Floating point operations will give excess precision that we may not be
3925 // able to tolerate. If we DO allow excess precision, just leave it,
3926 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003927 // FIXME: Why would we need to round FP ops more than integer ones?
3928 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003929 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003930 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3931 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003932 break;
3933
Chris Lattner4d978642005-01-15 22:16:26 +00003934 case ISD::SDIV:
3935 case ISD::SREM:
3936 // These operators require that their input be sign extended.
3937 Tmp1 = PromoteOp(Node->getOperand(0));
3938 Tmp2 = PromoteOp(Node->getOperand(1));
3939 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003940 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3941 DAG.getValueType(VT));
3942 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3943 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003944 }
3945 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3946
3947 // Perform FP_ROUND: this is probably overly pessimistic.
3948 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003949 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3950 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003951 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003952 case ISD::FDIV:
3953 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003954 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003955 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00003956 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3957 case Legal:
3958 Tmp1 = LegalizeOp(Node->getOperand(0));
3959 break;
3960 case Promote:
3961 Tmp1 = PromoteOp(Node->getOperand(0));
3962 break;
3963 case Expand:
3964 assert(0 && "not implemented");
3965 }
3966 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3967 case Legal:
3968 Tmp2 = LegalizeOp(Node->getOperand(1));
3969 break;
3970 case Promote:
3971 Tmp2 = PromoteOp(Node->getOperand(1));
3972 break;
3973 case Expand:
3974 assert(0 && "not implemented");
3975 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00003976 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3977
3978 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003979 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003980 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3981 DAG.getValueType(VT));
3982 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003983
3984 case ISD::UDIV:
3985 case ISD::UREM:
3986 // These operators require that their input be zero extended.
3987 Tmp1 = PromoteOp(Node->getOperand(0));
3988 Tmp2 = PromoteOp(Node->getOperand(1));
3989 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003990 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3991 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003992 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3993 break;
3994
3995 case ISD::SHL:
3996 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003997 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003998 break;
3999 case ISD::SRA:
4000 // The input value must be properly sign extended.
4001 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00004002 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4003 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004004 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004005 break;
4006 case ISD::SRL:
4007 // The input value must be properly zero extended.
4008 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00004009 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004010 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004011 break;
Nate Begeman595ec732006-01-28 03:14:31 +00004012
4013 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004014 Tmp1 = Node->getOperand(0); // Get the chain.
4015 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00004016 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4017 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4018 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4019 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00004020 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00004021 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00004022 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00004023 // Increment the pointer, VAList, to the next vaarg
4024 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4025 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4026 TLI.getPointerTy()));
4027 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00004028 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
4029 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00004030 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00004031 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00004032 }
4033 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004034 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00004035 break;
4036
Evan Chenge71fe34d2006-10-09 20:57:25 +00004037 case ISD::LOAD: {
4038 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00004039 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4040 ? ISD::EXTLOAD : LD->getExtensionType();
4041 Result = DAG.getExtLoad(ExtType, NVT,
4042 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00004043 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00004044 LD->getLoadedVT(),
4045 LD->isVolatile(),
4046 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004047 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004048 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004049 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00004050 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004051 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004052 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4053 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004054 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004055 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00004056 case ISD::SELECT_CC:
4057 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4058 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4059 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004060 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004061 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00004062 case ISD::BSWAP:
4063 Tmp1 = Node->getOperand(0);
4064 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4065 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4066 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004067 DAG.getConstant(MVT::getSizeInBits(NVT) -
4068 MVT::getSizeInBits(VT),
Nate Begeman2fba8a32006-01-14 03:14:10 +00004069 TLI.getShiftAmountTy()));
4070 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004071 case ISD::CTPOP:
4072 case ISD::CTTZ:
4073 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004074 // Zero extend the argument
4075 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004076 // Perform the larger operation, then subtract if needed.
4077 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004078 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004079 case ISD::CTPOP:
4080 Result = Tmp1;
4081 break;
4082 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004083 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00004084 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004085 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4086 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004087 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004088 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004089 break;
4090 case ISD::CTLZ:
4091 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004092 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004093 DAG.getConstant(MVT::getSizeInBits(NVT) -
4094 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004095 break;
4096 }
4097 break;
Dan Gohmana8665142007-06-25 16:23:39 +00004098 case ISD::EXTRACT_SUBVECTOR:
4099 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00004100 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00004101 case ISD::EXTRACT_VECTOR_ELT:
4102 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4103 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004104 }
4105
4106 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004107
4108 // Make sure the result is itself legal.
4109 Result = LegalizeOp(Result);
4110
4111 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004112 AddPromotedOperand(Op, Result);
4113 return Result;
4114}
Chris Lattnerdc750592005-01-07 07:47:09 +00004115
Dan Gohmana8665142007-06-25 16:23:39 +00004116/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4117/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4118/// based on the vector type. The return type of this matches the element type
4119/// of the vector, which may not be legal for the target.
4120SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00004121 // We know that operand #0 is the Vec vector. If the index is a constant
4122 // or if the invec is a supported hardware type, we can use it. Otherwise,
4123 // lower to a store then an indexed load.
4124 SDOperand Vec = Op.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00004125 SDOperand Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00004126
Dan Gohman60028182007-09-24 15:54:53 +00004127 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +00004128 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner6f423252006-03-31 17:55:51 +00004129
Dan Gohmana8665142007-06-25 16:23:39 +00004130 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4131 default: assert(0 && "This action is not supported yet!");
4132 case TargetLowering::Custom: {
4133 Vec = LegalizeOp(Vec);
4134 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4135 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4136 if (Tmp3.Val)
4137 return Tmp3;
4138 break;
4139 }
4140 case TargetLowering::Legal:
4141 if (isTypeLegal(TVT)) {
4142 Vec = LegalizeOp(Vec);
4143 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00004144 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00004145 }
4146 break;
4147 case TargetLowering::Expand:
4148 break;
4149 }
4150
4151 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00004152 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004153 Op = ScalarizeVectorOp(Vec);
4154 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4155 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner6f423252006-03-31 17:55:51 +00004156 SDOperand Lo, Hi;
4157 SplitVectorOp(Vec, Lo, Hi);
4158 if (CIdx->getValue() < NumElems/2) {
4159 Vec = Lo;
4160 } else {
4161 Vec = Hi;
Dan Gohmana8665142007-06-25 16:23:39 +00004162 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4163 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00004164 }
Dan Gohmana8665142007-06-25 16:23:39 +00004165
Chris Lattner6f423252006-03-31 17:55:51 +00004166 // It's now an extract from the appropriate high or low part. Recurse.
4167 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004168 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00004169 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00004170 // Store the value to a temporary stack slot, then LOAD the scalar
4171 // element back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004172 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmana8665142007-06-25 16:23:39 +00004173 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4174
4175 // Add the offset to the index.
4176 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4177 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4178 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling070aca52007-10-18 08:32:37 +00004179
4180 if (MVT::getSizeInBits(Idx.getValueType()) >
4181 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattner064c31e2007-10-19 16:47:35 +00004182 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004183 else
Chris Lattner064c31e2007-10-19 16:47:35 +00004184 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004185
Dan Gohmana8665142007-06-25 16:23:39 +00004186 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4187
4188 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00004189 }
Dan Gohmana8665142007-06-25 16:23:39 +00004190 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00004191}
4192
Dan Gohmana8665142007-06-25 16:23:39 +00004193/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00004194/// we assume the operation can be split if it is not already legal.
Dan Gohmana8665142007-06-25 16:23:39 +00004195SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00004196 // We know that operand #0 is the Vec vector. For now we assume the index
4197 // is a constant and that the extracted result is a supported hardware type.
4198 SDOperand Vec = Op.getOperand(0);
4199 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4200
Dan Gohmana8665142007-06-25 16:23:39 +00004201 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00004202
4203 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4204 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004205 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00004206 }
4207
4208 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4209 SDOperand Lo, Hi;
4210 SplitVectorOp(Vec, Lo, Hi);
4211 if (CIdx->getValue() < NumElems/2) {
4212 Vec = Lo;
4213 } else {
4214 Vec = Hi;
4215 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4216 }
4217
4218 // It's now an extract from the appropriate high or low part. Recurse.
4219 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004220 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00004221}
4222
Nate Begeman7e7f4392006-02-01 07:19:44 +00004223/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4224/// with condition CC on the current target. This usually involves legalizing
4225/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4226/// there may be no choice but to create a new SetCC node to represent the
4227/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4228/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4229void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4230 SDOperand &RHS,
4231 SDOperand &CC) {
Dale Johannesenf864ac92007-10-06 01:24:11 +00004232 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman7e7f4392006-02-01 07:19:44 +00004233
4234 switch (getTypeAction(LHS.getValueType())) {
4235 case Legal:
4236 Tmp1 = LegalizeOp(LHS); // LHS
4237 Tmp2 = LegalizeOp(RHS); // RHS
4238 break;
4239 case Promote:
4240 Tmp1 = PromoteOp(LHS); // LHS
4241 Tmp2 = PromoteOp(RHS); // RHS
4242
4243 // If this is an FP compare, the operands have already been extended.
4244 if (MVT::isInteger(LHS.getValueType())) {
4245 MVT::ValueType VT = LHS.getValueType();
4246 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4247
4248 // Otherwise, we have to insert explicit sign or zero extends. Note
4249 // that we could insert sign extends for ALL conditions, but zero extend
4250 // is cheaper on many machines (an AND instead of two shifts), so prefer
4251 // it.
4252 switch (cast<CondCodeSDNode>(CC)->get()) {
4253 default: assert(0 && "Unknown integer comparison!");
4254 case ISD::SETEQ:
4255 case ISD::SETNE:
4256 case ISD::SETUGE:
4257 case ISD::SETUGT:
4258 case ISD::SETULE:
4259 case ISD::SETULT:
4260 // ALL of these operations will work if we either sign or zero extend
4261 // the operands (including the unsigned comparisons!). Zero extend is
4262 // usually a simpler/cheaper operation, so prefer it.
4263 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4264 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4265 break;
4266 case ISD::SETGE:
4267 case ISD::SETGT:
4268 case ISD::SETLT:
4269 case ISD::SETLE:
4270 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4271 DAG.getValueType(VT));
4272 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4273 DAG.getValueType(VT));
4274 break;
4275 }
4276 }
4277 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004278 case Expand: {
4279 MVT::ValueType VT = LHS.getValueType();
4280 if (VT == MVT::f32 || VT == MVT::f64) {
4281 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00004282 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004283 switch (cast<CondCodeSDNode>(CC)->get()) {
4284 case ISD::SETEQ:
4285 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004286 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004287 break;
4288 case ISD::SETNE:
4289 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004290 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004291 break;
4292 case ISD::SETGE:
4293 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004294 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004295 break;
4296 case ISD::SETLT:
4297 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004298 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004299 break;
4300 case ISD::SETLE:
4301 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004302 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004303 break;
4304 case ISD::SETGT:
4305 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004306 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004307 break;
4308 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00004309 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4310 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004311 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00004312 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004313 break;
4314 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004315 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004316 switch (cast<CondCodeSDNode>(CC)->get()) {
4317 case ISD::SETONE:
4318 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00004319 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004320 // Fallthrough
4321 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004322 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004323 break;
4324 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004325 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004326 break;
4327 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004328 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004329 break;
4330 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004331 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004332 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004333 case ISD::SETUEQ:
4334 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004335 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004336 default: assert(0 && "Unsupported FP setcc!");
4337 }
4338 }
4339
4340 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004341 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00004342 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004343 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004344 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00004345 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00004346 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004347 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00004348 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00004349 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004350 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004351 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00004352 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004353 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4354 Tmp2 = SDOperand();
4355 }
4356 LHS = Tmp1;
4357 RHS = Tmp2;
4358 return;
4359 }
4360
Nate Begeman7e7f4392006-02-01 07:19:44 +00004361 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4362 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004363 ExpandOp(RHS, RHSLo, RHSHi);
4364 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4365
4366 if (VT==MVT::ppcf128) {
4367 // FIXME: This generated code sucks. We want to generate
4368 // FCMP crN, hi1, hi2
4369 // BNE crN, L:
4370 // FCMP crN, lo1, lo2
4371 // The following can be improved, but not that much.
4372 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4373 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4374 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4375 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4376 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4377 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4378 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4379 Tmp2 = SDOperand();
4380 break;
4381 }
4382
4383 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004384 case ISD::SETEQ:
4385 case ISD::SETNE:
4386 if (RHSLo == RHSHi)
4387 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4388 if (RHSCST->isAllOnesValue()) {
4389 // Comparison to -1.
4390 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4391 Tmp2 = RHSLo;
4392 break;
4393 }
4394
4395 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4396 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4397 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4398 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4399 break;
4400 default:
4401 // If this is a comparison of the sign bit, just look at the top part.
4402 // X > -1, x < 0
4403 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4404 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4405 CST->getValue() == 0) || // X < 0
4406 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4407 CST->isAllOnesValue())) { // X > -1
4408 Tmp1 = LHSHi;
4409 Tmp2 = RHSHi;
4410 break;
4411 }
4412
4413 // FIXME: This generated code sucks.
4414 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00004415 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004416 default: assert(0 && "Unknown integer setcc!");
4417 case ISD::SETLT:
4418 case ISD::SETULT: LowCC = ISD::SETULT; break;
4419 case ISD::SETGT:
4420 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4421 case ISD::SETLE:
4422 case ISD::SETULE: LowCC = ISD::SETULE; break;
4423 case ISD::SETGE:
4424 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4425 }
4426
4427 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4428 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4429 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4430
4431 // NOTE: on targets without efficient SELECT of bools, we can always use
4432 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00004433 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4434 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4435 false, DagCombineInfo);
4436 if (!Tmp1.Val)
4437 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4438 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4439 CCCode, false, DagCombineInfo);
4440 if (!Tmp2.Val)
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004441 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng93049452007-02-08 22:16:19 +00004442
4443 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4444 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4445 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4446 (Tmp2C && Tmp2C->getValue() == 0 &&
4447 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4448 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4449 (Tmp2C && Tmp2C->getValue() == 1 &&
4450 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4451 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4452 // low part is known false, returns high part.
4453 // For LE / GE, if high part is known false, ignore the low part.
4454 // For LT / GT, if high part is known true, ignore the low part.
4455 Tmp1 = Tmp2;
4456 Tmp2 = SDOperand();
4457 } else {
4458 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4459 ISD::SETEQ, false, DagCombineInfo);
4460 if (!Result.Val)
4461 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4462 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4463 Result, Tmp1, Tmp2));
4464 Tmp1 = Result;
4465 Tmp2 = SDOperand();
4466 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004467 }
4468 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004469 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004470 LHS = Tmp1;
4471 RHS = Tmp2;
4472}
4473
Chris Lattner36e663d2005-12-23 00:16:34 +00004474/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00004475/// The resultant code need not be legal. Note that SrcOp is the input operand
4476/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00004477SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4478 SDOperand SrcOp) {
4479 // Create the stack frame object.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004480 SDOperand FIPtr = DAG.CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004481
4482 // Emit a store to the stack slot.
Evan Chengab51cf22006-10-13 21:14:26 +00004483 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004484 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004485 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004486}
4487
Chris Lattner6be79822006-04-04 17:23:26 +00004488SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4489 // Create a vector sized/aligned stack slot, store the value to element #0,
4490 // then load the whole vector back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004491 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00004492 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00004493 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004494 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00004495}
4496
4497
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004498/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00004499/// support the operation, but do support the resultant vector type.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004500SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4501
4502 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00004503 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004504 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004505 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00004506 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004507 std::map<SDOperand, std::vector<unsigned> > Values;
4508 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004509 bool isConstant = true;
4510 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4511 SplatValue.getOpcode() != ISD::UNDEF)
4512 isConstant = false;
4513
Evan Cheng1d2e9952006-03-24 01:17:21 +00004514 for (unsigned i = 1; i < NumElems; ++i) {
4515 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00004516 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004517 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004518 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004519 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00004520 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004521
4522 // If this isn't a constant element or an undef, we can't use a constant
4523 // pool load.
4524 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4525 V.getOpcode() != ISD::UNDEF)
4526 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004527 }
4528
4529 if (isOnlyLowElement) {
4530 // If the low element is an undef too, then this whole things is an undef.
4531 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4532 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4533 // Otherwise, turn this into a scalar_to_vector node.
4534 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4535 Node->getOperand(0));
4536 }
4537
Chris Lattner77e271c2006-03-24 07:29:17 +00004538 // If all elements are constants, create a load from the constant pool.
4539 if (isConstant) {
4540 MVT::ValueType VT = Node->getValueType(0);
4541 const Type *OpNTy =
4542 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4543 std::vector<Constant*> CV;
4544 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4545 if (ConstantFPSDNode *V =
4546 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00004547 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004548 } else if (ConstantSDNode *V =
4549 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00004550 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004551 } else {
4552 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4553 CV.push_back(UndefValue::get(OpNTy));
4554 }
4555 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00004556 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00004557 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00004558 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004559 }
4560
Chris Lattner21e68c82006-03-20 01:52:29 +00004561 if (SplatValue.Val) { // Splat of one value?
4562 // Build the shuffle constant vector: <0, 0, 0, 0>
4563 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00004564 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00004565 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004566 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004567 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4568 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00004569
4570 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004571 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00004572 // Get the splatted value into the low element of a vector register.
4573 SDOperand LowValVec =
4574 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4575
4576 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4577 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4578 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4579 SplatMask);
4580 }
4581 }
4582
Evan Cheng1d2e9952006-03-24 01:17:21 +00004583 // If there are only two unique elements, we may be able to turn this into a
4584 // vector shuffle.
4585 if (Values.size() == 2) {
4586 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4587 MVT::ValueType MaskVT =
4588 MVT::getIntVectorWithNumElements(NumElems);
4589 std::vector<SDOperand> MaskVec(NumElems);
4590 unsigned i = 0;
4591 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4592 E = Values.end(); I != E; ++I) {
4593 for (std::vector<unsigned>::iterator II = I->second.begin(),
4594 EE = I->second.end(); II != EE; ++II)
Dan Gohman5c441312007-06-14 22:58:02 +00004595 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004596 i += NumElems;
4597 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004598 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4599 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004600
4601 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004602 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4603 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004604 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004605 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4606 E = Values.end(); I != E; ++I) {
4607 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4608 I->first);
4609 Ops.push_back(Op);
4610 }
4611 Ops.push_back(ShuffleMask);
4612
4613 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004614 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4615 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004616 }
4617 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004618
4619 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4620 // aligned object on the stack, store each element into it, then load
4621 // the result as a vector.
4622 MVT::ValueType VT = Node->getValueType(0);
4623 // Create the stack frame object.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004624 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004625
4626 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004627 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004628 unsigned TypeByteSize =
4629 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004630 // Store (in the right endianness) the elements to memory.
4631 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4632 // Ignore undef elements.
4633 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4634
Chris Lattner8fa445a2006-03-22 01:46:54 +00004635 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004636
4637 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4638 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4639
Evan Chengdf9ac472006-10-05 23:01:46 +00004640 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00004641 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004642 }
4643
4644 SDOperand StoreChain;
4645 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004646 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4647 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004648 else
4649 StoreChain = DAG.getEntryNode();
4650
4651 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004652 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004653}
4654
Chris Lattner4157c412005-04-02 04:00:59 +00004655void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4656 SDOperand Op, SDOperand Amt,
4657 SDOperand &Lo, SDOperand &Hi) {
4658 // Expand the subcomponents.
4659 SDOperand LHSL, LHSH;
4660 ExpandOp(Op, LHSL, LHSH);
4661
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004662 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00004663 MVT::ValueType VT = LHSL.getValueType();
4664 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00004665 Hi = Lo.getValue(1);
4666}
4667
4668
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004669/// ExpandShift - Try to find a clever way to expand this shift operation out to
4670/// smaller elements. If we can't find a way that is more efficient than a
4671/// libcall on this target, return false. Otherwise, return true with the
4672/// low-parts expanded into Lo and Hi.
4673bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4674 SDOperand &Lo, SDOperand &Hi) {
4675 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4676 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00004677
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004678 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00004679 SDOperand ShAmt = LegalizeOp(Amt);
4680 MVT::ValueType ShTy = ShAmt.getValueType();
4681 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4682 unsigned NVTBits = MVT::getSizeInBits(NVT);
4683
Chris Lattnerfbbe5702007-10-14 20:35:12 +00004684 // Handle the case when Amt is an immediate.
Nate Begemanb0674922005-04-06 21:13:14 +00004685 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4686 unsigned Cst = CN->getValue();
4687 // Expand the incoming operand to be shifted, so that we have its parts
4688 SDOperand InL, InH;
4689 ExpandOp(Op, InL, InH);
4690 switch(Opc) {
4691 case ISD::SHL:
4692 if (Cst > VTBits) {
4693 Lo = DAG.getConstant(0, NVT);
4694 Hi = DAG.getConstant(0, NVT);
4695 } else if (Cst > NVTBits) {
4696 Lo = DAG.getConstant(0, NVT);
4697 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004698 } else if (Cst == NVTBits) {
4699 Lo = DAG.getConstant(0, NVT);
4700 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00004701 } else {
4702 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4703 Hi = DAG.getNode(ISD::OR, NVT,
4704 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4705 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4706 }
4707 return true;
4708 case ISD::SRL:
4709 if (Cst > VTBits) {
4710 Lo = DAG.getConstant(0, NVT);
4711 Hi = DAG.getConstant(0, NVT);
4712 } else if (Cst > NVTBits) {
4713 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4714 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00004715 } else if (Cst == NVTBits) {
4716 Lo = InH;
4717 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00004718 } else {
4719 Lo = DAG.getNode(ISD::OR, NVT,
4720 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4721 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4722 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4723 }
4724 return true;
4725 case ISD::SRA:
4726 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004727 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004728 DAG.getConstant(NVTBits-1, ShTy));
4729 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004730 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004731 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004732 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004733 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004734 } else if (Cst == NVTBits) {
4735 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004736 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004737 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004738 } else {
4739 Lo = DAG.getNode(ISD::OR, NVT,
4740 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4741 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4742 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4743 }
4744 return true;
4745 }
4746 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004747
4748 // Okay, the shift amount isn't constant. However, if we can tell that it is
4749 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4750 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00004751 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00004752
4753 // If we know that the high bit of the shift amount is one, then we can do
4754 // this as a couple of simple shifts.
4755 if (KnownOne & Mask) {
4756 // Mask out the high bit, which we know is set.
4757 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4758 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4759
4760 // Expand the incoming operand to be shifted, so that we have its parts
4761 SDOperand InL, InH;
4762 ExpandOp(Op, InL, InH);
4763 switch(Opc) {
4764 case ISD::SHL:
4765 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4766 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4767 return true;
4768 case ISD::SRL:
4769 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4770 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4771 return true;
4772 case ISD::SRA:
4773 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4774 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4775 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4776 return true;
4777 }
4778 }
4779
4780 // If we know that the high bit of the shift amount is zero, then we can do
4781 // this as a couple of simple shifts.
4782 if (KnownZero & Mask) {
4783 // Compute 32-amt.
4784 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4785 DAG.getConstant(NVTBits, Amt.getValueType()),
4786 Amt);
4787
4788 // Expand the incoming operand to be shifted, so that we have its parts
4789 SDOperand InL, InH;
4790 ExpandOp(Op, InL, InH);
4791 switch(Opc) {
4792 case ISD::SHL:
4793 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4794 Hi = DAG.getNode(ISD::OR, NVT,
4795 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4796 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4797 return true;
4798 case ISD::SRL:
4799 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4800 Lo = DAG.getNode(ISD::OR, NVT,
4801 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4802 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4803 return true;
4804 case ISD::SRA:
4805 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4806 Lo = DAG.getNode(ISD::OR, NVT,
4807 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4808 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4809 return true;
4810 }
4811 }
4812
Nate Begemanb0674922005-04-06 21:13:14 +00004813 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004814}
Chris Lattneraac464e2005-01-21 06:05:23 +00004815
Chris Lattner4add7e32005-01-23 04:42:50 +00004816
Chris Lattneraac464e2005-01-21 06:05:23 +00004817// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4818// does not fit into a register, return the lo part and set the hi part to the
4819// by-reg argument. If it does fit into a single register, return the result
4820// and leave the Hi part unset.
4821SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004822 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004823 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4824 // The input chain to this libcall is the entry node of the function.
4825 // Legalizing the call will automatically add the previous call to the
4826 // dependence.
4827 SDOperand InChain = DAG.getEntryNode();
4828
Chris Lattneraac464e2005-01-21 06:05:23 +00004829 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004830 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004831 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4832 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4833 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004834 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00004835 Entry.isSExt = isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00004836 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004837 }
4838 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004839
Chris Lattner06bbeb62005-05-11 19:02:11 +00004840 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004841 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004842 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004843 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004844 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004845
Chris Lattner462505f2006-02-13 09:18:02 +00004846 // Legalize the call sequence, starting with the chain. This will advance
4847 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4848 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4849 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004850 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004851 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004852 default: assert(0 && "Unknown thing");
4853 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004854 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004855 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004856 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004857 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004858 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004859 }
Chris Lattner63022662005-09-02 20:26:58 +00004860 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004861}
4862
Chris Lattner4add7e32005-01-23 04:42:50 +00004863
Evan Chengbf535fc2007-04-27 07:33:31 +00004864/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4865///
Chris Lattneraac464e2005-01-21 06:05:23 +00004866SDOperand SelectionDAGLegalize::
4867ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004868 assert(getTypeAction(Source.getValueType()) == Expand &&
4869 "This is not an expansion!");
4870 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4871
Chris Lattner06bbeb62005-05-11 19:02:11 +00004872 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004873 assert(Source.getValueType() == MVT::i64 &&
4874 "This only works for 64-bit -> FP");
4875 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4876 // incoming integer is set. To handle this, we dynamically test to see if
4877 // it is set, and, if so, add a fudge factor.
4878 SDOperand Lo, Hi;
4879 ExpandOp(Source, Lo, Hi);
4880
Chris Lattner2a4f7312005-05-13 04:45:13 +00004881 // If this is unsigned, and not supported, first perform the conversion to
4882 // signed, then adjust the result if the sign bit is set.
4883 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4884 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4885
Chris Lattnerd47675e2005-08-09 20:20:18 +00004886 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4887 DAG.getConstant(0, Hi.getValueType()),
4888 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004889 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4890 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4891 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004892 uint64_t FF = 0x5f800000ULL;
4893 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004894 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004895
Chris Lattnerc30405e2005-08-26 17:15:30 +00004896 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004897 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4898 SDOperand FudgeInReg;
4899 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004900 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen7f724e92007-09-16 16:51:49 +00004901 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Chengbf535fc2007-04-27 07:33:31 +00004902 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen7f724e92007-09-16 16:51:49 +00004903 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00004904 CPIdx, NULL, 0, MVT::f32);
4905 else
4906 assert(0 && "Unexpected conversion");
4907
Evan Chengbf535fc2007-04-27 07:33:31 +00004908 MVT::ValueType SCVT = SignedConv.getValueType();
4909 if (SCVT != DestTy) {
4910 // Destination type needs to be expanded as well. The FADD now we are
4911 // constructing will be expanded into a libcall.
4912 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4913 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4914 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4915 SignedConv, SignedConv.getValue(1));
4916 }
4917 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4918 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00004919 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00004920 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00004921
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004922 // Check to see if the target has a custom way to lower this. If so, use it.
4923 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4924 default: assert(0 && "This action not implemented for this operation!");
4925 case TargetLowering::Legal:
4926 case TargetLowering::Expand:
4927 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004928 case TargetLowering::Custom: {
4929 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4930 Source), DAG);
4931 if (NV.Val)
4932 return LegalizeOp(NV);
4933 break; // The target decided this was legal after all
4934 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004935 }
4936
Chris Lattner153587e2005-05-12 07:00:44 +00004937 // Expand the source, then glue it back together for the call. We must expand
4938 // the source in case it is shared (this pass of legalize must traverse it).
4939 SDOperand SrcLo, SrcHi;
4940 ExpandOp(Source, SrcLo, SrcHi);
4941 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4942
Evan Cheng31cbddf2007-01-12 02:11:51 +00004943 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004944 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004945 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004946 else {
4947 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00004948 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004949 }
Chris Lattner462505f2006-02-13 09:18:02 +00004950
Evan Chengbf535fc2007-04-27 07:33:31 +00004951 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner462505f2006-02-13 09:18:02 +00004952 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4953 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004954 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4955 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00004956}
Misha Brukman835702a2005-04-21 22:36:52 +00004957
Chris Lattner689bdcc2006-01-28 08:25:58 +00004958/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4959/// INT_TO_FP operation of the specified operand when the target requests that
4960/// we expand it. At this point, we know that the result and operand types are
4961/// legal for the target.
4962SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4963 SDOperand Op0,
4964 MVT::ValueType DestVT) {
4965 if (Op0.getValueType() == MVT::i32) {
4966 // simple 32-bit [signed|unsigned] integer to float/double expansion
4967
Chris Lattner50ee0e42007-01-20 22:35:55 +00004968 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner689bdcc2006-01-28 08:25:58 +00004969 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner50ee0e42007-01-20 22:35:55 +00004970 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4971 unsigned StackAlign =
Chris Lattner945e4372007-02-14 05:52:17 +00004972 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004973 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004974 // get address of 8 byte buffer
4975 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4976 // word offset constant for Hi/Lo address computation
4977 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4978 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00004979 SDOperand Hi = StackSlot;
4980 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4981 if (TLI.isLittleEndian())
4982 std::swap(Hi, Lo);
4983
Chris Lattner689bdcc2006-01-28 08:25:58 +00004984 // if signed map to unsigned space
4985 SDOperand Op0Mapped;
4986 if (isSigned) {
4987 // constant used to invert sign bit (signed to unsigned mapping)
4988 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4989 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4990 } else {
4991 Op0Mapped = Op0;
4992 }
4993 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00004994 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00004995 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004996 // initial hi portion of constructed double
4997 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4998 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00004999 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005000 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00005001 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005002 // FP constant to bias correct the final result
5003 SDOperand Bias = DAG.getConstantFP(isSigned ?
5004 BitsToDouble(0x4330000080000000ULL)
5005 : BitsToDouble(0x4330000000000000ULL),
5006 MVT::f64);
5007 // subtract the bias
5008 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5009 // final result
5010 SDOperand Result;
5011 // handle final rounding
5012 if (DestVT == MVT::f64) {
5013 // do nothing
5014 Result = Sub;
Dale Johannesen7f724e92007-09-16 16:51:49 +00005015 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
5016 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
5017 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5018 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005019 }
5020 return Result;
5021 }
5022 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5023 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5024
5025 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5026 DAG.getConstant(0, Op0.getValueType()),
5027 ISD::SETLT);
5028 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
5029 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5030 SignSet, Four, Zero);
5031
5032 // If the sign bit of the integer is set, the large number will be treated
5033 // as a negative number. To counteract this, the dynamic code adds an
5034 // offset depending on the data type.
5035 uint64_t FF;
5036 switch (Op0.getValueType()) {
5037 default: assert(0 && "Unsupported integer type!");
5038 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5039 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5040 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5041 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5042 }
5043 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00005044 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005045
5046 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5047 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5048 SDOperand FudgeInReg;
5049 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00005050 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005051 else {
Dale Johannesen7d67e542007-09-19 23:55:34 +00005052 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner689bdcc2006-01-28 08:25:58 +00005053 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00005054 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00005055 }
5056
5057 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5058}
5059
5060/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5061/// *INT_TO_FP operation of the specified operand when the target requests that
5062/// we promote it. At this point, we know that the result and operand types are
5063/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5064/// operation that takes a larger input.
5065SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5066 MVT::ValueType DestVT,
5067 bool isSigned) {
5068 // First step, figure out the appropriate *INT_TO_FP operation to use.
5069 MVT::ValueType NewInTy = LegalOp.getValueType();
5070
5071 unsigned OpToUse = 0;
5072
5073 // Scan for the appropriate larger type to use.
5074 while (1) {
5075 NewInTy = (MVT::ValueType)(NewInTy+1);
5076 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5077
5078 // If the target supports SINT_TO_FP of this type, use it.
5079 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5080 default: break;
5081 case TargetLowering::Legal:
5082 if (!TLI.isTypeLegal(NewInTy))
5083 break; // Can't use this datatype.
5084 // FALL THROUGH.
5085 case TargetLowering::Custom:
5086 OpToUse = ISD::SINT_TO_FP;
5087 break;
5088 }
5089 if (OpToUse) break;
5090 if (isSigned) continue;
5091
5092 // If the target supports UINT_TO_FP of this type, use it.
5093 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5094 default: break;
5095 case TargetLowering::Legal:
5096 if (!TLI.isTypeLegal(NewInTy))
5097 break; // Can't use this datatype.
5098 // FALL THROUGH.
5099 case TargetLowering::Custom:
5100 OpToUse = ISD::UINT_TO_FP;
5101 break;
5102 }
5103 if (OpToUse) break;
5104
5105 // Otherwise, try a larger type.
5106 }
5107
5108 // Okay, we found the operation and type to use. Zero extend our input to the
5109 // desired type then run the operation on it.
5110 return DAG.getNode(OpToUse, DestVT,
5111 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5112 NewInTy, LegalOp));
5113}
5114
5115/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5116/// FP_TO_*INT operation of the specified operand when the target requests that
5117/// we promote it. At this point, we know that the result and operand types are
5118/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5119/// operation that returns a larger result.
5120SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5121 MVT::ValueType DestVT,
5122 bool isSigned) {
5123 // First step, figure out the appropriate FP_TO*INT operation to use.
5124 MVT::ValueType NewOutTy = DestVT;
5125
5126 unsigned OpToUse = 0;
5127
5128 // Scan for the appropriate larger type to use.
5129 while (1) {
5130 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5131 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5132
5133 // If the target supports FP_TO_SINT returning this type, use it.
5134 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5135 default: break;
5136 case TargetLowering::Legal:
5137 if (!TLI.isTypeLegal(NewOutTy))
5138 break; // Can't use this datatype.
5139 // FALL THROUGH.
5140 case TargetLowering::Custom:
5141 OpToUse = ISD::FP_TO_SINT;
5142 break;
5143 }
5144 if (OpToUse) break;
5145
5146 // If the target supports FP_TO_UINT of this type, use it.
5147 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5148 default: break;
5149 case TargetLowering::Legal:
5150 if (!TLI.isTypeLegal(NewOutTy))
5151 break; // Can't use this datatype.
5152 // FALL THROUGH.
5153 case TargetLowering::Custom:
5154 OpToUse = ISD::FP_TO_UINT;
5155 break;
5156 }
5157 if (OpToUse) break;
5158
5159 // Otherwise, try a larger type.
5160 }
5161
5162 // Okay, we found the operation and type to use. Truncate the result of the
5163 // extended FP_TO_*INT operation to the desired size.
5164 return DAG.getNode(ISD::TRUNCATE, DestVT,
5165 DAG.getNode(OpToUse, NewOutTy, LegalOp));
5166}
5167
5168/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5169///
5170SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5171 MVT::ValueType VT = Op.getValueType();
5172 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5173 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5174 switch (VT) {
5175 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5176 case MVT::i16:
5177 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5178 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5179 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5180 case MVT::i32:
5181 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5182 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5183 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5184 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5185 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5186 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5187 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5188 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5189 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5190 case MVT::i64:
5191 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5192 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5193 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5194 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5195 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5196 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5197 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5198 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5199 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5200 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5201 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5202 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5203 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5204 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5205 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5206 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5207 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5208 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5209 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5210 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5211 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5212 }
5213}
5214
5215/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5216///
5217SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5218 switch (Opc) {
5219 default: assert(0 && "Cannot expand this yet!");
5220 case ISD::CTPOP: {
5221 static const uint64_t mask[6] = {
5222 0x5555555555555555ULL, 0x3333333333333333ULL,
5223 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5224 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5225 };
5226 MVT::ValueType VT = Op.getValueType();
5227 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005228 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005229 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5230 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5231 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5232 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5233 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5234 DAG.getNode(ISD::AND, VT,
5235 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5236 }
5237 return Op;
5238 }
5239 case ISD::CTLZ: {
5240 // for now, we do this:
5241 // x = x | (x >> 1);
5242 // x = x | (x >> 2);
5243 // ...
5244 // x = x | (x >>16);
5245 // x = x | (x >>32); // for 64-bit input
5246 // return popcount(~x);
5247 //
5248 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5249 MVT::ValueType VT = Op.getValueType();
5250 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005251 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005252 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5253 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5254 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5255 }
5256 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5257 return DAG.getNode(ISD::CTPOP, VT, Op);
5258 }
5259 case ISD::CTTZ: {
5260 // for now, we use: { return popcount(~x & (x - 1)); }
5261 // unless the target has ctlz but not ctpop, in which case we use:
5262 // { return 32 - nlz(~x & (x-1)); }
5263 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5264 MVT::ValueType VT = Op.getValueType();
5265 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5266 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5267 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5268 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5269 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5270 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5271 TLI.isOperationLegal(ISD::CTLZ, VT))
5272 return DAG.getNode(ISD::SUB, VT,
Dan Gohman1796f1f2007-05-18 17:52:13 +00005273 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00005274 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5275 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5276 }
5277 }
5278}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005279
Chris Lattnerdc750592005-01-07 07:47:09 +00005280/// ExpandOp - Expand the specified SDOperand into its two component pieces
5281/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5282/// LegalizeNodes map is filled in for any results that are not expanded, the
5283/// ExpandedNodes map is filled in for any results that are expanded, and the
5284/// Lo/Hi values are returned.
5285void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5286 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00005287 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00005288 SDNode *Node = Op.Val;
5289 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00005290 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohmana8665142007-06-25 16:23:39 +00005291 MVT::isVector(VT)) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00005292 "Cannot expand to FP value or to larger int value!");
5293
Chris Lattner1a570f12005-09-02 20:32:45 +00005294 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005295 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00005296 = ExpandedNodes.find(Op);
5297 if (I != ExpandedNodes.end()) {
5298 Lo = I->second.first;
5299 Hi = I->second.second;
5300 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00005301 }
5302
Chris Lattnerdc750592005-01-07 07:47:09 +00005303 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00005304 case ISD::CopyFromReg:
5305 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen666323e2007-10-10 01:01:31 +00005306 case ISD::FP_ROUND_INREG:
5307 if (VT == MVT::ppcf128 &&
5308 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5309 TargetLowering::Custom) {
Dale Johannesen6472eb62007-10-11 23:32:15 +00005310 SDOperand SrcLo, SrcHi, Src;
5311 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5312 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5313 SDOperand Result = TLI.LowerOperation(
5314 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen666323e2007-10-10 01:01:31 +00005315 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5316 Lo = Result.Val->getOperand(0);
5317 Hi = Result.Val->getOperand(1);
5318 break;
5319 }
5320 // fall through
Chris Lattner44cab002006-01-21 04:27:00 +00005321 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005322#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005323 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005324#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00005325 assert(0 && "Do not know how to expand this operator!");
5326 abort();
Dale Johannesenb066c1f2007-10-31 00:32:36 +00005327 case ISD::EXTRACT_VECTOR_ELT:
5328 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5329 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5330 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5331 return ExpandOp(Lo, Lo, Hi);
Nate Begemancda9aa72005-04-01 22:34:39 +00005332 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00005333 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00005334 Lo = DAG.getNode(ISD::UNDEF, NVT);
5335 Hi = DAG.getNode(ISD::UNDEF, NVT);
5336 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005337 case ISD::Constant: {
5338 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5339 Lo = DAG.getConstant(Cst, NVT);
5340 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5341 break;
5342 }
Evan Cheng47833a12006-12-12 21:32:44 +00005343 case ISD::ConstantFP: {
5344 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen007aa372007-10-11 18:07:22 +00005345 if (CFP->getValueType(0) == MVT::ppcf128) {
5346 APInt api = CFP->getValueAPF().convertToAPInt();
5347 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5348 MVT::f64);
5349 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5350 MVT::f64);
5351 break;
5352 }
Evan Cheng3766fc602006-12-12 22:19:28 +00005353 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00005354 if (getTypeAction(Lo.getValueType()) == Expand)
5355 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005356 break;
5357 }
Chris Lattner32e08b72005-03-28 22:03:13 +00005358 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005359 // Return the operands.
5360 Lo = Node->getOperand(0);
5361 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00005362 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005363
5364 case ISD::SIGN_EXTEND_INREG:
5365 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00005366 // sext_inreg the low part if needed.
5367 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5368
5369 // The high part gets the sign extension from the lo-part. This handles
5370 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005371 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5372 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5373 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005374 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00005375
Nate Begeman2fba8a32006-01-14 03:14:10 +00005376 case ISD::BSWAP: {
5377 ExpandOp(Node->getOperand(0), Lo, Hi);
5378 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5379 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5380 Lo = TempLo;
5381 break;
5382 }
5383
Chris Lattner55e9cde2005-05-11 04:51:16 +00005384 case ISD::CTPOP:
5385 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00005386 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5387 DAG.getNode(ISD::CTPOP, NVT, Lo),
5388 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00005389 Hi = DAG.getConstant(0, NVT);
5390 break;
5391
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005392 case ISD::CTLZ: {
5393 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005394 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005395 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5396 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005397 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5398 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005399 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5400 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5401
5402 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5403 Hi = DAG.getConstant(0, NVT);
5404 break;
5405 }
5406
5407 case ISD::CTTZ: {
5408 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005409 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005410 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5411 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005412 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5413 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005414 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5415 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5416
5417 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5418 Hi = DAG.getConstant(0, NVT);
5419 break;
5420 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00005421
Nate Begemane74795c2006-01-25 18:21:52 +00005422 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005423 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5424 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00005425 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5426 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5427
5428 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005429 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00005430 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5431 if (!TLI.isLittleEndian())
5432 std::swap(Lo, Hi);
5433 break;
5434 }
5435
Chris Lattnerdc750592005-01-07 07:47:09 +00005436 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005437 LoadSDNode *LD = cast<LoadSDNode>(Node);
5438 SDOperand Ch = LD->getChain(); // Legalize the chain.
5439 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5440 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman2af30632007-07-09 22:18:38 +00005441 int SVOffset = LD->getSrcValueOffset();
5442 unsigned Alignment = LD->getAlignment();
5443 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00005444
Evan Chenge71fe34d2006-10-09 20:57:25 +00005445 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman2af30632007-07-09 22:18:38 +00005446 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5447 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00005448 if (VT == MVT::f32 || VT == MVT::f64) {
5449 // f32->i32 or f64->i64 one to one expansion.
5450 // Remember that we legalized the chain.
5451 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00005452 // Recursively expand the new load.
5453 if (getTypeAction(NVT) == Expand)
5454 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005455 break;
5456 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00005457
Evan Chenge71fe34d2006-10-09 20:57:25 +00005458 // Increment the pointer to the other half.
5459 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5460 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5461 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005462 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00005463 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman2af30632007-07-09 22:18:38 +00005464 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5465 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00005466
Evan Chenge71fe34d2006-10-09 20:57:25 +00005467 // Build a factor node to remember that this load is independent of the
5468 // other one.
5469 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5470 Hi.getValue(1));
5471
5472 // Remember that we legalized the chain.
5473 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5474 if (!TLI.isLittleEndian())
5475 std::swap(Lo, Hi);
5476 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00005477 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00005478
Dale Johannesen6802d0c2007-10-19 20:29:00 +00005479 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5480 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Chenge370e0e2006-12-13 03:19:57 +00005481 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5482 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005483 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00005484 // Remember that we legalized the chain.
5485 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5486 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5487 break;
5488 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00005489
5490 if (EVT == NVT)
5491 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005492 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005493 else
5494 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005495 SVOffset, EVT, isVolatile,
5496 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005497
5498 // Remember that we legalized the chain.
5499 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5500
5501 if (ExtType == ISD::SEXTLOAD) {
5502 // The high part is obtained by SRA'ing all but one of the bits of the
5503 // lo part.
5504 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5505 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5506 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5507 } else if (ExtType == ISD::ZEXTLOAD) {
5508 // The high part is just a zero.
5509 Hi = DAG.getConstant(0, NVT);
5510 } else /* if (ExtType == ISD::EXTLOAD) */ {
5511 // The high part is undefined.
5512 Hi = DAG.getNode(ISD::UNDEF, NVT);
5513 }
5514 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005515 break;
5516 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005517 case ISD::AND:
5518 case ISD::OR:
5519 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5520 SDOperand LL, LH, RL, RH;
5521 ExpandOp(Node->getOperand(0), LL, LH);
5522 ExpandOp(Node->getOperand(1), RL, RH);
5523 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5524 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5525 break;
5526 }
5527 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005528 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00005529 ExpandOp(Node->getOperand(1), LL, LH);
5530 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00005531 if (getTypeAction(NVT) == Expand)
5532 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005533 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00005534 if (VT != MVT::f32)
5535 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00005536 break;
5537 }
Nate Begemane5b86d72005-08-10 20:51:12 +00005538 case ISD::SELECT_CC: {
5539 SDOperand TL, TH, FL, FH;
5540 ExpandOp(Node->getOperand(2), TL, TH);
5541 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00005542 if (getTypeAction(NVT) == Expand)
5543 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00005544 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5545 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00005546 if (VT != MVT::f32)
5547 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5548 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00005549 break;
5550 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005551 case ISD::ANY_EXTEND:
5552 // The low part is any extension of the input (which degenerates to a copy).
5553 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5554 // The high part is undefined.
5555 Hi = DAG.getNode(ISD::UNDEF, NVT);
5556 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005557 case ISD::SIGN_EXTEND: {
5558 // The low part is just a sign extension of the input (which degenerates to
5559 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005560 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005561
Chris Lattnerdc750592005-01-07 07:47:09 +00005562 // The high part is obtained by SRA'ing all but one of the bits of the lo
5563 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00005564 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005565 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5566 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00005567 break;
5568 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005569 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00005570 // The low part is just a zero extension of the input (which degenerates to
5571 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005572 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005573
Chris Lattnerdc750592005-01-07 07:47:09 +00005574 // The high part is just a zero.
5575 Hi = DAG.getConstant(0, NVT);
5576 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00005577
Chris Lattner59b27fa372007-02-13 23:55:16 +00005578 case ISD::TRUNCATE: {
5579 // The input value must be larger than this value. Expand *it*.
5580 SDOperand NewLo;
5581 ExpandOp(Node->getOperand(0), NewLo, Hi);
5582
5583 // The low part is now either the right size, or it is closer. If not the
5584 // right size, make an illegal truncate so we recursively expand it.
5585 if (NewLo.getValueType() != Node->getValueType(0))
5586 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5587 ExpandOp(NewLo, Lo, Hi);
5588 break;
5589 }
5590
Chris Lattner36e663d2005-12-23 00:16:34 +00005591 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005592 SDOperand Tmp;
5593 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5594 // If the target wants to, allow it to lower this itself.
5595 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5596 case Expand: assert(0 && "cannot expand FP!");
5597 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5598 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5599 }
5600 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5601 }
5602
Evan Cheng4eee7242006-12-09 02:42:38 +00005603 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00005604 if (VT == MVT::f32 || VT == MVT::f64) {
5605 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00005606 if (getTypeAction(NVT) == Expand)
5607 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00005608 break;
5609 }
5610
5611 // If source operand will be expanded to the same type as VT, i.e.
5612 // i64 <- f64, i32 <- f32, expand the source operand instead.
5613 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5614 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5615 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005616 break;
5617 }
5618
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005619 // Turn this into a load/store pair by default.
5620 if (Tmp.Val == 0)
Evan Cheng3432ab92006-12-11 19:27:14 +00005621 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005622
Chris Lattner36e663d2005-12-23 00:16:34 +00005623 ExpandOp(Tmp, Lo, Hi);
5624 break;
5625 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005626
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005627 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00005628 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5629 TargetLowering::Custom &&
5630 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005631 Lo = TLI.LowerOperation(Op, DAG);
5632 assert(Lo.Val && "Node must be custom expanded!");
5633 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00005634 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005635 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005636 break;
5637
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005638 // These operators cannot be expanded directly, emit them as calls to
5639 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005640 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005641 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00005642 SDOperand Op;
5643 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5644 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005645 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5646 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00005647 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005648
Chris Lattner941d84a2005-07-30 01:40:57 +00005649 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5650
Chris Lattnerfe68d752005-07-29 00:33:32 +00005651 // Now that the custom expander is done, expand the result, which is still
5652 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005653 if (Op.Val) {
5654 ExpandOp(Op, Lo, Hi);
5655 break;
5656 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005657 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005658
Dale Johannesenc0154c02007-10-05 20:04:43 +00005659 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005660 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005661 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00005662 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005663 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00005664 else if (Node->getOperand(0).getValueType() == MVT::f80)
5665 LC = RTLIB::FPTOSINT_F80_I64;
5666 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5667 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005668 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5669 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005670 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005671 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005672
Evan Cheng31cbddf2007-01-12 02:11:51 +00005673 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005674 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005675 SDOperand Op;
5676 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5677 case Expand: assert(0 && "cannot expand FP!");
5678 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5679 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5680 }
5681
5682 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5683
5684 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005685 if (Op.Val) {
5686 ExpandOp(Op, Lo, Hi);
5687 break;
5688 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005689 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005690
Evan Chengfd11ef42007-10-05 01:09:32 +00005691 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005692 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005693 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen789b5a52007-09-28 18:44:17 +00005694 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005695 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00005696 else if (Node->getOperand(0).getValueType() == MVT::f80)
5697 LC = RTLIB::FPTOUINT_F80_I64;
5698 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5699 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005700 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5701 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005702 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005703 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005704
Evan Cheng870e4f82006-01-09 18:31:59 +00005705 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005706 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005707 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005708 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005709 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005710 Op = TLI.LowerOperation(Op, DAG);
5711 if (Op.Val) {
5712 // Now that the custom expander is done, expand the result, which is
5713 // still VT.
5714 ExpandOp(Op, Lo, Hi);
5715 break;
5716 }
5717 }
5718
Chris Lattner72b503b2006-09-13 03:50:39 +00005719 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5720 // this X << 1 as X+X.
5721 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5722 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5723 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5724 SDOperand LoOps[2], HiOps[3];
5725 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5726 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5727 LoOps[1] = LoOps[0];
5728 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5729
5730 HiOps[1] = HiOps[0];
5731 HiOps[2] = Lo.getValue(1);
5732 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5733 break;
5734 }
5735 }
5736
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005737 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005738 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005739 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005740
5741 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005742 TargetLowering::LegalizeAction Action =
5743 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5744 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5745 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005746 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005747 break;
5748 }
5749
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005750 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005751 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5752 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005753 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005754 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005755
Evan Cheng870e4f82006-01-09 18:31:59 +00005756 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005757 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005758 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005759 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005760 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005761 Op = TLI.LowerOperation(Op, DAG);
5762 if (Op.Val) {
5763 // Now that the custom expander is done, expand the result, which is
5764 // still VT.
5765 ExpandOp(Op, Lo, Hi);
5766 break;
5767 }
5768 }
5769
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005770 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005771 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005772 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005773
5774 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005775 TargetLowering::LegalizeAction Action =
5776 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5777 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5778 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005779 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005780 break;
5781 }
5782
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005783 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005784 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5785 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005786 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005787 }
5788
5789 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005790 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005791 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005792 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005793 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005794 Op = TLI.LowerOperation(Op, DAG);
5795 if (Op.Val) {
5796 // Now that the custom expander is done, expand the result, which is
5797 // still VT.
5798 ExpandOp(Op, Lo, Hi);
5799 break;
5800 }
5801 }
5802
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005803 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005804 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005805 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005806
5807 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005808 TargetLowering::LegalizeAction Action =
5809 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5810 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5811 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005812 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005813 break;
5814 }
5815
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005816 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005817 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5818 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005819 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005820 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005821
Misha Brukman835702a2005-04-21 22:36:52 +00005822 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005823 case ISD::SUB: {
5824 // If the target wants to custom expand this, let them.
5825 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5826 TargetLowering::Custom) {
5827 Op = TLI.LowerOperation(Op, DAG);
5828 if (Op.Val) {
5829 ExpandOp(Op, Lo, Hi);
5830 break;
5831 }
5832 }
5833
5834 // Expand the subcomponents.
5835 SDOperand LHSL, LHSH, RHSL, RHSH;
5836 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5837 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005838 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5839 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005840 LoOps[0] = LHSL;
5841 LoOps[1] = RHSL;
5842 HiOps[0] = LHSH;
5843 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005844 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005845 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005846 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005847 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005848 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005849 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005850 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005851 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005852 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005853 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005854 }
Chris Lattner2135bc02007-05-17 18:15:41 +00005855
5856 case ISD::ADDC:
5857 case ISD::SUBC: {
5858 // Expand the subcomponents.
5859 SDOperand LHSL, LHSH, RHSL, RHSH;
5860 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5861 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5862 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5863 SDOperand LoOps[2] = { LHSL, RHSL };
5864 SDOperand HiOps[3] = { LHSH, RHSH };
5865
5866 if (Node->getOpcode() == ISD::ADDC) {
5867 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5868 HiOps[2] = Lo.getValue(1);
5869 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5870 } else {
5871 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5872 HiOps[2] = Lo.getValue(1);
5873 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5874 }
5875 // Remember that we legalized the flag.
5876 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5877 break;
5878 }
5879 case ISD::ADDE:
5880 case ISD::SUBE: {
5881 // Expand the subcomponents.
5882 SDOperand LHSL, LHSH, RHSL, RHSH;
5883 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5884 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5885 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5886 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5887 SDOperand HiOps[3] = { LHSH, RHSH };
5888
5889 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5890 HiOps[2] = Lo.getValue(1);
5891 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5892
5893 // Remember that we legalized the flag.
5894 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5895 break;
5896 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005897 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005898 // If the target wants to custom expand this, let them.
5899 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00005900 SDOperand New = TLI.LowerOperation(Op, DAG);
5901 if (New.Val) {
5902 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005903 break;
5904 }
5905 }
5906
Evan Chenge93762d2006-09-01 18:17:58 +00005907 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5908 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohmana1603612007-10-08 18:33:35 +00005909 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
5910 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
5911 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanadd0c632005-04-11 03:01:51 +00005912 SDOperand LL, LH, RL, RH;
5913 ExpandOp(Node->getOperand(0), LL, LH);
5914 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohmana1603612007-10-08 18:33:35 +00005915 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
5916 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
5917 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
5918 // FIXME: generalize this to handle other bit sizes
5919 if (LHSSB == 32 && RHSSB == 32 &&
5920 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
5921 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
5922 // The inputs are both zero-extended.
5923 if (HasUMUL_LOHI) {
5924 // We can emit a umul_lohi.
5925 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5926 Hi = SDOperand(Lo.Val, 1);
5927 break;
5928 }
5929 if (HasMULHU) {
5930 // We can emit a mulhu+mul.
5931 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5932 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5933 break;
5934 }
Dan Gohmana1603612007-10-08 18:33:35 +00005935 }
5936 if (LHSSB > BitSize && RHSSB > BitSize) {
5937 // The input values are both sign-extended.
5938 if (HasSMUL_LOHI) {
5939 // We can emit a smul_lohi.
5940 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5941 Hi = SDOperand(Lo.Val, 1);
5942 break;
5943 }
5944 if (HasMULHS) {
5945 // We can emit a mulhs+mul.
5946 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5947 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
5948 break;
5949 }
5950 }
5951 if (HasUMUL_LOHI) {
5952 // Lo,Hi = umul LHS, RHS.
5953 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
5954 DAG.getVTList(NVT, NVT), LL, RL);
5955 Lo = UMulLOHI;
5956 Hi = UMulLOHI.getValue(1);
Nate Begeman43144a22005-08-30 02:44:00 +00005957 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5958 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5959 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5960 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00005961 break;
Nate Begeman43144a22005-08-30 02:44:00 +00005962 }
Dale Johannesena4a972e2007-10-24 22:26:08 +00005963 if (HasMULHU) {
5964 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5965 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5966 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5967 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5968 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5969 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
5970 break;
5971 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005972 }
Evan Chenge93762d2006-09-01 18:17:58 +00005973
Dan Gohmana1603612007-10-08 18:33:35 +00005974 // If nothing else, we can make a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005975 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5976 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00005977 break;
5978 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005979 case ISD::SDIV:
5980 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5981 break;
5982 case ISD::UDIV:
5983 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5984 break;
5985 case ISD::SREM:
5986 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5987 break;
5988 case ISD::UREM:
5989 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5990 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00005991
Evan Cheng4eee7242006-12-09 02:42:38 +00005992 case ISD::FADD:
Dale Johannesenc0154c02007-10-05 20:04:43 +00005993 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::ADD_F32 :
5994 VT == MVT::f64 ? RTLIB::ADD_F64 :
5995 VT == MVT::ppcf128 ?
5996 RTLIB::ADD_PPCF128 :
5997 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00005998 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005999 break;
6000 case ISD::FSUB:
Dale Johannesenc0154c02007-10-05 20:04:43 +00006001 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::SUB_F32 :
6002 VT == MVT::f64 ? RTLIB::SUB_F64 :
6003 VT == MVT::ppcf128 ?
6004 RTLIB::SUB_PPCF128 :
6005 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006006 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006007 break;
6008 case ISD::FMUL:
Dale Johannesenc0154c02007-10-05 20:04:43 +00006009 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::MUL_F32 :
6010 VT == MVT::f64 ? RTLIB::MUL_F64 :
6011 VT == MVT::ppcf128 ?
6012 RTLIB::MUL_PPCF128 :
6013 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006014 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006015 break;
6016 case ISD::FDIV:
Dale Johannesenc0154c02007-10-05 20:04:43 +00006017 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::DIV_F32 :
6018 VT == MVT::f64 ? RTLIB::DIV_F64 :
6019 VT == MVT::ppcf128 ?
6020 RTLIB::DIV_PPCF128 :
6021 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006022 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006023 break;
6024 case ISD::FP_EXTEND:
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006025 if (VT == MVT::ppcf128) {
6026 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6027 Node->getOperand(0).getValueType()==MVT::f64);
6028 const uint64_t zero = 0;
6029 if (Node->getOperand(0).getValueType()==MVT::f32)
6030 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6031 else
6032 Hi = Node->getOperand(0);
6033 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6034 break;
6035 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006036 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006037 break;
6038 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00006039 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006040 break;
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006041 case ISD::FPOWI:
Dale Johannesen25a00a62007-09-28 01:08:20 +00006042 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) ? RTLIB::POWI_F32 :
6043 (VT == MVT::f64) ? RTLIB::POWI_F64 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00006044 (VT == MVT::f80) ? RTLIB::POWI_F80 :
6045 (VT == MVT::ppcf128) ?
6046 RTLIB::POWI_PPCF128 :
6047 RTLIB::UNKNOWN_LIBCALL),
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006048 Node, false, Hi);
6049 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006050 case ISD::FSQRT:
6051 case ISD::FSIN:
6052 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006053 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00006054 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006055 case ISD::FSQRT:
Dale Johannesen25a00a62007-09-28 01:08:20 +00006056 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00006057 (VT == MVT::f64) ? RTLIB::SQRT_F64 :
6058 (VT == MVT::f80) ? RTLIB::SQRT_F80 :
6059 (VT == MVT::ppcf128) ? RTLIB::SQRT_PPCF128 :
6060 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006061 break;
6062 case ISD::FSIN:
6063 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
6064 break;
6065 case ISD::FCOS:
6066 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
6067 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006068 default: assert(0 && "Unreachable!");
6069 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006070 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00006071 break;
6072 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006073 case ISD::FABS: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006074 if (VT == MVT::ppcf128) {
6075 SDOperand Tmp;
6076 ExpandOp(Node->getOperand(0), Lo, Tmp);
6077 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6078 // lo = hi==fabs(hi) ? lo : -lo;
6079 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6080 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6081 DAG.getCondCode(ISD::SETEQ));
6082 break;
6083 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006084 SDOperand Mask = (VT == MVT::f64)
6085 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6086 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6087 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6088 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6089 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6090 if (getTypeAction(NVT) == Expand)
6091 ExpandOp(Lo, Lo, Hi);
6092 break;
6093 }
6094 case ISD::FNEG: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006095 if (VT == MVT::ppcf128) {
6096 ExpandOp(Node->getOperand(0), Lo, Hi);
6097 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6098 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6099 break;
6100 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006101 SDOperand Mask = (VT == MVT::f64)
6102 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6103 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6104 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6105 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6106 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6107 if (getTypeAction(NVT) == Expand)
6108 ExpandOp(Lo, Lo, Hi);
6109 break;
6110 }
Evan Cheng003feb02007-01-04 21:56:39 +00006111 case ISD::FCOPYSIGN: {
6112 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6113 if (getTypeAction(NVT) == Expand)
6114 ExpandOp(Lo, Lo, Hi);
6115 break;
6116 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006117 case ISD::SINT_TO_FP:
6118 case ISD::UINT_TO_FP: {
6119 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6120 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006121 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006122 static uint64_t zero = 0;
6123 if (isSigned) {
6124 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6125 Node->getOperand(0)));
6126 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6127 } else {
6128 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6129 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6130 Node->getOperand(0)));
6131 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6132 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006133 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006134 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6135 DAG.getConstant(0, MVT::i32),
6136 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6137 DAG.getConstantFP(
6138 APFloat(APInt(128, 2, TwoE32)),
6139 MVT::ppcf128)),
6140 Hi,
6141 DAG.getCondCode(ISD::SETLT)),
6142 Lo, Hi);
6143 }
6144 break;
6145 }
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006146 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6147 // si64->ppcf128 done by libcall, below
6148 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6149 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6150 Lo, Hi);
6151 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6152 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6153 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6154 DAG.getConstant(0, MVT::i64),
6155 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6156 DAG.getConstantFP(
6157 APFloat(APInt(128, 2, TwoE64)),
6158 MVT::ppcf128)),
6159 Hi,
6160 DAG.getCondCode(ISD::SETLT)),
6161 Lo, Hi);
6162 break;
6163 }
Evan Cheng75439b32007-09-27 07:35:39 +00006164 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006165 if (Node->getOperand(0).getValueType() == MVT::i64) {
6166 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006167 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006168 else if (VT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006169 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00006170 else if (VT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00006171 assert(isSigned);
Dale Johannesenc0154c02007-10-05 20:04:43 +00006172 LC = RTLIB::SINTTOFP_I64_F80;
6173 }
6174 else if (VT == MVT::ppcf128) {
6175 assert(isSigned);
6176 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006177 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006178 } else {
6179 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006180 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006181 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00006182 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006183 }
6184
6185 // Promote the operand if needed.
6186 if (getTypeAction(SrcVT) == Promote) {
6187 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6188 Tmp = isSigned
6189 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6190 DAG.getValueType(SrcVT))
6191 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6192 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6193 }
Evan Chengbf535fc2007-04-27 07:33:31 +00006194
6195 const char *LibCall = TLI.getLibcallName(LC);
6196 if (LibCall)
6197 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6198 else {
6199 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6200 Node->getOperand(0));
6201 if (getTypeAction(Lo.getValueType()) == Expand)
6202 ExpandOp(Lo, Lo, Hi);
6203 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006204 break;
6205 }
Evan Chengf3a80c62006-12-13 02:38:13 +00006206 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006207
Chris Lattnerac12f682005-12-21 18:02:52 +00006208 // Make sure the resultant values have been legalized themselves, unless this
6209 // is a type that requires multi-step expansion.
6210 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6211 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00006212 if (Hi.Val)
6213 // Don't legalize the high part if it is expanded to a single node.
6214 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00006215 }
Evan Cheng870e4f82006-01-09 18:31:59 +00006216
6217 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006218 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00006219 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00006220}
6221
Dan Gohmana8665142007-06-25 16:23:39 +00006222/// SplitVectorOp - Given an operand of vector type, break it down into
6223/// two smaller values, still of vector type.
Chris Lattner32206f52006-03-18 01:44:44 +00006224void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6225 SDOperand &Hi) {
Dan Gohmana8665142007-06-25 16:23:39 +00006226 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00006227 SDNode *Node = Op.Val;
Dan Gohman60028182007-09-24 15:54:53 +00006228 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattner32206f52006-03-18 01:44:44 +00006229 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begemanbd117f02007-11-15 21:15:26 +00006230
Dan Gohman60028182007-09-24 15:54:53 +00006231 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begemanbd117f02007-11-15 21:15:26 +00006232
6233 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6234 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6235
6236 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6237 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6238
Chris Lattner32206f52006-03-18 01:44:44 +00006239 // See if we already split it.
6240 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6241 = SplitNodes.find(Op);
6242 if (I != SplitNodes.end()) {
6243 Lo = I->second.first;
6244 Hi = I->second.second;
6245 return;
6246 }
6247
6248 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006249 default:
6250#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006251 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006252#endif
6253 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohmana8665142007-06-25 16:23:39 +00006254 case ISD::BUILD_PAIR:
6255 Lo = Node->getOperand(0);
6256 Hi = Node->getOperand(1);
6257 break;
Dan Gohmana90183e2007-09-28 23:53:40 +00006258 case ISD::INSERT_VECTOR_ELT: {
6259 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6260 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6261 SDOperand ScalarOp = Node->getOperand(1);
Nate Begemanbd117f02007-11-15 21:15:26 +00006262 if (Index < NewNumElts_Lo)
6263 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohmana90183e2007-09-28 23:53:40 +00006264 DAG.getConstant(Index, TLI.getPointerTy()));
6265 else
Nate Begemanbd117f02007-11-15 21:15:26 +00006266 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6267 DAG.getConstant(Index - NewNumElts_Lo,
6268 TLI.getPointerTy()));
Dan Gohmana90183e2007-09-28 23:53:40 +00006269 break;
6270 }
Dan Gohmana8665142007-06-25 16:23:39 +00006271 case ISD::BUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006272 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begemanbd117f02007-11-15 21:15:26 +00006273 Node->op_begin()+NewNumElts_Lo);
6274 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006275
Nate Begemanbd117f02007-11-15 21:15:26 +00006276 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmana8665142007-06-25 16:23:39 +00006277 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00006278 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006279 break;
6280 }
Dan Gohmana8665142007-06-25 16:23:39 +00006281 case ISD::CONCAT_VECTORS: {
Nate Begemanbd117f02007-11-15 21:15:26 +00006282 // FIXME: Handle non-power-of-two vectors?
Dan Gohmana8665142007-06-25 16:23:39 +00006283 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6284 if (NewNumSubvectors == 1) {
6285 Lo = Node->getOperand(0);
6286 Hi = Node->getOperand(1);
6287 } else {
6288 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6289 Node->op_begin()+NewNumSubvectors);
Nate Begemanbd117f02007-11-15 21:15:26 +00006290 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00006291
Dan Gohmana8665142007-06-25 16:23:39 +00006292 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6293 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00006294 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmana8665142007-06-25 16:23:39 +00006295 }
Dan Gohman26455c42007-06-13 15:12:02 +00006296 break;
6297 }
Dan Gohman8f518b982007-10-17 14:48:28 +00006298 case ISD::SELECT: {
6299 SDOperand Cond = Node->getOperand(0);
6300
6301 SDOperand LL, LH, RL, RH;
6302 SplitVectorOp(Node->getOperand(1), LL, LH);
6303 SplitVectorOp(Node->getOperand(2), RL, RH);
6304
6305 if (MVT::isVector(Cond.getValueType())) {
6306 // Handle a vector merge.
6307 SDOperand CL, CH;
6308 SplitVectorOp(Cond, CL, CH);
Nate Begemanbd117f02007-11-15 21:15:26 +00006309 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6310 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00006311 } else {
6312 // Handle a simple select with vector operands.
Nate Begemanbd117f02007-11-15 21:15:26 +00006313 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6314 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00006315 }
6316 break;
6317 }
Dan Gohmana8665142007-06-25 16:23:39 +00006318 case ISD::ADD:
6319 case ISD::SUB:
6320 case ISD::MUL:
6321 case ISD::FADD:
6322 case ISD::FSUB:
6323 case ISD::FMUL:
6324 case ISD::SDIV:
6325 case ISD::UDIV:
6326 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00006327 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006328 case ISD::AND:
6329 case ISD::OR:
6330 case ISD::XOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00006331 SDOperand LL, LH, RL, RH;
6332 SplitVectorOp(Node->getOperand(0), LL, LH);
6333 SplitVectorOp(Node->getOperand(1), RL, RH);
6334
Nate Begemanbd117f02007-11-15 21:15:26 +00006335 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6336 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00006337 break;
6338 }
Dan Gohman2a7de412007-10-11 23:57:53 +00006339 case ISD::FPOWI: {
6340 SDOperand L, H;
6341 SplitVectorOp(Node->getOperand(0), L, H);
6342
Nate Begemanbd117f02007-11-15 21:15:26 +00006343 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6344 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman2a7de412007-10-11 23:57:53 +00006345 break;
6346 }
6347 case ISD::CTTZ:
6348 case ISD::CTLZ:
6349 case ISD::CTPOP:
6350 case ISD::FNEG:
6351 case ISD::FABS:
6352 case ISD::FSQRT:
6353 case ISD::FSIN:
6354 case ISD::FCOS: {
6355 SDOperand L, H;
6356 SplitVectorOp(Node->getOperand(0), L, H);
6357
Nate Begemanbd117f02007-11-15 21:15:26 +00006358 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6359 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman2a7de412007-10-11 23:57:53 +00006360 break;
6361 }
Dan Gohmana8665142007-06-25 16:23:39 +00006362 case ISD::LOAD: {
6363 LoadSDNode *LD = cast<LoadSDNode>(Node);
6364 SDOperand Ch = LD->getChain();
6365 SDOperand Ptr = LD->getBasePtr();
6366 const Value *SV = LD->getSrcValue();
6367 int SVOffset = LD->getSrcValueOffset();
6368 unsigned Alignment = LD->getAlignment();
6369 bool isVolatile = LD->isVolatile();
6370
Nate Begemanbd117f02007-11-15 21:15:26 +00006371 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6372 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattner32206f52006-03-18 01:44:44 +00006373 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6374 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00006375 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00006376 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begemanbd117f02007-11-15 21:15:26 +00006377 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00006378
6379 // Build a factor node to remember that this load is independent of the
6380 // other one.
6381 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6382 Hi.getValue(1));
6383
6384 // Remember that we legalized the chain.
6385 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00006386 break;
6387 }
Dan Gohmana8665142007-06-25 16:23:39 +00006388 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006389 // We know the result is a vector. The input may be either a vector or a
6390 // scalar value.
Dan Gohman0de76942007-06-29 00:09:08 +00006391 SDOperand InOp = Node->getOperand(0);
6392 if (!MVT::isVector(InOp.getValueType()) ||
6393 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6394 // The input is a scalar or single-element vector.
6395 // Lower to a store/load so that it can be split.
6396 // FIXME: this could be improved probably.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00006397 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006398
Evan Chengdf9ac472006-10-05 23:01:46 +00006399 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman0de76942007-06-29 00:09:08 +00006400 InOp, Ptr, NULL, 0);
6401 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006402 }
Dan Gohman0de76942007-06-29 00:09:08 +00006403 // Split the vector and convert each of the pieces now.
6404 SplitVectorOp(InOp, Lo, Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00006405 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6406 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman0de76942007-06-29 00:09:08 +00006407 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006408 }
Chris Lattner32206f52006-03-18 01:44:44 +00006409 }
6410
6411 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006412 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00006413 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00006414 assert(isNew && "Value already split?!?");
Chris Lattner32206f52006-03-18 01:44:44 +00006415}
6416
6417
Dan Gohmanf4e86da2007-06-27 14:06:22 +00006418/// ScalarizeVectorOp - Given an operand of single-element vector type
6419/// (e.g. v1f32), convert it into the equivalent operation that returns a
6420/// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +00006421SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6422 assert(MVT::isVector(Op.getValueType()) &&
6423 "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00006424 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00006425 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6426 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00006427
Dan Gohmana8665142007-06-25 16:23:39 +00006428 // See if we already scalarized it.
6429 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6430 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00006431
6432 SDOperand Result;
6433 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00006434 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006435#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006436 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006437#endif
Dan Gohmana8665142007-06-25 16:23:39 +00006438 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6439 case ISD::ADD:
6440 case ISD::FADD:
6441 case ISD::SUB:
6442 case ISD::FSUB:
6443 case ISD::MUL:
6444 case ISD::FMUL:
6445 case ISD::SDIV:
6446 case ISD::UDIV:
6447 case ISD::FDIV:
6448 case ISD::SREM:
6449 case ISD::UREM:
6450 case ISD::FREM:
Dan Gohman2a7de412007-10-11 23:57:53 +00006451 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006452 case ISD::AND:
6453 case ISD::OR:
6454 case ISD::XOR:
6455 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00006456 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00006457 ScalarizeVectorOp(Node->getOperand(0)),
6458 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00006459 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006460 case ISD::FNEG:
6461 case ISD::FABS:
6462 case ISD::FSQRT:
6463 case ISD::FSIN:
6464 case ISD::FCOS:
6465 Result = DAG.getNode(Node->getOpcode(),
6466 NewVT,
6467 ScalarizeVectorOp(Node->getOperand(0)));
6468 break;
Dan Gohman4f056f32007-10-12 14:13:46 +00006469 case ISD::FPOWI:
6470 Result = DAG.getNode(Node->getOpcode(),
6471 NewVT,
6472 ScalarizeVectorOp(Node->getOperand(0)),
6473 Node->getOperand(1));
6474 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006475 case ISD::LOAD: {
6476 LoadSDNode *LD = cast<LoadSDNode>(Node);
6477 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6478 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00006479
Dan Gohmana8665142007-06-25 16:23:39 +00006480 const Value *SV = LD->getSrcValue();
6481 int SVOffset = LD->getSrcValueOffset();
6482 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6483 LD->isVolatile(), LD->getAlignment());
6484
Chris Lattner32206f52006-03-18 01:44:44 +00006485 // Remember that we legalized the chain.
6486 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6487 break;
6488 }
Dan Gohmana8665142007-06-25 16:23:39 +00006489 case ISD::BUILD_VECTOR:
6490 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00006491 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006492 case ISD::INSERT_VECTOR_ELT:
6493 // Returning the inserted scalar element.
6494 Result = Node->getOperand(1);
6495 break;
6496 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00006497 assert(Node->getOperand(0).getValueType() == NewVT &&
6498 "Concat of non-legal vectors not yet supported!");
6499 Result = Node->getOperand(0);
6500 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006501 case ISD::VECTOR_SHUFFLE: {
6502 // Figure out if the scalar is the LHS or RHS and return it.
6503 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6504 if (cast<ConstantSDNode>(EltNum)->getValue())
6505 Result = ScalarizeVectorOp(Node->getOperand(1));
6506 else
6507 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00006508 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006509 }
6510 case ISD::EXTRACT_SUBVECTOR:
6511 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00006512 assert(Result.getValueType() == NewVT);
6513 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006514 case ISD::BIT_CONVERT:
6515 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattnerf6f94d32006-03-28 20:24:43 +00006516 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006517 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00006518 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00006519 ScalarizeVectorOp(Op.getOperand(1)),
6520 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00006521 break;
Chris Lattner32206f52006-03-18 01:44:44 +00006522 }
6523
6524 if (TLI.isTypeLegal(NewVT))
6525 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00006526 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6527 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00006528 return Result;
6529}
6530
Chris Lattnerdc750592005-01-07 07:47:09 +00006531
6532// SelectionDAG::Legalize - This is the entry point for the file.
6533//
Chris Lattner4add7e32005-01-23 04:42:50 +00006534void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00006535 if (ViewLegalizeDAGs) viewGraph();
6536
Chris Lattnerdc750592005-01-07 07:47:09 +00006537 /// run - This is the main entry point to this class.
6538 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00006539 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00006540}
6541