blob: 0731299f3595075564b6ced79bfbbcd31ee2ed85 [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"
Duncan Sands1826ded2007-10-28 12:59:45 +000026#include "llvm/Support/Alignment.h"
Chris Lattneref598052006-04-02 03:07:27 +000027#include "llvm/Support/CommandLine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Duncan Sands1826ded2007-10-28 12:59:45 +000029#include "llvm/Support/MathExtras.h"
Chris Lattnere83030b2007-02-03 01:12:36 +000030#include "llvm/ADT/DenseMap.h"
Chris Lattner97af9d52006-08-08 01:09:31 +000031#include "llvm/ADT/SmallVector.h"
Chris Lattnerebeb48d2007-02-04 00:27:56 +000032#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng1d2e9952006-03-24 01:17:21 +000033#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000034using namespace llvm;
35
Chris Lattneref598052006-04-02 03:07:27 +000036#ifndef NDEBUG
37static cl::opt<bool>
38ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
39 cl::desc("Pop up a window to show dags before legalize"));
40#else
41static const bool ViewLegalizeDAGs = 0;
42#endif
43
Chris Lattnerdc750592005-01-07 07:47:09 +000044//===----------------------------------------------------------------------===//
45/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
46/// hacks on it until the target machine can handle it. This involves
47/// eliminating value sizes the machine cannot handle (promoting small sizes to
48/// large sizes or splitting up large values into small values) as well as
49/// eliminating operations the machine cannot handle.
50///
51/// This code also does a small amount of optimization and recognition of idioms
52/// as part of its processing. For example, if a target does not support a
53/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
54/// will attempt merge setcc and brc instructions into brcc's.
55///
56namespace {
Chris Lattner54a34cd2006-06-28 21:58:30 +000057class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattnerdc750592005-01-07 07:47:09 +000058 TargetLowering &TLI;
59 SelectionDAG &DAG;
60
Chris Lattner462505f2006-02-13 09:18:02 +000061 // Libcall insertion helpers.
62
63 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
64 /// legalized. We use this to ensure that calls are properly serialized
65 /// against each other, including inserted libcalls.
66 SDOperand LastCALLSEQ_END;
67
68 /// IsLegalizingCall - This member is used *only* for purposes of providing
69 /// helpful assertions that a libcall isn't created while another call is
70 /// being legalized (which could lead to non-serialized call sequences).
71 bool IsLegalizingCall;
72
Chris Lattnerdc750592005-01-07 07:47:09 +000073 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000074 Legal, // The target natively supports this operation.
75 Promote, // This operation should be executed in a larger type.
Chris Lattneraa2372562006-05-24 17:04:05 +000076 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000077 };
Chris Lattner462505f2006-02-13 09:18:02 +000078
Chris Lattnerdc750592005-01-07 07:47:09 +000079 /// ValueTypeActions - This is a bitvector that contains two bits for each
80 /// value type, where the two bits correspond to the LegalizeAction enum.
81 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000082 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000083
Chris Lattnerdc750592005-01-07 07:47:09 +000084 /// LegalizedNodes - For nodes that are of legal width, and that have more
85 /// than one use, this map indicates what regularized operand to use. This
86 /// allows us to avoid legalizing the same thing more than once.
Chris Lattnered39c862007-02-04 00:50:02 +000087 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000088
Chris Lattner1f2c9d82005-01-15 05:21:40 +000089 /// PromotedNodes - For nodes that are below legal width, and that have more
90 /// than one use, this map indicates what promoted value to use. This allows
91 /// us to avoid promoting the same thing more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +000092 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner1f2c9d82005-01-15 05:21:40 +000093
Chris Lattner32206f52006-03-18 01:44:44 +000094 /// ExpandedNodes - For nodes that need to be expanded this map indicates
95 /// which which operands are the expanded version of the input. This allows
96 /// us to avoid expanding the same node more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +000097 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000098
Chris Lattner32206f52006-03-18 01:44:44 +000099 /// SplitNodes - For vector nodes that need to be split, this map indicates
100 /// which which operands are the split version of the input. This allows us
101 /// to avoid splitting the same node more than once.
102 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
103
Dan Gohmana8665142007-06-25 16:23:39 +0000104 /// ScalarizedNodes - For nodes that need to be converted from vector types to
105 /// scalar types, this contains the mapping of ones we have already
Chris Lattner32206f52006-03-18 01:44:44 +0000106 /// processed to the result.
Dan Gohmana8665142007-06-25 16:23:39 +0000107 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattner32206f52006-03-18 01:44:44 +0000108
Chris Lattnerea4ca942005-01-07 22:28:47 +0000109 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +0000110 LegalizedNodes.insert(std::make_pair(From, To));
111 // If someone requests legalization of the new node, return itself.
112 if (From != To)
113 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000114 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000115 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner4b0ddb22007-02-04 01:17:38 +0000116 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000117 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +0000118 // If someone requests legalization of the new node, return itself.
119 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000120 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000121
Chris Lattnerdc750592005-01-07 07:47:09 +0000122public:
123
Chris Lattner4add7e32005-01-23 04:42:50 +0000124 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000125
Chris Lattnerdc750592005-01-07 07:47:09 +0000126 /// getTypeAction - Return how we should legalize values of this type, either
127 /// it is already legal or we need to expand it into multiple registers of
128 /// smaller integer type, or we need to promote it to a larger type.
129 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +0000130 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +0000131 }
132
133 /// isTypeLegal - Return true if this type is legal on this target.
134 ///
135 bool isTypeLegal(MVT::ValueType VT) const {
136 return getTypeAction(VT) == Legal;
137 }
138
Chris Lattnerdc750592005-01-07 07:47:09 +0000139 void LegalizeDAG();
140
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000141private:
Dan Gohmana8665142007-06-25 16:23:39 +0000142 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000143 /// appropriate for its type.
144 void HandleOp(SDOperand Op);
145
146 /// LegalizeOp - We know that the specified value has a legal type.
147 /// Recursively ensure that the operands have legal types, then return the
148 /// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000149 SDOperand LegalizeOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000150
Dan Gohman2a7de412007-10-11 23:57:53 +0000151 /// UnrollVectorOp - We know that the given vector has a legal type, however
152 /// the operation it performs is not legal and is an operation that we have
153 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
154 /// operating on each element individually.
155 SDOperand UnrollVectorOp(SDOperand O);
156
Chris Lattner32206f52006-03-18 01:44:44 +0000157 /// PromoteOp - Given an operation that produces a value in an invalid type,
158 /// promote it to compute the value into a larger type. The produced value
159 /// will have the correct bits for the low portion of the register, but no
160 /// guarantee is made about the top bits: it may be zero, sign-extended, or
161 /// garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000162 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000163
Chris Lattner32206f52006-03-18 01:44:44 +0000164 /// ExpandOp - Expand the specified SDOperand into its two component pieces
165 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
166 /// the LegalizeNodes map is filled in for any results that are not expanded,
167 /// the ExpandedNodes map is filled in for any results that are expanded, and
168 /// the Lo/Hi values are returned. This applies to integer types and Vector
169 /// types.
170 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
171
Dan Gohmana8665142007-06-25 16:23:39 +0000172 /// SplitVectorOp - Given an operand of vector type, break it down into
173 /// two smaller values.
Chris Lattner32206f52006-03-18 01:44:44 +0000174 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
175
Dan Gohmanf4e86da2007-06-27 14:06:22 +0000176 /// ScalarizeVectorOp - Given an operand of single-element vector type
177 /// (e.g. v1f32), convert it into the equivalent operation that returns a
178 /// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +0000179 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000180
Chris Lattner6be79822006-04-04 17:23:26 +0000181 /// isShuffleLegal - Return true if a vector shuffle is legal with the
182 /// specified mask and type. Targets can specify exactly which masks they
183 /// support and the code generator is tasked with not creating illegal masks.
184 ///
185 /// Note that this will also return true for shuffles that are promoted to a
186 /// different type.
187 ///
188 /// If this is a legal shuffle, this method returns the (possibly promoted)
189 /// build_vector Mask. If it's not a legal shuffle, it returns null.
190 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
191
Chris Lattner4488f0c2006-07-26 23:55:56 +0000192 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000193 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000194
Nate Begeman7e7f4392006-02-01 07:19:44 +0000195 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
196
Reid Spencere63b6512006-12-31 05:55:36 +0000197 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattneraac464e2005-01-21 06:05:23 +0000198 SDOperand &Hi);
199 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
200 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000201
Chris Lattner36e663d2005-12-23 00:16:34 +0000202 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000203 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner6be79822006-04-04 17:23:26 +0000204 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000205 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
206 SDOperand LegalOp,
207 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000208 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
209 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000210 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
211 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000212
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000213 SDOperand ExpandBSWAP(SDOperand Op);
214 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000215 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
216 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000217 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
218 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000219
Dan Gohmana8665142007-06-25 16:23:39 +0000220 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner42a5fca2006-04-02 05:06:04 +0000221 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner6f423252006-03-31 17:55:51 +0000222
Chris Lattnerdc750592005-01-07 07:47:09 +0000223 SDOperand getIntPtrConstant(uint64_t Val) {
224 return DAG.getConstant(Val, TLI.getPointerTy());
225 }
226};
227}
228
Chris Lattner6be79822006-04-04 17:23:26 +0000229/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
230/// specified mask and type. Targets can specify exactly which masks they
231/// support and the code generator is tasked with not creating illegal masks.
232///
233/// Note that this will also return true for shuffles that are promoted to a
234/// different type.
235SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
236 SDOperand Mask) const {
237 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
238 default: return 0;
239 case TargetLowering::Legal:
240 case TargetLowering::Custom:
241 break;
242 case TargetLowering::Promote: {
243 // If this is promoted to a different type, convert the shuffle mask and
244 // ask if it is legal in the promoted type!
245 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
246
247 // If we changed # elements, change the shuffle mask.
248 unsigned NumEltsGrowth =
249 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
250 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
251 if (NumEltsGrowth > 1) {
252 // Renumber the elements.
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000253 SmallVector<SDOperand, 8> Ops;
Chris Lattner6be79822006-04-04 17:23:26 +0000254 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
255 SDOperand InOp = Mask.getOperand(i);
256 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
257 if (InOp.getOpcode() == ISD::UNDEF)
258 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
259 else {
260 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
261 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
262 }
263 }
264 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000265 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +0000266 }
267 VT = NVT;
268 break;
269 }
270 }
271 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
272}
273
Chris Lattner4add7e32005-01-23 04:42:50 +0000274SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
275 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
276 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000277 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000278 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000279}
280
Chris Lattnere31adc82007-06-18 21:28:10 +0000281/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
282/// contains all of a nodes operands before it contains the node.
283static void ComputeTopDownOrdering(SelectionDAG &DAG,
284 SmallVector<SDNode*, 64> &Order) {
285
286 DenseMap<SDNode*, unsigned> Visited;
287 std::vector<SDNode*> Worklist;
288 Worklist.reserve(128);
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000289
Chris Lattnere31adc82007-06-18 21:28:10 +0000290 // Compute ordering from all of the leaves in the graphs, those (like the
291 // entry node) that have no operands.
292 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
293 E = DAG.allnodes_end(); I != E; ++I) {
294 if (I->getNumOperands() == 0) {
295 Visited[I] = 0 - 1U;
296 Worklist.push_back(I);
297 }
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000298 }
299
Chris Lattnere31adc82007-06-18 21:28:10 +0000300 while (!Worklist.empty()) {
301 SDNode *N = Worklist.back();
302 Worklist.pop_back();
303
304 if (++Visited[N] != N->getNumOperands())
305 continue; // Haven't visited all operands yet
306
307 Order.push_back(N);
308
309 // Now that we have N in, add anything that uses it if all of their operands
310 // are now done.
311 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
312 UI != E; ++UI)
313 Worklist.push_back(*UI);
314 }
315
316 assert(Order.size() == Visited.size() &&
317 Order.size() ==
318 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
319 "Error: DAG is cyclic!");
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000320}
321
Chris Lattner44fe26f2005-07-29 00:11:56 +0000322
Chris Lattnerdc750592005-01-07 07:47:09 +0000323void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000324 LastCALLSEQ_END = DAG.getEntryNode();
325 IsLegalizingCall = false;
326
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000327 // The legalize process is inherently a bottom-up recursive process (users
328 // legalize their uses before themselves). Given infinite stack space, we
329 // could just start legalizing on the root and traverse the whole graph. In
330 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000331 // blocks. To avoid this problem, compute an ordering of the nodes where each
332 // node is only legalized after all of its operands are legalized.
Chris Lattner94c44c92007-02-04 01:20:02 +0000333 SmallVector<SDNode*, 64> Order;
Chris Lattnere31adc82007-06-18 21:28:10 +0000334 ComputeTopDownOrdering(DAG, Order);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000335
Chris Lattner32206f52006-03-18 01:44:44 +0000336 for (unsigned i = 0, e = Order.size(); i != e; ++i)
337 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000338
339 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000340 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000341 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
342 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000343
344 ExpandedNodes.clear();
345 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000346 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000347 SplitNodes.clear();
Dan Gohmana8665142007-06-25 16:23:39 +0000348 ScalarizedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000349
350 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000351 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000352}
353
Chris Lattner462505f2006-02-13 09:18:02 +0000354
355/// FindCallEndFromCallStart - Given a chained node that is part of a call
356/// sequence, find the CALLSEQ_END node that terminates the call sequence.
357static SDNode *FindCallEndFromCallStart(SDNode *Node) {
358 if (Node->getOpcode() == ISD::CALLSEQ_END)
359 return Node;
360 if (Node->use_empty())
361 return 0; // No CallSeqEnd
362
363 // The chain is usually at the end.
364 SDOperand TheChain(Node, Node->getNumValues()-1);
365 if (TheChain.getValueType() != MVT::Other) {
366 // Sometimes it's at the beginning.
367 TheChain = SDOperand(Node, 0);
368 if (TheChain.getValueType() != MVT::Other) {
369 // Otherwise, hunt for it.
370 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
371 if (Node->getValueType(i) == MVT::Other) {
372 TheChain = SDOperand(Node, i);
373 break;
374 }
375
376 // Otherwise, we walked into a node without a chain.
377 if (TheChain.getValueType() != MVT::Other)
378 return 0;
379 }
380 }
381
382 for (SDNode::use_iterator UI = Node->use_begin(),
383 E = Node->use_end(); UI != E; ++UI) {
384
385 // Make sure to only follow users of our token chain.
386 SDNode *User = *UI;
387 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
388 if (User->getOperand(i) == TheChain)
389 if (SDNode *Result = FindCallEndFromCallStart(User))
390 return Result;
391 }
392 return 0;
393}
394
395/// FindCallStartFromCallEnd - Given a chained node that is part of a call
396/// sequence, find the CALLSEQ_START node that initiates the call sequence.
397static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
398 assert(Node && "Didn't find callseq_start for a call??");
399 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
400
401 assert(Node->getOperand(0).getValueType() == MVT::Other &&
402 "Node doesn't have a token chain argument!");
403 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
404}
405
406/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
407/// see if any uses can reach Dest. If no dest operands can get to dest,
408/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000409///
410/// Keep track of the nodes we fine that actually do lead to Dest in
411/// NodesLeadingTo. This avoids retraversing them exponential number of times.
412///
413bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000414 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000415 if (N == Dest) return true; // N certainly leads to Dest :)
416
Chris Lattner4488f0c2006-07-26 23:55:56 +0000417 // If we've already processed this node and it does lead to Dest, there is no
418 // need to reprocess it.
419 if (NodesLeadingTo.count(N)) return true;
420
Chris Lattner462505f2006-02-13 09:18:02 +0000421 // If the first result of this node has been already legalized, then it cannot
422 // reach N.
423 switch (getTypeAction(N->getValueType(0))) {
424 case Legal:
425 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
426 break;
427 case Promote:
428 if (PromotedNodes.count(SDOperand(N, 0))) return false;
429 break;
430 case Expand:
431 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
432 break;
433 }
434
435 // Okay, this node has not already been legalized. Check and legalize all
436 // operands. If none lead to Dest, then we can legalize this node.
437 bool OperandsLeadToDest = false;
438 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
439 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000440 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000441
Chris Lattner4488f0c2006-07-26 23:55:56 +0000442 if (OperandsLeadToDest) {
443 NodesLeadingTo.insert(N);
444 return true;
445 }
Chris Lattner462505f2006-02-13 09:18:02 +0000446
447 // Okay, this node looks safe, legalize it and return false.
Chris Lattner916ae072006-04-17 22:10:08 +0000448 HandleOp(SDOperand(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000449 return false;
450}
451
Dan Gohmana8665142007-06-25 16:23:39 +0000452/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000453/// appropriate for its type.
454void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohmana8665142007-06-25 16:23:39 +0000455 MVT::ValueType VT = Op.getValueType();
456 switch (getTypeAction(VT)) {
Chris Lattner32206f52006-03-18 01:44:44 +0000457 default: assert(0 && "Bad type action!");
Dan Gohmana8665142007-06-25 16:23:39 +0000458 case Legal: (void)LegalizeOp(Op); break;
459 case Promote: (void)PromoteOp(Op); break;
Chris Lattner32206f52006-03-18 01:44:44 +0000460 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +0000461 if (!MVT::isVector(VT)) {
462 // If this is an illegal scalar, expand it into its two component
463 // pieces.
Chris Lattner32206f52006-03-18 01:44:44 +0000464 SDOperand X, Y;
Chris Lattner2ed652f2007-08-25 01:00:22 +0000465 if (Op.getOpcode() == ISD::TargetConstant)
466 break; // Allow illegal target nodes.
Chris Lattner32206f52006-03-18 01:44:44 +0000467 ExpandOp(Op, X, Y);
Dan Gohmana8665142007-06-25 16:23:39 +0000468 } else if (MVT::getVectorNumElements(VT) == 1) {
469 // If this is an illegal single element vector, convert it to a
470 // scalar operation.
471 (void)ScalarizeVectorOp(Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000472 } else {
Dan Gohmana8665142007-06-25 16:23:39 +0000473 // Otherwise, this is an illegal multiple element vector.
474 // Split it in half and legalize both parts.
475 SDOperand X, Y;
476 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000477 }
478 break;
479 }
480}
Chris Lattner462505f2006-02-13 09:18:02 +0000481
Evan Cheng22cf8992006-12-13 20:57:08 +0000482/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
483/// a load from the constant pool.
484static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000485 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000486 bool Extend = false;
487
488 // If a FP immediate is precise when represented as a float and if the
489 // target can do an extending load from float to double, we put it into
490 // the constant pool as a float, even if it's is statically typed as a
491 // double.
492 MVT::ValueType VT = CFP->getValueType(0);
493 bool isDouble = VT == MVT::f64;
Dale Johannesen7f724e92007-09-16 16:51:49 +0000494 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen98d3a082007-09-14 22:26:36 +0000495 CFP->getValueAPF());
Evan Cheng22cf8992006-12-13 20:57:08 +0000496 if (!UseCP) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000497 if (VT!=MVT::f64 && VT!=MVT::f32)
498 assert(0 && "Invalid type expansion");
Dale Johannesen028084e2007-09-12 03:30:33 +0000499 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
500 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000501 }
502
Dale Johannesend246b2c2007-08-30 00:23:21 +0000503 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng47833a12006-12-12 21:32:44 +0000504 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen98d3a082007-09-14 22:26:36 +0000505 // Do not try to be clever about long doubles (so far)
Evan Cheng47833a12006-12-12 21:32:44 +0000506 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
507 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
508 VT = MVT::f32;
509 Extend = true;
510 }
511
512 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
513 if (Extend) {
514 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
515 CPIdx, NULL, 0, MVT::f32);
516 } else {
517 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
518 }
519}
520
Chris Lattner462505f2006-02-13 09:18:02 +0000521
Evan Cheng003feb02007-01-04 21:56:39 +0000522/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
523/// operations.
524static
525SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
526 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng3b841dd2007-01-05 21:31:51 +0000527 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000528 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000529 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
530 "fcopysign expansion only supported for f32 and f64");
Evan Cheng003feb02007-01-04 21:56:39 +0000531 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000532
Evan Cheng003feb02007-01-04 21:56:39 +0000533 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000534 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000535 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
536 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000537 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000538 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000539 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000540 // Shift right or sign-extend it if the two operands have different types.
541 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
542 if (SizeDiff > 0) {
543 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
544 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
545 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
546 } else if (SizeDiff < 0)
547 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000548
549 // Clear the sign bit of first operand.
550 SDOperand Mask2 = (VT == MVT::f64)
551 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
552 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
553 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng003feb02007-01-04 21:56:39 +0000554 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000555 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
556
557 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000558 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
559 return Result;
560}
561
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000562/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
563static
564SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
565 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000566 SDOperand Chain = ST->getChain();
567 SDOperand Ptr = ST->getBasePtr();
568 SDOperand Val = ST->getValue();
569 MVT::ValueType VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000570 int Alignment = ST->getAlignment();
571 int SVOffset = ST->getSrcValueOffset();
572 if (MVT::isFloatingPoint(ST->getStoredVT())) {
573 // Expand to a bitconvert of the value to the integer type of the
574 // same size, then a (misaligned) int store.
575 MVT::ValueType intVT;
576 if (VT==MVT::f64)
577 intVT = MVT::i64;
578 else if (VT==MVT::f32)
579 intVT = MVT::i32;
580 else
581 assert(0 && "Unaligned load of unsupported floating point type");
582
583 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
584 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
585 SVOffset, ST->isVolatile(), Alignment);
586 }
587 assert(MVT::isInteger(ST->getStoredVT()) &&
588 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000589 // Get the half-size VT
590 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
591 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000592 int IncrementSize = NumBits / 8;
593
594 // Divide the stored value in two parts.
595 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
596 SDOperand Lo = Val;
597 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
598
599 // Store the two parts
600 SDOperand Store1, Store2;
601 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
602 ST->getSrcValue(), SVOffset, NewStoredVT,
603 ST->isVolatile(), Alignment);
604 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
605 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sands1826ded2007-10-28 12:59:45 +0000606 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000607 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
608 ST->getSrcValue(), SVOffset + IncrementSize,
609 NewStoredVT, ST->isVolatile(), Alignment);
610
611 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
612}
613
614/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
615static
616SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
617 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000618 int SVOffset = LD->getSrcValueOffset();
619 SDOperand Chain = LD->getChain();
620 SDOperand Ptr = LD->getBasePtr();
621 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000622 MVT::ValueType LoadedVT = LD->getLoadedVT();
623 if (MVT::isFloatingPoint(VT)) {
624 // Expand to a (misaligned) integer load of the same size,
625 // then bitconvert to floating point.
626 MVT::ValueType intVT;
627 if (LoadedVT==MVT::f64)
628 intVT = MVT::i64;
629 else if (LoadedVT==MVT::f32)
630 intVT = MVT::i32;
631 else
632 assert(0 && "Unaligned load of unsupported floating point type");
633
634 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
635 SVOffset, LD->isVolatile(),
636 LD->getAlignment());
637 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
638 if (LoadedVT != VT)
639 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
640
641 SDOperand Ops[] = { Result, Chain };
642 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
643 Ops, 2);
644 }
645 assert(MVT::isInteger(LoadedVT) && "Unaligned load of unsupported type.");
646 MVT::ValueType NewLoadedVT = LoadedVT - 1;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000647 int NumBits = MVT::getSizeInBits(NewLoadedVT);
648 int Alignment = LD->getAlignment();
649 int IncrementSize = NumBits / 8;
650 ISD::LoadExtType HiExtType = LD->getExtensionType();
651
652 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
653 if (HiExtType == ISD::NON_EXTLOAD)
654 HiExtType = ISD::ZEXTLOAD;
655
656 // Load the value in two parts
657 SDOperand Lo, Hi;
658 if (TLI.isLittleEndian()) {
659 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
660 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
661 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
662 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
663 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
664 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000665 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000666 } else {
667 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
668 NewLoadedVT,LD->isVolatile(), Alignment);
669 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
670 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
671 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
672 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000673 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000674 }
675
676 // aggregate the two parts
677 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
678 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
679 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
680
681 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
682 Hi.getValue(1));
683
684 SDOperand Ops[] = { Result, TF };
685 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
686}
Evan Cheng003feb02007-01-04 21:56:39 +0000687
Dan Gohman2a7de412007-10-11 23:57:53 +0000688/// UnrollVectorOp - We know that the given vector has a legal type, however
689/// the operation it performs is not legal and is an operation that we have
690/// no way of lowering. "Unroll" the vector, splitting out the scalars and
691/// operating on each element individually.
692SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
693 MVT::ValueType VT = Op.getValueType();
694 assert(isTypeLegal(VT) &&
695 "Caller should expand or promote operands that are not legal!");
696 assert(Op.Val->getNumValues() == 1 &&
697 "Can't unroll a vector with multiple results!");
698 unsigned NE = MVT::getVectorNumElements(VT);
699 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
700
701 SmallVector<SDOperand, 8> Scalars;
702 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
703 for (unsigned i = 0; i != NE; ++i) {
704 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
705 SDOperand Operand = Op.getOperand(j);
706 MVT::ValueType OperandVT = Operand.getValueType();
707 if (MVT::isVector(OperandVT)) {
708 // A vector operand; extract a single element.
709 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
710 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
711 OperandEltVT,
712 Operand,
713 DAG.getConstant(i, MVT::i32));
714 } else {
715 // A scalar operand; just use it as is.
716 Operands[j] = Operand;
717 }
718 }
719 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
720 &Operands[0], Operands.size()));
721 }
722
723 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
724}
725
Dan Gohmanff727882007-07-13 20:14:11 +0000726/// LegalizeOp - We know that the specified value has a legal type, and
727/// that its operands are legal. Now ensure that the operation itself
728/// is legal, recursively ensuring that the operands' operations remain
729/// legal.
Chris Lattnerdc750592005-01-07 07:47:09 +0000730SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner2ed652f2007-08-25 01:00:22 +0000731 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
732 return Op;
733
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000734 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000735 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000736 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000737
Chris Lattnerdc750592005-01-07 07:47:09 +0000738 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000739 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000740 if (Node->getNumValues() > 1) {
741 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000742 if (getTypeAction(Node->getValueType(i)) != Legal) {
743 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000744 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000745 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000746 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000747 }
748 }
749
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000750 // Note that LegalizeOp may be reentered even from single-use nodes, which
751 // means that we always must cache transformed nodes.
Chris Lattnered39c862007-02-04 00:50:02 +0000752 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000753 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000754
Nate Begemane5b86d72005-08-10 20:51:12 +0000755 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000756 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000757 bool isCustom = false;
758
Chris Lattnerdc750592005-01-07 07:47:09 +0000759 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000760 case ISD::FrameIndex:
761 case ISD::EntryToken:
762 case ISD::Register:
763 case ISD::BasicBlock:
764 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000765 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000766 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000767 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000768 case ISD::TargetConstantPool:
769 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000770 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000771 case ISD::TargetExternalSymbol:
772 case ISD::VALUETYPE:
773 case ISD::SRCVALUE:
774 case ISD::STRING:
775 case ISD::CONDCODE:
776 // Primitives must all be legal.
Duncan Sands052c8432007-10-16 09:07:20 +0000777 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattnereb637512006-01-28 08:31:04 +0000778 "This must be legal!");
779 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000780 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000781 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
782 // If this is a target node, legalize it by legalizing the operands then
783 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000784 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000785 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000786 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000787
Chris Lattner97af9d52006-08-08 01:09:31 +0000788 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000789
790 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
791 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
792 return Result.getValue(Op.ResNo);
793 }
794 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000795#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000796 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000797#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000798 assert(0 && "Do not know how to legalize this operator!");
799 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000800 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000801 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000802 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000803 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000804 case ISD::ConstantPool:
805 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000806 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
807 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000808 case TargetLowering::Custom:
809 Tmp1 = TLI.LowerOperation(Op, DAG);
810 if (Tmp1.Val) Result = Tmp1;
811 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000812 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000813 break;
814 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000815 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000816 case ISD::FRAMEADDR:
817 case ISD::RETURNADDR:
818 // The only option for these nodes is to custom lower them. If the target
819 // does not custom lower them, then return zero.
820 Tmp1 = TLI.LowerOperation(Op, DAG);
821 if (Tmp1.Val)
822 Result = Tmp1;
823 else
824 Result = DAG.getConstant(0, TLI.getPointerTy());
825 break;
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000826 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000827 MVT::ValueType VT = Node->getValueType(0);
828 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
829 default: assert(0 && "This action is not supported yet!");
830 case TargetLowering::Custom:
831 Result = TLI.LowerOperation(Op, DAG);
832 if (Result.Val) break;
833 // Fall Thru
834 case TargetLowering::Legal:
835 Result = DAG.getConstant(0, VT);
836 break;
837 }
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000838 }
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000839 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000840 case ISD::EXCEPTIONADDR: {
841 Tmp1 = LegalizeOp(Node->getOperand(0));
842 MVT::ValueType VT = Node->getValueType(0);
843 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
844 default: assert(0 && "This action is not supported yet!");
845 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000846 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey7f5872c2007-02-22 15:37:19 +0000847 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
848 }
849 break;
850 case TargetLowering::Custom:
851 Result = TLI.LowerOperation(Op, DAG);
852 if (Result.Val) break;
853 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000854 case TargetLowering::Legal: {
855 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
856 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
857 Ops, 2).getValue(Op.ResNo);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000858 break;
859 }
860 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000861 }
Jim Laskey7f5872c2007-02-22 15:37:19 +0000862 break;
Jim Laskey644af6b2007-02-28 20:43:58 +0000863 case ISD::EHSELECTION: {
864 Tmp1 = LegalizeOp(Node->getOperand(0));
865 Tmp2 = LegalizeOp(Node->getOperand(1));
866 MVT::ValueType VT = Node->getValueType(0);
867 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
868 default: assert(0 && "This action is not supported yet!");
869 case TargetLowering::Expand: {
870 unsigned Reg = TLI.getExceptionSelectorRegister();
871 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
872 }
873 break;
874 case TargetLowering::Custom:
875 Result = TLI.LowerOperation(Op, DAG);
876 if (Result.Val) break;
877 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000878 case TargetLowering::Legal: {
879 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
880 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
881 Ops, 2).getValue(Op.ResNo);
Jim Laskey644af6b2007-02-28 20:43:58 +0000882 break;
883 }
884 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000885 }
Jim Laskey644af6b2007-02-28 20:43:58 +0000886 break;
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000887 case ISD::EH_RETURN: {
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000888 MVT::ValueType VT = Node->getValueType(0);
889 // The only "good" option for this node is to custom lower it.
890 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
891 default: assert(0 && "This action is not supported at all!");
892 case TargetLowering::Custom:
893 Result = TLI.LowerOperation(Op, DAG);
894 if (Result.Val) break;
895 // Fall Thru
896 case TargetLowering::Legal:
897 // Target does not know, how to lower this, lower to noop
898 Result = LegalizeOp(Node->getOperand(0));
899 break;
900 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000901 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000902 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000903 case ISD::AssertSext:
904 case ISD::AssertZext:
905 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000906 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000907 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000908 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000909 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000910 Result = Node->getOperand(Op.ResNo);
911 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000912 case ISD::CopyFromReg:
913 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000914 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000915 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000916 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000917 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000918 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000919 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000920 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000921 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
922 } else {
923 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
924 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000925 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
926 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000927 // Since CopyFromReg produces two values, make sure to remember that we
928 // legalized both of them.
929 AddLegalizedOperand(Op.getValue(0), Result);
930 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
931 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000932 case ISD::UNDEF: {
933 MVT::ValueType VT = Op.getValueType();
934 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000935 default: assert(0 && "This action is not supported yet!");
936 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000937 if (MVT::isInteger(VT))
938 Result = DAG.getConstant(0, VT);
939 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf04d37d2007-09-26 17:26:49 +0000940 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
941 VT);
Nate Begemancda9aa72005-04-01 22:34:39 +0000942 else
943 assert(0 && "Unknown value type!");
944 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000945 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000946 break;
947 }
948 break;
949 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000950
Chris Lattnere55d1712006-03-28 00:40:33 +0000951 case ISD::INTRINSIC_W_CHAIN:
952 case ISD::INTRINSIC_WO_CHAIN:
953 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +0000954 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +0000955 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
956 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000957 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +0000958
959 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000960 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000961 TargetLowering::Custom) {
962 Tmp3 = TLI.LowerOperation(Result, DAG);
963 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000964 }
965
966 if (Result.Val->getNumValues() == 1) break;
967
968 // Must have return value and chain result.
969 assert(Result.Val->getNumValues() == 2 &&
970 "Cannot return more than two values!");
971
972 // Since loads produce two values, make sure to remember that we
973 // legalized both of them.
974 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
975 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
976 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000977 }
Chris Lattner435b4022005-11-29 06:21:05 +0000978
979 case ISD::LOCATION:
980 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
981 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
982
983 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
984 case TargetLowering::Promote:
985 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000986 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +0000987 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000988 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +0000989 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000990
Jim Laskeyc56315c2007-01-26 21:22:28 +0000991 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000992 const std::string &FName =
993 cast<StringSDNode>(Node->getOperand(3))->getValue();
994 const std::string &DirName =
995 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000996 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000997
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000998 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +0000999 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +00001000 SDOperand LineOp = Node->getOperand(1);
1001 SDOperand ColOp = Node->getOperand(2);
1002
1003 if (useDEBUG_LOC) {
1004 Ops.push_back(LineOp); // line #
1005 Ops.push_back(ColOp); // col #
1006 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001007 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001008 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +00001009 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1010 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +00001011 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001012 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskeyf9e54452007-01-26 14:34:52 +00001013 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001014 }
Jim Laskey9e296be2005-12-21 20:51:37 +00001015 } else {
1016 Result = Tmp1; // chain
1017 }
Chris Lattner435b4022005-11-29 06:21:05 +00001018 break;
Chris Lattnerc06da622005-12-18 23:54:29 +00001019 }
Chris Lattner435b4022005-11-29 06:21:05 +00001020 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +00001021 if (Tmp1 != Node->getOperand(0) ||
1022 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001023 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +00001024 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +00001025 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1026 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1027 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1028 } else {
1029 // Otherwise promote them.
1030 Ops.push_back(PromoteOp(Node->getOperand(1)));
1031 Ops.push_back(PromoteOp(Node->getOperand(2)));
1032 }
Chris Lattner435b4022005-11-29 06:21:05 +00001033 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1034 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +00001035 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +00001036 }
1037 break;
1038 }
1039 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001040
1041 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +00001042 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +00001043 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +00001044 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +00001045 case TargetLowering::Legal:
1046 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1047 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1048 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1049 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001050 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001051 break;
1052 }
1053 break;
1054
Jim Laskeyf9e54452007-01-26 14:34:52 +00001055 case ISD::LABEL:
1056 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1057 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001058 default: assert(0 && "This action is not supported yet!");
1059 case TargetLowering::Legal:
1060 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1061 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001062 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +00001063 break;
Chris Lattner567b9252007-03-03 19:21:38 +00001064 case TargetLowering::Expand:
1065 Result = LegalizeOp(Node->getOperand(0));
1066 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001067 }
Nate Begeman5965bd12006-02-17 05:43:56 +00001068 break;
Chris Lattner435b4022005-11-29 06:21:05 +00001069
Scott Michel9d09c5c2007-08-08 23:23:31 +00001070 case ISD::Constant: {
1071 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1072 unsigned opAction =
1073 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1074
Chris Lattnerdc750592005-01-07 07:47:09 +00001075 // We know we don't need to expand constants here, constants only have one
1076 // value and we check that it is fine above.
1077
Scott Michel9d09c5c2007-08-08 23:23:31 +00001078 if (opAction == TargetLowering::Custom) {
1079 Tmp1 = TLI.LowerOperation(Result, DAG);
1080 if (Tmp1.Val)
1081 Result = Tmp1;
1082 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001083 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +00001084 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001085 case ISD::ConstantFP: {
1086 // Spill FP immediates to the constant pool if the target cannot directly
1087 // codegen them. Targets often have some immediate values that can be
1088 // efficiently generated into an FP register without a load. We explicitly
1089 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +00001090 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1091
1092 // Check to see if this FP immediate is already legal.
1093 bool isLegal = false;
1094 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1095 E = TLI.legal_fpimm_end(); I != E; ++I)
1096 if (CFP->isExactlyValue(*I)) {
1097 isLegal = true;
1098 break;
1099 }
1100
Chris Lattner758b0ac2006-01-29 06:26:56 +00001101 // If this is a legal constant, turn it into a TargetConstantFP node.
1102 if (isLegal) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00001103 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1104 CFP->getValueType(0));
Chris Lattner758b0ac2006-01-29 06:26:56 +00001105 break;
1106 }
1107
1108 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1109 default: assert(0 && "This action is not supported yet!");
1110 case TargetLowering::Custom:
1111 Tmp3 = TLI.LowerOperation(Result, DAG);
1112 if (Tmp3.Val) {
1113 Result = Tmp3;
1114 break;
1115 }
1116 // FALLTHROUGH
1117 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +00001118 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001119 }
1120 break;
1121 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001122 case ISD::TokenFactor:
1123 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001124 Tmp1 = LegalizeOp(Node->getOperand(0));
1125 Tmp2 = LegalizeOp(Node->getOperand(1));
1126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1127 } else if (Node->getNumOperands() == 3) {
1128 Tmp1 = LegalizeOp(Node->getOperand(0));
1129 Tmp2 = LegalizeOp(Node->getOperand(1));
1130 Tmp3 = LegalizeOp(Node->getOperand(2));
1131 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001132 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +00001133 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001134 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001135 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1136 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001137 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001138 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001139 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001140
1141 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001142 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001143 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001144 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1145 assert(Tmp3.Val && "Target didn't custom lower this node!");
1146 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1147 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001148
Chris Lattneraaa23d92006-05-16 22:53:20 +00001149 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001150 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +00001151 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1152 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001153 if (Op.ResNo == i)
1154 Tmp2 = Tmp1;
1155 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1156 }
1157 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001158 case ISD::EXTRACT_SUBREG: {
1159 Tmp1 = LegalizeOp(Node->getOperand(0));
1160 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1161 assert(idx && "Operand must be a constant");
1162 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1163 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1164 }
1165 break;
1166 case ISD::INSERT_SUBREG: {
1167 Tmp1 = LegalizeOp(Node->getOperand(0));
1168 Tmp2 = LegalizeOp(Node->getOperand(1));
1169 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1170 assert(idx && "Operand must be a constant");
1171 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1172 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1173 }
1174 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001175 case ISD::BUILD_VECTOR:
1176 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001177 default: assert(0 && "This action is not supported yet!");
1178 case TargetLowering::Custom:
1179 Tmp3 = TLI.LowerOperation(Result, DAG);
1180 if (Tmp3.Val) {
1181 Result = Tmp3;
1182 break;
1183 }
1184 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001185 case TargetLowering::Expand:
1186 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001187 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001188 }
Chris Lattner29b23012006-03-19 01:17:20 +00001189 break;
1190 case ISD::INSERT_VECTOR_ELT:
1191 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1192 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1193 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1194 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1195
1196 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1197 Node->getValueType(0))) {
1198 default: assert(0 && "This action is not supported yet!");
1199 case TargetLowering::Legal:
1200 break;
1201 case TargetLowering::Custom:
1202 Tmp3 = TLI.LowerOperation(Result, DAG);
1203 if (Tmp3.Val) {
1204 Result = Tmp3;
1205 break;
1206 }
1207 // FALLTHROUGH
1208 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001209 // If the insert index is a constant, codegen this as a scalar_to_vector,
1210 // then a shuffle that inserts it into the right position in the vector.
1211 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1212 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1213 Tmp1.getValueType(), Tmp2);
1214
1215 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1216 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman5c441312007-06-14 22:58:02 +00001217 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner326870b2006-04-17 19:21:01 +00001218
1219 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1220 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1221 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001222 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +00001223 for (unsigned i = 0; i != NumElts; ++i) {
1224 if (i != InsertPos->getValue())
1225 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1226 else
1227 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1228 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001229 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1230 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +00001231
1232 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1233 Tmp1, ScVec, ShufMask);
1234 Result = LegalizeOp(Result);
1235 break;
1236 }
1237
Chris Lattner29b23012006-03-19 01:17:20 +00001238 // If the target doesn't support this, we have to spill the input vector
1239 // to a temporary stack slot, update the element, then reload it. This is
1240 // badness. We could also load the value into a vector register (either
1241 // with a "move to register" or "extload into register" instruction, then
1242 // permute it into place, if the idx is a constant and if the idx is
1243 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001244 MVT::ValueType VT = Tmp1.getValueType();
1245 MVT::ValueType EltVT = Tmp2.getValueType();
1246 MVT::ValueType IdxVT = Tmp3.getValueType();
1247 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattnerd6f7d442007-10-15 17:48:57 +00001248 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001249 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +00001250 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001251
1252 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001253 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1254 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001255 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001256 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1257 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1258 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001259 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +00001260 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001261 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +00001262 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +00001263 break;
1264 }
1265 }
1266 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001267 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001268 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1269 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1270 break;
1271 }
1272
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001273 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1274 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1275 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1276 Node->getValueType(0))) {
1277 default: assert(0 && "This action is not supported yet!");
1278 case TargetLowering::Legal:
1279 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001280 case TargetLowering::Custom:
1281 Tmp3 = TLI.LowerOperation(Result, DAG);
1282 if (Tmp3.Val) {
1283 Result = Tmp3;
1284 break;
1285 }
1286 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001287 case TargetLowering::Expand:
1288 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001289 break;
1290 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001291 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001292 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001293 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1294 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1295 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1296
1297 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001298 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1299 default: assert(0 && "Unknown operation action!");
1300 case TargetLowering::Legal:
1301 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1302 "vector shuffle should not be created if not legal!");
1303 break;
1304 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001305 Tmp3 = TLI.LowerOperation(Result, DAG);
1306 if (Tmp3.Val) {
1307 Result = Tmp3;
1308 break;
1309 }
1310 // FALLTHROUGH
1311 case TargetLowering::Expand: {
1312 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00001313 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng9fa89592006-04-05 06:07:11 +00001314 MVT::ValueType PtrVT = TLI.getPointerTy();
1315 SDOperand Mask = Node->getOperand(2);
1316 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001317 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001318 for (unsigned i = 0; i != NumElems; ++i) {
1319 SDOperand Arg = Mask.getOperand(i);
1320 if (Arg.getOpcode() == ISD::UNDEF) {
1321 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1322 } else {
1323 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1324 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1325 if (Idx < NumElems)
1326 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1327 DAG.getConstant(Idx, PtrVT)));
1328 else
1329 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1330 DAG.getConstant(Idx - NumElems, PtrVT)));
1331 }
1332 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001333 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001334 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001335 }
Chris Lattner6be79822006-04-04 17:23:26 +00001336 case TargetLowering::Promote: {
1337 // Change base type to a different vector type.
1338 MVT::ValueType OVT = Node->getValueType(0);
1339 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1340
1341 // Cast the two input vectors.
1342 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1343 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1344
1345 // Convert the shuffle mask to the right # elements.
1346 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1347 assert(Tmp3.Val && "Shuffle not legal?");
1348 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1349 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1350 break;
1351 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001352 }
1353 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001354
1355 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001356 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001357 Tmp2 = LegalizeOp(Node->getOperand(1));
1358 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001359 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001360 break;
1361
Dan Gohmana8665142007-06-25 16:23:39 +00001362 case ISD::EXTRACT_SUBVECTOR:
1363 Tmp1 = Node->getOperand(0);
1364 Tmp2 = LegalizeOp(Node->getOperand(1));
1365 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1366 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001367 break;
1368
Chris Lattner462505f2006-02-13 09:18:02 +00001369 case ISD::CALLSEQ_START: {
1370 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1371
1372 // Recursively Legalize all of the inputs of the call end that do not lead
1373 // to this call start. This ensures that any libcalls that need be inserted
1374 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001375 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001376 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001377 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1378 NodesLeadingTo);
1379 }
Chris Lattner462505f2006-02-13 09:18:02 +00001380
1381 // Now that we legalized all of the inputs (which may have inserted
1382 // libcalls) create the new CALLSEQ_START node.
1383 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1384
1385 // Merge in the last call, to ensure that this call start after the last
1386 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001387 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001388 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1389 Tmp1 = LegalizeOp(Tmp1);
1390 }
Chris Lattner462505f2006-02-13 09:18:02 +00001391
1392 // Do not try to legalize the target-specific arguments (#1+).
1393 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001394 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001395 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001396 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001397 }
1398
1399 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001400 AddLegalizedOperand(Op.getValue(0), Result);
1401 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1402 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1403
Chris Lattner462505f2006-02-13 09:18:02 +00001404 // Now that the callseq_start and all of the non-call nodes above this call
1405 // sequence have been legalized, legalize the call itself. During this
1406 // process, no libcalls can/will be inserted, guaranteeing that no calls
1407 // can overlap.
1408 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1409 SDOperand InCallSEQ = LastCALLSEQ_END;
1410 // Note that we are selecting this call!
1411 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1412 IsLegalizingCall = true;
1413
1414 // Legalize the call, starting from the CALLSEQ_END.
1415 LegalizeOp(LastCALLSEQ_END);
1416 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1417 return Result;
1418 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001419 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001420 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1421 // will cause this node to be legalized as well as handling libcalls right.
1422 if (LastCALLSEQ_END.Val != Node) {
1423 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001424 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001425 assert(I != LegalizedNodes.end() &&
1426 "Legalizing the call start should have legalized this node!");
1427 return I->second;
1428 }
1429
1430 // Otherwise, the call start has been legalized and everything is going
1431 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001432 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001433 // Do not try to legalize the target-specific arguments (#1+), except for
1434 // an optional flag input.
1435 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1436 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001437 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001438 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001439 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001440 }
1441 } else {
1442 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1443 if (Tmp1 != Node->getOperand(0) ||
1444 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001445 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001446 Ops[0] = Tmp1;
1447 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001448 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001449 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001450 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001451 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001452 // This finishes up call legalization.
1453 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001454
1455 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1456 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1457 if (Node->getNumValues() == 2)
1458 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1459 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001460 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng631ccc62007-08-16 23:50:06 +00001461 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerec26b482005-01-09 19:03:49 +00001462 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1463 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1464 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001465 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001466
Chris Lattnerd02b0542006-01-28 10:58:55 +00001467 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001468 Tmp2 = Result.getValue(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001469 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Cheng7f4ec822006-01-11 22:14:47 +00001470 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001471 case TargetLowering::Expand: {
1472 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1473 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1474 " not tell us which reg is the stack pointer!");
1475 SDOperand Chain = Tmp1.getOperand(0);
1476 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001477 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1478 Chain = SP.getValue(1);
1479 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1480 unsigned StackAlign =
1481 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1482 if (Align > StackAlign)
Evan Chengcb6d65e2007-08-17 18:02:22 +00001483 SP = DAG.getNode(ISD::AND, VT, SP,
1484 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng631ccc62007-08-16 23:50:06 +00001485 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
1486 Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001487 Tmp1 = LegalizeOp(Tmp1);
1488 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001489 break;
1490 }
1491 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001492 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1493 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001494 Tmp1 = LegalizeOp(Tmp3);
1495 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001496 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001497 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001498 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001499 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001500 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001501 // Since this op produce two values, make sure to remember that we
1502 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001503 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1504 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001505 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001506 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001507 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001508 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001509 bool Changed = false;
1510 // Legalize all of the operands of the inline asm, in case they are nodes
1511 // that need to be expanded or something. Note we skip the asm string and
1512 // all of the TargetConstant flags.
1513 SDOperand Op = LegalizeOp(Ops[0]);
1514 Changed = Op != Ops[0];
1515 Ops[0] = Op;
1516
1517 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1518 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1519 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1520 for (++i; NumVals; ++i, --NumVals) {
1521 SDOperand Op = LegalizeOp(Ops[i]);
1522 if (Op != Ops[i]) {
1523 Changed = true;
1524 Ops[i] = Op;
1525 }
1526 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001527 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001528
1529 if (HasInFlag) {
1530 Op = LegalizeOp(Ops.back());
1531 Changed |= Op != Ops.back();
1532 Ops.back() = Op;
1533 }
1534
1535 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001536 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001537
1538 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001539 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001540 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1541 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001542 }
Chris Lattner68a12142005-01-07 22:12:08 +00001543 case ISD::BR:
1544 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001545 // Ensure that libcalls are emitted before a branch.
1546 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1547 Tmp1 = LegalizeOp(Tmp1);
1548 LastCALLSEQ_END = DAG.getEntryNode();
1549
Chris Lattnerd02b0542006-01-28 10:58:55 +00001550 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001551 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001552 case ISD::BRIND:
1553 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1554 // Ensure that libcalls are emitted before a branch.
1555 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1556 Tmp1 = LegalizeOp(Tmp1);
1557 LastCALLSEQ_END = DAG.getEntryNode();
1558
1559 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1560 default: assert(0 && "Indirect target must be legal type (pointer)!");
1561 case Legal:
1562 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1563 break;
1564 }
1565 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1566 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001567 case ISD::BR_JT:
1568 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1569 // Ensure that libcalls are emitted before a branch.
1570 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1571 Tmp1 = LegalizeOp(Tmp1);
1572 LastCALLSEQ_END = DAG.getEntryNode();
1573
1574 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1575 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1576
1577 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1578 default: assert(0 && "This action is not supported yet!");
1579 case TargetLowering::Legal: break;
1580 case TargetLowering::Custom:
1581 Tmp1 = TLI.LowerOperation(Result, DAG);
1582 if (Tmp1.Val) Result = Tmp1;
1583 break;
1584 case TargetLowering::Expand: {
1585 SDOperand Chain = Result.getOperand(0);
1586 SDOperand Table = Result.getOperand(1);
1587 SDOperand Index = Result.getOperand(2);
1588
1589 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001590 MachineFunction &MF = DAG.getMachineFunction();
1591 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001592 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1593 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001594
1595 SDOperand LD;
1596 switch (EntrySize) {
1597 default: assert(0 && "Size of jump table not supported yet."); break;
1598 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1599 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1600 }
1601
Evan Cheng797d56f2007-11-09 01:32:10 +00001602 Addr = LD;
Jim Laskey70323a82006-12-14 19:17:33 +00001603 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001604 // For PIC, the sequence is:
1605 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng797d56f2007-11-09 01:32:10 +00001606 // RelocBase can be JumpTable, GOT or some sort of global base.
1607 if (PTy != MVT::i32)
1608 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1609 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1610 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng84a28d42006-10-30 08:00:44 +00001611 }
Evan Cheng797d56f2007-11-09 01:32:10 +00001612 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng84a28d42006-10-30 08:00:44 +00001613 }
1614 }
1615 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001616 case ISD::BRCOND:
1617 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001618 // Ensure that libcalls are emitted before a return.
1619 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1620 Tmp1 = LegalizeOp(Tmp1);
1621 LastCALLSEQ_END = DAG.getEntryNode();
1622
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001623 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1624 case Expand: assert(0 && "It's impossible to expand bools");
1625 case Legal:
1626 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1627 break;
1628 case Promote:
1629 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001630
1631 // The top bits of the promoted condition are not necessarily zero, ensure
1632 // that the value is properly zero extended.
Dan Gohman309d3d52007-06-22 14:59:07 +00001633 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerdb189382006-11-27 04:39:56 +00001634 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1635 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001636 break;
1637 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001638
1639 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001640 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001641
1642 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1643 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001644 case TargetLowering::Legal: break;
1645 case TargetLowering::Custom:
1646 Tmp1 = TLI.LowerOperation(Result, DAG);
1647 if (Tmp1.Val) Result = Tmp1;
1648 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001649 case TargetLowering::Expand:
1650 // Expand brcond's setcc into its constituent parts and create a BR_CC
1651 // Node.
1652 if (Tmp2.getOpcode() == ISD::SETCC) {
1653 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1654 Tmp2.getOperand(0), Tmp2.getOperand(1),
1655 Node->getOperand(2));
1656 } else {
1657 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1658 DAG.getCondCode(ISD::SETNE), Tmp2,
1659 DAG.getConstant(0, Tmp2.getValueType()),
1660 Node->getOperand(2));
1661 }
1662 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001663 }
1664 break;
1665 case ISD::BR_CC:
1666 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001667 // Ensure that libcalls are emitted before a branch.
1668 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1669 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001670 Tmp2 = Node->getOperand(2); // LHS
1671 Tmp3 = Node->getOperand(3); // RHS
1672 Tmp4 = Node->getOperand(1); // CC
1673
1674 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001675 LastCALLSEQ_END = DAG.getEntryNode();
1676
Nate Begeman7e7f4392006-02-01 07:19:44 +00001677 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1678 // the LHS is a legal SETCC itself. In this case, we need to compare
1679 // the result against zero to select between true and false values.
1680 if (Tmp3.Val == 0) {
1681 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1682 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001683 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001684
1685 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1686 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001687
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001688 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1689 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001690 case TargetLowering::Legal: break;
1691 case TargetLowering::Custom:
1692 Tmp4 = TLI.LowerOperation(Result, DAG);
1693 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001694 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001695 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001696 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001697 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001698 LoadSDNode *LD = cast<LoadSDNode>(Node);
1699 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1700 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001701
Evan Chenge71fe34d2006-10-09 20:57:25 +00001702 ISD::LoadExtType ExtType = LD->getExtensionType();
1703 if (ExtType == ISD::NON_EXTLOAD) {
1704 MVT::ValueType VT = Node->getValueType(0);
1705 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1706 Tmp3 = Result.getValue(0);
1707 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001708
Evan Chenge71fe34d2006-10-09 20:57:25 +00001709 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1710 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001711 case TargetLowering::Legal:
1712 // If this is an unaligned load and the target doesn't support it,
1713 // expand it.
1714 if (!TLI.allowsUnalignedMemoryAccesses()) {
1715 unsigned ABIAlignment = TLI.getTargetData()->
1716 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1717 if (LD->getAlignment() < ABIAlignment){
1718 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1719 TLI);
1720 Tmp3 = Result.getOperand(0);
1721 Tmp4 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001722 Tmp3 = LegalizeOp(Tmp3);
1723 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001724 }
1725 }
1726 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001727 case TargetLowering::Custom:
1728 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1729 if (Tmp1.Val) {
1730 Tmp3 = LegalizeOp(Tmp1);
1731 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001732 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001733 break;
1734 case TargetLowering::Promote: {
1735 // Only promote a load of vector type to another.
1736 assert(MVT::isVector(VT) && "Cannot promote this load!");
1737 // Change base type to a different vector type.
1738 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1739
1740 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001741 LD->getSrcValueOffset(),
1742 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001743 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1744 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001745 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001746 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001747 }
1748 // Since loads produce two values, make sure to remember that we
1749 // legalized both of them.
1750 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1751 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1752 return Op.ResNo ? Tmp4 : Tmp3;
1753 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001754 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001755 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1756 default: assert(0 && "This action is not supported yet!");
1757 case TargetLowering::Promote:
1758 assert(SrcVT == MVT::i1 &&
1759 "Can only promote extending LOAD from i1 -> i8!");
1760 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1761 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00001762 MVT::i8, LD->isVolatile(), LD->getAlignment());
Duncan Sandsd42c8122007-10-17 13:49:58 +00001763 Tmp1 = Result.getValue(0);
1764 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001765 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001766 case TargetLowering::Custom:
1767 isCustom = true;
1768 // FALLTHROUGH
1769 case TargetLowering::Legal:
1770 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1771 Tmp1 = Result.getValue(0);
1772 Tmp2 = Result.getValue(1);
1773
1774 if (isCustom) {
1775 Tmp3 = TLI.LowerOperation(Result, DAG);
1776 if (Tmp3.Val) {
1777 Tmp1 = LegalizeOp(Tmp3);
1778 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1779 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001780 } else {
1781 // If this is an unaligned load and the target doesn't support it,
1782 // expand it.
1783 if (!TLI.allowsUnalignedMemoryAccesses()) {
1784 unsigned ABIAlignment = TLI.getTargetData()->
1785 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1786 if (LD->getAlignment() < ABIAlignment){
1787 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1788 TLI);
1789 Tmp1 = Result.getOperand(0);
1790 Tmp2 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001791 Tmp1 = LegalizeOp(Tmp1);
1792 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001793 }
1794 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001795 }
1796 break;
1797 case TargetLowering::Expand:
1798 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1799 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1800 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001801 LD->getSrcValueOffset(),
1802 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001803 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1804 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1805 Tmp2 = LegalizeOp(Load.getValue(1));
1806 break;
1807 }
Chris Lattner296a83c2007-02-01 04:55:59 +00001808 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001809 // Turn the unsupported load into an EXTLOAD followed by an explicit
1810 // zero/sign extend inreg.
1811 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1812 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001813 LD->getSrcValueOffset(), SrcVT,
1814 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001815 SDOperand ValRes;
1816 if (ExtType == ISD::SEXTLOAD)
1817 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1818 Result, DAG.getValueType(SrcVT));
1819 else
1820 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1821 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1822 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1823 break;
1824 }
1825 // Since loads produce two values, make sure to remember that we legalized
1826 // both of them.
1827 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1828 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1829 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001830 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001831 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001832 case ISD::EXTRACT_ELEMENT: {
1833 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1834 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001835 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001836 case Legal:
1837 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1838 // 1 -> Hi
1839 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1840 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1841 TLI.getShiftAmountTy()));
1842 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1843 } else {
1844 // 0 -> Lo
1845 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1846 Node->getOperand(0));
1847 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001848 break;
1849 case Expand:
1850 // Get both the low and high parts.
1851 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1852 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1853 Result = Tmp2; // 1 -> Hi
1854 else
1855 Result = Tmp1; // 0 -> Lo
1856 break;
1857 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001858 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001859 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001860
1861 case ISD::CopyToReg:
1862 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001863
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001864 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001865 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001866 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001867 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001868 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001869 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001870 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001871 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001872 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001873 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001874 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1875 Tmp3);
1876 } else {
1877 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001878 }
1879
1880 // Since this produces two values, make sure to remember that we legalized
1881 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001882 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001883 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001884 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001885 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001886 break;
1887
1888 case ISD::RET:
1889 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001890
1891 // Ensure that libcalls are emitted before a return.
1892 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1893 Tmp1 = LegalizeOp(Tmp1);
1894 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001895
Chris Lattnerdc750592005-01-07 07:47:09 +00001896 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001897 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001898 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001899 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001900 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001901 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001902 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001903 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001904 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +00001905 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001906 SDOperand Lo, Hi;
1907 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00001908
1909 // Big endian systems want the hi reg first.
1910 if (!TLI.isLittleEndian())
1911 std::swap(Lo, Hi);
1912
Evan Cheng3432ab92006-12-11 19:27:14 +00001913 if (Hi.Val)
1914 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1915 else
1916 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001917 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001918 } else {
1919 SDNode *InVal = Tmp2.Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00001920 int InIx = Tmp2.ResNo;
1921 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
1922 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001923
Dan Gohmana8665142007-06-25 16:23:39 +00001924 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001925 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001926 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00001927 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001928 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00001929 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001930 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001931 } else if (NumElems == 1) {
1932 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00001933 Tmp2 = ScalarizeVectorOp(Tmp2);
1934 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001935 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001936
1937 // FIXME: Returns of gcc generic vectors smaller than a legal type
1938 // should be returned in integer registers!
1939
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001940 // The scalarized value type may not be legal, e.g. it might require
1941 // promotion or expansion. Relegalize the return.
1942 Result = LegalizeOp(Result);
1943 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001944 // FIXME: Returns of gcc generic vectors larger than a legal vector
1945 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001946 SDOperand Lo, Hi;
1947 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00001948 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001949 Result = LegalizeOp(Result);
1950 }
1951 }
Misha Brukman835702a2005-04-21 22:36:52 +00001952 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001953 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001954 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00001955 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001956 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001957 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001958 }
1959 break;
1960 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001961 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001962 break;
1963 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00001964 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00001965 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00001966 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00001967 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1968 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001969 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00001970 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001971 break;
1972 case Expand: {
1973 SDOperand Lo, Hi;
Dan Gohman3b62d722007-06-27 16:08:04 +00001974 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001975 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001976 ExpandOp(Node->getOperand(i), Lo, Hi);
1977 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00001978 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00001979 if (Hi.Val) {
1980 NewValues.push_back(Hi);
1981 NewValues.push_back(Node->getOperand(i+1));
1982 }
Misha Brukman835702a2005-04-21 22:36:52 +00001983 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001984 }
1985 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001986 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001987 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001988
1989 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00001990 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00001991 else
Chris Lattner97af9d52006-08-08 01:09:31 +00001992 Result = DAG.getNode(ISD::RET, MVT::Other,
1993 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00001994 break;
1995 }
1996 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001997
Chris Lattner4d1ea712006-01-29 21:02:23 +00001998 if (Result.getOpcode() == ISD::RET) {
1999 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2000 default: assert(0 && "This action is not supported yet!");
2001 case TargetLowering::Legal: break;
2002 case TargetLowering::Custom:
2003 Tmp1 = TLI.LowerOperation(Result, DAG);
2004 if (Tmp1.Val) Result = Tmp1;
2005 break;
2006 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002007 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002008 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002009 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00002010 StoreSDNode *ST = cast<StoreSDNode>(Node);
2011 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2012 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00002013 int SVOffset = ST->getSrcValueOffset();
2014 unsigned Alignment = ST->getAlignment();
2015 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00002016
Evan Chengab51cf22006-10-13 21:14:26 +00002017 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002018 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2019 // FIXME: We shouldn't do this for TargetConstantFP's.
2020 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2021 // to phase ordering between legalized code and the dag combiner. This
2022 // probably means that we need to integrate dag combiner and legalizer
2023 // together.
Dale Johannesen98d3a082007-09-14 22:26:36 +00002024 // We generally can't do this one for long doubles.
Chris Lattner5e6fe052007-10-13 06:35:54 +00002025 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002026 if (CFP->getValueType(0) == MVT::f32 &&
2027 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen028084e2007-09-12 03:30:33 +00002028 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2029 convertToAPInt().getZExtValue(),
Dale Johannesen245dceb2007-09-11 18:32:33 +00002030 MVT::i32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00002031 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2032 SVOffset, isVolatile, Alignment);
2033 break;
2034 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002035 // If this target supports 64-bit registers, do a single 64-bit store.
2036 if (getTypeAction(MVT::i64) == Legal) {
2037 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2038 getZExtValue(), MVT::i64);
2039 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2040 SVOffset, isVolatile, Alignment);
2041 break;
2042 } else if (getTypeAction(MVT::i32) == Legal) {
2043 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2044 // stores. If the target supports neither 32- nor 64-bits, this
2045 // xform is certainly not worth it.
2046 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2047 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2048 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2049 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2050
2051 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2052 SVOffset, isVolatile, Alignment);
2053 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2054 getIntPtrConstant(4));
2055 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sands1826ded2007-10-28 12:59:45 +00002056 isVolatile, MinAlign(Alignment, 4U));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002057
2058 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2059 break;
2060 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002061 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002062 }
2063
Evan Chengab51cf22006-10-13 21:14:26 +00002064 switch (getTypeAction(ST->getStoredVT())) {
2065 case Legal: {
2066 Tmp3 = LegalizeOp(ST->getValue());
2067 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2068 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00002069
Evan Chengab51cf22006-10-13 21:14:26 +00002070 MVT::ValueType VT = Tmp3.getValueType();
2071 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2072 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002073 case TargetLowering::Legal:
2074 // If this is an unaligned store and the target doesn't support it,
2075 // expand it.
2076 if (!TLI.allowsUnalignedMemoryAccesses()) {
2077 unsigned ABIAlignment = TLI.getTargetData()->
2078 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2079 if (ST->getAlignment() < ABIAlignment)
2080 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2081 TLI);
2082 }
2083 break;
Evan Chengab51cf22006-10-13 21:14:26 +00002084 case TargetLowering::Custom:
2085 Tmp1 = TLI.LowerOperation(Result, DAG);
2086 if (Tmp1.Val) Result = Tmp1;
2087 break;
2088 case TargetLowering::Promote:
2089 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2090 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2091 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2092 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00002093 ST->getSrcValue(), SVOffset, isVolatile,
2094 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002095 break;
2096 }
2097 break;
2098 }
2099 case Promote:
2100 // Truncate the value and store the result.
2101 Tmp3 = PromoteOp(ST->getValue());
2102 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002103 SVOffset, ST->getStoredVT(),
2104 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002105 break;
2106
2107 case Expand:
2108 unsigned IncrementSize = 0;
2109 SDOperand Lo, Hi;
2110
2111 // If this is a vector type, then we have to calculate the increment as
2112 // the product of the element size in bytes, and the number of elements
2113 // in the high half of the vector.
Dan Gohmana8665142007-06-25 16:23:39 +00002114 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002115 SDNode *InVal = ST->getValue().Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00002116 int InIx = ST->getValue().ResNo;
2117 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2118 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Evan Chengab51cf22006-10-13 21:14:26 +00002119
Dan Gohmana8665142007-06-25 16:23:39 +00002120 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002121 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00002122 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002123 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002124 // Turn this into a normal store of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002125 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00002126 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002127 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002128 Result = LegalizeOp(Result);
2129 break;
2130 } else if (NumElems == 1) {
2131 // Turn this into a normal store of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002132 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00002133 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002134 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002135 // The scalarized value type may not be legal, e.g. it might require
2136 // promotion or expansion. Relegalize the scalar store.
2137 Result = LegalizeOp(Result);
2138 break;
2139 } else {
2140 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2141 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2142 }
2143 } else {
2144 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00002145 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002146
2147 if (!TLI.isLittleEndian())
2148 std::swap(Lo, Hi);
2149 }
2150
2151 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002152 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002153
2154 if (Hi.Val == NULL) {
2155 // Must be int <-> float one-to-one expansion.
2156 Result = Lo;
2157 break;
2158 }
2159
Evan Chengab51cf22006-10-13 21:14:26 +00002160 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2161 getIntPtrConstant(IncrementSize));
2162 assert(isTypeLegal(Tmp2.getValueType()) &&
2163 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002164 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00002165 Alignment = MinAlign(Alignment, IncrementSize);
Evan Chengab51cf22006-10-13 21:14:26 +00002166 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002167 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002168 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2169 break;
2170 }
2171 } else {
2172 // Truncating store
2173 assert(isTypeLegal(ST->getValue().getValueType()) &&
2174 "Cannot handle illegal TRUNCSTORE yet!");
2175 Tmp3 = LegalizeOp(ST->getValue());
2176
2177 // The only promote case we handle is TRUNCSTORE:i1 X into
2178 // -> TRUNCSTORE:i8 (and X, 1)
2179 if (ST->getStoredVT() == MVT::i1 &&
2180 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2181 // Promote the bool to a mask then store.
2182 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2183 DAG.getConstant(1, Tmp3.getValueType()));
2184 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002185 SVOffset, MVT::i8,
2186 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002187 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2188 Tmp2 != ST->getBasePtr()) {
2189 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2190 ST->getOffset());
2191 }
2192
2193 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2194 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002195 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002196 case TargetLowering::Legal:
2197 // If this is an unaligned store and the target doesn't support it,
2198 // expand it.
2199 if (!TLI.allowsUnalignedMemoryAccesses()) {
2200 unsigned ABIAlignment = TLI.getTargetData()->
2201 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2202 if (ST->getAlignment() < ABIAlignment)
2203 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2204 TLI);
2205 }
2206 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002207 case TargetLowering::Custom:
2208 Tmp1 = TLI.LowerOperation(Result, DAG);
2209 if (Tmp1.Val) Result = Tmp1;
2210 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002211 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002212 }
2213 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002214 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002215 case ISD::PCMARKER:
2216 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002217 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002218 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002219 case ISD::STACKSAVE:
2220 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002221 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2222 Tmp1 = Result.getValue(0);
2223 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002224
Chris Lattnerb3266452006-01-13 02:50:02 +00002225 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2226 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002227 case TargetLowering::Legal: break;
2228 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002229 Tmp3 = TLI.LowerOperation(Result, DAG);
2230 if (Tmp3.Val) {
2231 Tmp1 = LegalizeOp(Tmp3);
2232 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002233 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002234 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002235 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002236 // Expand to CopyFromReg if the target set
2237 // StackPointerRegisterToSaveRestore.
2238 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002239 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002240 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002241 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002242 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002243 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2244 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002245 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002246 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002247 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002248
2249 // Since stacksave produce two values, make sure to remember that we
2250 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002251 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2252 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2253 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002254
Chris Lattnerb3266452006-01-13 02:50:02 +00002255 case ISD::STACKRESTORE:
2256 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2257 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002258 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002259
2260 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2261 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002262 case TargetLowering::Legal: break;
2263 case TargetLowering::Custom:
2264 Tmp1 = TLI.LowerOperation(Result, DAG);
2265 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002266 break;
2267 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002268 // Expand to CopyToReg if the target set
2269 // StackPointerRegisterToSaveRestore.
2270 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2271 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2272 } else {
2273 Result = Tmp1;
2274 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002275 break;
2276 }
2277 break;
2278
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002279 case ISD::READCYCLECOUNTER:
2280 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002281 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002282 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2283 Node->getValueType(0))) {
2284 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002285 case TargetLowering::Legal:
2286 Tmp1 = Result.getValue(0);
2287 Tmp2 = Result.getValue(1);
2288 break;
Evan Cheng69739932006-11-29 08:26:18 +00002289 case TargetLowering::Custom:
2290 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002291 Tmp1 = LegalizeOp(Result.getValue(0));
2292 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002293 break;
2294 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002295
2296 // Since rdcc produce two values, make sure to remember that we legalized
2297 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002298 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2299 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002300 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002301
Chris Lattner39c67442005-01-14 22:08:15 +00002302 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002303 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2304 case Expand: assert(0 && "It's impossible to expand bools");
2305 case Legal:
2306 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2307 break;
2308 case Promote:
2309 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002310 // Make sure the condition is either zero or one.
Dan Gohman309d3d52007-06-22 14:59:07 +00002311 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattner3abb6362006-11-28 01:03:30 +00002312 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2313 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002314 break;
2315 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002316 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002317 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002318
Chris Lattnerd02b0542006-01-28 10:58:55 +00002319 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002320
Nate Begeman987121a2005-08-23 04:29:48 +00002321 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002322 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002323 case TargetLowering::Legal: break;
2324 case TargetLowering::Custom: {
2325 Tmp1 = TLI.LowerOperation(Result, DAG);
2326 if (Tmp1.Val) Result = Tmp1;
2327 break;
2328 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002329 case TargetLowering::Expand:
2330 if (Tmp1.getOpcode() == ISD::SETCC) {
2331 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2332 Tmp2, Tmp3,
2333 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2334 } else {
2335 Result = DAG.getSelectCC(Tmp1,
2336 DAG.getConstant(0, Tmp1.getValueType()),
2337 Tmp2, Tmp3, ISD::SETNE);
2338 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002339 break;
2340 case TargetLowering::Promote: {
2341 MVT::ValueType NVT =
2342 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2343 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002344 if (MVT::isVector(Tmp2.getValueType())) {
2345 ExtOp = ISD::BIT_CONVERT;
2346 TruncOp = ISD::BIT_CONVERT;
2347 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002348 ExtOp = ISD::ANY_EXTEND;
2349 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002350 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002351 ExtOp = ISD::FP_EXTEND;
2352 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002353 }
2354 // Promote each of the values to the new type.
2355 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2356 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2357 // Perform the larger operation, then round down.
2358 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2359 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2360 break;
2361 }
2362 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002363 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002364 case ISD::SELECT_CC: {
2365 Tmp1 = Node->getOperand(0); // LHS
2366 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002367 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2368 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002369 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002370
Nate Begeman7e7f4392006-02-01 07:19:44 +00002371 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2372
2373 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2374 // the LHS is a legal SETCC itself. In this case, we need to compare
2375 // the result against zero to select between true and false values.
2376 if (Tmp2.Val == 0) {
2377 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2378 CC = DAG.getCondCode(ISD::SETNE);
2379 }
2380 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2381
2382 // Everything is legal, see if we should expand this op or something.
2383 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2384 default: assert(0 && "This action is not supported yet!");
2385 case TargetLowering::Legal: break;
2386 case TargetLowering::Custom:
2387 Tmp1 = TLI.LowerOperation(Result, DAG);
2388 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002389 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002390 }
2391 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002392 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002393 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002394 Tmp1 = Node->getOperand(0);
2395 Tmp2 = Node->getOperand(1);
2396 Tmp3 = Node->getOperand(2);
2397 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2398
2399 // If we had to Expand the SetCC operands into a SELECT node, then it may
2400 // not always be possible to return a true LHS & RHS. In this case, just
2401 // return the value we legalized, returned in the LHS
2402 if (Tmp2.Val == 0) {
2403 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002404 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002405 }
Nate Begeman987121a2005-08-23 04:29:48 +00002406
Chris Lattnerf263a232006-01-30 22:43:50 +00002407 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002408 default: assert(0 && "Cannot handle this action for SETCC yet!");
2409 case TargetLowering::Custom:
2410 isCustom = true;
2411 // FALLTHROUGH.
2412 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002413 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002414 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002415 Tmp4 = TLI.LowerOperation(Result, DAG);
2416 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002417 }
Nate Begeman987121a2005-08-23 04:29:48 +00002418 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002419 case TargetLowering::Promote: {
2420 // First step, figure out the appropriate operation to use.
2421 // Allow SETCC to not be supported for all legal data types
2422 // Mostly this targets FP
2423 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002424 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002425
2426 // Scan for the appropriate larger type to use.
2427 while (1) {
2428 NewInTy = (MVT::ValueType)(NewInTy+1);
2429
2430 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2431 "Fell off of the edge of the integer world");
2432 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2433 "Fell off of the edge of the floating point world");
2434
2435 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002436 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002437 break;
2438 }
2439 if (MVT::isInteger(NewInTy))
2440 assert(0 && "Cannot promote Legal Integer SETCC yet");
2441 else {
2442 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2443 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2444 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002445 Tmp1 = LegalizeOp(Tmp1);
2446 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002447 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002448 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002449 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002450 }
Nate Begeman987121a2005-08-23 04:29:48 +00002451 case TargetLowering::Expand:
2452 // Expand a setcc node into a select_cc of the same condition, lhs, and
2453 // rhs that selects between const 1 (true) and const 0 (false).
2454 MVT::ValueType VT = Node->getValueType(0);
2455 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2456 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002457 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002458 break;
2459 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002460 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002461 case ISD::MEMSET:
2462 case ISD::MEMCPY:
2463 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002464 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002465 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2466
2467 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2468 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2469 case Expand: assert(0 && "Cannot expand a byte!");
2470 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002471 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002472 break;
2473 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002474 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002475 break;
2476 }
2477 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002478 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002479 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002480
2481 SDOperand Tmp4;
2482 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002483 case Expand: {
2484 // Length is too big, just take the lo-part of the length.
2485 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002486 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002487 break;
2488 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002489 case Legal:
2490 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002491 break;
2492 case Promote:
2493 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002494 break;
2495 }
2496
2497 SDOperand Tmp5;
2498 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2499 case Expand: assert(0 && "Cannot expand this yet!");
2500 case Legal:
2501 Tmp5 = LegalizeOp(Node->getOperand(4));
2502 break;
2503 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002504 Tmp5 = PromoteOp(Node->getOperand(4));
2505 break;
2506 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002507
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002508 SDOperand Tmp6;
2509 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2510 case Expand: assert(0 && "Cannot expand this yet!");
2511 case Legal:
2512 Tmp6 = LegalizeOp(Node->getOperand(5));
2513 break;
2514 case Promote:
2515 Tmp6 = PromoteOp(Node->getOperand(5));
2516 break;
2517 }
2518
Chris Lattner3c0dd462005-01-16 07:29:19 +00002519 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2520 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002521 case TargetLowering::Custom:
2522 isCustom = true;
2523 // FALLTHROUGH
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002524 case TargetLowering::Legal: {
2525 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2526 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002527 if (isCustom) {
2528 Tmp1 = TLI.LowerOperation(Result, DAG);
2529 if (Tmp1.Val) Result = Tmp1;
2530 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002531 break;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002532 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002533 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002534 // Otherwise, the target does not support this operation. Lower the
2535 // operation to an explicit libcall as appropriate.
2536 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002537 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002538 TargetLowering::ArgListTy Args;
2539 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002540
Reid Spencer6dced922005-01-12 14:53:45 +00002541 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002542 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002543 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00002544 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002545 // Extend the (previously legalized) ubyte argument to be an int value
2546 // for the call.
2547 if (Tmp3.getValueType() > MVT::i32)
2548 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2549 else
2550 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002551 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencere63b6512006-12-31 05:55:36 +00002552 Args.push_back(Entry);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002553 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002554 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002555
2556 FnName = "memset";
2557 } else if (Node->getOpcode() == ISD::MEMCPY ||
2558 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002559 Entry.Ty = IntPtrTy;
Reid Spencer791864c2007-01-03 04:22:32 +00002560 Entry.Node = Tmp2; Args.push_back(Entry);
2561 Entry.Node = Tmp3; Args.push_back(Entry);
2562 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002563 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2564 } else {
2565 assert(0 && "Unknown op!");
2566 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002567
Chris Lattner85d70c62005-01-11 05:57:22 +00002568 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002569 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002570 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002571 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002572 break;
2573 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002574 }
2575 break;
2576 }
Chris Lattner5385db52005-05-09 20:23:03 +00002577
Chris Lattner4157c412005-04-02 04:00:59 +00002578 case ISD::SHL_PARTS:
2579 case ISD::SRA_PARTS:
2580 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002581 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002582 bool Changed = false;
2583 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2584 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2585 Changed |= Ops.back() != Node->getOperand(i);
2586 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002587 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002588 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002589
Evan Cheng870e4f82006-01-09 18:31:59 +00002590 switch (TLI.getOperationAction(Node->getOpcode(),
2591 Node->getValueType(0))) {
2592 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002593 case TargetLowering::Legal: break;
2594 case TargetLowering::Custom:
2595 Tmp1 = TLI.LowerOperation(Result, DAG);
2596 if (Tmp1.Val) {
2597 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002598 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002599 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002600 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2601 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002602 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002603 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002604 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002605 return RetVal;
2606 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002607 break;
2608 }
2609
Chris Lattner13fe99c2005-04-02 05:00:07 +00002610 // Since these produce multiple values, make sure to remember that we
2611 // legalized all of them.
2612 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2613 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2614 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002615 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002616
2617 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002618 case ISD::ADD:
2619 case ISD::SUB:
2620 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002621 case ISD::MULHS:
2622 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002623 case ISD::UDIV:
2624 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002625 case ISD::AND:
2626 case ISD::OR:
2627 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002628 case ISD::SHL:
2629 case ISD::SRL:
2630 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002631 case ISD::FADD:
2632 case ISD::FSUB:
2633 case ISD::FMUL:
2634 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00002635 case ISD::FPOW:
Chris Lattnerdc750592005-01-07 07:47:09 +00002636 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002637 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2638 case Expand: assert(0 && "Not possible");
2639 case Legal:
2640 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2641 break;
2642 case Promote:
2643 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2644 break;
2645 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002646
2647 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002648
Andrew Lenharth72594262005-12-24 23:42:32 +00002649 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002650 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002651 case TargetLowering::Legal: break;
2652 case TargetLowering::Custom:
2653 Tmp1 = TLI.LowerOperation(Result, DAG);
2654 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002655 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002656 case TargetLowering::Expand: {
Dan Gohmana1603612007-10-08 18:33:35 +00002657 MVT::ValueType VT = Op.getValueType();
2658
2659 // See if multiply or divide can be lowered using two-result operations.
2660 SDVTList VTs = DAG.getVTList(VT, VT);
2661 if (Node->getOpcode() == ISD::MUL) {
2662 // We just need the low half of the multiply; try both the signed
2663 // and unsigned forms. If the target supports both SMUL_LOHI and
2664 // UMUL_LOHI, form a preference by checking which forms of plain
2665 // MULH it supports.
2666 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2667 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2668 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2669 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2670 unsigned OpToUse = 0;
2671 if (HasSMUL_LOHI && !HasMULHS) {
2672 OpToUse = ISD::SMUL_LOHI;
2673 } else if (HasUMUL_LOHI && !HasMULHU) {
2674 OpToUse = ISD::UMUL_LOHI;
2675 } else if (HasSMUL_LOHI) {
2676 OpToUse = ISD::SMUL_LOHI;
2677 } else if (HasUMUL_LOHI) {
2678 OpToUse = ISD::UMUL_LOHI;
2679 }
2680 if (OpToUse) {
2681 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2682 break;
2683 }
2684 }
2685 if (Node->getOpcode() == ISD::MULHS &&
2686 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2687 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2688 break;
2689 }
2690 if (Node->getOpcode() == ISD::MULHU &&
2691 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2692 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2693 break;
2694 }
2695 if (Node->getOpcode() == ISD::SDIV &&
2696 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2697 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2698 break;
2699 }
2700 if (Node->getOpcode() == ISD::UDIV &&
2701 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2702 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2703 break;
2704 }
2705
Dan Gohman2a7de412007-10-11 23:57:53 +00002706 // Check to see if we have a libcall for this operator.
2707 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2708 bool isSigned = false;
2709 switch (Node->getOpcode()) {
2710 case ISD::UDIV:
2711 case ISD::SDIV:
2712 if (VT == MVT::i32) {
2713 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng31cbddf2007-01-12 02:11:51 +00002714 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman2a7de412007-10-11 23:57:53 +00002715 isSigned = Node->getOpcode() == ISD::SDIV;
2716 }
2717 break;
2718 case ISD::FPOW:
2719 LC = VT == MVT::f32 ? RTLIB::POW_F32 :
2720 VT == MVT::f64 ? RTLIB::POW_F64 :
2721 VT == MVT::f80 ? RTLIB::POW_F80 :
2722 VT == MVT::ppcf128 ? RTLIB::POW_PPCF128 :
2723 RTLIB::UNKNOWN_LIBCALL;
2724 break;
2725 default: break;
2726 }
2727 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2728 SDOperand Dummy;
2729 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002730 break;
2731 }
2732
Chris Lattner87f08092006-04-02 03:57:31 +00002733 assert(MVT::isVector(Node->getValueType(0)) &&
2734 "Cannot expand this binary operator!");
2735 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman2a7de412007-10-11 23:57:53 +00002736 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattner87f08092006-04-02 03:57:31 +00002737 break;
2738 }
Evan Cheng119266e2006-04-12 21:20:24 +00002739 case TargetLowering::Promote: {
2740 switch (Node->getOpcode()) {
2741 default: assert(0 && "Do not know how to promote this BinOp!");
2742 case ISD::AND:
2743 case ISD::OR:
2744 case ISD::XOR: {
2745 MVT::ValueType OVT = Node->getValueType(0);
2746 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2747 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2748 // Bit convert each of the values to the new type.
2749 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2750 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2751 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2752 // Bit convert the result back the original type.
2753 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2754 break;
2755 }
2756 }
2757 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002758 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002759 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002760
Dan Gohman12334ac2007-10-05 14:17:22 +00002761 case ISD::SMUL_LOHI:
2762 case ISD::UMUL_LOHI:
2763 case ISD::SDIVREM:
2764 case ISD::UDIVREM:
2765 // These nodes will only be produced by target-specific lowering, so
2766 // they shouldn't be here if they aren't legal.
Duncan Sands052c8432007-10-16 09:07:20 +00002767 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman12334ac2007-10-05 14:17:22 +00002768 "This must be legal!");
Dan Gohmana1603612007-10-08 18:33:35 +00002769
2770 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2771 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2772 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman12334ac2007-10-05 14:17:22 +00002773 break;
2774
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002775 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2776 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2777 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2778 case Expand: assert(0 && "Not possible");
2779 case Legal:
2780 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2781 break;
2782 case Promote:
2783 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2784 break;
2785 }
2786
2787 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2788
2789 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2790 default: assert(0 && "Operation not supported");
2791 case TargetLowering::Custom:
2792 Tmp1 = TLI.LowerOperation(Result, DAG);
2793 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002794 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002795 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002796 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00002797 // If this target supports fabs/fneg natively and select is cheap,
2798 // do this efficiently.
2799 if (!TLI.isSelectExpensive() &&
2800 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2801 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00002802 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00002803 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002804 // Get the sign bit of the RHS.
2805 MVT::ValueType IVT =
2806 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2807 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2808 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2809 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2810 // Get the absolute value of the result.
2811 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2812 // Select between the nabs and abs value based on the sign bit of
2813 // the input.
2814 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2815 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2816 AbsVal),
2817 AbsVal);
2818 Result = LegalizeOp(Result);
2819 break;
2820 }
2821
2822 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002823 MVT::ValueType NVT =
2824 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2825 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2826 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2827 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002828 break;
2829 }
Evan Cheng003feb02007-01-04 21:56:39 +00002830 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002831 break;
2832
Nate Begeman5965bd12006-02-17 05:43:56 +00002833 case ISD::ADDC:
2834 case ISD::SUBC:
2835 Tmp1 = LegalizeOp(Node->getOperand(0));
2836 Tmp2 = LegalizeOp(Node->getOperand(1));
2837 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2838 // Since this produces two values, make sure to remember that we legalized
2839 // both of them.
2840 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2841 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2842 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002843
Nate Begeman5965bd12006-02-17 05:43:56 +00002844 case ISD::ADDE:
2845 case ISD::SUBE:
2846 Tmp1 = LegalizeOp(Node->getOperand(0));
2847 Tmp2 = LegalizeOp(Node->getOperand(1));
2848 Tmp3 = LegalizeOp(Node->getOperand(2));
2849 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2850 // Since this produces two values, make sure to remember that we legalized
2851 // both of them.
2852 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2853 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2854 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002855
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002856 case ISD::BUILD_PAIR: {
2857 MVT::ValueType PairTy = Node->getValueType(0);
2858 // TODO: handle the case where the Lo and Hi operands are not of legal type
2859 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2860 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2861 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002862 case TargetLowering::Promote:
2863 case TargetLowering::Custom:
2864 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002865 case TargetLowering::Legal:
2866 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2867 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2868 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002869 case TargetLowering::Expand:
2870 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2871 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2872 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2873 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2874 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002875 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002876 break;
2877 }
2878 break;
2879 }
2880
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002881 case ISD::UREM:
2882 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002883 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002884 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2885 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002886
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002887 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002888 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2889 case TargetLowering::Custom:
2890 isCustom = true;
2891 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002892 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002893 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002894 if (isCustom) {
2895 Tmp1 = TLI.LowerOperation(Result, DAG);
2896 if (Tmp1.Val) Result = Tmp1;
2897 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002898 break;
Dan Gohmana1603612007-10-08 18:33:35 +00002899 case TargetLowering::Expand: {
Evan Cheng1fc7c362006-09-18 23:28:33 +00002900 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002901 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohmana1603612007-10-08 18:33:35 +00002902 MVT::ValueType VT = Node->getValueType(0);
2903
2904 // See if remainder can be lowered using two-result operations.
2905 SDVTList VTs = DAG.getVTList(VT, VT);
2906 if (Node->getOpcode() == ISD::SREM &&
2907 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2908 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2909 break;
2910 }
2911 if (Node->getOpcode() == ISD::UREM &&
2912 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2913 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2914 break;
2915 }
2916
2917 if (MVT::isInteger(VT)) {
2918 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002919 TargetLowering::Legal) {
2920 // X % Y -> X-X/Y*Y
Evan Cheng1fc7c362006-09-18 23:28:33 +00002921 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002922 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2923 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman08143e32007-11-05 23:35:22 +00002924 } else if (MVT::isVector(VT)) {
2925 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002926 } else {
Dan Gohmana1603612007-10-08 18:33:35 +00002927 assert(VT == MVT::i32 &&
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002928 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00002929 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2930 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002931 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002932 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002933 }
Dan Gohmanccfc0282007-11-06 22:11:54 +00002934 } else {
2935 assert(MVT::isFloatingPoint(VT) &&
2936 "remainder op must have integer or floating-point type");
Dan Gohman08143e32007-11-05 23:35:22 +00002937 if (MVT::isVector(VT)) {
2938 Result = LegalizeOp(UnrollVectorOp(Op));
2939 } else {
2940 // Floating point mod -> fmod libcall.
2941 RTLIB::Libcall LC = VT == MVT::f32
2942 ? RTLIB::REM_F32 : RTLIB::REM_F64;
2943 SDOperand Dummy;
2944 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2945 false/*sign irrelevant*/, Dummy);
2946 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002947 }
2948 break;
2949 }
Dan Gohmana1603612007-10-08 18:33:35 +00002950 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002951 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002952 case ISD::VAARG: {
2953 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2954 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2955
Chris Lattner364b89a2006-01-28 07:42:08 +00002956 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002957 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2958 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002959 case TargetLowering::Custom:
2960 isCustom = true;
2961 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002962 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002963 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2964 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002965 Tmp1 = Result.getValue(1);
2966
2967 if (isCustom) {
2968 Tmp2 = TLI.LowerOperation(Result, DAG);
2969 if (Tmp2.Val) {
2970 Result = LegalizeOp(Tmp2);
2971 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2972 }
2973 }
Nate Begemane74795c2006-01-25 18:21:52 +00002974 break;
2975 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002976 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00002977 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00002978 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002979 // Increment the pointer, VAList, to the next vaarg
2980 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2981 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2982 TLI.getPointerTy()));
2983 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00002984 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2985 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002986 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00002987 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002988 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002989 Result = LegalizeOp(Result);
2990 break;
2991 }
2992 }
2993 // Since VAARG produces two values, make sure to remember that we
2994 // legalized both of them.
2995 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002996 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2997 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002998 }
2999
3000 case ISD::VACOPY:
3001 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3002 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3003 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3004
3005 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3006 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003007 case TargetLowering::Custom:
3008 isCustom = true;
3009 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003010 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003011 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3012 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003013 if (isCustom) {
3014 Tmp1 = TLI.LowerOperation(Result, DAG);
3015 if (Tmp1.Val) Result = Tmp1;
3016 }
Nate Begemane74795c2006-01-25 18:21:52 +00003017 break;
3018 case TargetLowering::Expand:
3019 // This defaults to loading a pointer from the input and storing it to the
3020 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003021 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00003022 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00003023 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
3024 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00003025 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
3026 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00003027 break;
3028 }
3029 break;
3030
3031 case ISD::VAEND:
3032 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3033 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3034
3035 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3036 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003037 case TargetLowering::Custom:
3038 isCustom = true;
3039 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003040 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003041 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003042 if (isCustom) {
3043 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3044 if (Tmp1.Val) Result = Tmp1;
3045 }
Nate Begemane74795c2006-01-25 18:21:52 +00003046 break;
3047 case TargetLowering::Expand:
3048 Result = Tmp1; // Default to a no-op, return the chain
3049 break;
3050 }
3051 break;
3052
3053 case ISD::VASTART:
3054 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3055 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3056
Chris Lattnerd02b0542006-01-28 10:58:55 +00003057 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3058
Nate Begemane74795c2006-01-25 18:21:52 +00003059 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3060 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003061 case TargetLowering::Legal: break;
3062 case TargetLowering::Custom:
3063 Tmp1 = TLI.LowerOperation(Result, DAG);
3064 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00003065 break;
3066 }
3067 break;
3068
Nate Begeman1b8121b2006-01-11 21:21:00 +00003069 case ISD::ROTL:
3070 case ISD::ROTR:
3071 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3072 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00003073 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00003074 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3075 default:
3076 assert(0 && "ROTL/ROTR legalize operation not supported");
3077 break;
3078 case TargetLowering::Legal:
3079 break;
3080 case TargetLowering::Custom:
3081 Tmp1 = TLI.LowerOperation(Result, DAG);
3082 if (Tmp1.Val) Result = Tmp1;
3083 break;
3084 case TargetLowering::Promote:
3085 assert(0 && "Do not know how to promote ROTL/ROTR");
3086 break;
3087 case TargetLowering::Expand:
3088 assert(0 && "Do not know how to expand ROTL/ROTR");
3089 break;
3090 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00003091 break;
3092
Nate Begeman2fba8a32006-01-14 03:14:10 +00003093 case ISD::BSWAP:
3094 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3095 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003096 case TargetLowering::Custom:
3097 assert(0 && "Cannot custom legalize this yet!");
3098 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003099 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003100 break;
3101 case TargetLowering::Promote: {
3102 MVT::ValueType OVT = Tmp1.getValueType();
3103 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohman1796f1f2007-05-18 17:52:13 +00003104 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00003105
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003106 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3107 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3108 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3109 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3110 break;
3111 }
3112 case TargetLowering::Expand:
3113 Result = ExpandBSWAP(Tmp1);
3114 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003115 }
3116 break;
3117
Andrew Lenharth5e177822005-05-03 17:19:30 +00003118 case ISD::CTPOP:
3119 case ISD::CTTZ:
3120 case ISD::CTLZ:
3121 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3122 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00003123 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00003124 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003125 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00003126 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00003127 TargetLowering::Custom) {
3128 Tmp1 = TLI.LowerOperation(Result, DAG);
3129 if (Tmp1.Val) {
3130 Result = Tmp1;
3131 }
Scott Michel34e2d222007-07-30 21:00:31 +00003132 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003133 break;
3134 case TargetLowering::Promote: {
3135 MVT::ValueType OVT = Tmp1.getValueType();
3136 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00003137
3138 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00003139 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3140 // Perform the larger operation, then subtract if needed.
3141 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003142 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00003143 case ISD::CTPOP:
3144 Result = Tmp1;
3145 break;
3146 case ISD::CTTZ:
3147 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00003148 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003149 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00003150 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003151 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel34e2d222007-07-30 21:00:31 +00003152 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003153 break;
3154 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003155 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003156 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003157 DAG.getConstant(MVT::getSizeInBits(NVT) -
3158 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00003159 break;
3160 }
3161 break;
3162 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003163 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003164 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003165 break;
3166 }
3167 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003168
Chris Lattner13fe99c2005-04-02 05:00:07 +00003169 // Unary operators
3170 case ISD::FABS:
3171 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00003172 case ISD::FSQRT:
3173 case ISD::FSIN:
3174 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003175 Tmp1 = LegalizeOp(Node->getOperand(0));
3176 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003177 case TargetLowering::Promote:
3178 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00003179 isCustom = true;
3180 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00003181 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003182 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00003183 if (isCustom) {
3184 Tmp1 = TLI.LowerOperation(Result, DAG);
3185 if (Tmp1.Val) Result = Tmp1;
3186 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003187 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00003188 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003189 switch (Node->getOpcode()) {
3190 default: assert(0 && "Unreachable!");
3191 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003192 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3193 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003194 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00003195 break;
Chris Lattner80026402005-04-30 04:43:14 +00003196 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003197 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3198 MVT::ValueType VT = Node->getValueType(0);
3199 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003200 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003201 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3202 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00003203 break;
3204 }
3205 case ISD::FSQRT:
3206 case ISD::FSIN:
3207 case ISD::FCOS: {
3208 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman2a7de412007-10-11 23:57:53 +00003209
3210 // Expand unsupported unary vector operators by unrolling them.
3211 if (MVT::isVector(VT)) {
3212 Result = LegalizeOp(UnrollVectorOp(Op));
3213 break;
3214 }
3215
Evan Cheng31cbddf2007-01-12 02:11:51 +00003216 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00003217 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00003218 case ISD::FSQRT:
Dale Johannesen25a00a62007-09-28 01:08:20 +00003219 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00003220 VT == MVT::f64 ? RTLIB::SQRT_F64 :
3221 VT == MVT::f80 ? RTLIB::SQRT_F80 :
3222 VT == MVT::ppcf128 ? RTLIB::SQRT_PPCF128 :
3223 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003224 break;
3225 case ISD::FSIN:
3226 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3227 break;
3228 case ISD::FCOS:
3229 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3230 break;
Chris Lattner80026402005-04-30 04:43:14 +00003231 default: assert(0 && "Unreachable!");
3232 }
Nate Begeman77558da2005-08-04 21:43:28 +00003233 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003234 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3235 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00003236 break;
3237 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003238 }
3239 break;
3240 }
3241 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003242 case ISD::FPOWI: {
Dan Gohman2a7de412007-10-11 23:57:53 +00003243 MVT::ValueType VT = Node->getValueType(0);
3244
3245 // Expand unsupported unary vector operators by unrolling them.
3246 if (MVT::isVector(VT)) {
3247 Result = LegalizeOp(UnrollVectorOp(Op));
3248 break;
3249 }
3250
3251 // We always lower FPOWI into a libcall. No target support for it yet.
Dale Johannesen25a00a62007-09-28 01:08:20 +00003252 RTLIB::Libcall LC =
Dan Gohman2a7de412007-10-11 23:57:53 +00003253 VT == MVT::f32 ? RTLIB::POWI_F32 :
3254 VT == MVT::f64 ? RTLIB::POWI_F64 :
3255 VT == MVT::f80 ? RTLIB::POWI_F80 :
3256 VT == MVT::ppcf128 ? RTLIB::POWI_PPCF128 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00003257 RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003258 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003259 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3260 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003261 break;
3262 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003263 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003264 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00003265 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohmana8665142007-06-25 16:23:39 +00003266 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3267 // The input has to be a vector type, we have to either scalarize it, pack
3268 // it, or convert it based on whether the input vector type is legal.
3269 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00003270 int InIx = Node->getOperand(0).ResNo;
3271 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3272 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmana8665142007-06-25 16:23:39 +00003273
3274 // Figure out if there is a simple type corresponding to this Vector
3275 // type. If so, convert to the vector type.
3276 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3277 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003278 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003279 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3280 LegalizeOp(Node->getOperand(0)));
3281 break;
3282 } else if (NumElems == 1) {
3283 // Turn this into a bit convert of the scalar input.
3284 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3285 ScalarizeVectorOp(Node->getOperand(0)));
3286 break;
3287 } else {
3288 // FIXME: UNIMP! Store then reload
3289 assert(0 && "Cast from unsupported vector type not implemented yet!");
3290 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003291 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003292 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3293 Node->getOperand(0).getValueType())) {
3294 default: assert(0 && "Unknown operation action!");
3295 case TargetLowering::Expand:
3296 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3297 break;
3298 case TargetLowering::Legal:
3299 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003300 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003301 break;
3302 }
3303 }
3304 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00003305
Chris Lattner13fe99c2005-04-02 05:00:07 +00003306 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00003307 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003308 case ISD::UINT_TO_FP: {
3309 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00003310 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3311 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00003312 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003313 Node->getOperand(0).getValueType())) {
3314 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003315 case TargetLowering::Custom:
3316 isCustom = true;
3317 // FALLTHROUGH
3318 case TargetLowering::Legal:
3319 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003320 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003321 if (isCustom) {
3322 Tmp1 = TLI.LowerOperation(Result, DAG);
3323 if (Tmp1.Val) Result = Tmp1;
3324 }
3325 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003326 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00003327 Result = ExpandLegalINT_TO_FP(isSigned,
3328 LegalizeOp(Node->getOperand(0)),
3329 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003330 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003331 case TargetLowering::Promote:
3332 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3333 Node->getValueType(0),
3334 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003335 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00003336 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003337 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00003338 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003339 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3340 Node->getValueType(0), Node->getOperand(0));
3341 break;
3342 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003343 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003344 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003345 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3346 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003347 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003348 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3349 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003350 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003351 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3352 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003353 break;
3354 }
3355 break;
3356 }
3357 case ISD::TRUNCATE:
3358 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3359 case Legal:
3360 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003361 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003362 break;
3363 case Expand:
3364 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3365
3366 // Since the result is legal, we should just be able to truncate the low
3367 // part of the source.
3368 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3369 break;
3370 case Promote:
3371 Result = PromoteOp(Node->getOperand(0));
3372 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3373 break;
3374 }
3375 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003376
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003377 case ISD::FP_TO_SINT:
3378 case ISD::FP_TO_UINT:
3379 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3380 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00003381 Tmp1 = LegalizeOp(Node->getOperand(0));
3382
Chris Lattner44fe26f2005-07-29 00:11:56 +00003383 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3384 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003385 case TargetLowering::Custom:
3386 isCustom = true;
3387 // FALLTHROUGH
3388 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003389 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003390 if (isCustom) {
3391 Tmp1 = TLI.LowerOperation(Result, DAG);
3392 if (Tmp1.Val) Result = Tmp1;
3393 }
3394 break;
3395 case TargetLowering::Promote:
3396 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3397 Node->getOpcode() == ISD::FP_TO_SINT);
3398 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00003399 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00003400 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3401 SDOperand True, False;
3402 MVT::ValueType VT = Node->getOperand(0).getValueType();
3403 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenb59d25f2007-09-19 17:53:26 +00003404 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003405 const uint64_t zero[] = {0, 0};
3406 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3407 uint64_t x = 1ULL << ShiftAmt;
Neil Booth5f009732007-10-07 11:45:55 +00003408 (void)apf.convertFromZeroExtendedInteger
3409 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003410 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begeman36853ee2005-08-14 01:20:53 +00003411 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3412 Node->getOperand(0), Tmp2, ISD::SETLT);
3413 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3414 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00003415 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00003416 Tmp2));
3417 False = DAG.getNode(ISD::XOR, NVT, False,
3418 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003419 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3420 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00003421 } else {
3422 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3423 }
3424 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003425 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003426 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003427 case Expand: {
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003428 MVT::ValueType VT = Op.getValueType();
Dale Johannesen666323e2007-10-10 01:01:31 +00003429 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesen6472eb62007-10-11 23:32:15 +00003430 // Convert ppcf128 to i32
Dale Johannesen666323e2007-10-10 01:01:31 +00003431 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Dale Johannesen6472eb62007-10-11 23:32:15 +00003432 if (Node->getOpcode()==ISD::FP_TO_SINT)
3433 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
Dale Johannesen666323e2007-10-10 01:01:31 +00003434 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3435 (DAG.getNode(ISD::FP_ROUND_INREG,
3436 MVT::ppcf128, Node->getOperand(0),
3437 DAG.getValueType(MVT::f64)))));
Dale Johannesen6472eb62007-10-11 23:32:15 +00003438 else {
3439 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3440 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3441 Tmp2 = DAG.getConstantFP(apf, OVT);
3442 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3443 // FIXME: generated code sucks.
3444 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3445 DAG.getNode(ISD::ADD, MVT::i32,
3446 DAG.getNode(ISD::FP_TO_SINT, VT,
3447 DAG.getNode(ISD::FSUB, OVT,
3448 Node->getOperand(0), Tmp2)),
3449 DAG.getConstant(0x80000000, MVT::i32)),
3450 DAG.getNode(ISD::FP_TO_SINT, VT,
3451 Node->getOperand(0)),
3452 DAG.getCondCode(ISD::SETGE));
3453 }
Dale Johannesen666323e2007-10-10 01:01:31 +00003454 break;
3455 }
Dale Johannesen6472eb62007-10-11 23:32:15 +00003456 // Convert f32 / f64 to i32 / i64.
Evan Cheng31cbddf2007-01-12 02:11:51 +00003457 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003458 switch (Node->getOpcode()) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003459 case ISD::FP_TO_SINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003460 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003461 LC = (VT == MVT::i32)
3462 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003463 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003464 LC = (VT == MVT::i32)
3465 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003466 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003467 assert(VT == MVT::i64);
Dale Johannesenc0154c02007-10-05 20:04:43 +00003468 LC = RTLIB::FPTOSINT_F80_I64;
3469 }
3470 else if (OVT == MVT::ppcf128) {
3471 assert(VT == MVT::i64);
3472 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003473 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003474 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003475 }
3476 case ISD::FP_TO_UINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003477 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003478 LC = (VT == MVT::i32)
3479 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003480 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003481 LC = (VT == MVT::i32)
3482 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003483 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003484 LC = (VT == MVT::i32)
Dale Johannesenc0154c02007-10-05 20:04:43 +00003485 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3486 }
3487 else if (OVT == MVT::ppcf128) {
3488 assert(VT == MVT::i64);
3489 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003490 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003491 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003492 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003493 default: assert(0 && "Unreachable!");
3494 }
3495 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003496 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3497 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003498 break;
3499 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003500 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003501 Tmp1 = PromoteOp(Node->getOperand(0));
3502 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3503 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003504 break;
3505 }
3506 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003507
Dale Johannesenc339e452007-08-09 17:27:48 +00003508 case ISD::FP_EXTEND:
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003509 case ISD::FP_ROUND: {
3510 MVT::ValueType newVT = Op.getValueType();
3511 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3512 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesenf864ac92007-10-06 01:24:11 +00003513 if (Node->getOpcode() == ISD::FP_ROUND && oldVT == MVT::ppcf128) {
3514 SDOperand Lo, Hi;
3515 ExpandOp(Node->getOperand(0), Lo, Hi);
3516 if (newVT == MVT::f64)
3517 Result = Hi;
3518 else
3519 Result = DAG.getNode(ISD::FP_ROUND, newVT, Hi);
3520 break;
Dale Johannesenc339e452007-08-09 17:27:48 +00003521 } else {
Dale Johannesenf864ac92007-10-06 01:24:11 +00003522 // The only other way we can lower this is to turn it into a STORE,
3523 // LOAD pair, targetting a temporary location (a stack slot).
3524
3525 // NOTE: there is a choice here between constantly creating new stack
3526 // slots and always reusing the same one. We currently always create
3527 // new ones, as reuse may inhibit scheduling.
3528 MVT::ValueType slotVT =
3529 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3530 const Type *Ty = MVT::getTypeForValueType(slotVT);
Duncan Sands44b87212007-11-01 20:53:16 +00003531 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Dale Johannesenf864ac92007-10-06 01:24:11 +00003532 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3533 MachineFunction &MF = DAG.getMachineFunction();
3534 int SSFI =
3535 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3536 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3537 if (Node->getOpcode() == ISD::FP_EXTEND) {
3538 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3539 StackSlot, NULL, 0);
3540 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3541 Result, StackSlot, NULL, 0, oldVT);
3542 } else {
3543 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3544 StackSlot, NULL, 0, newVT);
Duncan Sands052c8432007-10-16 09:07:20 +00003545 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0);
Dale Johannesenf864ac92007-10-06 01:24:11 +00003546 }
3547 break;
Dale Johannesenc339e452007-08-09 17:27:48 +00003548 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003549 }
Dale Johannesena2b3c172007-07-03 00:53:03 +00003550 }
3551 // FALL THROUGH
Chris Lattner7753f172005-09-02 00:18:10 +00003552 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003553 case ISD::ZERO_EXTEND:
3554 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003555 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003556 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003557 case Legal:
3558 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003559 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003560 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003561 case Promote:
3562 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003563 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003564 Tmp1 = PromoteOp(Node->getOperand(0));
3565 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003566 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003567 case ISD::ZERO_EXTEND:
3568 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003569 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003570 Result = DAG.getZeroExtendInReg(Result,
3571 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003572 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003573 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003574 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003575 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003576 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003577 Result,
3578 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003579 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003580 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003581 Result = PromoteOp(Node->getOperand(0));
3582 if (Result.getValueType() != Op.getValueType())
3583 // Dynamically dead while we have only 2 FP types.
3584 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3585 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003586 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00003587 Result = PromoteOp(Node->getOperand(0));
3588 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3589 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003590 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003591 }
3592 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003593 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003594 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003595 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003596 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003597
3598 // If this operation is not supported, convert it to a shl/shr or load/store
3599 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003600 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3601 default: assert(0 && "This action not supported for this op yet!");
3602 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003603 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003604 break;
3605 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003606 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003607 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003608 // NOTE: we could fall back on load/store here too for targets without
3609 // SAR. However, it is doubtful that any exist.
3610 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3611 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003612 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003613 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3614 Node->getOperand(0), ShiftCst);
3615 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3616 Result, ShiftCst);
3617 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003618 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003619 // EXTLOAD pair, targetting a temporary location (a stack slot).
3620
3621 // NOTE: there is a choice here between constantly creating new stack
3622 // slots and always reusing the same one. We currently always create
3623 // new ones, as reuse may inhibit scheduling.
3624 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Duncan Sands44b87212007-11-01 20:53:16 +00003625 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
Chris Lattner945e4372007-02-14 05:52:17 +00003626 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner99222f72005-01-15 07:15:18 +00003627 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00003628 int SSFI =
Chris Lattner1deacd62007-04-28 06:42:38 +00003629 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner99222f72005-01-15 07:15:18 +00003630 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003631 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3632 StackSlot, NULL, 0, ExtraVT);
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003633 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003634 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00003635 } else {
3636 assert(0 && "Unknown op");
3637 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003638 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003639 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003640 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003641 }
Duncan Sands644f9172007-07-27 12:58:54 +00003642 case ISD::TRAMPOLINE: {
3643 SDOperand Ops[6];
3644 for (unsigned i = 0; i != 6; ++i)
3645 Ops[i] = LegalizeOp(Node->getOperand(i));
3646 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3647 // The only option for this node is to custom lower it.
3648 Result = TLI.LowerOperation(Result, DAG);
3649 assert(Result.Val && "Should always custom lower!");
Duncan Sands86e01192007-09-11 14:10:23 +00003650
3651 // Since trampoline produces two values, make sure to remember that we
3652 // legalized both of them.
3653 Tmp1 = LegalizeOp(Result.getValue(1));
3654 Result = LegalizeOp(Result);
3655 AddLegalizedOperand(SDOperand(Node, 0), Result);
3656 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3657 return Op.ResNo ? Tmp1 : Result;
Duncan Sands644f9172007-07-27 12:58:54 +00003658 }
Chris Lattner99222f72005-01-15 07:15:18 +00003659 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003660
Chris Lattner101ea662006-04-08 04:13:17 +00003661 assert(Result.getValueType() == Op.getValueType() &&
3662 "Bad legalization!");
3663
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003664 // Make sure that the generated code is itself legal.
3665 if (Result != Op)
3666 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003667
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003668 // Note that LegalizeOp may be reentered even from single-use nodes, which
3669 // means that we always must cache transformed nodes.
3670 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003671 return Result;
3672}
3673
Chris Lattner4d978642005-01-15 22:16:26 +00003674/// PromoteOp - Given an operation that produces a value in an invalid type,
3675/// promote it to compute the value into a larger type. The produced value will
3676/// have the correct bits for the low portion of the register, but no guarantee
3677/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003678SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3679 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003680 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003681 assert(getTypeAction(VT) == Promote &&
3682 "Caller should expand or legalize operands that are not promotable!");
3683 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3684 "Cannot promote to smaller type!");
3685
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003686 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003687 SDOperand Result;
3688 SDNode *Node = Op.Val;
3689
Chris Lattner4b0ddb22007-02-04 01:17:38 +00003690 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00003691 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003692
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003693 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003694 case ISD::CopyFromReg:
3695 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003696 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003697#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00003698 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003699#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003700 assert(0 && "Do not know how to promote this operator!");
3701 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003702 case ISD::UNDEF:
3703 Result = DAG.getNode(ISD::UNDEF, NVT);
3704 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003705 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003706 if (VT != MVT::i1)
3707 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3708 else
3709 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003710 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3711 break;
3712 case ISD::ConstantFP:
3713 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3714 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3715 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003716
Chris Lattner2cb338d2005-01-18 02:59:52 +00003717 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003718 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003719 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3720 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003721 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003722
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003723 case ISD::TRUNCATE:
3724 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3725 case Legal:
3726 Result = LegalizeOp(Node->getOperand(0));
3727 assert(Result.getValueType() >= NVT &&
3728 "This truncation doesn't make sense!");
3729 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3730 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3731 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003732 case Promote:
3733 // The truncation is not required, because we don't guarantee anything
3734 // about high bits anyway.
3735 Result = PromoteOp(Node->getOperand(0));
3736 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003737 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003738 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3739 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003740 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003741 }
3742 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003743 case ISD::SIGN_EXTEND:
3744 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003745 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003746 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3747 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3748 case Legal:
3749 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003750 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003751 break;
3752 case Promote:
3753 // Promote the reg if it's smaller.
3754 Result = PromoteOp(Node->getOperand(0));
3755 // The high bits are not guaranteed to be anything. Insert an extend.
3756 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003757 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003758 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003759 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003760 Result = DAG.getZeroExtendInReg(Result,
3761 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003762 break;
3763 }
3764 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003765 case ISD::BIT_CONVERT:
3766 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3767 Result = PromoteOp(Result);
3768 break;
3769
Chris Lattner4d978642005-01-15 22:16:26 +00003770 case ISD::FP_EXTEND:
3771 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3772 case ISD::FP_ROUND:
3773 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3774 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3775 case Promote: assert(0 && "Unreachable with 2 FP types!");
3776 case Legal:
3777 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003778 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003779 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003780 break;
3781 }
3782 break;
3783
3784 case ISD::SINT_TO_FP:
3785 case ISD::UINT_TO_FP:
3786 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3787 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003788 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003789 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003790 break;
3791
3792 case Promote:
3793 Result = PromoteOp(Node->getOperand(0));
3794 if (Node->getOpcode() == ISD::SINT_TO_FP)
3795 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003796 Result,
3797 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003798 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003799 Result = DAG.getZeroExtendInReg(Result,
3800 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003801 // No extra round required here.
3802 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003803 break;
3804 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003805 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3806 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003807 // Round if we cannot tolerate excess precision.
3808 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003809 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3810 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003811 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003812 }
Chris Lattner4d978642005-01-15 22:16:26 +00003813 break;
3814
Chris Lattner268d4572005-12-09 17:32:47 +00003815 case ISD::SIGN_EXTEND_INREG:
3816 Result = PromoteOp(Node->getOperand(0));
3817 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3818 Node->getOperand(1));
3819 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003820 case ISD::FP_TO_SINT:
3821 case ISD::FP_TO_UINT:
3822 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3823 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003824 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003825 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003826 break;
3827 case Promote:
3828 // The input result is prerounded, so we don't have to do anything
3829 // special.
3830 Tmp1 = PromoteOp(Node->getOperand(0));
3831 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003832 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003833 // If we're promoting a UINT to a larger size, check to see if the new node
3834 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3835 // we can use that instead. This allows us to generate better code for
3836 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3837 // legal, such as PowerPC.
3838 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003839 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003840 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3841 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003842 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3843 } else {
3844 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3845 }
Chris Lattner4d978642005-01-15 22:16:26 +00003846 break;
3847
Chris Lattner13fe99c2005-04-02 05:00:07 +00003848 case ISD::FABS:
3849 case ISD::FNEG:
3850 Tmp1 = PromoteOp(Node->getOperand(0));
3851 assert(Tmp1.getValueType() == NVT);
3852 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3853 // NOTE: we do not have to do any extra rounding here for
3854 // NoExcessFPPrecision, because we know the input will have the appropriate
3855 // precision, and these operations don't modify precision at all.
3856 break;
3857
Chris Lattner9d6fa982005-04-28 21:44:33 +00003858 case ISD::FSQRT:
3859 case ISD::FSIN:
3860 case ISD::FCOS:
3861 Tmp1 = PromoteOp(Node->getOperand(0));
3862 assert(Tmp1.getValueType() == NVT);
3863 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003864 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003865 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3866 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003867 break;
3868
Chris Lattnerca401aa2007-03-03 23:43:21 +00003869 case ISD::FPOWI: {
3870 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3871 // directly as well, which may be better.
3872 Tmp1 = PromoteOp(Node->getOperand(0));
3873 assert(Tmp1.getValueType() == NVT);
3874 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3875 if (NoExcessFPPrecision)
3876 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3877 DAG.getValueType(VT));
3878 break;
3879 }
3880
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003881 case ISD::AND:
3882 case ISD::OR:
3883 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003884 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003885 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003886 case ISD::MUL:
3887 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003888 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003889 // that too is okay if they are integer operations.
3890 Tmp1 = PromoteOp(Node->getOperand(0));
3891 Tmp2 = PromoteOp(Node->getOperand(1));
3892 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3893 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003894 break;
3895 case ISD::FADD:
3896 case ISD::FSUB:
3897 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003898 Tmp1 = PromoteOp(Node->getOperand(0));
3899 Tmp2 = PromoteOp(Node->getOperand(1));
3900 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3901 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3902
3903 // Floating point operations will give excess precision that we may not be
3904 // able to tolerate. If we DO allow excess precision, just leave it,
3905 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003906 // FIXME: Why would we need to round FP ops more than integer ones?
3907 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003908 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003909 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3910 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003911 break;
3912
Chris Lattner4d978642005-01-15 22:16:26 +00003913 case ISD::SDIV:
3914 case ISD::SREM:
3915 // These operators require that their input be sign extended.
3916 Tmp1 = PromoteOp(Node->getOperand(0));
3917 Tmp2 = PromoteOp(Node->getOperand(1));
3918 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003919 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3920 DAG.getValueType(VT));
3921 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3922 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003923 }
3924 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3925
3926 // Perform FP_ROUND: this is probably overly pessimistic.
3927 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003928 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3929 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003930 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003931 case ISD::FDIV:
3932 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003933 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003934 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00003935 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3936 case Legal:
3937 Tmp1 = LegalizeOp(Node->getOperand(0));
3938 break;
3939 case Promote:
3940 Tmp1 = PromoteOp(Node->getOperand(0));
3941 break;
3942 case Expand:
3943 assert(0 && "not implemented");
3944 }
3945 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3946 case Legal:
3947 Tmp2 = LegalizeOp(Node->getOperand(1));
3948 break;
3949 case Promote:
3950 Tmp2 = PromoteOp(Node->getOperand(1));
3951 break;
3952 case Expand:
3953 assert(0 && "not implemented");
3954 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00003955 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3956
3957 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003958 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003959 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3960 DAG.getValueType(VT));
3961 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003962
3963 case ISD::UDIV:
3964 case ISD::UREM:
3965 // These operators require that their input be zero extended.
3966 Tmp1 = PromoteOp(Node->getOperand(0));
3967 Tmp2 = PromoteOp(Node->getOperand(1));
3968 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003969 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3970 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003971 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3972 break;
3973
3974 case ISD::SHL:
3975 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003976 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003977 break;
3978 case ISD::SRA:
3979 // The input value must be properly sign extended.
3980 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003981 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3982 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003983 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003984 break;
3985 case ISD::SRL:
3986 // The input value must be properly zero extended.
3987 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003988 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003989 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003990 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003991
3992 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003993 Tmp1 = Node->getOperand(0); // Get the chain.
3994 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003995 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3996 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3997 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3998 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00003999 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00004000 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00004001 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00004002 // Increment the pointer, VAList, to the next vaarg
4003 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4004 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4005 TLI.getPointerTy()));
4006 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00004007 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
4008 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00004009 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00004010 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00004011 }
4012 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004013 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00004014 break;
4015
Evan Chenge71fe34d2006-10-09 20:57:25 +00004016 case ISD::LOAD: {
4017 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00004018 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4019 ? ISD::EXTLOAD : LD->getExtensionType();
4020 Result = DAG.getExtLoad(ExtType, NVT,
4021 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00004022 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00004023 LD->getLoadedVT(),
4024 LD->isVolatile(),
4025 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004026 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004027 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004028 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00004029 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004030 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004031 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4032 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004033 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004034 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00004035 case ISD::SELECT_CC:
4036 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4037 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4038 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004039 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004040 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00004041 case ISD::BSWAP:
4042 Tmp1 = Node->getOperand(0);
4043 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4044 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4045 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004046 DAG.getConstant(MVT::getSizeInBits(NVT) -
4047 MVT::getSizeInBits(VT),
Nate Begeman2fba8a32006-01-14 03:14:10 +00004048 TLI.getShiftAmountTy()));
4049 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004050 case ISD::CTPOP:
4051 case ISD::CTTZ:
4052 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004053 // Zero extend the argument
4054 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004055 // Perform the larger operation, then subtract if needed.
4056 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004057 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004058 case ISD::CTPOP:
4059 Result = Tmp1;
4060 break;
4061 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004062 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00004063 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004064 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4065 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004066 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004067 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004068 break;
4069 case ISD::CTLZ:
4070 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004071 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004072 DAG.getConstant(MVT::getSizeInBits(NVT) -
4073 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004074 break;
4075 }
4076 break;
Dan Gohmana8665142007-06-25 16:23:39 +00004077 case ISD::EXTRACT_SUBVECTOR:
4078 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00004079 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00004080 case ISD::EXTRACT_VECTOR_ELT:
4081 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4082 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004083 }
4084
4085 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004086
4087 // Make sure the result is itself legal.
4088 Result = LegalizeOp(Result);
4089
4090 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004091 AddPromotedOperand(Op, Result);
4092 return Result;
4093}
Chris Lattnerdc750592005-01-07 07:47:09 +00004094
Dan Gohmana8665142007-06-25 16:23:39 +00004095/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4096/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4097/// based on the vector type. The return type of this matches the element type
4098/// of the vector, which may not be legal for the target.
4099SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00004100 // We know that operand #0 is the Vec vector. If the index is a constant
4101 // or if the invec is a supported hardware type, we can use it. Otherwise,
4102 // lower to a store then an indexed load.
4103 SDOperand Vec = Op.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00004104 SDOperand Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00004105
Dan Gohman60028182007-09-24 15:54:53 +00004106 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +00004107 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner6f423252006-03-31 17:55:51 +00004108
Dan Gohmana8665142007-06-25 16:23:39 +00004109 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4110 default: assert(0 && "This action is not supported yet!");
4111 case TargetLowering::Custom: {
4112 Vec = LegalizeOp(Vec);
4113 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4114 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4115 if (Tmp3.Val)
4116 return Tmp3;
4117 break;
4118 }
4119 case TargetLowering::Legal:
4120 if (isTypeLegal(TVT)) {
4121 Vec = LegalizeOp(Vec);
4122 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00004123 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00004124 }
4125 break;
4126 case TargetLowering::Expand:
4127 break;
4128 }
4129
4130 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00004131 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004132 Op = ScalarizeVectorOp(Vec);
4133 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4134 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner6f423252006-03-31 17:55:51 +00004135 SDOperand Lo, Hi;
4136 SplitVectorOp(Vec, Lo, Hi);
4137 if (CIdx->getValue() < NumElems/2) {
4138 Vec = Lo;
4139 } else {
4140 Vec = Hi;
Dan Gohmana8665142007-06-25 16:23:39 +00004141 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4142 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00004143 }
Dan Gohmana8665142007-06-25 16:23:39 +00004144
Chris Lattner6f423252006-03-31 17:55:51 +00004145 // It's now an extract from the appropriate high or low part. Recurse.
4146 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004147 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00004148 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00004149 // Store the value to a temporary stack slot, then LOAD the scalar
4150 // element back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004151 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmana8665142007-06-25 16:23:39 +00004152 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4153
4154 // Add the offset to the index.
4155 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4156 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4157 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling070aca52007-10-18 08:32:37 +00004158
4159 if (MVT::getSizeInBits(Idx.getValueType()) >
4160 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattner064c31e2007-10-19 16:47:35 +00004161 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004162 else
Chris Lattner064c31e2007-10-19 16:47:35 +00004163 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004164
Dan Gohmana8665142007-06-25 16:23:39 +00004165 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4166
4167 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00004168 }
Dan Gohmana8665142007-06-25 16:23:39 +00004169 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00004170}
4171
Dan Gohmana8665142007-06-25 16:23:39 +00004172/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00004173/// we assume the operation can be split if it is not already legal.
Dan Gohmana8665142007-06-25 16:23:39 +00004174SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00004175 // We know that operand #0 is the Vec vector. For now we assume the index
4176 // is a constant and that the extracted result is a supported hardware type.
4177 SDOperand Vec = Op.getOperand(0);
4178 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4179
Dan Gohmana8665142007-06-25 16:23:39 +00004180 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00004181
4182 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4183 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004184 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00004185 }
4186
4187 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4188 SDOperand Lo, Hi;
4189 SplitVectorOp(Vec, Lo, Hi);
4190 if (CIdx->getValue() < NumElems/2) {
4191 Vec = Lo;
4192 } else {
4193 Vec = Hi;
4194 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4195 }
4196
4197 // It's now an extract from the appropriate high or low part. Recurse.
4198 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004199 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00004200}
4201
Nate Begeman7e7f4392006-02-01 07:19:44 +00004202/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4203/// with condition CC on the current target. This usually involves legalizing
4204/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4205/// there may be no choice but to create a new SetCC node to represent the
4206/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4207/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4208void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4209 SDOperand &RHS,
4210 SDOperand &CC) {
Dale Johannesenf864ac92007-10-06 01:24:11 +00004211 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman7e7f4392006-02-01 07:19:44 +00004212
4213 switch (getTypeAction(LHS.getValueType())) {
4214 case Legal:
4215 Tmp1 = LegalizeOp(LHS); // LHS
4216 Tmp2 = LegalizeOp(RHS); // RHS
4217 break;
4218 case Promote:
4219 Tmp1 = PromoteOp(LHS); // LHS
4220 Tmp2 = PromoteOp(RHS); // RHS
4221
4222 // If this is an FP compare, the operands have already been extended.
4223 if (MVT::isInteger(LHS.getValueType())) {
4224 MVT::ValueType VT = LHS.getValueType();
4225 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4226
4227 // Otherwise, we have to insert explicit sign or zero extends. Note
4228 // that we could insert sign extends for ALL conditions, but zero extend
4229 // is cheaper on many machines (an AND instead of two shifts), so prefer
4230 // it.
4231 switch (cast<CondCodeSDNode>(CC)->get()) {
4232 default: assert(0 && "Unknown integer comparison!");
4233 case ISD::SETEQ:
4234 case ISD::SETNE:
4235 case ISD::SETUGE:
4236 case ISD::SETUGT:
4237 case ISD::SETULE:
4238 case ISD::SETULT:
4239 // ALL of these operations will work if we either sign or zero extend
4240 // the operands (including the unsigned comparisons!). Zero extend is
4241 // usually a simpler/cheaper operation, so prefer it.
4242 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4243 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4244 break;
4245 case ISD::SETGE:
4246 case ISD::SETGT:
4247 case ISD::SETLT:
4248 case ISD::SETLE:
4249 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4250 DAG.getValueType(VT));
4251 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4252 DAG.getValueType(VT));
4253 break;
4254 }
4255 }
4256 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004257 case Expand: {
4258 MVT::ValueType VT = LHS.getValueType();
4259 if (VT == MVT::f32 || VT == MVT::f64) {
4260 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00004261 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004262 switch (cast<CondCodeSDNode>(CC)->get()) {
4263 case ISD::SETEQ:
4264 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004265 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004266 break;
4267 case ISD::SETNE:
4268 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004269 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004270 break;
4271 case ISD::SETGE:
4272 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004273 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004274 break;
4275 case ISD::SETLT:
4276 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004277 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004278 break;
4279 case ISD::SETLE:
4280 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004281 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004282 break;
4283 case ISD::SETGT:
4284 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004285 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004286 break;
4287 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00004288 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4289 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004290 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00004291 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004292 break;
4293 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004294 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004295 switch (cast<CondCodeSDNode>(CC)->get()) {
4296 case ISD::SETONE:
4297 // SETONE = SETOLT | SETOGT
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 // Fallthrough
4300 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004301 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004302 break;
4303 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004304 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004305 break;
4306 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004307 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004308 break;
4309 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004310 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004311 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004312 case ISD::SETUEQ:
4313 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004314 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004315 default: assert(0 && "Unsupported FP setcc!");
4316 }
4317 }
4318
4319 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004320 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00004321 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004322 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004323 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00004324 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00004325 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004326 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00004327 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00004328 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004329 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004330 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00004331 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004332 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4333 Tmp2 = SDOperand();
4334 }
4335 LHS = Tmp1;
4336 RHS = Tmp2;
4337 return;
4338 }
4339
Nate Begeman7e7f4392006-02-01 07:19:44 +00004340 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4341 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004342 ExpandOp(RHS, RHSLo, RHSHi);
4343 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4344
4345 if (VT==MVT::ppcf128) {
4346 // FIXME: This generated code sucks. We want to generate
4347 // FCMP crN, hi1, hi2
4348 // BNE crN, L:
4349 // FCMP crN, lo1, lo2
4350 // The following can be improved, but not that much.
4351 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4352 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4353 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4354 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4355 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4356 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4357 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4358 Tmp2 = SDOperand();
4359 break;
4360 }
4361
4362 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004363 case ISD::SETEQ:
4364 case ISD::SETNE:
4365 if (RHSLo == RHSHi)
4366 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4367 if (RHSCST->isAllOnesValue()) {
4368 // Comparison to -1.
4369 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4370 Tmp2 = RHSLo;
4371 break;
4372 }
4373
4374 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4375 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4376 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4377 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4378 break;
4379 default:
4380 // If this is a comparison of the sign bit, just look at the top part.
4381 // X > -1, x < 0
4382 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4383 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4384 CST->getValue() == 0) || // X < 0
4385 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4386 CST->isAllOnesValue())) { // X > -1
4387 Tmp1 = LHSHi;
4388 Tmp2 = RHSHi;
4389 break;
4390 }
4391
4392 // FIXME: This generated code sucks.
4393 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00004394 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004395 default: assert(0 && "Unknown integer setcc!");
4396 case ISD::SETLT:
4397 case ISD::SETULT: LowCC = ISD::SETULT; break;
4398 case ISD::SETGT:
4399 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4400 case ISD::SETLE:
4401 case ISD::SETULE: LowCC = ISD::SETULE; break;
4402 case ISD::SETGE:
4403 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4404 }
4405
4406 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4407 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4408 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4409
4410 // NOTE: on targets without efficient SELECT of bools, we can always use
4411 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00004412 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4413 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4414 false, DagCombineInfo);
4415 if (!Tmp1.Val)
4416 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4417 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4418 CCCode, false, DagCombineInfo);
4419 if (!Tmp2.Val)
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004420 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng93049452007-02-08 22:16:19 +00004421
4422 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4423 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4424 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4425 (Tmp2C && Tmp2C->getValue() == 0 &&
4426 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4427 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4428 (Tmp2C && Tmp2C->getValue() == 1 &&
4429 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4430 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4431 // low part is known false, returns high part.
4432 // For LE / GE, if high part is known false, ignore the low part.
4433 // For LT / GT, if high part is known true, ignore the low part.
4434 Tmp1 = Tmp2;
4435 Tmp2 = SDOperand();
4436 } else {
4437 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4438 ISD::SETEQ, false, DagCombineInfo);
4439 if (!Result.Val)
4440 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4441 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4442 Result, Tmp1, Tmp2));
4443 Tmp1 = Result;
4444 Tmp2 = SDOperand();
4445 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004446 }
4447 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004448 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004449 LHS = Tmp1;
4450 RHS = Tmp2;
4451}
4452
Chris Lattner36e663d2005-12-23 00:16:34 +00004453/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00004454/// The resultant code need not be legal. Note that SrcOp is the input operand
4455/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00004456SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4457 SDOperand SrcOp) {
4458 // Create the stack frame object.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004459 SDOperand FIPtr = DAG.CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004460
4461 // Emit a store to the stack slot.
Evan Chengab51cf22006-10-13 21:14:26 +00004462 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004463 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004464 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004465}
4466
Chris Lattner6be79822006-04-04 17:23:26 +00004467SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4468 // Create a vector sized/aligned stack slot, store the value to element #0,
4469 // then load the whole vector back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004470 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00004471 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00004472 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004473 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00004474}
4475
4476
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004477/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00004478/// support the operation, but do support the resultant vector type.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004479SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4480
4481 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00004482 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004483 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004484 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00004485 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004486 std::map<SDOperand, std::vector<unsigned> > Values;
4487 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004488 bool isConstant = true;
4489 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4490 SplatValue.getOpcode() != ISD::UNDEF)
4491 isConstant = false;
4492
Evan Cheng1d2e9952006-03-24 01:17:21 +00004493 for (unsigned i = 1; i < NumElems; ++i) {
4494 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00004495 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004496 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004497 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004498 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00004499 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004500
4501 // If this isn't a constant element or an undef, we can't use a constant
4502 // pool load.
4503 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4504 V.getOpcode() != ISD::UNDEF)
4505 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004506 }
4507
4508 if (isOnlyLowElement) {
4509 // If the low element is an undef too, then this whole things is an undef.
4510 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4511 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4512 // Otherwise, turn this into a scalar_to_vector node.
4513 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4514 Node->getOperand(0));
4515 }
4516
Chris Lattner77e271c2006-03-24 07:29:17 +00004517 // If all elements are constants, create a load from the constant pool.
4518 if (isConstant) {
4519 MVT::ValueType VT = Node->getValueType(0);
4520 const Type *OpNTy =
4521 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4522 std::vector<Constant*> CV;
4523 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4524 if (ConstantFPSDNode *V =
4525 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00004526 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004527 } else if (ConstantSDNode *V =
4528 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00004529 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004530 } else {
4531 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4532 CV.push_back(UndefValue::get(OpNTy));
4533 }
4534 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00004535 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00004536 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00004537 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004538 }
4539
Chris Lattner21e68c82006-03-20 01:52:29 +00004540 if (SplatValue.Val) { // Splat of one value?
4541 // Build the shuffle constant vector: <0, 0, 0, 0>
4542 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00004543 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00004544 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004545 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004546 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4547 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00004548
4549 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004550 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00004551 // Get the splatted value into the low element of a vector register.
4552 SDOperand LowValVec =
4553 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4554
4555 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4556 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4557 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4558 SplatMask);
4559 }
4560 }
4561
Evan Cheng1d2e9952006-03-24 01:17:21 +00004562 // If there are only two unique elements, we may be able to turn this into a
4563 // vector shuffle.
4564 if (Values.size() == 2) {
4565 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4566 MVT::ValueType MaskVT =
4567 MVT::getIntVectorWithNumElements(NumElems);
4568 std::vector<SDOperand> MaskVec(NumElems);
4569 unsigned i = 0;
4570 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4571 E = Values.end(); I != E; ++I) {
4572 for (std::vector<unsigned>::iterator II = I->second.begin(),
4573 EE = I->second.end(); II != EE; ++II)
Dan Gohman5c441312007-06-14 22:58:02 +00004574 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004575 i += NumElems;
4576 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004577 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4578 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004579
4580 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004581 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4582 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004583 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004584 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4585 E = Values.end(); I != E; ++I) {
4586 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4587 I->first);
4588 Ops.push_back(Op);
4589 }
4590 Ops.push_back(ShuffleMask);
4591
4592 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004593 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4594 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004595 }
4596 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004597
4598 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4599 // aligned object on the stack, store each element into it, then load
4600 // the result as a vector.
4601 MVT::ValueType VT = Node->getValueType(0);
4602 // Create the stack frame object.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004603 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004604
4605 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004606 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004607 unsigned TypeByteSize =
4608 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004609 // Store (in the right endianness) the elements to memory.
4610 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4611 // Ignore undef elements.
4612 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4613
Chris Lattner8fa445a2006-03-22 01:46:54 +00004614 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004615
4616 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4617 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4618
Evan Chengdf9ac472006-10-05 23:01:46 +00004619 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00004620 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004621 }
4622
4623 SDOperand StoreChain;
4624 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004625 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4626 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004627 else
4628 StoreChain = DAG.getEntryNode();
4629
4630 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004631 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004632}
4633
Chris Lattner4157c412005-04-02 04:00:59 +00004634void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4635 SDOperand Op, SDOperand Amt,
4636 SDOperand &Lo, SDOperand &Hi) {
4637 // Expand the subcomponents.
4638 SDOperand LHSL, LHSH;
4639 ExpandOp(Op, LHSL, LHSH);
4640
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004641 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00004642 MVT::ValueType VT = LHSL.getValueType();
4643 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00004644 Hi = Lo.getValue(1);
4645}
4646
4647
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004648/// ExpandShift - Try to find a clever way to expand this shift operation out to
4649/// smaller elements. If we can't find a way that is more efficient than a
4650/// libcall on this target, return false. Otherwise, return true with the
4651/// low-parts expanded into Lo and Hi.
4652bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4653 SDOperand &Lo, SDOperand &Hi) {
4654 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4655 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00004656
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004657 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00004658 SDOperand ShAmt = LegalizeOp(Amt);
4659 MVT::ValueType ShTy = ShAmt.getValueType();
4660 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4661 unsigned NVTBits = MVT::getSizeInBits(NVT);
4662
Chris Lattnerfbbe5702007-10-14 20:35:12 +00004663 // Handle the case when Amt is an immediate.
Nate Begemanb0674922005-04-06 21:13:14 +00004664 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4665 unsigned Cst = CN->getValue();
4666 // Expand the incoming operand to be shifted, so that we have its parts
4667 SDOperand InL, InH;
4668 ExpandOp(Op, InL, InH);
4669 switch(Opc) {
4670 case ISD::SHL:
4671 if (Cst > VTBits) {
4672 Lo = DAG.getConstant(0, NVT);
4673 Hi = DAG.getConstant(0, NVT);
4674 } else if (Cst > NVTBits) {
4675 Lo = DAG.getConstant(0, NVT);
4676 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004677 } else if (Cst == NVTBits) {
4678 Lo = DAG.getConstant(0, NVT);
4679 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00004680 } else {
4681 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4682 Hi = DAG.getNode(ISD::OR, NVT,
4683 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4684 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4685 }
4686 return true;
4687 case ISD::SRL:
4688 if (Cst > VTBits) {
4689 Lo = DAG.getConstant(0, NVT);
4690 Hi = DAG.getConstant(0, NVT);
4691 } else if (Cst > NVTBits) {
4692 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4693 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00004694 } else if (Cst == NVTBits) {
4695 Lo = InH;
4696 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00004697 } else {
4698 Lo = DAG.getNode(ISD::OR, NVT,
4699 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4700 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4701 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4702 }
4703 return true;
4704 case ISD::SRA:
4705 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004706 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004707 DAG.getConstant(NVTBits-1, ShTy));
4708 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004709 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004710 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004711 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004712 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004713 } else if (Cst == NVTBits) {
4714 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004715 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004716 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004717 } else {
4718 Lo = DAG.getNode(ISD::OR, NVT,
4719 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4720 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4721 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4722 }
4723 return true;
4724 }
4725 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004726
4727 // Okay, the shift amount isn't constant. However, if we can tell that it is
4728 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4729 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00004730 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00004731
4732 // If we know that the high bit of the shift amount is one, then we can do
4733 // this as a couple of simple shifts.
4734 if (KnownOne & Mask) {
4735 // Mask out the high bit, which we know is set.
4736 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4737 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4738
4739 // Expand the incoming operand to be shifted, so that we have its parts
4740 SDOperand InL, InH;
4741 ExpandOp(Op, InL, InH);
4742 switch(Opc) {
4743 case ISD::SHL:
4744 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4745 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4746 return true;
4747 case ISD::SRL:
4748 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4749 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4750 return true;
4751 case ISD::SRA:
4752 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4753 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4754 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4755 return true;
4756 }
4757 }
4758
4759 // If we know that the high bit of the shift amount is zero, then we can do
4760 // this as a couple of simple shifts.
4761 if (KnownZero & Mask) {
4762 // Compute 32-amt.
4763 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4764 DAG.getConstant(NVTBits, Amt.getValueType()),
4765 Amt);
4766
4767 // Expand the incoming operand to be shifted, so that we have its parts
4768 SDOperand InL, InH;
4769 ExpandOp(Op, InL, InH);
4770 switch(Opc) {
4771 case ISD::SHL:
4772 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4773 Hi = DAG.getNode(ISD::OR, NVT,
4774 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4775 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4776 return true;
4777 case ISD::SRL:
4778 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4779 Lo = DAG.getNode(ISD::OR, NVT,
4780 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4781 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4782 return true;
4783 case ISD::SRA:
4784 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4785 Lo = DAG.getNode(ISD::OR, NVT,
4786 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4787 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4788 return true;
4789 }
4790 }
4791
Nate Begemanb0674922005-04-06 21:13:14 +00004792 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004793}
Chris Lattneraac464e2005-01-21 06:05:23 +00004794
Chris Lattner4add7e32005-01-23 04:42:50 +00004795
Chris Lattneraac464e2005-01-21 06:05:23 +00004796// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4797// does not fit into a register, return the lo part and set the hi part to the
4798// by-reg argument. If it does fit into a single register, return the result
4799// and leave the Hi part unset.
4800SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004801 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004802 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4803 // The input chain to this libcall is the entry node of the function.
4804 // Legalizing the call will automatically add the previous call to the
4805 // dependence.
4806 SDOperand InChain = DAG.getEntryNode();
4807
Chris Lattneraac464e2005-01-21 06:05:23 +00004808 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004809 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004810 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4811 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4812 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004813 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00004814 Entry.isSExt = isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00004815 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004816 }
4817 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004818
Chris Lattner06bbeb62005-05-11 19:02:11 +00004819 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004820 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004821 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004822 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004823 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004824
Chris Lattner462505f2006-02-13 09:18:02 +00004825 // Legalize the call sequence, starting with the chain. This will advance
4826 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4827 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4828 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004829 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004830 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004831 default: assert(0 && "Unknown thing");
4832 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004833 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004834 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004835 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004836 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004837 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004838 }
Chris Lattner63022662005-09-02 20:26:58 +00004839 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004840}
4841
Chris Lattner4add7e32005-01-23 04:42:50 +00004842
Evan Chengbf535fc2007-04-27 07:33:31 +00004843/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4844///
Chris Lattneraac464e2005-01-21 06:05:23 +00004845SDOperand SelectionDAGLegalize::
4846ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004847 assert(getTypeAction(Source.getValueType()) == Expand &&
4848 "This is not an expansion!");
4849 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4850
Chris Lattner06bbeb62005-05-11 19:02:11 +00004851 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004852 assert(Source.getValueType() == MVT::i64 &&
4853 "This only works for 64-bit -> FP");
4854 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4855 // incoming integer is set. To handle this, we dynamically test to see if
4856 // it is set, and, if so, add a fudge factor.
4857 SDOperand Lo, Hi;
4858 ExpandOp(Source, Lo, Hi);
4859
Chris Lattner2a4f7312005-05-13 04:45:13 +00004860 // If this is unsigned, and not supported, first perform the conversion to
4861 // signed, then adjust the result if the sign bit is set.
4862 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4863 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4864
Chris Lattnerd47675e2005-08-09 20:20:18 +00004865 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4866 DAG.getConstant(0, Hi.getValueType()),
4867 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004868 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4869 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4870 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004871 uint64_t FF = 0x5f800000ULL;
4872 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004873 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004874
Chris Lattnerc30405e2005-08-26 17:15:30 +00004875 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004876 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4877 SDOperand FudgeInReg;
4878 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004879 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen7f724e92007-09-16 16:51:49 +00004880 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Chengbf535fc2007-04-27 07:33:31 +00004881 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen7f724e92007-09-16 16:51:49 +00004882 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00004883 CPIdx, NULL, 0, MVT::f32);
4884 else
4885 assert(0 && "Unexpected conversion");
4886
Evan Chengbf535fc2007-04-27 07:33:31 +00004887 MVT::ValueType SCVT = SignedConv.getValueType();
4888 if (SCVT != DestTy) {
4889 // Destination type needs to be expanded as well. The FADD now we are
4890 // constructing will be expanded into a libcall.
4891 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4892 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4893 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4894 SignedConv, SignedConv.getValue(1));
4895 }
4896 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4897 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00004898 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00004899 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00004900
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004901 // Check to see if the target has a custom way to lower this. If so, use it.
4902 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4903 default: assert(0 && "This action not implemented for this operation!");
4904 case TargetLowering::Legal:
4905 case TargetLowering::Expand:
4906 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004907 case TargetLowering::Custom: {
4908 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4909 Source), DAG);
4910 if (NV.Val)
4911 return LegalizeOp(NV);
4912 break; // The target decided this was legal after all
4913 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004914 }
4915
Chris Lattner153587e2005-05-12 07:00:44 +00004916 // Expand the source, then glue it back together for the call. We must expand
4917 // the source in case it is shared (this pass of legalize must traverse it).
4918 SDOperand SrcLo, SrcHi;
4919 ExpandOp(Source, SrcLo, SrcHi);
4920 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4921
Evan Cheng31cbddf2007-01-12 02:11:51 +00004922 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004923 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004924 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004925 else {
4926 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00004927 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004928 }
Chris Lattner462505f2006-02-13 09:18:02 +00004929
Evan Chengbf535fc2007-04-27 07:33:31 +00004930 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner462505f2006-02-13 09:18:02 +00004931 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4932 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004933 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4934 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00004935}
Misha Brukman835702a2005-04-21 22:36:52 +00004936
Chris Lattner689bdcc2006-01-28 08:25:58 +00004937/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4938/// INT_TO_FP operation of the specified operand when the target requests that
4939/// we expand it. At this point, we know that the result and operand types are
4940/// legal for the target.
4941SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4942 SDOperand Op0,
4943 MVT::ValueType DestVT) {
4944 if (Op0.getValueType() == MVT::i32) {
4945 // simple 32-bit [signed|unsigned] integer to float/double expansion
4946
Chris Lattner50ee0e42007-01-20 22:35:55 +00004947 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner689bdcc2006-01-28 08:25:58 +00004948 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner50ee0e42007-01-20 22:35:55 +00004949 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4950 unsigned StackAlign =
Chris Lattner945e4372007-02-14 05:52:17 +00004951 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004952 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004953 // get address of 8 byte buffer
4954 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4955 // word offset constant for Hi/Lo address computation
4956 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4957 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00004958 SDOperand Hi = StackSlot;
4959 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4960 if (TLI.isLittleEndian())
4961 std::swap(Hi, Lo);
4962
Chris Lattner689bdcc2006-01-28 08:25:58 +00004963 // if signed map to unsigned space
4964 SDOperand Op0Mapped;
4965 if (isSigned) {
4966 // constant used to invert sign bit (signed to unsigned mapping)
4967 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4968 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4969 } else {
4970 Op0Mapped = Op0;
4971 }
4972 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00004973 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00004974 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004975 // initial hi portion of constructed double
4976 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4977 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00004978 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004979 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00004980 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004981 // FP constant to bias correct the final result
4982 SDOperand Bias = DAG.getConstantFP(isSigned ?
4983 BitsToDouble(0x4330000080000000ULL)
4984 : BitsToDouble(0x4330000000000000ULL),
4985 MVT::f64);
4986 // subtract the bias
4987 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4988 // final result
4989 SDOperand Result;
4990 // handle final rounding
4991 if (DestVT == MVT::f64) {
4992 // do nothing
4993 Result = Sub;
Dale Johannesen7f724e92007-09-16 16:51:49 +00004994 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
4995 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
4996 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
4997 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004998 }
4999 return Result;
5000 }
5001 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5002 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5003
5004 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5005 DAG.getConstant(0, Op0.getValueType()),
5006 ISD::SETLT);
5007 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
5008 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5009 SignSet, Four, Zero);
5010
5011 // If the sign bit of the integer is set, the large number will be treated
5012 // as a negative number. To counteract this, the dynamic code adds an
5013 // offset depending on the data type.
5014 uint64_t FF;
5015 switch (Op0.getValueType()) {
5016 default: assert(0 && "Unsupported integer type!");
5017 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5018 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5019 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5020 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5021 }
5022 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00005023 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005024
5025 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5026 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5027 SDOperand FudgeInReg;
5028 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00005029 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005030 else {
Dale Johannesen7d67e542007-09-19 23:55:34 +00005031 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner689bdcc2006-01-28 08:25:58 +00005032 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00005033 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00005034 }
5035
5036 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5037}
5038
5039/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5040/// *INT_TO_FP operation of the specified operand when the target requests that
5041/// we promote it. At this point, we know that the result and operand types are
5042/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5043/// operation that takes a larger input.
5044SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5045 MVT::ValueType DestVT,
5046 bool isSigned) {
5047 // First step, figure out the appropriate *INT_TO_FP operation to use.
5048 MVT::ValueType NewInTy = LegalOp.getValueType();
5049
5050 unsigned OpToUse = 0;
5051
5052 // Scan for the appropriate larger type to use.
5053 while (1) {
5054 NewInTy = (MVT::ValueType)(NewInTy+1);
5055 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5056
5057 // If the target supports SINT_TO_FP of this type, use it.
5058 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5059 default: break;
5060 case TargetLowering::Legal:
5061 if (!TLI.isTypeLegal(NewInTy))
5062 break; // Can't use this datatype.
5063 // FALL THROUGH.
5064 case TargetLowering::Custom:
5065 OpToUse = ISD::SINT_TO_FP;
5066 break;
5067 }
5068 if (OpToUse) break;
5069 if (isSigned) continue;
5070
5071 // If the target supports UINT_TO_FP of this type, use it.
5072 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5073 default: break;
5074 case TargetLowering::Legal:
5075 if (!TLI.isTypeLegal(NewInTy))
5076 break; // Can't use this datatype.
5077 // FALL THROUGH.
5078 case TargetLowering::Custom:
5079 OpToUse = ISD::UINT_TO_FP;
5080 break;
5081 }
5082 if (OpToUse) break;
5083
5084 // Otherwise, try a larger type.
5085 }
5086
5087 // Okay, we found the operation and type to use. Zero extend our input to the
5088 // desired type then run the operation on it.
5089 return DAG.getNode(OpToUse, DestVT,
5090 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5091 NewInTy, LegalOp));
5092}
5093
5094/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5095/// FP_TO_*INT operation of the specified operand when the target requests that
5096/// we promote it. At this point, we know that the result and operand types are
5097/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5098/// operation that returns a larger result.
5099SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5100 MVT::ValueType DestVT,
5101 bool isSigned) {
5102 // First step, figure out the appropriate FP_TO*INT operation to use.
5103 MVT::ValueType NewOutTy = DestVT;
5104
5105 unsigned OpToUse = 0;
5106
5107 // Scan for the appropriate larger type to use.
5108 while (1) {
5109 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5110 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5111
5112 // If the target supports FP_TO_SINT returning this type, use it.
5113 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5114 default: break;
5115 case TargetLowering::Legal:
5116 if (!TLI.isTypeLegal(NewOutTy))
5117 break; // Can't use this datatype.
5118 // FALL THROUGH.
5119 case TargetLowering::Custom:
5120 OpToUse = ISD::FP_TO_SINT;
5121 break;
5122 }
5123 if (OpToUse) break;
5124
5125 // If the target supports FP_TO_UINT of this type, use it.
5126 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5127 default: break;
5128 case TargetLowering::Legal:
5129 if (!TLI.isTypeLegal(NewOutTy))
5130 break; // Can't use this datatype.
5131 // FALL THROUGH.
5132 case TargetLowering::Custom:
5133 OpToUse = ISD::FP_TO_UINT;
5134 break;
5135 }
5136 if (OpToUse) break;
5137
5138 // Otherwise, try a larger type.
5139 }
5140
5141 // Okay, we found the operation and type to use. Truncate the result of the
5142 // extended FP_TO_*INT operation to the desired size.
5143 return DAG.getNode(ISD::TRUNCATE, DestVT,
5144 DAG.getNode(OpToUse, NewOutTy, LegalOp));
5145}
5146
5147/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5148///
5149SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5150 MVT::ValueType VT = Op.getValueType();
5151 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5152 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5153 switch (VT) {
5154 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5155 case MVT::i16:
5156 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5157 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5158 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5159 case MVT::i32:
5160 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5161 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5162 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5163 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5164 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5165 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5166 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5167 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5168 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5169 case MVT::i64:
5170 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5171 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5172 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5173 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5174 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5175 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5176 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5177 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5178 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5179 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5180 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5181 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5182 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5183 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5184 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5185 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5186 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5187 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5188 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5189 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5190 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5191 }
5192}
5193
5194/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5195///
5196SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5197 switch (Opc) {
5198 default: assert(0 && "Cannot expand this yet!");
5199 case ISD::CTPOP: {
5200 static const uint64_t mask[6] = {
5201 0x5555555555555555ULL, 0x3333333333333333ULL,
5202 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5203 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5204 };
5205 MVT::ValueType VT = Op.getValueType();
5206 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005207 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005208 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5209 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5210 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5211 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5212 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5213 DAG.getNode(ISD::AND, VT,
5214 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5215 }
5216 return Op;
5217 }
5218 case ISD::CTLZ: {
5219 // for now, we do this:
5220 // x = x | (x >> 1);
5221 // x = x | (x >> 2);
5222 // ...
5223 // x = x | (x >>16);
5224 // x = x | (x >>32); // for 64-bit input
5225 // return popcount(~x);
5226 //
5227 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5228 MVT::ValueType VT = Op.getValueType();
5229 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005230 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005231 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5232 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5233 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5234 }
5235 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5236 return DAG.getNode(ISD::CTPOP, VT, Op);
5237 }
5238 case ISD::CTTZ: {
5239 // for now, we use: { return popcount(~x & (x - 1)); }
5240 // unless the target has ctlz but not ctpop, in which case we use:
5241 // { return 32 - nlz(~x & (x-1)); }
5242 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5243 MVT::ValueType VT = Op.getValueType();
5244 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5245 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5246 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5247 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5248 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5249 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5250 TLI.isOperationLegal(ISD::CTLZ, VT))
5251 return DAG.getNode(ISD::SUB, VT,
Dan Gohman1796f1f2007-05-18 17:52:13 +00005252 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00005253 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5254 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5255 }
5256 }
5257}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005258
Chris Lattnerdc750592005-01-07 07:47:09 +00005259/// ExpandOp - Expand the specified SDOperand into its two component pieces
5260/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5261/// LegalizeNodes map is filled in for any results that are not expanded, the
5262/// ExpandedNodes map is filled in for any results that are expanded, and the
5263/// Lo/Hi values are returned.
5264void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5265 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00005266 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00005267 SDNode *Node = Op.Val;
5268 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00005269 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohmana8665142007-06-25 16:23:39 +00005270 MVT::isVector(VT)) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00005271 "Cannot expand to FP value or to larger int value!");
5272
Chris Lattner1a570f12005-09-02 20:32:45 +00005273 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005274 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00005275 = ExpandedNodes.find(Op);
5276 if (I != ExpandedNodes.end()) {
5277 Lo = I->second.first;
5278 Hi = I->second.second;
5279 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00005280 }
5281
Chris Lattnerdc750592005-01-07 07:47:09 +00005282 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00005283 case ISD::CopyFromReg:
5284 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen666323e2007-10-10 01:01:31 +00005285 case ISD::FP_ROUND_INREG:
5286 if (VT == MVT::ppcf128 &&
5287 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5288 TargetLowering::Custom) {
Dale Johannesen6472eb62007-10-11 23:32:15 +00005289 SDOperand SrcLo, SrcHi, Src;
5290 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5291 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5292 SDOperand Result = TLI.LowerOperation(
5293 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen666323e2007-10-10 01:01:31 +00005294 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5295 Lo = Result.Val->getOperand(0);
5296 Hi = Result.Val->getOperand(1);
5297 break;
5298 }
5299 // fall through
Chris Lattner44cab002006-01-21 04:27:00 +00005300 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005301#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005302 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005303#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00005304 assert(0 && "Do not know how to expand this operator!");
5305 abort();
Dale Johannesenb066c1f2007-10-31 00:32:36 +00005306 case ISD::EXTRACT_VECTOR_ELT:
5307 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5308 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5309 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5310 return ExpandOp(Lo, Lo, Hi);
Nate Begemancda9aa72005-04-01 22:34:39 +00005311 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00005312 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00005313 Lo = DAG.getNode(ISD::UNDEF, NVT);
5314 Hi = DAG.getNode(ISD::UNDEF, NVT);
5315 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005316 case ISD::Constant: {
5317 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5318 Lo = DAG.getConstant(Cst, NVT);
5319 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5320 break;
5321 }
Evan Cheng47833a12006-12-12 21:32:44 +00005322 case ISD::ConstantFP: {
5323 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen007aa372007-10-11 18:07:22 +00005324 if (CFP->getValueType(0) == MVT::ppcf128) {
5325 APInt api = CFP->getValueAPF().convertToAPInt();
5326 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5327 MVT::f64);
5328 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5329 MVT::f64);
5330 break;
5331 }
Evan Cheng3766fc602006-12-12 22:19:28 +00005332 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00005333 if (getTypeAction(Lo.getValueType()) == Expand)
5334 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005335 break;
5336 }
Chris Lattner32e08b72005-03-28 22:03:13 +00005337 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005338 // Return the operands.
5339 Lo = Node->getOperand(0);
5340 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00005341 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005342
5343 case ISD::SIGN_EXTEND_INREG:
5344 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00005345 // sext_inreg the low part if needed.
5346 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5347
5348 // The high part gets the sign extension from the lo-part. This handles
5349 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005350 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5351 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5352 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005353 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00005354
Nate Begeman2fba8a32006-01-14 03:14:10 +00005355 case ISD::BSWAP: {
5356 ExpandOp(Node->getOperand(0), Lo, Hi);
5357 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5358 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5359 Lo = TempLo;
5360 break;
5361 }
5362
Chris Lattner55e9cde2005-05-11 04:51:16 +00005363 case ISD::CTPOP:
5364 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00005365 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5366 DAG.getNode(ISD::CTPOP, NVT, Lo),
5367 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00005368 Hi = DAG.getConstant(0, NVT);
5369 break;
5370
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005371 case ISD::CTLZ: {
5372 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005373 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005374 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5375 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005376 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5377 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005378 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5379 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5380
5381 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5382 Hi = DAG.getConstant(0, NVT);
5383 break;
5384 }
5385
5386 case ISD::CTTZ: {
5387 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005388 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005389 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5390 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005391 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5392 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005393 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5394 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5395
5396 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5397 Hi = DAG.getConstant(0, NVT);
5398 break;
5399 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00005400
Nate Begemane74795c2006-01-25 18:21:52 +00005401 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005402 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5403 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00005404 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5405 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5406
5407 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005408 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00005409 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5410 if (!TLI.isLittleEndian())
5411 std::swap(Lo, Hi);
5412 break;
5413 }
5414
Chris Lattnerdc750592005-01-07 07:47:09 +00005415 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005416 LoadSDNode *LD = cast<LoadSDNode>(Node);
5417 SDOperand Ch = LD->getChain(); // Legalize the chain.
5418 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5419 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman2af30632007-07-09 22:18:38 +00005420 int SVOffset = LD->getSrcValueOffset();
5421 unsigned Alignment = LD->getAlignment();
5422 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00005423
Evan Chenge71fe34d2006-10-09 20:57:25 +00005424 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman2af30632007-07-09 22:18:38 +00005425 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5426 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00005427 if (VT == MVT::f32 || VT == MVT::f64) {
5428 // f32->i32 or f64->i64 one to one expansion.
5429 // Remember that we legalized the chain.
5430 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00005431 // Recursively expand the new load.
5432 if (getTypeAction(NVT) == Expand)
5433 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005434 break;
5435 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00005436
Evan Chenge71fe34d2006-10-09 20:57:25 +00005437 // Increment the pointer to the other half.
5438 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5439 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5440 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005441 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00005442 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman2af30632007-07-09 22:18:38 +00005443 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5444 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00005445
Evan Chenge71fe34d2006-10-09 20:57:25 +00005446 // Build a factor node to remember that this load is independent of the
5447 // other one.
5448 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5449 Hi.getValue(1));
5450
5451 // Remember that we legalized the chain.
5452 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5453 if (!TLI.isLittleEndian())
5454 std::swap(Lo, Hi);
5455 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00005456 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00005457
Dale Johannesen6802d0c2007-10-19 20:29:00 +00005458 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5459 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Chenge370e0e2006-12-13 03:19:57 +00005460 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5461 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005462 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00005463 // Remember that we legalized the chain.
5464 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5465 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5466 break;
5467 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00005468
5469 if (EVT == NVT)
5470 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005471 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005472 else
5473 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005474 SVOffset, EVT, isVolatile,
5475 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005476
5477 // Remember that we legalized the chain.
5478 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5479
5480 if (ExtType == ISD::SEXTLOAD) {
5481 // The high part is obtained by SRA'ing all but one of the bits of the
5482 // lo part.
5483 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5484 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5485 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5486 } else if (ExtType == ISD::ZEXTLOAD) {
5487 // The high part is just a zero.
5488 Hi = DAG.getConstant(0, NVT);
5489 } else /* if (ExtType == ISD::EXTLOAD) */ {
5490 // The high part is undefined.
5491 Hi = DAG.getNode(ISD::UNDEF, NVT);
5492 }
5493 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005494 break;
5495 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005496 case ISD::AND:
5497 case ISD::OR:
5498 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5499 SDOperand LL, LH, RL, RH;
5500 ExpandOp(Node->getOperand(0), LL, LH);
5501 ExpandOp(Node->getOperand(1), RL, RH);
5502 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5503 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5504 break;
5505 }
5506 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005507 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00005508 ExpandOp(Node->getOperand(1), LL, LH);
5509 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00005510 if (getTypeAction(NVT) == Expand)
5511 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005512 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00005513 if (VT != MVT::f32)
5514 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00005515 break;
5516 }
Nate Begemane5b86d72005-08-10 20:51:12 +00005517 case ISD::SELECT_CC: {
5518 SDOperand TL, TH, FL, FH;
5519 ExpandOp(Node->getOperand(2), TL, TH);
5520 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00005521 if (getTypeAction(NVT) == Expand)
5522 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00005523 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5524 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00005525 if (VT != MVT::f32)
5526 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5527 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00005528 break;
5529 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005530 case ISD::ANY_EXTEND:
5531 // The low part is any extension of the input (which degenerates to a copy).
5532 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5533 // The high part is undefined.
5534 Hi = DAG.getNode(ISD::UNDEF, NVT);
5535 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005536 case ISD::SIGN_EXTEND: {
5537 // The low part is just a sign extension of the input (which degenerates to
5538 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005539 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005540
Chris Lattnerdc750592005-01-07 07:47:09 +00005541 // The high part is obtained by SRA'ing all but one of the bits of the lo
5542 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00005543 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005544 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5545 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00005546 break;
5547 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005548 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00005549 // The low part is just a zero extension of the input (which degenerates to
5550 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005551 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005552
Chris Lattnerdc750592005-01-07 07:47:09 +00005553 // The high part is just a zero.
5554 Hi = DAG.getConstant(0, NVT);
5555 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00005556
Chris Lattner59b27fa372007-02-13 23:55:16 +00005557 case ISD::TRUNCATE: {
5558 // The input value must be larger than this value. Expand *it*.
5559 SDOperand NewLo;
5560 ExpandOp(Node->getOperand(0), NewLo, Hi);
5561
5562 // The low part is now either the right size, or it is closer. If not the
5563 // right size, make an illegal truncate so we recursively expand it.
5564 if (NewLo.getValueType() != Node->getValueType(0))
5565 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5566 ExpandOp(NewLo, Lo, Hi);
5567 break;
5568 }
5569
Chris Lattner36e663d2005-12-23 00:16:34 +00005570 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005571 SDOperand Tmp;
5572 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5573 // If the target wants to, allow it to lower this itself.
5574 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5575 case Expand: assert(0 && "cannot expand FP!");
5576 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5577 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5578 }
5579 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5580 }
5581
Evan Cheng4eee7242006-12-09 02:42:38 +00005582 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00005583 if (VT == MVT::f32 || VT == MVT::f64) {
5584 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00005585 if (getTypeAction(NVT) == Expand)
5586 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00005587 break;
5588 }
5589
5590 // If source operand will be expanded to the same type as VT, i.e.
5591 // i64 <- f64, i32 <- f32, expand the source operand instead.
5592 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5593 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5594 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005595 break;
5596 }
5597
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005598 // Turn this into a load/store pair by default.
5599 if (Tmp.Val == 0)
Evan Cheng3432ab92006-12-11 19:27:14 +00005600 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005601
Chris Lattner36e663d2005-12-23 00:16:34 +00005602 ExpandOp(Tmp, Lo, Hi);
5603 break;
5604 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005605
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005606 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00005607 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5608 TargetLowering::Custom &&
5609 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005610 Lo = TLI.LowerOperation(Op, DAG);
5611 assert(Lo.Val && "Node must be custom expanded!");
5612 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00005613 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005614 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005615 break;
5616
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005617 // These operators cannot be expanded directly, emit them as calls to
5618 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005619 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005620 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00005621 SDOperand Op;
5622 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5623 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005624 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5625 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00005626 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005627
Chris Lattner941d84a2005-07-30 01:40:57 +00005628 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5629
Chris Lattnerfe68d752005-07-29 00:33:32 +00005630 // Now that the custom expander is done, expand the result, which is still
5631 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005632 if (Op.Val) {
5633 ExpandOp(Op, Lo, Hi);
5634 break;
5635 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005636 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005637
Dale Johannesenc0154c02007-10-05 20:04:43 +00005638 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005639 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005640 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00005641 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005642 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00005643 else if (Node->getOperand(0).getValueType() == MVT::f80)
5644 LC = RTLIB::FPTOSINT_F80_I64;
5645 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5646 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005647 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5648 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005649 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005650 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005651
Evan Cheng31cbddf2007-01-12 02:11:51 +00005652 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005653 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005654 SDOperand Op;
5655 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5656 case Expand: assert(0 && "cannot expand FP!");
5657 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5658 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5659 }
5660
5661 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5662
5663 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005664 if (Op.Val) {
5665 ExpandOp(Op, Lo, Hi);
5666 break;
5667 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005668 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005669
Evan Chengfd11ef42007-10-05 01:09:32 +00005670 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005671 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005672 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen789b5a52007-09-28 18:44:17 +00005673 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005674 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00005675 else if (Node->getOperand(0).getValueType() == MVT::f80)
5676 LC = RTLIB::FPTOUINT_F80_I64;
5677 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5678 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005679 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5680 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005681 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005682 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005683
Evan Cheng870e4f82006-01-09 18:31:59 +00005684 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005685 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005686 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005687 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005688 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005689 Op = TLI.LowerOperation(Op, DAG);
5690 if (Op.Val) {
5691 // Now that the custom expander is done, expand the result, which is
5692 // still VT.
5693 ExpandOp(Op, Lo, Hi);
5694 break;
5695 }
5696 }
5697
Chris Lattner72b503b2006-09-13 03:50:39 +00005698 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5699 // this X << 1 as X+X.
5700 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5701 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5702 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5703 SDOperand LoOps[2], HiOps[3];
5704 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5705 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5706 LoOps[1] = LoOps[0];
5707 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5708
5709 HiOps[1] = HiOps[0];
5710 HiOps[2] = Lo.getValue(1);
5711 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5712 break;
5713 }
5714 }
5715
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005716 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005717 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005718 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005719
5720 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005721 TargetLowering::LegalizeAction Action =
5722 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5723 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5724 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005725 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005726 break;
5727 }
5728
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005729 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005730 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5731 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005732 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005733 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005734
Evan Cheng870e4f82006-01-09 18:31:59 +00005735 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005736 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005737 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005738 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005739 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005740 Op = TLI.LowerOperation(Op, DAG);
5741 if (Op.Val) {
5742 // Now that the custom expander is done, expand the result, which is
5743 // still VT.
5744 ExpandOp(Op, Lo, Hi);
5745 break;
5746 }
5747 }
5748
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005749 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005750 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005751 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005752
5753 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005754 TargetLowering::LegalizeAction Action =
5755 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5756 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5757 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005758 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005759 break;
5760 }
5761
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005762 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005763 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5764 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005765 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005766 }
5767
5768 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005769 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005770 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005771 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005772 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005773 Op = TLI.LowerOperation(Op, DAG);
5774 if (Op.Val) {
5775 // Now that the custom expander is done, expand the result, which is
5776 // still VT.
5777 ExpandOp(Op, Lo, Hi);
5778 break;
5779 }
5780 }
5781
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005782 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005783 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005784 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005785
5786 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005787 TargetLowering::LegalizeAction Action =
5788 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5789 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5790 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005791 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005792 break;
5793 }
5794
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005795 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005796 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5797 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005798 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005799 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005800
Misha Brukman835702a2005-04-21 22:36:52 +00005801 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005802 case ISD::SUB: {
5803 // If the target wants to custom expand this, let them.
5804 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5805 TargetLowering::Custom) {
5806 Op = TLI.LowerOperation(Op, DAG);
5807 if (Op.Val) {
5808 ExpandOp(Op, Lo, Hi);
5809 break;
5810 }
5811 }
5812
5813 // Expand the subcomponents.
5814 SDOperand LHSL, LHSH, RHSL, RHSH;
5815 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5816 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005817 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5818 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005819 LoOps[0] = LHSL;
5820 LoOps[1] = RHSL;
5821 HiOps[0] = LHSH;
5822 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005823 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005824 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005825 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005826 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005827 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005828 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005829 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005830 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005831 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005832 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005833 }
Chris Lattner2135bc02007-05-17 18:15:41 +00005834
5835 case ISD::ADDC:
5836 case ISD::SUBC: {
5837 // Expand the subcomponents.
5838 SDOperand LHSL, LHSH, RHSL, RHSH;
5839 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5840 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5841 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5842 SDOperand LoOps[2] = { LHSL, RHSL };
5843 SDOperand HiOps[3] = { LHSH, RHSH };
5844
5845 if (Node->getOpcode() == ISD::ADDC) {
5846 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5847 HiOps[2] = Lo.getValue(1);
5848 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5849 } else {
5850 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5851 HiOps[2] = Lo.getValue(1);
5852 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5853 }
5854 // Remember that we legalized the flag.
5855 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5856 break;
5857 }
5858 case ISD::ADDE:
5859 case ISD::SUBE: {
5860 // Expand the subcomponents.
5861 SDOperand LHSL, LHSH, RHSL, RHSH;
5862 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5863 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5864 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5865 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5866 SDOperand HiOps[3] = { LHSH, RHSH };
5867
5868 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5869 HiOps[2] = Lo.getValue(1);
5870 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5871
5872 // Remember that we legalized the flag.
5873 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5874 break;
5875 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005876 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005877 // If the target wants to custom expand this, let them.
5878 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00005879 SDOperand New = TLI.LowerOperation(Op, DAG);
5880 if (New.Val) {
5881 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005882 break;
5883 }
5884 }
5885
Evan Chenge93762d2006-09-01 18:17:58 +00005886 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5887 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohmana1603612007-10-08 18:33:35 +00005888 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
5889 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
5890 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanadd0c632005-04-11 03:01:51 +00005891 SDOperand LL, LH, RL, RH;
5892 ExpandOp(Node->getOperand(0), LL, LH);
5893 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohmana1603612007-10-08 18:33:35 +00005894 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
5895 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
5896 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
5897 // FIXME: generalize this to handle other bit sizes
5898 if (LHSSB == 32 && RHSSB == 32 &&
5899 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
5900 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
5901 // The inputs are both zero-extended.
5902 if (HasUMUL_LOHI) {
5903 // We can emit a umul_lohi.
5904 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5905 Hi = SDOperand(Lo.Val, 1);
5906 break;
5907 }
5908 if (HasMULHU) {
5909 // We can emit a mulhu+mul.
5910 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5911 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5912 break;
5913 }
Dan Gohmana1603612007-10-08 18:33:35 +00005914 }
5915 if (LHSSB > BitSize && RHSSB > BitSize) {
5916 // The input values are both sign-extended.
5917 if (HasSMUL_LOHI) {
5918 // We can emit a smul_lohi.
5919 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5920 Hi = SDOperand(Lo.Val, 1);
5921 break;
5922 }
5923 if (HasMULHS) {
5924 // We can emit a mulhs+mul.
5925 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5926 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
5927 break;
5928 }
5929 }
5930 if (HasUMUL_LOHI) {
5931 // Lo,Hi = umul LHS, RHS.
5932 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
5933 DAG.getVTList(NVT, NVT), LL, RL);
5934 Lo = UMulLOHI;
5935 Hi = UMulLOHI.getValue(1);
Nate Begeman43144a22005-08-30 02:44:00 +00005936 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5937 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5938 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5939 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00005940 break;
Nate Begeman43144a22005-08-30 02:44:00 +00005941 }
Dale Johannesena4a972e2007-10-24 22:26:08 +00005942 if (HasMULHU) {
5943 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5944 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5945 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5946 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5947 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5948 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
5949 break;
5950 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005951 }
Evan Chenge93762d2006-09-01 18:17:58 +00005952
Dan Gohmana1603612007-10-08 18:33:35 +00005953 // If nothing else, we can make a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005954 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5955 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00005956 break;
5957 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005958 case ISD::SDIV:
5959 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5960 break;
5961 case ISD::UDIV:
5962 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5963 break;
5964 case ISD::SREM:
5965 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5966 break;
5967 case ISD::UREM:
5968 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5969 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00005970
Evan Cheng4eee7242006-12-09 02:42:38 +00005971 case ISD::FADD:
Dale Johannesenc0154c02007-10-05 20:04:43 +00005972 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::ADD_F32 :
5973 VT == MVT::f64 ? RTLIB::ADD_F64 :
5974 VT == MVT::ppcf128 ?
5975 RTLIB::ADD_PPCF128 :
5976 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00005977 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005978 break;
5979 case ISD::FSUB:
Dale Johannesenc0154c02007-10-05 20:04:43 +00005980 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::SUB_F32 :
5981 VT == MVT::f64 ? RTLIB::SUB_F64 :
5982 VT == MVT::ppcf128 ?
5983 RTLIB::SUB_PPCF128 :
5984 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00005985 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005986 break;
5987 case ISD::FMUL:
Dale Johannesenc0154c02007-10-05 20:04:43 +00005988 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::MUL_F32 :
5989 VT == MVT::f64 ? RTLIB::MUL_F64 :
5990 VT == MVT::ppcf128 ?
5991 RTLIB::MUL_PPCF128 :
5992 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00005993 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005994 break;
5995 case ISD::FDIV:
Dale Johannesenc0154c02007-10-05 20:04:43 +00005996 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::DIV_F32 :
5997 VT == MVT::f64 ? RTLIB::DIV_F64 :
5998 VT == MVT::ppcf128 ?
5999 RTLIB::DIV_PPCF128 :
6000 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006001 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006002 break;
6003 case ISD::FP_EXTEND:
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006004 if (VT == MVT::ppcf128) {
6005 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6006 Node->getOperand(0).getValueType()==MVT::f64);
6007 const uint64_t zero = 0;
6008 if (Node->getOperand(0).getValueType()==MVT::f32)
6009 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6010 else
6011 Hi = Node->getOperand(0);
6012 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6013 break;
6014 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006015 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006016 break;
6017 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00006018 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006019 break;
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006020 case ISD::FPOWI:
Dale Johannesen25a00a62007-09-28 01:08:20 +00006021 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) ? RTLIB::POWI_F32 :
6022 (VT == MVT::f64) ? RTLIB::POWI_F64 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00006023 (VT == MVT::f80) ? RTLIB::POWI_F80 :
6024 (VT == MVT::ppcf128) ?
6025 RTLIB::POWI_PPCF128 :
6026 RTLIB::UNKNOWN_LIBCALL),
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006027 Node, false, Hi);
6028 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006029 case ISD::FSQRT:
6030 case ISD::FSIN:
6031 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006032 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00006033 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006034 case ISD::FSQRT:
Dale Johannesen25a00a62007-09-28 01:08:20 +00006035 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00006036 (VT == MVT::f64) ? RTLIB::SQRT_F64 :
6037 (VT == MVT::f80) ? RTLIB::SQRT_F80 :
6038 (VT == MVT::ppcf128) ? RTLIB::SQRT_PPCF128 :
6039 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006040 break;
6041 case ISD::FSIN:
6042 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
6043 break;
6044 case ISD::FCOS:
6045 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
6046 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006047 default: assert(0 && "Unreachable!");
6048 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006049 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00006050 break;
6051 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006052 case ISD::FABS: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006053 if (VT == MVT::ppcf128) {
6054 SDOperand Tmp;
6055 ExpandOp(Node->getOperand(0), Lo, Tmp);
6056 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6057 // lo = hi==fabs(hi) ? lo : -lo;
6058 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6059 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6060 DAG.getCondCode(ISD::SETEQ));
6061 break;
6062 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006063 SDOperand Mask = (VT == MVT::f64)
6064 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6065 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6066 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6067 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6068 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6069 if (getTypeAction(NVT) == Expand)
6070 ExpandOp(Lo, Lo, Hi);
6071 break;
6072 }
6073 case ISD::FNEG: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006074 if (VT == MVT::ppcf128) {
6075 ExpandOp(Node->getOperand(0), Lo, Hi);
6076 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6077 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6078 break;
6079 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006080 SDOperand Mask = (VT == MVT::f64)
6081 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6082 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6083 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6084 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6085 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6086 if (getTypeAction(NVT) == Expand)
6087 ExpandOp(Lo, Lo, Hi);
6088 break;
6089 }
Evan Cheng003feb02007-01-04 21:56:39 +00006090 case ISD::FCOPYSIGN: {
6091 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6092 if (getTypeAction(NVT) == Expand)
6093 ExpandOp(Lo, Lo, Hi);
6094 break;
6095 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006096 case ISD::SINT_TO_FP:
6097 case ISD::UINT_TO_FP: {
6098 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6099 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006100 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006101 static uint64_t zero = 0;
6102 if (isSigned) {
6103 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6104 Node->getOperand(0)));
6105 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6106 } else {
6107 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6108 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6109 Node->getOperand(0)));
6110 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6111 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006112 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006113 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6114 DAG.getConstant(0, MVT::i32),
6115 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6116 DAG.getConstantFP(
6117 APFloat(APInt(128, 2, TwoE32)),
6118 MVT::ppcf128)),
6119 Hi,
6120 DAG.getCondCode(ISD::SETLT)),
6121 Lo, Hi);
6122 }
6123 break;
6124 }
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006125 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6126 // si64->ppcf128 done by libcall, below
6127 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6128 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6129 Lo, Hi);
6130 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6131 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6132 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6133 DAG.getConstant(0, MVT::i64),
6134 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6135 DAG.getConstantFP(
6136 APFloat(APInt(128, 2, TwoE64)),
6137 MVT::ppcf128)),
6138 Hi,
6139 DAG.getCondCode(ISD::SETLT)),
6140 Lo, Hi);
6141 break;
6142 }
Evan Cheng75439b32007-09-27 07:35:39 +00006143 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006144 if (Node->getOperand(0).getValueType() == MVT::i64) {
6145 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006146 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006147 else if (VT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006148 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00006149 else if (VT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00006150 assert(isSigned);
Dale Johannesenc0154c02007-10-05 20:04:43 +00006151 LC = RTLIB::SINTTOFP_I64_F80;
6152 }
6153 else if (VT == MVT::ppcf128) {
6154 assert(isSigned);
6155 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006156 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006157 } else {
6158 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006159 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006160 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00006161 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006162 }
6163
6164 // Promote the operand if needed.
6165 if (getTypeAction(SrcVT) == Promote) {
6166 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6167 Tmp = isSigned
6168 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6169 DAG.getValueType(SrcVT))
6170 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6171 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6172 }
Evan Chengbf535fc2007-04-27 07:33:31 +00006173
6174 const char *LibCall = TLI.getLibcallName(LC);
6175 if (LibCall)
6176 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6177 else {
6178 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6179 Node->getOperand(0));
6180 if (getTypeAction(Lo.getValueType()) == Expand)
6181 ExpandOp(Lo, Lo, Hi);
6182 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006183 break;
6184 }
Evan Chengf3a80c62006-12-13 02:38:13 +00006185 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006186
Chris Lattnerac12f682005-12-21 18:02:52 +00006187 // Make sure the resultant values have been legalized themselves, unless this
6188 // is a type that requires multi-step expansion.
6189 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6190 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00006191 if (Hi.Val)
6192 // Don't legalize the high part if it is expanded to a single node.
6193 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00006194 }
Evan Cheng870e4f82006-01-09 18:31:59 +00006195
6196 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006197 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00006198 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00006199}
6200
Dan Gohmana8665142007-06-25 16:23:39 +00006201/// SplitVectorOp - Given an operand of vector type, break it down into
6202/// two smaller values, still of vector type.
Chris Lattner32206f52006-03-18 01:44:44 +00006203void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6204 SDOperand &Hi) {
Dan Gohmana8665142007-06-25 16:23:39 +00006205 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00006206 SDNode *Node = Op.Val;
Dan Gohman60028182007-09-24 15:54:53 +00006207 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattner32206f52006-03-18 01:44:44 +00006208 assert(NumElements > 1 && "Cannot split a single element vector!");
6209 unsigned NewNumElts = NumElements/2;
Dan Gohman60028182007-09-24 15:54:53 +00006210 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Dan Gohmana8665142007-06-25 16:23:39 +00006211 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattner32206f52006-03-18 01:44:44 +00006212
6213 // See if we already split it.
6214 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6215 = SplitNodes.find(Op);
6216 if (I != SplitNodes.end()) {
6217 Lo = I->second.first;
6218 Hi = I->second.second;
6219 return;
6220 }
6221
6222 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006223 default:
6224#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006225 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006226#endif
6227 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohmana8665142007-06-25 16:23:39 +00006228 case ISD::BUILD_PAIR:
6229 Lo = Node->getOperand(0);
6230 Hi = Node->getOperand(1);
6231 break;
Dan Gohmana90183e2007-09-28 23:53:40 +00006232 case ISD::INSERT_VECTOR_ELT: {
6233 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6234 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6235 SDOperand ScalarOp = Node->getOperand(1);
6236 if (Index < NewNumElts)
6237 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Lo, ScalarOp,
6238 DAG.getConstant(Index, TLI.getPointerTy()));
6239 else
6240 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Hi, ScalarOp,
6241 DAG.getConstant(Index - NewNumElts, TLI.getPointerTy()));
6242 break;
6243 }
Dan Gohmana8665142007-06-25 16:23:39 +00006244 case ISD::BUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006245 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6246 Node->op_begin()+NewNumElts);
Dan Gohmana8665142007-06-25 16:23:39 +00006247 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006248
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006249 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohmana8665142007-06-25 16:23:39 +00006250 Node->op_end());
6251 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006252 break;
6253 }
Dan Gohmana8665142007-06-25 16:23:39 +00006254 case ISD::CONCAT_VECTORS: {
6255 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6256 if (NewNumSubvectors == 1) {
6257 Lo = Node->getOperand(0);
6258 Hi = Node->getOperand(1);
6259 } else {
6260 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6261 Node->op_begin()+NewNumSubvectors);
6262 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00006263
Dan Gohmana8665142007-06-25 16:23:39 +00006264 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6265 Node->op_end());
6266 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
6267 }
Dan Gohman26455c42007-06-13 15:12:02 +00006268 break;
6269 }
Dan Gohman8f518b982007-10-17 14:48:28 +00006270 case ISD::SELECT: {
6271 SDOperand Cond = Node->getOperand(0);
6272
6273 SDOperand LL, LH, RL, RH;
6274 SplitVectorOp(Node->getOperand(1), LL, LH);
6275 SplitVectorOp(Node->getOperand(2), RL, RH);
6276
6277 if (MVT::isVector(Cond.getValueType())) {
6278 // Handle a vector merge.
6279 SDOperand CL, CH;
6280 SplitVectorOp(Cond, CL, CH);
6281 Lo = DAG.getNode(Node->getOpcode(), NewVT, CL, LL, RL);
6282 Hi = DAG.getNode(Node->getOpcode(), NewVT, CH, LH, RH);
6283 } else {
6284 // Handle a simple select with vector operands.
6285 Lo = DAG.getNode(Node->getOpcode(), NewVT, Cond, LL, RL);
6286 Hi = DAG.getNode(Node->getOpcode(), NewVT, Cond, LH, RH);
6287 }
6288 break;
6289 }
Dan Gohmana8665142007-06-25 16:23:39 +00006290 case ISD::ADD:
6291 case ISD::SUB:
6292 case ISD::MUL:
6293 case ISD::FADD:
6294 case ISD::FSUB:
6295 case ISD::FMUL:
6296 case ISD::SDIV:
6297 case ISD::UDIV:
6298 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00006299 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006300 case ISD::AND:
6301 case ISD::OR:
6302 case ISD::XOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00006303 SDOperand LL, LH, RL, RH;
6304 SplitVectorOp(Node->getOperand(0), LL, LH);
6305 SplitVectorOp(Node->getOperand(1), RL, RH);
6306
Dan Gohmana8665142007-06-25 16:23:39 +00006307 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
6308 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00006309 break;
6310 }
Dan Gohman2a7de412007-10-11 23:57:53 +00006311 case ISD::FPOWI: {
6312 SDOperand L, H;
6313 SplitVectorOp(Node->getOperand(0), L, H);
6314
6315 Lo = DAG.getNode(Node->getOpcode(), NewVT, L, Node->getOperand(1));
6316 Hi = DAG.getNode(Node->getOpcode(), NewVT, H, Node->getOperand(1));
6317 break;
6318 }
6319 case ISD::CTTZ:
6320 case ISD::CTLZ:
6321 case ISD::CTPOP:
6322 case ISD::FNEG:
6323 case ISD::FABS:
6324 case ISD::FSQRT:
6325 case ISD::FSIN:
6326 case ISD::FCOS: {
6327 SDOperand L, H;
6328 SplitVectorOp(Node->getOperand(0), L, H);
6329
6330 Lo = DAG.getNode(Node->getOpcode(), NewVT, L);
6331 Hi = DAG.getNode(Node->getOpcode(), NewVT, H);
6332 break;
6333 }
Dan Gohmana8665142007-06-25 16:23:39 +00006334 case ISD::LOAD: {
6335 LoadSDNode *LD = cast<LoadSDNode>(Node);
6336 SDOperand Ch = LD->getChain();
6337 SDOperand Ptr = LD->getBasePtr();
6338 const Value *SV = LD->getSrcValue();
6339 int SVOffset = LD->getSrcValueOffset();
6340 unsigned Alignment = LD->getAlignment();
6341 bool isVolatile = LD->isVolatile();
6342
6343 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6344 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattner32206f52006-03-18 01:44:44 +00006345 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6346 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00006347 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00006348 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohmana8665142007-06-25 16:23:39 +00006349 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00006350
6351 // Build a factor node to remember that this load is independent of the
6352 // other one.
6353 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6354 Hi.getValue(1));
6355
6356 // Remember that we legalized the chain.
6357 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00006358 break;
6359 }
Dan Gohmana8665142007-06-25 16:23:39 +00006360 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006361 // We know the result is a vector. The input may be either a vector or a
6362 // scalar value.
Dan Gohman0de76942007-06-29 00:09:08 +00006363 SDOperand InOp = Node->getOperand(0);
6364 if (!MVT::isVector(InOp.getValueType()) ||
6365 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6366 // The input is a scalar or single-element vector.
6367 // Lower to a store/load so that it can be split.
6368 // FIXME: this could be improved probably.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00006369 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006370
Evan Chengdf9ac472006-10-05 23:01:46 +00006371 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman0de76942007-06-29 00:09:08 +00006372 InOp, Ptr, NULL, 0);
6373 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006374 }
Dan Gohman0de76942007-06-29 00:09:08 +00006375 // Split the vector and convert each of the pieces now.
6376 SplitVectorOp(InOp, Lo, Hi);
6377 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
6378 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
6379 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006380 }
Chris Lattner32206f52006-03-18 01:44:44 +00006381 }
6382
6383 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006384 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00006385 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00006386 assert(isNew && "Value already split?!?");
Chris Lattner32206f52006-03-18 01:44:44 +00006387}
6388
6389
Dan Gohmanf4e86da2007-06-27 14:06:22 +00006390/// ScalarizeVectorOp - Given an operand of single-element vector type
6391/// (e.g. v1f32), convert it into the equivalent operation that returns a
6392/// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +00006393SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6394 assert(MVT::isVector(Op.getValueType()) &&
6395 "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00006396 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00006397 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6398 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00006399
Dan Gohmana8665142007-06-25 16:23:39 +00006400 // See if we already scalarized it.
6401 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6402 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00006403
6404 SDOperand Result;
6405 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00006406 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006407#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006408 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006409#endif
Dan Gohmana8665142007-06-25 16:23:39 +00006410 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6411 case ISD::ADD:
6412 case ISD::FADD:
6413 case ISD::SUB:
6414 case ISD::FSUB:
6415 case ISD::MUL:
6416 case ISD::FMUL:
6417 case ISD::SDIV:
6418 case ISD::UDIV:
6419 case ISD::FDIV:
6420 case ISD::SREM:
6421 case ISD::UREM:
6422 case ISD::FREM:
Dan Gohman2a7de412007-10-11 23:57:53 +00006423 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006424 case ISD::AND:
6425 case ISD::OR:
6426 case ISD::XOR:
6427 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00006428 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00006429 ScalarizeVectorOp(Node->getOperand(0)),
6430 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00006431 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006432 case ISD::FNEG:
6433 case ISD::FABS:
6434 case ISD::FSQRT:
6435 case ISD::FSIN:
6436 case ISD::FCOS:
6437 Result = DAG.getNode(Node->getOpcode(),
6438 NewVT,
6439 ScalarizeVectorOp(Node->getOperand(0)));
6440 break;
Dan Gohman4f056f32007-10-12 14:13:46 +00006441 case ISD::FPOWI:
6442 Result = DAG.getNode(Node->getOpcode(),
6443 NewVT,
6444 ScalarizeVectorOp(Node->getOperand(0)),
6445 Node->getOperand(1));
6446 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006447 case ISD::LOAD: {
6448 LoadSDNode *LD = cast<LoadSDNode>(Node);
6449 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6450 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00006451
Dan Gohmana8665142007-06-25 16:23:39 +00006452 const Value *SV = LD->getSrcValue();
6453 int SVOffset = LD->getSrcValueOffset();
6454 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6455 LD->isVolatile(), LD->getAlignment());
6456
Chris Lattner32206f52006-03-18 01:44:44 +00006457 // Remember that we legalized the chain.
6458 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6459 break;
6460 }
Dan Gohmana8665142007-06-25 16:23:39 +00006461 case ISD::BUILD_VECTOR:
6462 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00006463 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006464 case ISD::INSERT_VECTOR_ELT:
6465 // Returning the inserted scalar element.
6466 Result = Node->getOperand(1);
6467 break;
6468 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00006469 assert(Node->getOperand(0).getValueType() == NewVT &&
6470 "Concat of non-legal vectors not yet supported!");
6471 Result = Node->getOperand(0);
6472 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006473 case ISD::VECTOR_SHUFFLE: {
6474 // Figure out if the scalar is the LHS or RHS and return it.
6475 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6476 if (cast<ConstantSDNode>(EltNum)->getValue())
6477 Result = ScalarizeVectorOp(Node->getOperand(1));
6478 else
6479 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00006480 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006481 }
6482 case ISD::EXTRACT_SUBVECTOR:
6483 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00006484 assert(Result.getValueType() == NewVT);
6485 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006486 case ISD::BIT_CONVERT:
6487 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattnerf6f94d32006-03-28 20:24:43 +00006488 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006489 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00006490 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00006491 ScalarizeVectorOp(Op.getOperand(1)),
6492 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00006493 break;
Chris Lattner32206f52006-03-18 01:44:44 +00006494 }
6495
6496 if (TLI.isTypeLegal(NewVT))
6497 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00006498 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6499 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00006500 return Result;
6501}
6502
Chris Lattnerdc750592005-01-07 07:47:09 +00006503
6504// SelectionDAG::Legalize - This is the entry point for the file.
6505//
Chris Lattner4add7e32005-01-23 04:42:50 +00006506void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00006507 if (ViewLegalizeDAGs) viewGraph();
6508
Chris Lattnerdc750592005-01-07 07:47:09 +00006509 /// run - This is the main entry point to this class.
6510 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00006511 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00006512}
6513