blob: e65926360000bb27c106fbbad31184c2f79960bd [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//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey70323a82006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Cheng631ccc62007-08-16 23:50:06 +000019#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000020#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000021#include "llvm/Target/TargetData.h"
Evan Cheng84a28d42006-10-30 08:00:44 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000023#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000024#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000025#include "llvm/Constants.h"
Reid Spencera94d3942007-01-19 21:13:56 +000026#include "llvm/DerivedTypes.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 Lattner87bc3e72008-01-16 07:45:30 +0000202 SDOperand EmitStackConvert(SDOperand SrcOp, MVT::ValueType SlotVT,
203 MVT::ValueType DestVT);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000204 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner6be79822006-04-04 17:23:26 +0000205 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000206 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
207 SDOperand LegalOp,
208 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000209 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
210 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000211 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
212 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000213
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000214 SDOperand ExpandBSWAP(SDOperand Op);
215 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000216 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
217 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000218 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
219 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000220
Dan Gohmana8665142007-06-25 16:23:39 +0000221 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner42a5fca2006-04-02 05:06:04 +0000222 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner6f423252006-03-31 17:55:51 +0000223
Chris Lattnerdc750592005-01-07 07:47:09 +0000224 SDOperand getIntPtrConstant(uint64_t Val) {
225 return DAG.getConstant(Val, TLI.getPointerTy());
226 }
227};
228}
229
Chris Lattner6be79822006-04-04 17:23:26 +0000230/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
231/// specified mask and type. Targets can specify exactly which masks they
232/// support and the code generator is tasked with not creating illegal masks.
233///
234/// Note that this will also return true for shuffles that are promoted to a
235/// different type.
236SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
237 SDOperand Mask) const {
238 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
239 default: return 0;
240 case TargetLowering::Legal:
241 case TargetLowering::Custom:
242 break;
243 case TargetLowering::Promote: {
244 // If this is promoted to a different type, convert the shuffle mask and
245 // ask if it is legal in the promoted type!
246 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
247
248 // If we changed # elements, change the shuffle mask.
249 unsigned NumEltsGrowth =
250 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
251 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
252 if (NumEltsGrowth > 1) {
253 // Renumber the elements.
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000254 SmallVector<SDOperand, 8> Ops;
Chris Lattner6be79822006-04-04 17:23:26 +0000255 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
256 SDOperand InOp = Mask.getOperand(i);
257 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
258 if (InOp.getOpcode() == ISD::UNDEF)
259 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
260 else {
261 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
262 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
263 }
264 }
265 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000266 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +0000267 }
268 VT = NVT;
269 break;
270 }
271 }
272 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
273}
274
Chris Lattner4add7e32005-01-23 04:42:50 +0000275SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
276 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
277 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000278 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000279 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000280}
281
Chris Lattnere31adc82007-06-18 21:28:10 +0000282/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
283/// contains all of a nodes operands before it contains the node.
284static void ComputeTopDownOrdering(SelectionDAG &DAG,
285 SmallVector<SDNode*, 64> &Order) {
286
287 DenseMap<SDNode*, unsigned> Visited;
288 std::vector<SDNode*> Worklist;
289 Worklist.reserve(128);
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000290
Chris Lattnere31adc82007-06-18 21:28:10 +0000291 // Compute ordering from all of the leaves in the graphs, those (like the
292 // entry node) that have no operands.
293 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
294 E = DAG.allnodes_end(); I != E; ++I) {
295 if (I->getNumOperands() == 0) {
296 Visited[I] = 0 - 1U;
297 Worklist.push_back(I);
298 }
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000299 }
300
Chris Lattnere31adc82007-06-18 21:28:10 +0000301 while (!Worklist.empty()) {
302 SDNode *N = Worklist.back();
303 Worklist.pop_back();
304
305 if (++Visited[N] != N->getNumOperands())
306 continue; // Haven't visited all operands yet
307
308 Order.push_back(N);
309
310 // Now that we have N in, add anything that uses it if all of their operands
311 // are now done.
312 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
313 UI != E; ++UI)
314 Worklist.push_back(*UI);
315 }
316
317 assert(Order.size() == Visited.size() &&
318 Order.size() ==
319 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
320 "Error: DAG is cyclic!");
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000321}
322
Chris Lattner44fe26f2005-07-29 00:11:56 +0000323
Chris Lattnerdc750592005-01-07 07:47:09 +0000324void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000325 LastCALLSEQ_END = DAG.getEntryNode();
326 IsLegalizingCall = false;
327
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000328 // The legalize process is inherently a bottom-up recursive process (users
329 // legalize their uses before themselves). Given infinite stack space, we
330 // could just start legalizing on the root and traverse the whole graph. In
331 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000332 // blocks. To avoid this problem, compute an ordering of the nodes where each
333 // node is only legalized after all of its operands are legalized.
Chris Lattner94c44c92007-02-04 01:20:02 +0000334 SmallVector<SDNode*, 64> Order;
Chris Lattnere31adc82007-06-18 21:28:10 +0000335 ComputeTopDownOrdering(DAG, Order);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000336
Chris Lattner32206f52006-03-18 01:44:44 +0000337 for (unsigned i = 0, e = Order.size(); i != e; ++i)
338 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000339
340 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000341 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000342 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
343 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000344
345 ExpandedNodes.clear();
346 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000347 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000348 SplitNodes.clear();
Dan Gohmana8665142007-06-25 16:23:39 +0000349 ScalarizedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000350
351 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000352 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000353}
354
Chris Lattner462505f2006-02-13 09:18:02 +0000355
356/// FindCallEndFromCallStart - Given a chained node that is part of a call
357/// sequence, find the CALLSEQ_END node that terminates the call sequence.
358static SDNode *FindCallEndFromCallStart(SDNode *Node) {
359 if (Node->getOpcode() == ISD::CALLSEQ_END)
360 return Node;
361 if (Node->use_empty())
362 return 0; // No CallSeqEnd
363
364 // The chain is usually at the end.
365 SDOperand TheChain(Node, Node->getNumValues()-1);
366 if (TheChain.getValueType() != MVT::Other) {
367 // Sometimes it's at the beginning.
368 TheChain = SDOperand(Node, 0);
369 if (TheChain.getValueType() != MVT::Other) {
370 // Otherwise, hunt for it.
371 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
372 if (Node->getValueType(i) == MVT::Other) {
373 TheChain = SDOperand(Node, i);
374 break;
375 }
376
377 // Otherwise, we walked into a node without a chain.
378 if (TheChain.getValueType() != MVT::Other)
379 return 0;
380 }
381 }
382
383 for (SDNode::use_iterator UI = Node->use_begin(),
384 E = Node->use_end(); UI != E; ++UI) {
385
386 // Make sure to only follow users of our token chain.
387 SDNode *User = *UI;
388 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
389 if (User->getOperand(i) == TheChain)
390 if (SDNode *Result = FindCallEndFromCallStart(User))
391 return Result;
392 }
393 return 0;
394}
395
396/// FindCallStartFromCallEnd - Given a chained node that is part of a call
397/// sequence, find the CALLSEQ_START node that initiates the call sequence.
398static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
399 assert(Node && "Didn't find callseq_start for a call??");
400 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
401
402 assert(Node->getOperand(0).getValueType() == MVT::Other &&
403 "Node doesn't have a token chain argument!");
404 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
405}
406
407/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
408/// see if any uses can reach Dest. If no dest operands can get to dest,
409/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000410///
411/// Keep track of the nodes we fine that actually do lead to Dest in
412/// NodesLeadingTo. This avoids retraversing them exponential number of times.
413///
414bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000415 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000416 if (N == Dest) return true; // N certainly leads to Dest :)
417
Chris Lattner4488f0c2006-07-26 23:55:56 +0000418 // If we've already processed this node and it does lead to Dest, there is no
419 // need to reprocess it.
420 if (NodesLeadingTo.count(N)) return true;
421
Chris Lattner462505f2006-02-13 09:18:02 +0000422 // If the first result of this node has been already legalized, then it cannot
423 // reach N.
424 switch (getTypeAction(N->getValueType(0))) {
425 case Legal:
426 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
427 break;
428 case Promote:
429 if (PromotedNodes.count(SDOperand(N, 0))) return false;
430 break;
431 case Expand:
432 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
433 break;
434 }
435
436 // Okay, this node has not already been legalized. Check and legalize all
437 // operands. If none lead to Dest, then we can legalize this node.
438 bool OperandsLeadToDest = false;
439 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
440 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000441 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000442
Chris Lattner4488f0c2006-07-26 23:55:56 +0000443 if (OperandsLeadToDest) {
444 NodesLeadingTo.insert(N);
445 return true;
446 }
Chris Lattner462505f2006-02-13 09:18:02 +0000447
448 // Okay, this node looks safe, legalize it and return false.
Chris Lattner916ae072006-04-17 22:10:08 +0000449 HandleOp(SDOperand(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000450 return false;
451}
452
Dan Gohmana8665142007-06-25 16:23:39 +0000453/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000454/// appropriate for its type.
455void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohmana8665142007-06-25 16:23:39 +0000456 MVT::ValueType VT = Op.getValueType();
457 switch (getTypeAction(VT)) {
Chris Lattner32206f52006-03-18 01:44:44 +0000458 default: assert(0 && "Bad type action!");
Dan Gohmana8665142007-06-25 16:23:39 +0000459 case Legal: (void)LegalizeOp(Op); break;
460 case Promote: (void)PromoteOp(Op); break;
Chris Lattner32206f52006-03-18 01:44:44 +0000461 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +0000462 if (!MVT::isVector(VT)) {
463 // If this is an illegal scalar, expand it into its two component
464 // pieces.
Chris Lattner32206f52006-03-18 01:44:44 +0000465 SDOperand X, Y;
Chris Lattner2ed652f2007-08-25 01:00:22 +0000466 if (Op.getOpcode() == ISD::TargetConstant)
467 break; // Allow illegal target nodes.
Chris Lattner32206f52006-03-18 01:44:44 +0000468 ExpandOp(Op, X, Y);
Dan Gohmana8665142007-06-25 16:23:39 +0000469 } else if (MVT::getVectorNumElements(VT) == 1) {
470 // If this is an illegal single element vector, convert it to a
471 // scalar operation.
472 (void)ScalarizeVectorOp(Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000473 } else {
Dan Gohmana8665142007-06-25 16:23:39 +0000474 // Otherwise, this is an illegal multiple element vector.
475 // Split it in half and legalize both parts.
476 SDOperand X, Y;
477 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000478 }
479 break;
480 }
481}
Chris Lattner462505f2006-02-13 09:18:02 +0000482
Evan Cheng22cf8992006-12-13 20:57:08 +0000483/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
484/// a load from the constant pool.
485static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000486 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000487 bool Extend = false;
488
489 // If a FP immediate is precise when represented as a float and if the
490 // target can do an extending load from float to double, we put it into
491 // the constant pool as a float, even if it's is statically typed as a
492 // double.
493 MVT::ValueType VT = CFP->getValueType(0);
494 bool isDouble = VT == MVT::f64;
Dale Johannesen7f724e92007-09-16 16:51:49 +0000495 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen98d3a082007-09-14 22:26:36 +0000496 CFP->getValueAPF());
Evan Cheng22cf8992006-12-13 20:57:08 +0000497 if (!UseCP) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000498 if (VT!=MVT::f64 && VT!=MVT::f32)
499 assert(0 && "Invalid type expansion");
Dale Johannesen028084e2007-09-12 03:30:33 +0000500 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
501 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000502 }
503
Dale Johannesend246b2c2007-08-30 00:23:21 +0000504 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng47833a12006-12-12 21:32:44 +0000505 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen98d3a082007-09-14 22:26:36 +0000506 // Do not try to be clever about long doubles (so far)
Evan Cheng47833a12006-12-12 21:32:44 +0000507 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
508 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
509 VT = MVT::f32;
510 Extend = true;
511 }
512
513 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
514 if (Extend) {
515 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
516 CPIdx, NULL, 0, MVT::f32);
517 } else {
518 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
519 }
520}
521
Chris Lattner462505f2006-02-13 09:18:02 +0000522
Evan Cheng003feb02007-01-04 21:56:39 +0000523/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
524/// operations.
525static
526SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
527 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng3b841dd2007-01-05 21:31:51 +0000528 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000529 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000530 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
531 "fcopysign expansion only supported for f32 and f64");
Evan Cheng003feb02007-01-04 21:56:39 +0000532 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000533
Evan Cheng003feb02007-01-04 21:56:39 +0000534 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000535 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000536 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
537 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000538 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000539 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000540 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000541 // Shift right or sign-extend it if the two operands have different types.
542 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
543 if (SizeDiff > 0) {
544 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
545 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
546 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
547 } else if (SizeDiff < 0)
548 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000549
550 // Clear the sign bit of first operand.
551 SDOperand Mask2 = (VT == MVT::f64)
552 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
553 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
554 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng003feb02007-01-04 21:56:39 +0000555 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000556 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
557
558 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000559 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
560 return Result;
561}
562
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000563/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
564static
565SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
566 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000567 SDOperand Chain = ST->getChain();
568 SDOperand Ptr = ST->getBasePtr();
569 SDOperand Val = ST->getValue();
570 MVT::ValueType VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000571 int Alignment = ST->getAlignment();
572 int SVOffset = ST->getSrcValueOffset();
573 if (MVT::isFloatingPoint(ST->getStoredVT())) {
574 // Expand to a bitconvert of the value to the integer type of the
575 // same size, then a (misaligned) int store.
576 MVT::ValueType intVT;
577 if (VT==MVT::f64)
578 intVT = MVT::i64;
579 else if (VT==MVT::f32)
580 intVT = MVT::i32;
581 else
582 assert(0 && "Unaligned load of unsupported floating point type");
583
584 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
585 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
586 SVOffset, ST->isVolatile(), Alignment);
587 }
588 assert(MVT::isInteger(ST->getStoredVT()) &&
589 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000590 // Get the half-size VT
591 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
592 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000593 int IncrementSize = NumBits / 8;
594
595 // Divide the stored value in two parts.
596 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
597 SDOperand Lo = Val;
598 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
599
600 // Store the two parts
601 SDOperand Store1, Store2;
602 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
603 ST->getSrcValue(), SVOffset, NewStoredVT,
604 ST->isVolatile(), Alignment);
605 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
606 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sands1826ded2007-10-28 12:59:45 +0000607 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000608 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
609 ST->getSrcValue(), SVOffset + IncrementSize,
610 NewStoredVT, ST->isVolatile(), Alignment);
611
612 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
613}
614
615/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
616static
617SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
618 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000619 int SVOffset = LD->getSrcValueOffset();
620 SDOperand Chain = LD->getChain();
621 SDOperand Ptr = LD->getBasePtr();
622 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000623 MVT::ValueType LoadedVT = LD->getLoadedVT();
Chris Lattner09c03932007-11-19 21:38:03 +0000624 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT)) {
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000625 // Expand to a (misaligned) integer load of the same size,
626 // then bitconvert to floating point.
627 MVT::ValueType intVT;
Chris Lattner09c03932007-11-19 21:38:03 +0000628 if (LoadedVT == MVT::f64)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000629 intVT = MVT::i64;
Chris Lattner09c03932007-11-19 21:38:03 +0000630 else if (LoadedVT == MVT::f32)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000631 intVT = MVT::i32;
632 else
633 assert(0 && "Unaligned load of unsupported floating point type");
634
635 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
636 SVOffset, LD->isVolatile(),
637 LD->getAlignment());
638 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
639 if (LoadedVT != VT)
640 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
641
642 SDOperand Ops[] = { Result, Chain };
643 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
644 Ops, 2);
645 }
Chris Lattner09c03932007-11-19 21:38:03 +0000646 assert((MVT::isInteger(LoadedVT) || MVT::isVector(LoadedVT)) &&
647 "Unaligned load of unsupported type.");
648
649 // Compute the new VT that is half the size of the old one. We either have an
650 // integer MVT or we have a vector MVT.
651 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
652 MVT::ValueType NewLoadedVT;
653 if (!MVT::isVector(LoadedVT)) {
654 NewLoadedVT = MVT::getIntegerType(NumBits/2);
655 } else {
656 // FIXME: This is not right for <1 x anything> it is also not right for
657 // non-power-of-two vectors.
658 NewLoadedVT = MVT::getVectorType(MVT::getVectorElementType(LoadedVT),
659 MVT::getVectorNumElements(LoadedVT)/2);
660 }
661 NumBits >>= 1;
662
663 unsigned Alignment = LD->getAlignment();
664 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000665 ISD::LoadExtType HiExtType = LD->getExtensionType();
666
667 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
668 if (HiExtType == ISD::NON_EXTLOAD)
669 HiExtType = ISD::ZEXTLOAD;
670
671 // Load the value in two parts
672 SDOperand Lo, Hi;
673 if (TLI.isLittleEndian()) {
674 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
675 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
676 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
677 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
678 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
679 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000680 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000681 } else {
682 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
683 NewLoadedVT,LD->isVolatile(), Alignment);
684 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
685 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
686 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
687 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000688 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000689 }
690
691 // aggregate the two parts
692 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
693 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
694 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
695
696 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
697 Hi.getValue(1));
698
699 SDOperand Ops[] = { Result, TF };
700 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
701}
Evan Cheng003feb02007-01-04 21:56:39 +0000702
Dan Gohman2a7de412007-10-11 23:57:53 +0000703/// UnrollVectorOp - We know that the given vector has a legal type, however
704/// the operation it performs is not legal and is an operation that we have
705/// no way of lowering. "Unroll" the vector, splitting out the scalars and
706/// operating on each element individually.
707SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
708 MVT::ValueType VT = Op.getValueType();
709 assert(isTypeLegal(VT) &&
710 "Caller should expand or promote operands that are not legal!");
711 assert(Op.Val->getNumValues() == 1 &&
712 "Can't unroll a vector with multiple results!");
713 unsigned NE = MVT::getVectorNumElements(VT);
714 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
715
716 SmallVector<SDOperand, 8> Scalars;
717 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
718 for (unsigned i = 0; i != NE; ++i) {
719 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
720 SDOperand Operand = Op.getOperand(j);
721 MVT::ValueType OperandVT = Operand.getValueType();
722 if (MVT::isVector(OperandVT)) {
723 // A vector operand; extract a single element.
724 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
725 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
726 OperandEltVT,
727 Operand,
728 DAG.getConstant(i, MVT::i32));
729 } else {
730 // A scalar operand; just use it as is.
731 Operands[j] = Operand;
732 }
733 }
734 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
735 &Operands[0], Operands.size()));
736 }
737
738 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
739}
740
Duncan Sands53c954f2008-01-10 10:28:30 +0000741/// GetFPLibCall - Return the right libcall for the given floating point type.
742static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
743 RTLIB::Libcall Call_F32,
744 RTLIB::Libcall Call_F64,
745 RTLIB::Libcall Call_F80,
746 RTLIB::Libcall Call_PPCF128) {
747 return
748 VT == MVT::f32 ? Call_F32 :
749 VT == MVT::f64 ? Call_F64 :
750 VT == MVT::f80 ? Call_F80 :
751 VT == MVT::ppcf128 ? Call_PPCF128 :
752 RTLIB::UNKNOWN_LIBCALL;
753}
754
Dan Gohmanff727882007-07-13 20:14:11 +0000755/// LegalizeOp - We know that the specified value has a legal type, and
756/// that its operands are legal. Now ensure that the operation itself
757/// is legal, recursively ensuring that the operands' operations remain
758/// legal.
Chris Lattnerdc750592005-01-07 07:47:09 +0000759SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner2ed652f2007-08-25 01:00:22 +0000760 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
761 return Op;
762
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000763 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000764 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000765 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000766
Chris Lattnerdc750592005-01-07 07:47:09 +0000767 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000768 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000769 if (Node->getNumValues() > 1) {
770 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000771 if (getTypeAction(Node->getValueType(i)) != Legal) {
772 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000773 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000774 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000775 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000776 }
777 }
778
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000779 // Note that LegalizeOp may be reentered even from single-use nodes, which
780 // means that we always must cache transformed nodes.
Chris Lattnered39c862007-02-04 00:50:02 +0000781 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000782 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000783
Nate Begemane5b86d72005-08-10 20:51:12 +0000784 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000785 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000786 bool isCustom = false;
787
Chris Lattnerdc750592005-01-07 07:47:09 +0000788 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000789 case ISD::FrameIndex:
790 case ISD::EntryToken:
791 case ISD::Register:
792 case ISD::BasicBlock:
793 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000794 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000795 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000796 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000797 case ISD::TargetConstantPool:
798 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000799 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000800 case ISD::TargetExternalSymbol:
801 case ISD::VALUETYPE:
802 case ISD::SRCVALUE:
803 case ISD::STRING:
804 case ISD::CONDCODE:
805 // Primitives must all be legal.
Duncan Sands052c8432007-10-16 09:07:20 +0000806 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattnereb637512006-01-28 08:31:04 +0000807 "This must be legal!");
808 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000809 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000810 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
811 // If this is a target node, legalize it by legalizing the operands then
812 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000813 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000814 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000815 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000816
Chris Lattner97af9d52006-08-08 01:09:31 +0000817 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000818
819 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
820 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
821 return Result.getValue(Op.ResNo);
822 }
823 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000824#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000825 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000826#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000827 assert(0 && "Do not know how to legalize this operator!");
828 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000829 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000830 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000831 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000832 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000833 case ISD::ConstantPool:
834 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000835 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
836 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000837 case TargetLowering::Custom:
838 Tmp1 = TLI.LowerOperation(Op, DAG);
839 if (Tmp1.Val) Result = Tmp1;
840 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000841 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000842 break;
843 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000844 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000845 case ISD::FRAMEADDR:
846 case ISD::RETURNADDR:
847 // The only option for these nodes is to custom lower them. If the target
848 // does not custom lower them, then return zero.
849 Tmp1 = TLI.LowerOperation(Op, DAG);
850 if (Tmp1.Val)
851 Result = Tmp1;
852 else
853 Result = DAG.getConstant(0, TLI.getPointerTy());
854 break;
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000855 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000856 MVT::ValueType VT = Node->getValueType(0);
857 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
858 default: assert(0 && "This action is not supported yet!");
859 case TargetLowering::Custom:
860 Result = TLI.LowerOperation(Op, DAG);
861 if (Result.Val) break;
862 // Fall Thru
863 case TargetLowering::Legal:
864 Result = DAG.getConstant(0, VT);
865 break;
866 }
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000867 }
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000868 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000869 case ISD::EXCEPTIONADDR: {
870 Tmp1 = LegalizeOp(Node->getOperand(0));
871 MVT::ValueType VT = Node->getValueType(0);
872 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
873 default: assert(0 && "This action is not supported yet!");
874 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000875 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +0000876 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000877 }
878 break;
879 case TargetLowering::Custom:
880 Result = TLI.LowerOperation(Op, DAG);
881 if (Result.Val) break;
882 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000883 case TargetLowering::Legal: {
884 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
885 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sands57a60f02007-12-31 18:35:50 +0000886 Ops, 2);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000887 break;
888 }
889 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000890 }
Duncan Sands57a60f02007-12-31 18:35:50 +0000891 if (Result.Val->getNumValues() == 1) break;
892
893 assert(Result.Val->getNumValues() == 2 &&
894 "Cannot return more than two values!");
895
896 // Since we produced two values, make sure to remember that we
897 // legalized both of them.
898 Tmp1 = LegalizeOp(Result);
899 Tmp2 = LegalizeOp(Result.getValue(1));
900 AddLegalizedOperand(Op.getValue(0), Tmp1);
901 AddLegalizedOperand(Op.getValue(1), Tmp2);
902 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey644af6b2007-02-28 20:43:58 +0000903 case ISD::EHSELECTION: {
904 Tmp1 = LegalizeOp(Node->getOperand(0));
905 Tmp2 = LegalizeOp(Node->getOperand(1));
906 MVT::ValueType VT = Node->getValueType(0);
907 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
908 default: assert(0 && "This action is not supported yet!");
909 case TargetLowering::Expand: {
910 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +0000911 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey644af6b2007-02-28 20:43:58 +0000912 }
913 break;
914 case TargetLowering::Custom:
915 Result = TLI.LowerOperation(Op, DAG);
916 if (Result.Val) break;
917 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000918 case TargetLowering::Legal: {
919 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
920 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sands57a60f02007-12-31 18:35:50 +0000921 Ops, 2);
Jim Laskey644af6b2007-02-28 20:43:58 +0000922 break;
923 }
924 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000925 }
Duncan Sands57a60f02007-12-31 18:35:50 +0000926 if (Result.Val->getNumValues() == 1) break;
927
928 assert(Result.Val->getNumValues() == 2 &&
929 "Cannot return more than two values!");
930
931 // Since we produced two values, make sure to remember that we
932 // legalized both of them.
933 Tmp1 = LegalizeOp(Result);
934 Tmp2 = LegalizeOp(Result.getValue(1));
935 AddLegalizedOperand(Op.getValue(0), Tmp1);
936 AddLegalizedOperand(Op.getValue(1), Tmp2);
937 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000938 case ISD::EH_RETURN: {
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000939 MVT::ValueType VT = Node->getValueType(0);
940 // The only "good" option for this node is to custom lower it.
941 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
942 default: assert(0 && "This action is not supported at all!");
943 case TargetLowering::Custom:
944 Result = TLI.LowerOperation(Op, DAG);
945 if (Result.Val) break;
946 // Fall Thru
947 case TargetLowering::Legal:
948 // Target does not know, how to lower this, lower to noop
949 Result = LegalizeOp(Node->getOperand(0));
950 break;
951 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000952 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000953 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000954 case ISD::AssertSext:
955 case ISD::AssertZext:
956 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000957 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000958 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000959 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000960 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000961 Result = Node->getOperand(Op.ResNo);
962 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000963 case ISD::CopyFromReg:
964 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000965 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000966 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000967 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000968 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000969 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000970 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000971 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000972 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
973 } else {
974 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
975 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000976 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
977 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000978 // Since CopyFromReg produces two values, make sure to remember that we
979 // legalized both of them.
980 AddLegalizedOperand(Op.getValue(0), Result);
981 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
982 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000983 case ISD::UNDEF: {
984 MVT::ValueType VT = Op.getValueType();
985 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000986 default: assert(0 && "This action is not supported yet!");
987 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000988 if (MVT::isInteger(VT))
989 Result = DAG.getConstant(0, VT);
990 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf04d37d2007-09-26 17:26:49 +0000991 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
992 VT);
Nate Begemancda9aa72005-04-01 22:34:39 +0000993 else
994 assert(0 && "Unknown value type!");
995 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000996 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000997 break;
998 }
999 break;
1000 }
Chris Lattnera4f68052006-03-24 02:26:29 +00001001
Chris Lattnere55d1712006-03-28 00:40:33 +00001002 case ISD::INTRINSIC_W_CHAIN:
1003 case ISD::INTRINSIC_WO_CHAIN:
1004 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001005 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +00001006 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1007 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001008 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +00001009
1010 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +00001011 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +00001012 TargetLowering::Custom) {
1013 Tmp3 = TLI.LowerOperation(Result, DAG);
1014 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +00001015 }
1016
1017 if (Result.Val->getNumValues() == 1) break;
1018
1019 // Must have return value and chain result.
1020 assert(Result.Val->getNumValues() == 2 &&
1021 "Cannot return more than two values!");
1022
1023 // Since loads produce two values, make sure to remember that we
1024 // legalized both of them.
1025 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1026 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1027 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +00001028 }
Chris Lattner435b4022005-11-29 06:21:05 +00001029
1030 case ISD::LOCATION:
1031 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1032 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1033
1034 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1035 case TargetLowering::Promote:
1036 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +00001037 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +00001038 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +00001039 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +00001040 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001041
Jim Laskeyc56315c2007-01-26 21:22:28 +00001042 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001043 const std::string &FName =
1044 cast<StringSDNode>(Node->getOperand(3))->getValue();
1045 const std::string &DirName =
1046 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +00001047 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001048
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001049 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +00001050 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +00001051 SDOperand LineOp = Node->getOperand(1);
1052 SDOperand ColOp = Node->getOperand(2);
1053
1054 if (useDEBUG_LOC) {
1055 Ops.push_back(LineOp); // line #
1056 Ops.push_back(ColOp); // col #
1057 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001058 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001059 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +00001060 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1061 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +00001062 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001063 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskeyf9e54452007-01-26 14:34:52 +00001064 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001065 }
Jim Laskey9e296be2005-12-21 20:51:37 +00001066 } else {
1067 Result = Tmp1; // chain
1068 }
Chris Lattner435b4022005-11-29 06:21:05 +00001069 break;
Chris Lattnerc06da622005-12-18 23:54:29 +00001070 }
Chris Lattner435b4022005-11-29 06:21:05 +00001071 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +00001072 if (Tmp1 != Node->getOperand(0) ||
1073 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001074 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +00001075 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +00001076 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1077 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1078 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1079 } else {
1080 // Otherwise promote them.
1081 Ops.push_back(PromoteOp(Node->getOperand(1)));
1082 Ops.push_back(PromoteOp(Node->getOperand(2)));
1083 }
Chris Lattner435b4022005-11-29 06:21:05 +00001084 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1085 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +00001086 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +00001087 }
1088 break;
1089 }
1090 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001091
1092 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +00001093 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +00001094 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +00001095 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +00001096 case TargetLowering::Legal:
1097 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1098 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1099 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1100 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001102 break;
1103 }
1104 break;
1105
Jim Laskeyf9e54452007-01-26 14:34:52 +00001106 case ISD::LABEL:
1107 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1108 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001109 default: assert(0 && "This action is not supported yet!");
1110 case TargetLowering::Legal:
1111 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1112 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001113 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +00001114 break;
Chris Lattner567b9252007-03-03 19:21:38 +00001115 case TargetLowering::Expand:
1116 Result = LegalizeOp(Node->getOperand(0));
1117 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001118 }
Nate Begeman5965bd12006-02-17 05:43:56 +00001119 break;
Chris Lattner435b4022005-11-29 06:21:05 +00001120
Scott Michel9d09c5c2007-08-08 23:23:31 +00001121 case ISD::Constant: {
1122 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1123 unsigned opAction =
1124 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1125
Chris Lattnerdc750592005-01-07 07:47:09 +00001126 // We know we don't need to expand constants here, constants only have one
1127 // value and we check that it is fine above.
1128
Scott Michel9d09c5c2007-08-08 23:23:31 +00001129 if (opAction == TargetLowering::Custom) {
1130 Tmp1 = TLI.LowerOperation(Result, DAG);
1131 if (Tmp1.Val)
1132 Result = Tmp1;
1133 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001134 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +00001135 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001136 case ISD::ConstantFP: {
1137 // Spill FP immediates to the constant pool if the target cannot directly
1138 // codegen them. Targets often have some immediate values that can be
1139 // efficiently generated into an FP register without a load. We explicitly
1140 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +00001141 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1142
1143 // Check to see if this FP immediate is already legal.
1144 bool isLegal = false;
1145 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1146 E = TLI.legal_fpimm_end(); I != E; ++I)
1147 if (CFP->isExactlyValue(*I)) {
1148 isLegal = true;
1149 break;
1150 }
1151
Chris Lattner758b0ac2006-01-29 06:26:56 +00001152 // If this is a legal constant, turn it into a TargetConstantFP node.
1153 if (isLegal) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00001154 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1155 CFP->getValueType(0));
Chris Lattner758b0ac2006-01-29 06:26:56 +00001156 break;
1157 }
1158
1159 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1160 default: assert(0 && "This action is not supported yet!");
1161 case TargetLowering::Custom:
1162 Tmp3 = TLI.LowerOperation(Result, DAG);
1163 if (Tmp3.Val) {
1164 Result = Tmp3;
1165 break;
1166 }
1167 // FALLTHROUGH
1168 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +00001169 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001170 }
1171 break;
1172 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001173 case ISD::TokenFactor:
1174 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001175 Tmp1 = LegalizeOp(Node->getOperand(0));
1176 Tmp2 = LegalizeOp(Node->getOperand(1));
1177 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1178 } else if (Node->getNumOperands() == 3) {
1179 Tmp1 = LegalizeOp(Node->getOperand(0));
1180 Tmp2 = LegalizeOp(Node->getOperand(1));
1181 Tmp3 = LegalizeOp(Node->getOperand(2));
1182 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001183 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +00001184 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001185 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001186 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1187 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001188 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001189 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001190 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001191
1192 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001193 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001194 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001195 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1196 assert(Tmp3.Val && "Target didn't custom lower this node!");
Bill Wendlingf359fed2007-11-13 00:44:25 +00001197
1198 // The number of incoming and outgoing values should match; unless the final
1199 // outgoing value is a flag.
1200 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1201 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1202 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1203 MVT::Flag)) &&
Chris Lattnera1cec012006-05-17 17:55:45 +00001204 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001205
Chris Lattneraaa23d92006-05-16 22:53:20 +00001206 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001207 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +00001208 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendlingf359fed2007-11-13 00:44:25 +00001209 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1210 continue;
Chris Lattnera1cec012006-05-17 17:55:45 +00001211 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001212 if (Op.ResNo == i)
1213 Tmp2 = Tmp1;
1214 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1215 }
1216 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001217 case ISD::EXTRACT_SUBREG: {
1218 Tmp1 = LegalizeOp(Node->getOperand(0));
1219 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1220 assert(idx && "Operand must be a constant");
1221 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1222 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1223 }
1224 break;
1225 case ISD::INSERT_SUBREG: {
1226 Tmp1 = LegalizeOp(Node->getOperand(0));
1227 Tmp2 = LegalizeOp(Node->getOperand(1));
1228 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1229 assert(idx && "Operand must be a constant");
1230 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1231 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1232 }
1233 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001234 case ISD::BUILD_VECTOR:
1235 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001236 default: assert(0 && "This action is not supported yet!");
1237 case TargetLowering::Custom:
1238 Tmp3 = TLI.LowerOperation(Result, DAG);
1239 if (Tmp3.Val) {
1240 Result = Tmp3;
1241 break;
1242 }
1243 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001244 case TargetLowering::Expand:
1245 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001246 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001247 }
Chris Lattner29b23012006-03-19 01:17:20 +00001248 break;
1249 case ISD::INSERT_VECTOR_ELT:
1250 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1251 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1252 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1253 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1254
1255 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1256 Node->getValueType(0))) {
1257 default: assert(0 && "This action is not supported yet!");
1258 case TargetLowering::Legal:
1259 break;
1260 case TargetLowering::Custom:
Nate Begeman5743da52008-01-05 20:47:37 +00001261 Tmp4 = TLI.LowerOperation(Result, DAG);
1262 if (Tmp4.Val) {
1263 Result = Tmp4;
Chris Lattner29b23012006-03-19 01:17:20 +00001264 break;
1265 }
1266 // FALLTHROUGH
1267 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001268 // If the insert index is a constant, codegen this as a scalar_to_vector,
1269 // then a shuffle that inserts it into the right position in the vector.
1270 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1271 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1272 Tmp1.getValueType(), Tmp2);
1273
1274 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1275 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman5c441312007-06-14 22:58:02 +00001276 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner326870b2006-04-17 19:21:01 +00001277
1278 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1279 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1280 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001281 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +00001282 for (unsigned i = 0; i != NumElts; ++i) {
1283 if (i != InsertPos->getValue())
1284 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1285 else
1286 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1287 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001288 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1289 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +00001290
1291 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1292 Tmp1, ScVec, ShufMask);
1293 Result = LegalizeOp(Result);
1294 break;
1295 }
1296
Chris Lattner29b23012006-03-19 01:17:20 +00001297 // If the target doesn't support this, we have to spill the input vector
1298 // to a temporary stack slot, update the element, then reload it. This is
1299 // badness. We could also load the value into a vector register (either
1300 // with a "move to register" or "extload into register" instruction, then
1301 // permute it into place, if the idx is a constant and if the idx is
1302 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001303 MVT::ValueType VT = Tmp1.getValueType();
1304 MVT::ValueType EltVT = Tmp2.getValueType();
1305 MVT::ValueType IdxVT = Tmp3.getValueType();
1306 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattnerd6f7d442007-10-15 17:48:57 +00001307 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001308 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +00001309 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001310
1311 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001312 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1313 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001314 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001315 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1316 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1317 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001318 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +00001319 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001320 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +00001321 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +00001322 break;
1323 }
1324 }
1325 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001326 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001327 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1328 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1329 break;
1330 }
1331
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001332 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1333 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1334 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1335 Node->getValueType(0))) {
1336 default: assert(0 && "This action is not supported yet!");
1337 case TargetLowering::Legal:
1338 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001339 case TargetLowering::Custom:
1340 Tmp3 = TLI.LowerOperation(Result, DAG);
1341 if (Tmp3.Val) {
1342 Result = Tmp3;
1343 break;
1344 }
1345 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001346 case TargetLowering::Expand:
1347 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001348 break;
1349 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001350 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001351 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001352 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1353 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1354 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1355
1356 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001357 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1358 default: assert(0 && "Unknown operation action!");
1359 case TargetLowering::Legal:
1360 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1361 "vector shuffle should not be created if not legal!");
1362 break;
1363 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001364 Tmp3 = TLI.LowerOperation(Result, DAG);
1365 if (Tmp3.Val) {
1366 Result = Tmp3;
1367 break;
1368 }
1369 // FALLTHROUGH
1370 case TargetLowering::Expand: {
1371 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00001372 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng9fa89592006-04-05 06:07:11 +00001373 MVT::ValueType PtrVT = TLI.getPointerTy();
1374 SDOperand Mask = Node->getOperand(2);
1375 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001376 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001377 for (unsigned i = 0; i != NumElems; ++i) {
1378 SDOperand Arg = Mask.getOperand(i);
1379 if (Arg.getOpcode() == ISD::UNDEF) {
1380 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1381 } else {
1382 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1383 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1384 if (Idx < NumElems)
1385 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1386 DAG.getConstant(Idx, PtrVT)));
1387 else
1388 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1389 DAG.getConstant(Idx - NumElems, PtrVT)));
1390 }
1391 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001392 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001393 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001394 }
Chris Lattner6be79822006-04-04 17:23:26 +00001395 case TargetLowering::Promote: {
1396 // Change base type to a different vector type.
1397 MVT::ValueType OVT = Node->getValueType(0);
1398 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1399
1400 // Cast the two input vectors.
1401 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1402 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1403
1404 // Convert the shuffle mask to the right # elements.
1405 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1406 assert(Tmp3.Val && "Shuffle not legal?");
1407 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1408 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1409 break;
1410 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001411 }
1412 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001413
1414 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001415 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001416 Tmp2 = LegalizeOp(Node->getOperand(1));
1417 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001418 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001419 break;
1420
Dan Gohmana8665142007-06-25 16:23:39 +00001421 case ISD::EXTRACT_SUBVECTOR:
1422 Tmp1 = Node->getOperand(0);
1423 Tmp2 = LegalizeOp(Node->getOperand(1));
1424 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1425 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001426 break;
1427
Chris Lattner462505f2006-02-13 09:18:02 +00001428 case ISD::CALLSEQ_START: {
1429 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1430
1431 // Recursively Legalize all of the inputs of the call end that do not lead
1432 // to this call start. This ensures that any libcalls that need be inserted
1433 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001434 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001435 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001436 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1437 NodesLeadingTo);
1438 }
Chris Lattner462505f2006-02-13 09:18:02 +00001439
1440 // Now that we legalized all of the inputs (which may have inserted
1441 // libcalls) create the new CALLSEQ_START node.
1442 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1443
1444 // Merge in the last call, to ensure that this call start after the last
1445 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001446 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001447 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1448 Tmp1 = LegalizeOp(Tmp1);
1449 }
Chris Lattner462505f2006-02-13 09:18:02 +00001450
1451 // Do not try to legalize the target-specific arguments (#1+).
1452 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001453 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001454 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001455 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001456 }
1457
1458 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001459 AddLegalizedOperand(Op.getValue(0), Result);
1460 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1461 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1462
Chris Lattner462505f2006-02-13 09:18:02 +00001463 // Now that the callseq_start and all of the non-call nodes above this call
1464 // sequence have been legalized, legalize the call itself. During this
1465 // process, no libcalls can/will be inserted, guaranteeing that no calls
1466 // can overlap.
1467 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1468 SDOperand InCallSEQ = LastCALLSEQ_END;
1469 // Note that we are selecting this call!
1470 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1471 IsLegalizingCall = true;
1472
1473 // Legalize the call, starting from the CALLSEQ_END.
1474 LegalizeOp(LastCALLSEQ_END);
1475 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1476 return Result;
1477 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001478 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001479 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1480 // will cause this node to be legalized as well as handling libcalls right.
1481 if (LastCALLSEQ_END.Val != Node) {
1482 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001483 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001484 assert(I != LegalizedNodes.end() &&
1485 "Legalizing the call start should have legalized this node!");
1486 return I->second;
1487 }
1488
1489 // Otherwise, the call start has been legalized and everything is going
1490 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001491 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001492 // Do not try to legalize the target-specific arguments (#1+), except for
1493 // an optional flag input.
1494 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1495 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001496 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001497 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001498 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001499 }
1500 } else {
1501 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1502 if (Tmp1 != Node->getOperand(0) ||
1503 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001504 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001505 Ops[0] = Tmp1;
1506 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001507 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001508 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001509 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001510 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001511 // This finishes up call legalization.
1512 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001513
1514 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1515 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1516 if (Node->getNumValues() == 2)
1517 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1518 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001519 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng631ccc62007-08-16 23:50:06 +00001520 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerec26b482005-01-09 19:03:49 +00001521 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1522 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1523 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001524 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001525
Chris Lattnerd02b0542006-01-28 10:58:55 +00001526 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001527 Tmp2 = Result.getValue(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001528 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Cheng7f4ec822006-01-11 22:14:47 +00001529 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001530 case TargetLowering::Expand: {
1531 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1532 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1533 " not tell us which reg is the stack pointer!");
1534 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendlingf359fed2007-11-13 00:44:25 +00001535
1536 // Chain the dynamic stack allocation so that it doesn't modify the stack
1537 // pointer when other instructions are using the stack.
1538 Chain = DAG.getCALLSEQ_START(Chain,
1539 DAG.getConstant(0, TLI.getPointerTy()));
1540
Chris Lattner59b82f92006-01-15 08:54:32 +00001541 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001542 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1543 Chain = SP.getValue(1);
1544 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1545 unsigned StackAlign =
1546 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1547 if (Align > StackAlign)
Evan Chengcb6d65e2007-08-17 18:02:22 +00001548 SP = DAG.getNode(ISD::AND, VT, SP,
1549 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng631ccc62007-08-16 23:50:06 +00001550 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendlingf359fed2007-11-13 00:44:25 +00001551 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1552
1553 Tmp2 =
1554 DAG.getCALLSEQ_END(Chain,
1555 DAG.getConstant(0, TLI.getPointerTy()),
1556 DAG.getConstant(0, TLI.getPointerTy()),
1557 SDOperand());
1558
Chris Lattnerd02b0542006-01-28 10:58:55 +00001559 Tmp1 = LegalizeOp(Tmp1);
1560 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001561 break;
1562 }
1563 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001564 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1565 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001566 Tmp1 = LegalizeOp(Tmp3);
1567 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001568 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001569 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001570 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001571 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001572 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001573 // Since this op produce two values, make sure to remember that we
1574 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001575 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1576 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001577 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001578 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001579 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001580 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001581 bool Changed = false;
1582 // Legalize all of the operands of the inline asm, in case they are nodes
1583 // that need to be expanded or something. Note we skip the asm string and
1584 // all of the TargetConstant flags.
1585 SDOperand Op = LegalizeOp(Ops[0]);
1586 Changed = Op != Ops[0];
1587 Ops[0] = Op;
1588
1589 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1590 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1591 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1592 for (++i; NumVals; ++i, --NumVals) {
1593 SDOperand Op = LegalizeOp(Ops[i]);
1594 if (Op != Ops[i]) {
1595 Changed = true;
1596 Ops[i] = Op;
1597 }
1598 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001599 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001600
1601 if (HasInFlag) {
1602 Op = LegalizeOp(Ops.back());
1603 Changed |= Op != Ops.back();
1604 Ops.back() = Op;
1605 }
1606
1607 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001608 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001609
1610 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001611 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001612 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1613 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001614 }
Chris Lattner68a12142005-01-07 22:12:08 +00001615 case ISD::BR:
1616 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001617 // Ensure that libcalls are emitted before a branch.
1618 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1619 Tmp1 = LegalizeOp(Tmp1);
1620 LastCALLSEQ_END = DAG.getEntryNode();
1621
Chris Lattnerd02b0542006-01-28 10:58:55 +00001622 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001623 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001624 case ISD::BRIND:
1625 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1626 // Ensure that libcalls are emitted before a branch.
1627 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1628 Tmp1 = LegalizeOp(Tmp1);
1629 LastCALLSEQ_END = DAG.getEntryNode();
1630
1631 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1632 default: assert(0 && "Indirect target must be legal type (pointer)!");
1633 case Legal:
1634 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1635 break;
1636 }
1637 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1638 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001639 case ISD::BR_JT:
1640 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1641 // Ensure that libcalls are emitted before a branch.
1642 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1643 Tmp1 = LegalizeOp(Tmp1);
1644 LastCALLSEQ_END = DAG.getEntryNode();
1645
1646 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1647 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1648
1649 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1650 default: assert(0 && "This action is not supported yet!");
1651 case TargetLowering::Legal: break;
1652 case TargetLowering::Custom:
1653 Tmp1 = TLI.LowerOperation(Result, DAG);
1654 if (Tmp1.Val) Result = Tmp1;
1655 break;
1656 case TargetLowering::Expand: {
1657 SDOperand Chain = Result.getOperand(0);
1658 SDOperand Table = Result.getOperand(1);
1659 SDOperand Index = Result.getOperand(2);
1660
1661 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001662 MachineFunction &MF = DAG.getMachineFunction();
1663 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001664 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1665 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001666
1667 SDOperand LD;
1668 switch (EntrySize) {
1669 default: assert(0 && "Size of jump table not supported yet."); break;
1670 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1671 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1672 }
1673
Evan Cheng797d56f2007-11-09 01:32:10 +00001674 Addr = LD;
Jim Laskey70323a82006-12-14 19:17:33 +00001675 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001676 // For PIC, the sequence is:
1677 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng797d56f2007-11-09 01:32:10 +00001678 // RelocBase can be JumpTable, GOT or some sort of global base.
1679 if (PTy != MVT::i32)
1680 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1681 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1682 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng84a28d42006-10-30 08:00:44 +00001683 }
Evan Cheng797d56f2007-11-09 01:32:10 +00001684 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng84a28d42006-10-30 08:00:44 +00001685 }
1686 }
1687 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001688 case ISD::BRCOND:
1689 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001690 // Ensure that libcalls are emitted before a return.
1691 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1692 Tmp1 = LegalizeOp(Tmp1);
1693 LastCALLSEQ_END = DAG.getEntryNode();
1694
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001695 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1696 case Expand: assert(0 && "It's impossible to expand bools");
1697 case Legal:
1698 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1699 break;
1700 case Promote:
1701 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001702
1703 // The top bits of the promoted condition are not necessarily zero, ensure
1704 // that the value is properly zero extended.
Dan Gohman309d3d52007-06-22 14:59:07 +00001705 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerdb189382006-11-27 04:39:56 +00001706 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1707 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001708 break;
1709 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001710
1711 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001712 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001713
1714 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1715 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001716 case TargetLowering::Legal: break;
1717 case TargetLowering::Custom:
1718 Tmp1 = TLI.LowerOperation(Result, DAG);
1719 if (Tmp1.Val) Result = Tmp1;
1720 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001721 case TargetLowering::Expand:
1722 // Expand brcond's setcc into its constituent parts and create a BR_CC
1723 // Node.
1724 if (Tmp2.getOpcode() == ISD::SETCC) {
1725 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1726 Tmp2.getOperand(0), Tmp2.getOperand(1),
1727 Node->getOperand(2));
1728 } else {
1729 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1730 DAG.getCondCode(ISD::SETNE), Tmp2,
1731 DAG.getConstant(0, Tmp2.getValueType()),
1732 Node->getOperand(2));
1733 }
1734 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001735 }
1736 break;
1737 case ISD::BR_CC:
1738 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001739 // Ensure that libcalls are emitted before a branch.
1740 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1741 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001742 Tmp2 = Node->getOperand(2); // LHS
1743 Tmp3 = Node->getOperand(3); // RHS
1744 Tmp4 = Node->getOperand(1); // CC
1745
1746 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001747 LastCALLSEQ_END = DAG.getEntryNode();
1748
Nate Begeman7e7f4392006-02-01 07:19:44 +00001749 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1750 // the LHS is a legal SETCC itself. In this case, we need to compare
1751 // the result against zero to select between true and false values.
1752 if (Tmp3.Val == 0) {
1753 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1754 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001755 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001756
1757 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1758 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001759
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001760 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1761 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001762 case TargetLowering::Legal: break;
1763 case TargetLowering::Custom:
1764 Tmp4 = TLI.LowerOperation(Result, DAG);
1765 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001766 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001767 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001768 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001769 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001770 LoadSDNode *LD = cast<LoadSDNode>(Node);
1771 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1772 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001773
Evan Chenge71fe34d2006-10-09 20:57:25 +00001774 ISD::LoadExtType ExtType = LD->getExtensionType();
1775 if (ExtType == ISD::NON_EXTLOAD) {
1776 MVT::ValueType VT = Node->getValueType(0);
1777 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1778 Tmp3 = Result.getValue(0);
1779 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001780
Evan Chenge71fe34d2006-10-09 20:57:25 +00001781 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1782 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001783 case TargetLowering::Legal:
1784 // If this is an unaligned load and the target doesn't support it,
1785 // expand it.
1786 if (!TLI.allowsUnalignedMemoryAccesses()) {
1787 unsigned ABIAlignment = TLI.getTargetData()->
1788 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1789 if (LD->getAlignment() < ABIAlignment){
1790 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1791 TLI);
1792 Tmp3 = Result.getOperand(0);
1793 Tmp4 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001794 Tmp3 = LegalizeOp(Tmp3);
1795 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001796 }
1797 }
1798 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001799 case TargetLowering::Custom:
1800 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1801 if (Tmp1.Val) {
1802 Tmp3 = LegalizeOp(Tmp1);
1803 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001804 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001805 break;
1806 case TargetLowering::Promote: {
1807 // Only promote a load of vector type to another.
1808 assert(MVT::isVector(VT) && "Cannot promote this load!");
1809 // Change base type to a different vector type.
1810 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1811
1812 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001813 LD->getSrcValueOffset(),
1814 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001815 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1816 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001817 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001818 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001819 }
1820 // Since loads produce two values, make sure to remember that we
1821 // legalized both of them.
1822 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1823 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1824 return Op.ResNo ? Tmp4 : Tmp3;
1825 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001826 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001827 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1828 default: assert(0 && "This action is not supported yet!");
1829 case TargetLowering::Promote:
1830 assert(SrcVT == MVT::i1 &&
1831 "Can only promote extending LOAD from i1 -> i8!");
1832 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1833 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00001834 MVT::i8, LD->isVolatile(), LD->getAlignment());
Duncan Sandsd42c8122007-10-17 13:49:58 +00001835 Tmp1 = Result.getValue(0);
1836 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001837 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001838 case TargetLowering::Custom:
1839 isCustom = true;
1840 // FALLTHROUGH
1841 case TargetLowering::Legal:
1842 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1843 Tmp1 = Result.getValue(0);
1844 Tmp2 = Result.getValue(1);
1845
1846 if (isCustom) {
1847 Tmp3 = TLI.LowerOperation(Result, DAG);
1848 if (Tmp3.Val) {
1849 Tmp1 = LegalizeOp(Tmp3);
1850 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1851 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001852 } else {
1853 // If this is an unaligned load and the target doesn't support it,
1854 // expand it.
1855 if (!TLI.allowsUnalignedMemoryAccesses()) {
1856 unsigned ABIAlignment = TLI.getTargetData()->
1857 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1858 if (LD->getAlignment() < ABIAlignment){
1859 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1860 TLI);
1861 Tmp1 = Result.getOperand(0);
1862 Tmp2 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001863 Tmp1 = LegalizeOp(Tmp1);
1864 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001865 }
1866 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001867 }
1868 break;
1869 case TargetLowering::Expand:
1870 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1871 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1872 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001873 LD->getSrcValueOffset(),
1874 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001875 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1876 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1877 Tmp2 = LegalizeOp(Load.getValue(1));
1878 break;
1879 }
Chris Lattner296a83c2007-02-01 04:55:59 +00001880 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001881 // Turn the unsupported load into an EXTLOAD followed by an explicit
1882 // zero/sign extend inreg.
1883 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1884 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001885 LD->getSrcValueOffset(), SrcVT,
1886 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001887 SDOperand ValRes;
1888 if (ExtType == ISD::SEXTLOAD)
1889 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1890 Result, DAG.getValueType(SrcVT));
1891 else
1892 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1893 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1894 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1895 break;
1896 }
1897 // Since loads produce two values, make sure to remember that we legalized
1898 // both of them.
1899 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1900 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1901 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001902 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001903 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001904 case ISD::EXTRACT_ELEMENT: {
1905 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1906 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001907 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001908 case Legal:
1909 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1910 // 1 -> Hi
1911 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1912 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1913 TLI.getShiftAmountTy()));
1914 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1915 } else {
1916 // 0 -> Lo
1917 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1918 Node->getOperand(0));
1919 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001920 break;
1921 case Expand:
1922 // Get both the low and high parts.
1923 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1924 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1925 Result = Tmp2; // 1 -> Hi
1926 else
1927 Result = Tmp1; // 0 -> Lo
1928 break;
1929 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001930 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001931 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001932
1933 case ISD::CopyToReg:
1934 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001935
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001936 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001937 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001938 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001939 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001940 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001941 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001942 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001943 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001944 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001945 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001946 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1947 Tmp3);
1948 } else {
1949 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001950 }
1951
1952 // Since this produces two values, make sure to remember that we legalized
1953 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001954 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001955 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001956 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001957 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001958 break;
1959
1960 case ISD::RET:
1961 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001962
1963 // Ensure that libcalls are emitted before a return.
1964 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1965 Tmp1 = LegalizeOp(Tmp1);
1966 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001967
Chris Lattnerdc750592005-01-07 07:47:09 +00001968 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001969 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001970 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001971 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001972 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001973 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001974 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001975 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001976 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +00001977 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001978 SDOperand Lo, Hi;
1979 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00001980
1981 // Big endian systems want the hi reg first.
1982 if (!TLI.isLittleEndian())
1983 std::swap(Lo, Hi);
1984
Evan Cheng3432ab92006-12-11 19:27:14 +00001985 if (Hi.Val)
1986 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1987 else
1988 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001989 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001990 } else {
1991 SDNode *InVal = Tmp2.Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00001992 int InIx = Tmp2.ResNo;
1993 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
1994 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001995
Dan Gohmana8665142007-06-25 16:23:39 +00001996 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001997 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001998 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00001999 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002000 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002001 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002002 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002003 } else if (NumElems == 1) {
2004 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002005 Tmp2 = ScalarizeVectorOp(Tmp2);
2006 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002007 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002008
2009 // FIXME: Returns of gcc generic vectors smaller than a legal type
2010 // should be returned in integer registers!
2011
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002012 // The scalarized value type may not be legal, e.g. it might require
2013 // promotion or expansion. Relegalize the return.
2014 Result = LegalizeOp(Result);
2015 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002016 // FIXME: Returns of gcc generic vectors larger than a legal vector
2017 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002018 SDOperand Lo, Hi;
2019 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00002020 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002021 Result = LegalizeOp(Result);
2022 }
2023 }
Misha Brukman835702a2005-04-21 22:36:52 +00002024 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002025 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002026 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00002027 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002028 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002029 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002030 }
2031 break;
2032 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00002033 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00002034 break;
2035 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00002036 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00002037 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00002038 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00002039 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2040 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002041 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00002042 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00002043 break;
2044 case Expand: {
2045 SDOperand Lo, Hi;
Dan Gohman3b62d722007-06-27 16:08:04 +00002046 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002047 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002048 ExpandOp(Node->getOperand(i), Lo, Hi);
2049 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00002050 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00002051 if (Hi.Val) {
2052 NewValues.push_back(Hi);
2053 NewValues.push_back(Node->getOperand(i+1));
2054 }
Misha Brukman835702a2005-04-21 22:36:52 +00002055 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002056 }
2057 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002058 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002059 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002060
2061 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00002062 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00002063 else
Chris Lattner97af9d52006-08-08 01:09:31 +00002064 Result = DAG.getNode(ISD::RET, MVT::Other,
2065 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00002066 break;
2067 }
2068 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002069
Chris Lattner4d1ea712006-01-29 21:02:23 +00002070 if (Result.getOpcode() == ISD::RET) {
2071 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2072 default: assert(0 && "This action is not supported yet!");
2073 case TargetLowering::Legal: break;
2074 case TargetLowering::Custom:
2075 Tmp1 = TLI.LowerOperation(Result, DAG);
2076 if (Tmp1.Val) Result = Tmp1;
2077 break;
2078 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002079 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002080 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002081 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00002082 StoreSDNode *ST = cast<StoreSDNode>(Node);
2083 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2084 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00002085 int SVOffset = ST->getSrcValueOffset();
2086 unsigned Alignment = ST->getAlignment();
2087 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00002088
Evan Chengab51cf22006-10-13 21:14:26 +00002089 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002090 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2091 // FIXME: We shouldn't do this for TargetConstantFP's.
2092 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2093 // to phase ordering between legalized code and the dag combiner. This
2094 // probably means that we need to integrate dag combiner and legalizer
2095 // together.
Dale Johannesen98d3a082007-09-14 22:26:36 +00002096 // We generally can't do this one for long doubles.
Chris Lattner5e6fe052007-10-13 06:35:54 +00002097 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002098 if (CFP->getValueType(0) == MVT::f32 &&
2099 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen028084e2007-09-12 03:30:33 +00002100 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2101 convertToAPInt().getZExtValue(),
Dale Johannesen245dceb2007-09-11 18:32:33 +00002102 MVT::i32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00002103 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2104 SVOffset, isVolatile, Alignment);
2105 break;
2106 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002107 // If this target supports 64-bit registers, do a single 64-bit store.
2108 if (getTypeAction(MVT::i64) == Legal) {
2109 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2110 getZExtValue(), MVT::i64);
2111 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2112 SVOffset, isVolatile, Alignment);
2113 break;
2114 } else if (getTypeAction(MVT::i32) == Legal) {
2115 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2116 // stores. If the target supports neither 32- nor 64-bits, this
2117 // xform is certainly not worth it.
2118 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2119 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2120 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2121 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2122
2123 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2124 SVOffset, isVolatile, Alignment);
2125 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2126 getIntPtrConstant(4));
2127 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sands1826ded2007-10-28 12:59:45 +00002128 isVolatile, MinAlign(Alignment, 4U));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002129
2130 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2131 break;
2132 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002133 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002134 }
2135
Evan Chengab51cf22006-10-13 21:14:26 +00002136 switch (getTypeAction(ST->getStoredVT())) {
2137 case Legal: {
2138 Tmp3 = LegalizeOp(ST->getValue());
2139 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2140 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00002141
Evan Chengab51cf22006-10-13 21:14:26 +00002142 MVT::ValueType VT = Tmp3.getValueType();
2143 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2144 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002145 case TargetLowering::Legal:
2146 // If this is an unaligned store and the target doesn't support it,
2147 // expand it.
2148 if (!TLI.allowsUnalignedMemoryAccesses()) {
2149 unsigned ABIAlignment = TLI.getTargetData()->
2150 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2151 if (ST->getAlignment() < ABIAlignment)
2152 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2153 TLI);
2154 }
2155 break;
Evan Chengab51cf22006-10-13 21:14:26 +00002156 case TargetLowering::Custom:
2157 Tmp1 = TLI.LowerOperation(Result, DAG);
2158 if (Tmp1.Val) Result = Tmp1;
2159 break;
2160 case TargetLowering::Promote:
2161 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2162 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2163 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2164 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00002165 ST->getSrcValue(), SVOffset, isVolatile,
2166 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002167 break;
2168 }
2169 break;
2170 }
2171 case Promote:
2172 // Truncate the value and store the result.
2173 Tmp3 = PromoteOp(ST->getValue());
2174 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002175 SVOffset, ST->getStoredVT(),
2176 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002177 break;
2178
2179 case Expand:
2180 unsigned IncrementSize = 0;
2181 SDOperand Lo, Hi;
2182
2183 // If this is a vector type, then we have to calculate the increment as
2184 // the product of the element size in bytes, and the number of elements
2185 // in the high half of the vector.
Dan Gohmana8665142007-06-25 16:23:39 +00002186 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002187 SDNode *InVal = ST->getValue().Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00002188 int InIx = ST->getValue().ResNo;
2189 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2190 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Evan Chengab51cf22006-10-13 21:14:26 +00002191
Dan Gohmana8665142007-06-25 16:23:39 +00002192 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002193 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00002194 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002195 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002196 // Turn this into a normal store of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002197 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00002198 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002199 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002200 Result = LegalizeOp(Result);
2201 break;
2202 } else if (NumElems == 1) {
2203 // Turn this into a normal store of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002204 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00002205 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002206 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002207 // The scalarized value type may not be legal, e.g. it might require
2208 // promotion or expansion. Relegalize the scalar store.
2209 Result = LegalizeOp(Result);
2210 break;
2211 } else {
2212 SplitVectorOp(Node->getOperand(1), Lo, Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00002213 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2214 MVT::getSizeInBits(EVT)/8;
Evan Chengab51cf22006-10-13 21:14:26 +00002215 }
2216 } else {
2217 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00002218 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002219
2220 if (!TLI.isLittleEndian())
2221 std::swap(Lo, Hi);
2222 }
2223
2224 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002225 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002226
2227 if (Hi.Val == NULL) {
2228 // Must be int <-> float one-to-one expansion.
2229 Result = Lo;
2230 break;
2231 }
2232
Evan Chengab51cf22006-10-13 21:14:26 +00002233 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2234 getIntPtrConstant(IncrementSize));
2235 assert(isTypeLegal(Tmp2.getValueType()) &&
2236 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002237 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00002238 Alignment = MinAlign(Alignment, IncrementSize);
Evan Chengab51cf22006-10-13 21:14:26 +00002239 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002240 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002241 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2242 break;
2243 }
2244 } else {
2245 // Truncating store
2246 assert(isTypeLegal(ST->getValue().getValueType()) &&
2247 "Cannot handle illegal TRUNCSTORE yet!");
2248 Tmp3 = LegalizeOp(ST->getValue());
2249
2250 // The only promote case we handle is TRUNCSTORE:i1 X into
2251 // -> TRUNCSTORE:i8 (and X, 1)
2252 if (ST->getStoredVT() == MVT::i1 &&
2253 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2254 // Promote the bool to a mask then store.
2255 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2256 DAG.getConstant(1, Tmp3.getValueType()));
2257 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002258 SVOffset, MVT::i8,
2259 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002260 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2261 Tmp2 != ST->getBasePtr()) {
2262 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2263 ST->getOffset());
2264 }
2265
2266 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2267 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002268 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002269 case TargetLowering::Legal:
2270 // If this is an unaligned store and the target doesn't support it,
2271 // expand it.
2272 if (!TLI.allowsUnalignedMemoryAccesses()) {
2273 unsigned ABIAlignment = TLI.getTargetData()->
2274 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2275 if (ST->getAlignment() < ABIAlignment)
2276 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2277 TLI);
2278 }
2279 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002280 case TargetLowering::Custom:
2281 Tmp1 = TLI.LowerOperation(Result, DAG);
2282 if (Tmp1.Val) Result = Tmp1;
2283 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002284 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002285 }
2286 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002287 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002288 case ISD::PCMARKER:
2289 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002290 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002291 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002292 case ISD::STACKSAVE:
2293 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002294 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2295 Tmp1 = Result.getValue(0);
2296 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002297
Chris Lattnerb3266452006-01-13 02:50:02 +00002298 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2299 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002300 case TargetLowering::Legal: break;
2301 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002302 Tmp3 = TLI.LowerOperation(Result, DAG);
2303 if (Tmp3.Val) {
2304 Tmp1 = LegalizeOp(Tmp3);
2305 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002306 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002307 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002308 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002309 // Expand to CopyFromReg if the target set
2310 // StackPointerRegisterToSaveRestore.
2311 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002312 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002313 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002314 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002315 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002316 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2317 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002318 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002319 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002320 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002321
2322 // Since stacksave produce two values, make sure to remember that we
2323 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002324 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2325 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2326 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002327
Chris Lattnerb3266452006-01-13 02:50:02 +00002328 case ISD::STACKRESTORE:
2329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002331 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002332
2333 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2334 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002335 case TargetLowering::Legal: break;
2336 case TargetLowering::Custom:
2337 Tmp1 = TLI.LowerOperation(Result, DAG);
2338 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002339 break;
2340 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002341 // Expand to CopyToReg if the target set
2342 // StackPointerRegisterToSaveRestore.
2343 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2344 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2345 } else {
2346 Result = Tmp1;
2347 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002348 break;
2349 }
2350 break;
2351
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002352 case ISD::READCYCLECOUNTER:
2353 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002354 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002355 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2356 Node->getValueType(0))) {
2357 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002358 case TargetLowering::Legal:
2359 Tmp1 = Result.getValue(0);
2360 Tmp2 = Result.getValue(1);
2361 break;
Evan Cheng69739932006-11-29 08:26:18 +00002362 case TargetLowering::Custom:
2363 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002364 Tmp1 = LegalizeOp(Result.getValue(0));
2365 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002366 break;
2367 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002368
2369 // Since rdcc produce two values, make sure to remember that we legalized
2370 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002371 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2372 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002373 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002374
Chris Lattner39c67442005-01-14 22:08:15 +00002375 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002376 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2377 case Expand: assert(0 && "It's impossible to expand bools");
2378 case Legal:
2379 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2380 break;
2381 case Promote:
2382 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002383 // Make sure the condition is either zero or one.
Dan Gohman309d3d52007-06-22 14:59:07 +00002384 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattner3abb6362006-11-28 01:03:30 +00002385 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2386 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002387 break;
2388 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002389 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002390 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002391
Chris Lattnerd02b0542006-01-28 10:58:55 +00002392 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002393
Nate Begeman987121a2005-08-23 04:29:48 +00002394 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002395 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002396 case TargetLowering::Legal: break;
2397 case TargetLowering::Custom: {
2398 Tmp1 = TLI.LowerOperation(Result, DAG);
2399 if (Tmp1.Val) Result = Tmp1;
2400 break;
2401 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002402 case TargetLowering::Expand:
2403 if (Tmp1.getOpcode() == ISD::SETCC) {
2404 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2405 Tmp2, Tmp3,
2406 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2407 } else {
2408 Result = DAG.getSelectCC(Tmp1,
2409 DAG.getConstant(0, Tmp1.getValueType()),
2410 Tmp2, Tmp3, ISD::SETNE);
2411 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002412 break;
2413 case TargetLowering::Promote: {
2414 MVT::ValueType NVT =
2415 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2416 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002417 if (MVT::isVector(Tmp2.getValueType())) {
2418 ExtOp = ISD::BIT_CONVERT;
2419 TruncOp = ISD::BIT_CONVERT;
2420 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002421 ExtOp = ISD::ANY_EXTEND;
2422 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002423 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002424 ExtOp = ISD::FP_EXTEND;
2425 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002426 }
2427 // Promote each of the values to the new type.
2428 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2429 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2430 // Perform the larger operation, then round down.
2431 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2432 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2433 break;
2434 }
2435 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002436 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002437 case ISD::SELECT_CC: {
2438 Tmp1 = Node->getOperand(0); // LHS
2439 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002440 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2441 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002442 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002443
Nate Begeman7e7f4392006-02-01 07:19:44 +00002444 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2445
2446 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2447 // the LHS is a legal SETCC itself. In this case, we need to compare
2448 // the result against zero to select between true and false values.
2449 if (Tmp2.Val == 0) {
2450 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2451 CC = DAG.getCondCode(ISD::SETNE);
2452 }
2453 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2454
2455 // Everything is legal, see if we should expand this op or something.
2456 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2457 default: assert(0 && "This action is not supported yet!");
2458 case TargetLowering::Legal: break;
2459 case TargetLowering::Custom:
2460 Tmp1 = TLI.LowerOperation(Result, DAG);
2461 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002462 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002463 }
2464 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002465 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002466 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002467 Tmp1 = Node->getOperand(0);
2468 Tmp2 = Node->getOperand(1);
2469 Tmp3 = Node->getOperand(2);
2470 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2471
2472 // If we had to Expand the SetCC operands into a SELECT node, then it may
2473 // not always be possible to return a true LHS & RHS. In this case, just
2474 // return the value we legalized, returned in the LHS
2475 if (Tmp2.Val == 0) {
2476 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002477 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002478 }
Nate Begeman987121a2005-08-23 04:29:48 +00002479
Chris Lattnerf263a232006-01-30 22:43:50 +00002480 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002481 default: assert(0 && "Cannot handle this action for SETCC yet!");
2482 case TargetLowering::Custom:
2483 isCustom = true;
2484 // FALLTHROUGH.
2485 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002486 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002487 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002488 Tmp4 = TLI.LowerOperation(Result, DAG);
2489 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002490 }
Nate Begeman987121a2005-08-23 04:29:48 +00002491 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002492 case TargetLowering::Promote: {
2493 // First step, figure out the appropriate operation to use.
2494 // Allow SETCC to not be supported for all legal data types
2495 // Mostly this targets FP
2496 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002497 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002498
2499 // Scan for the appropriate larger type to use.
2500 while (1) {
2501 NewInTy = (MVT::ValueType)(NewInTy+1);
2502
2503 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2504 "Fell off of the edge of the integer world");
2505 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2506 "Fell off of the edge of the floating point world");
2507
2508 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002509 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002510 break;
2511 }
2512 if (MVT::isInteger(NewInTy))
2513 assert(0 && "Cannot promote Legal Integer SETCC yet");
2514 else {
2515 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2516 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2517 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002518 Tmp1 = LegalizeOp(Tmp1);
2519 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002520 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002521 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002522 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002523 }
Nate Begeman987121a2005-08-23 04:29:48 +00002524 case TargetLowering::Expand:
2525 // Expand a setcc node into a select_cc of the same condition, lhs, and
2526 // rhs that selects between const 1 (true) and const 0 (false).
2527 MVT::ValueType VT = Node->getValueType(0);
2528 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2529 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002530 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002531 break;
2532 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002533 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002534 case ISD::MEMSET:
2535 case ISD::MEMCPY:
2536 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002537 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002538 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2539
2540 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2541 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2542 case Expand: assert(0 && "Cannot expand a byte!");
2543 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002544 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002545 break;
2546 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002547 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002548 break;
2549 }
2550 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002551 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002552 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002553
2554 SDOperand Tmp4;
2555 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002556 case Expand: {
2557 // Length is too big, just take the lo-part of the length.
2558 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002559 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002560 break;
2561 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002562 case Legal:
2563 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002564 break;
2565 case Promote:
2566 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002567 break;
2568 }
2569
2570 SDOperand Tmp5;
2571 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2572 case Expand: assert(0 && "Cannot expand this yet!");
2573 case Legal:
2574 Tmp5 = LegalizeOp(Node->getOperand(4));
2575 break;
2576 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002577 Tmp5 = PromoteOp(Node->getOperand(4));
2578 break;
2579 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002580
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002581 SDOperand Tmp6;
2582 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2583 case Expand: assert(0 && "Cannot expand this yet!");
2584 case Legal:
2585 Tmp6 = LegalizeOp(Node->getOperand(5));
2586 break;
2587 case Promote:
2588 Tmp6 = PromoteOp(Node->getOperand(5));
2589 break;
2590 }
2591
Chris Lattner3c0dd462005-01-16 07:29:19 +00002592 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2593 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002594 case TargetLowering::Custom:
2595 isCustom = true;
2596 // FALLTHROUGH
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002597 case TargetLowering::Legal: {
2598 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2599 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002600 if (isCustom) {
2601 Tmp1 = TLI.LowerOperation(Result, DAG);
2602 if (Tmp1.Val) Result = Tmp1;
2603 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002604 break;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002605 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002606 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002607 // Otherwise, the target does not support this operation. Lower the
2608 // operation to an explicit libcall as appropriate.
2609 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002610 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002611 TargetLowering::ArgListTy Args;
2612 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002613
Reid Spencer6dced922005-01-12 14:53:45 +00002614 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002615 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002616 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00002617 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002618 // Extend the (previously legalized) ubyte argument to be an int value
2619 // for the call.
2620 if (Tmp3.getValueType() > MVT::i32)
2621 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2622 else
2623 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002624 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencere63b6512006-12-31 05:55:36 +00002625 Args.push_back(Entry);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002626 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002627 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002628
2629 FnName = "memset";
2630 } else if (Node->getOpcode() == ISD::MEMCPY ||
2631 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002632 Entry.Ty = IntPtrTy;
Reid Spencer791864c2007-01-03 04:22:32 +00002633 Entry.Node = Tmp2; Args.push_back(Entry);
2634 Entry.Node = Tmp3; Args.push_back(Entry);
2635 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002636 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2637 } else {
2638 assert(0 && "Unknown op!");
2639 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002640
Chris Lattner85d70c62005-01-11 05:57:22 +00002641 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002642 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002643 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002644 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002645 break;
2646 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002647 }
2648 break;
2649 }
Chris Lattner5385db52005-05-09 20:23:03 +00002650
Chris Lattner4157c412005-04-02 04:00:59 +00002651 case ISD::SHL_PARTS:
2652 case ISD::SRA_PARTS:
2653 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002654 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002655 bool Changed = false;
2656 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2657 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2658 Changed |= Ops.back() != Node->getOperand(i);
2659 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002660 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002661 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002662
Evan Cheng870e4f82006-01-09 18:31:59 +00002663 switch (TLI.getOperationAction(Node->getOpcode(),
2664 Node->getValueType(0))) {
2665 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002666 case TargetLowering::Legal: break;
2667 case TargetLowering::Custom:
2668 Tmp1 = TLI.LowerOperation(Result, DAG);
2669 if (Tmp1.Val) {
2670 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002671 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002672 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002673 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2674 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002675 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002676 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002677 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002678 return RetVal;
2679 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002680 break;
2681 }
2682
Chris Lattner13fe99c2005-04-02 05:00:07 +00002683 // Since these produce multiple values, make sure to remember that we
2684 // legalized all of them.
2685 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2686 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2687 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002688 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002689
2690 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002691 case ISD::ADD:
2692 case ISD::SUB:
2693 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002694 case ISD::MULHS:
2695 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002696 case ISD::UDIV:
2697 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002698 case ISD::AND:
2699 case ISD::OR:
2700 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002701 case ISD::SHL:
2702 case ISD::SRL:
2703 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002704 case ISD::FADD:
2705 case ISD::FSUB:
2706 case ISD::FMUL:
2707 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00002708 case ISD::FPOW:
Chris Lattnerdc750592005-01-07 07:47:09 +00002709 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002710 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2711 case Expand: assert(0 && "Not possible");
2712 case Legal:
2713 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2714 break;
2715 case Promote:
2716 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2717 break;
2718 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002719
2720 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002721
Andrew Lenharth72594262005-12-24 23:42:32 +00002722 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002723 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002724 case TargetLowering::Legal: break;
2725 case TargetLowering::Custom:
2726 Tmp1 = TLI.LowerOperation(Result, DAG);
2727 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002728 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002729 case TargetLowering::Expand: {
Dan Gohmana1603612007-10-08 18:33:35 +00002730 MVT::ValueType VT = Op.getValueType();
2731
2732 // See if multiply or divide can be lowered using two-result operations.
2733 SDVTList VTs = DAG.getVTList(VT, VT);
2734 if (Node->getOpcode() == ISD::MUL) {
2735 // We just need the low half of the multiply; try both the signed
2736 // and unsigned forms. If the target supports both SMUL_LOHI and
2737 // UMUL_LOHI, form a preference by checking which forms of plain
2738 // MULH it supports.
2739 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2740 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2741 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2742 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2743 unsigned OpToUse = 0;
2744 if (HasSMUL_LOHI && !HasMULHS) {
2745 OpToUse = ISD::SMUL_LOHI;
2746 } else if (HasUMUL_LOHI && !HasMULHU) {
2747 OpToUse = ISD::UMUL_LOHI;
2748 } else if (HasSMUL_LOHI) {
2749 OpToUse = ISD::SMUL_LOHI;
2750 } else if (HasUMUL_LOHI) {
2751 OpToUse = ISD::UMUL_LOHI;
2752 }
2753 if (OpToUse) {
2754 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2755 break;
2756 }
2757 }
2758 if (Node->getOpcode() == ISD::MULHS &&
2759 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2760 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2761 break;
2762 }
2763 if (Node->getOpcode() == ISD::MULHU &&
2764 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2765 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2766 break;
2767 }
2768 if (Node->getOpcode() == ISD::SDIV &&
2769 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2770 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2771 break;
2772 }
2773 if (Node->getOpcode() == ISD::UDIV &&
2774 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2775 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2776 break;
2777 }
2778
Dan Gohman2a7de412007-10-11 23:57:53 +00002779 // Check to see if we have a libcall for this operator.
2780 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2781 bool isSigned = false;
2782 switch (Node->getOpcode()) {
2783 case ISD::UDIV:
2784 case ISD::SDIV:
2785 if (VT == MVT::i32) {
2786 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng31cbddf2007-01-12 02:11:51 +00002787 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman2a7de412007-10-11 23:57:53 +00002788 isSigned = Node->getOpcode() == ISD::SDIV;
2789 }
2790 break;
2791 case ISD::FPOW:
Duncan Sands53c954f2008-01-10 10:28:30 +00002792 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
2793 RTLIB::POW_PPCF128);
Dan Gohman2a7de412007-10-11 23:57:53 +00002794 break;
2795 default: break;
2796 }
2797 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2798 SDOperand Dummy;
2799 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002800 break;
2801 }
2802
Chris Lattner87f08092006-04-02 03:57:31 +00002803 assert(MVT::isVector(Node->getValueType(0)) &&
2804 "Cannot expand this binary operator!");
2805 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman2a7de412007-10-11 23:57:53 +00002806 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattner87f08092006-04-02 03:57:31 +00002807 break;
2808 }
Evan Cheng119266e2006-04-12 21:20:24 +00002809 case TargetLowering::Promote: {
2810 switch (Node->getOpcode()) {
2811 default: assert(0 && "Do not know how to promote this BinOp!");
2812 case ISD::AND:
2813 case ISD::OR:
2814 case ISD::XOR: {
2815 MVT::ValueType OVT = Node->getValueType(0);
2816 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2817 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2818 // Bit convert each of the values to the new type.
2819 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2820 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2821 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2822 // Bit convert the result back the original type.
2823 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2824 break;
2825 }
2826 }
2827 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002828 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002829 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002830
Dan Gohman12334ac2007-10-05 14:17:22 +00002831 case ISD::SMUL_LOHI:
2832 case ISD::UMUL_LOHI:
2833 case ISD::SDIVREM:
2834 case ISD::UDIVREM:
2835 // These nodes will only be produced by target-specific lowering, so
2836 // they shouldn't be here if they aren't legal.
Duncan Sands052c8432007-10-16 09:07:20 +00002837 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman12334ac2007-10-05 14:17:22 +00002838 "This must be legal!");
Dan Gohmana1603612007-10-08 18:33:35 +00002839
2840 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2841 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2842 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman12334ac2007-10-05 14:17:22 +00002843 break;
2844
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002845 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2846 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2847 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2848 case Expand: assert(0 && "Not possible");
2849 case Legal:
2850 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2851 break;
2852 case Promote:
2853 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2854 break;
2855 }
2856
2857 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2858
2859 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2860 default: assert(0 && "Operation not supported");
2861 case TargetLowering::Custom:
2862 Tmp1 = TLI.LowerOperation(Result, DAG);
2863 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002864 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002865 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002866 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00002867 // If this target supports fabs/fneg natively and select is cheap,
2868 // do this efficiently.
2869 if (!TLI.isSelectExpensive() &&
2870 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2871 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00002872 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00002873 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002874 // Get the sign bit of the RHS.
2875 MVT::ValueType IVT =
2876 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2877 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2878 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2879 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2880 // Get the absolute value of the result.
2881 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2882 // Select between the nabs and abs value based on the sign bit of
2883 // the input.
2884 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2885 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2886 AbsVal),
2887 AbsVal);
2888 Result = LegalizeOp(Result);
2889 break;
2890 }
2891
2892 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002893 MVT::ValueType NVT =
2894 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2895 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2896 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2897 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002898 break;
2899 }
Evan Cheng003feb02007-01-04 21:56:39 +00002900 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002901 break;
2902
Nate Begeman5965bd12006-02-17 05:43:56 +00002903 case ISD::ADDC:
2904 case ISD::SUBC:
2905 Tmp1 = LegalizeOp(Node->getOperand(0));
2906 Tmp2 = LegalizeOp(Node->getOperand(1));
2907 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2908 // Since this produces two values, make sure to remember that we legalized
2909 // both of them.
2910 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2911 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2912 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002913
Nate Begeman5965bd12006-02-17 05:43:56 +00002914 case ISD::ADDE:
2915 case ISD::SUBE:
2916 Tmp1 = LegalizeOp(Node->getOperand(0));
2917 Tmp2 = LegalizeOp(Node->getOperand(1));
2918 Tmp3 = LegalizeOp(Node->getOperand(2));
2919 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2920 // Since this produces two values, make sure to remember that we legalized
2921 // both of them.
2922 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2923 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2924 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002925
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002926 case ISD::BUILD_PAIR: {
2927 MVT::ValueType PairTy = Node->getValueType(0);
2928 // TODO: handle the case where the Lo and Hi operands are not of legal type
2929 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2930 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2931 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002932 case TargetLowering::Promote:
2933 case TargetLowering::Custom:
2934 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002935 case TargetLowering::Legal:
2936 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2937 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2938 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002939 case TargetLowering::Expand:
2940 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2941 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2942 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2943 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2944 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002945 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002946 break;
2947 }
2948 break;
2949 }
2950
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002951 case ISD::UREM:
2952 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002953 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002954 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2955 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002956
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002957 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002958 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2959 case TargetLowering::Custom:
2960 isCustom = true;
2961 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002962 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002963 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002964 if (isCustom) {
2965 Tmp1 = TLI.LowerOperation(Result, DAG);
2966 if (Tmp1.Val) Result = Tmp1;
2967 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002968 break;
Dan Gohmana1603612007-10-08 18:33:35 +00002969 case TargetLowering::Expand: {
Evan Cheng1fc7c362006-09-18 23:28:33 +00002970 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002971 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohmana1603612007-10-08 18:33:35 +00002972 MVT::ValueType VT = Node->getValueType(0);
2973
2974 // See if remainder can be lowered using two-result operations.
2975 SDVTList VTs = DAG.getVTList(VT, VT);
2976 if (Node->getOpcode() == ISD::SREM &&
2977 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2978 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2979 break;
2980 }
2981 if (Node->getOpcode() == ISD::UREM &&
2982 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2983 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2984 break;
2985 }
2986
2987 if (MVT::isInteger(VT)) {
2988 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002989 TargetLowering::Legal) {
2990 // X % Y -> X-X/Y*Y
Evan Cheng1fc7c362006-09-18 23:28:33 +00002991 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002992 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2993 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman08143e32007-11-05 23:35:22 +00002994 } else if (MVT::isVector(VT)) {
2995 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002996 } else {
Dan Gohmana1603612007-10-08 18:33:35 +00002997 assert(VT == MVT::i32 &&
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002998 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00002999 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3000 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003001 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003002 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003003 }
Dan Gohmanccfc0282007-11-06 22:11:54 +00003004 } else {
3005 assert(MVT::isFloatingPoint(VT) &&
3006 "remainder op must have integer or floating-point type");
Dan Gohman08143e32007-11-05 23:35:22 +00003007 if (MVT::isVector(VT)) {
3008 Result = LegalizeOp(UnrollVectorOp(Op));
3009 } else {
3010 // Floating point mod -> fmod libcall.
Duncan Sands53c954f2008-01-10 10:28:30 +00003011 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3012 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman08143e32007-11-05 23:35:22 +00003013 SDOperand Dummy;
3014 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3015 false/*sign irrelevant*/, Dummy);
3016 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003017 }
3018 break;
3019 }
Dan Gohmana1603612007-10-08 18:33:35 +00003020 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003021 break;
Nate Begemane74795c2006-01-25 18:21:52 +00003022 case ISD::VAARG: {
3023 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3024 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3025
Chris Lattner364b89a2006-01-28 07:42:08 +00003026 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00003027 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3028 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003029 case TargetLowering::Custom:
3030 isCustom = true;
3031 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003032 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003033 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3034 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003035 Tmp1 = Result.getValue(1);
3036
3037 if (isCustom) {
3038 Tmp2 = TLI.LowerOperation(Result, DAG);
3039 if (Tmp2.Val) {
3040 Result = LegalizeOp(Tmp2);
3041 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3042 }
3043 }
Nate Begemane74795c2006-01-25 18:21:52 +00003044 break;
3045 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00003046 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00003047 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00003048 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00003049 // Increment the pointer, VAList, to the next vaarg
3050 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3051 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3052 TLI.getPointerTy()));
3053 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00003054 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
3055 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00003056 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003057 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003058 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00003059 Result = LegalizeOp(Result);
3060 break;
3061 }
3062 }
3063 // Since VAARG produces two values, make sure to remember that we
3064 // legalized both of them.
3065 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003066 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3067 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00003068 }
3069
3070 case ISD::VACOPY:
3071 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3072 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3073 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3074
3075 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3076 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003077 case TargetLowering::Custom:
3078 isCustom = true;
3079 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003080 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003081 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3082 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003083 if (isCustom) {
3084 Tmp1 = TLI.LowerOperation(Result, DAG);
3085 if (Tmp1.Val) Result = Tmp1;
3086 }
Nate Begemane74795c2006-01-25 18:21:52 +00003087 break;
3088 case TargetLowering::Expand:
3089 // This defaults to loading a pointer from the input and storing it to the
3090 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003091 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00003092 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00003093 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
3094 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00003095 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
3096 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00003097 break;
3098 }
3099 break;
3100
3101 case ISD::VAEND:
3102 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3103 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3104
3105 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3106 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003107 case TargetLowering::Custom:
3108 isCustom = true;
3109 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003110 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003111 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003112 if (isCustom) {
3113 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3114 if (Tmp1.Val) Result = Tmp1;
3115 }
Nate Begemane74795c2006-01-25 18:21:52 +00003116 break;
3117 case TargetLowering::Expand:
3118 Result = Tmp1; // Default to a no-op, return the chain
3119 break;
3120 }
3121 break;
3122
3123 case ISD::VASTART:
3124 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3125 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3126
Chris Lattnerd02b0542006-01-28 10:58:55 +00003127 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3128
Nate Begemane74795c2006-01-25 18:21:52 +00003129 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3130 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003131 case TargetLowering::Legal: break;
3132 case TargetLowering::Custom:
3133 Tmp1 = TLI.LowerOperation(Result, DAG);
3134 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00003135 break;
3136 }
3137 break;
3138
Nate Begeman1b8121b2006-01-11 21:21:00 +00003139 case ISD::ROTL:
3140 case ISD::ROTR:
3141 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3142 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00003143 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00003144 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3145 default:
3146 assert(0 && "ROTL/ROTR legalize operation not supported");
3147 break;
3148 case TargetLowering::Legal:
3149 break;
3150 case TargetLowering::Custom:
3151 Tmp1 = TLI.LowerOperation(Result, DAG);
3152 if (Tmp1.Val) Result = Tmp1;
3153 break;
3154 case TargetLowering::Promote:
3155 assert(0 && "Do not know how to promote ROTL/ROTR");
3156 break;
3157 case TargetLowering::Expand:
3158 assert(0 && "Do not know how to expand ROTL/ROTR");
3159 break;
3160 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00003161 break;
3162
Nate Begeman2fba8a32006-01-14 03:14:10 +00003163 case ISD::BSWAP:
3164 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3165 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003166 case TargetLowering::Custom:
3167 assert(0 && "Cannot custom legalize this yet!");
3168 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003169 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003170 break;
3171 case TargetLowering::Promote: {
3172 MVT::ValueType OVT = Tmp1.getValueType();
3173 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohman1796f1f2007-05-18 17:52:13 +00003174 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00003175
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003176 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3177 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3178 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3179 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3180 break;
3181 }
3182 case TargetLowering::Expand:
3183 Result = ExpandBSWAP(Tmp1);
3184 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003185 }
3186 break;
3187
Andrew Lenharth5e177822005-05-03 17:19:30 +00003188 case ISD::CTPOP:
3189 case ISD::CTTZ:
3190 case ISD::CTLZ:
3191 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3192 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00003193 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00003194 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003195 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00003196 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00003197 TargetLowering::Custom) {
3198 Tmp1 = TLI.LowerOperation(Result, DAG);
3199 if (Tmp1.Val) {
3200 Result = Tmp1;
3201 }
Scott Michel34e2d222007-07-30 21:00:31 +00003202 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003203 break;
3204 case TargetLowering::Promote: {
3205 MVT::ValueType OVT = Tmp1.getValueType();
3206 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00003207
3208 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00003209 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3210 // Perform the larger operation, then subtract if needed.
3211 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003212 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00003213 case ISD::CTPOP:
3214 Result = Tmp1;
3215 break;
3216 case ISD::CTTZ:
3217 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00003218 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003219 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00003220 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003221 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel34e2d222007-07-30 21:00:31 +00003222 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003223 break;
3224 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003225 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003226 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003227 DAG.getConstant(MVT::getSizeInBits(NVT) -
3228 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00003229 break;
3230 }
3231 break;
3232 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003233 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003234 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003235 break;
3236 }
3237 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003238
Chris Lattner13fe99c2005-04-02 05:00:07 +00003239 // Unary operators
3240 case ISD::FABS:
3241 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00003242 case ISD::FSQRT:
3243 case ISD::FSIN:
3244 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003245 Tmp1 = LegalizeOp(Node->getOperand(0));
3246 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003247 case TargetLowering::Promote:
3248 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00003249 isCustom = true;
3250 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00003251 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003252 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00003253 if (isCustom) {
3254 Tmp1 = TLI.LowerOperation(Result, DAG);
3255 if (Tmp1.Val) Result = Tmp1;
3256 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003257 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00003258 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003259 switch (Node->getOpcode()) {
3260 default: assert(0 && "Unreachable!");
3261 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003262 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3263 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003264 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00003265 break;
Chris Lattner80026402005-04-30 04:43:14 +00003266 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003267 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3268 MVT::ValueType VT = Node->getValueType(0);
3269 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003270 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003271 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3272 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00003273 break;
3274 }
3275 case ISD::FSQRT:
3276 case ISD::FSIN:
3277 case ISD::FCOS: {
3278 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman2a7de412007-10-11 23:57:53 +00003279
3280 // Expand unsupported unary vector operators by unrolling them.
3281 if (MVT::isVector(VT)) {
3282 Result = LegalizeOp(UnrollVectorOp(Op));
3283 break;
3284 }
3285
Evan Cheng31cbddf2007-01-12 02:11:51 +00003286 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00003287 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00003288 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00003289 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3290 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003291 break;
3292 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00003293 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3294 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003295 break;
3296 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00003297 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3298 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003299 break;
Chris Lattner80026402005-04-30 04:43:14 +00003300 default: assert(0 && "Unreachable!");
3301 }
Nate Begeman77558da2005-08-04 21:43:28 +00003302 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003303 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3304 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00003305 break;
3306 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003307 }
3308 break;
3309 }
3310 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003311 case ISD::FPOWI: {
Dan Gohman2a7de412007-10-11 23:57:53 +00003312 MVT::ValueType VT = Node->getValueType(0);
3313
3314 // Expand unsupported unary vector operators by unrolling them.
3315 if (MVT::isVector(VT)) {
3316 Result = LegalizeOp(UnrollVectorOp(Op));
3317 break;
3318 }
3319
3320 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands53c954f2008-01-10 10:28:30 +00003321 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3322 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003323 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003324 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3325 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003326 break;
3327 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003328 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003329 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner87bc3e72008-01-16 07:45:30 +00003330 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3331 Node->getValueType(0));
Dan Gohmana8665142007-06-25 16:23:39 +00003332 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3333 // The input has to be a vector type, we have to either scalarize it, pack
3334 // it, or convert it based on whether the input vector type is legal.
3335 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00003336 int InIx = Node->getOperand(0).ResNo;
3337 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3338 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmana8665142007-06-25 16:23:39 +00003339
3340 // Figure out if there is a simple type corresponding to this Vector
3341 // type. If so, convert to the vector type.
3342 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3343 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003344 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003345 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3346 LegalizeOp(Node->getOperand(0)));
3347 break;
3348 } else if (NumElems == 1) {
3349 // Turn this into a bit convert of the scalar input.
3350 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3351 ScalarizeVectorOp(Node->getOperand(0)));
3352 break;
3353 } else {
3354 // FIXME: UNIMP! Store then reload
3355 assert(0 && "Cast from unsupported vector type not implemented yet!");
3356 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003357 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003358 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3359 Node->getOperand(0).getValueType())) {
3360 default: assert(0 && "Unknown operation action!");
3361 case TargetLowering::Expand:
Chris Lattner87bc3e72008-01-16 07:45:30 +00003362 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3363 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00003364 break;
3365 case TargetLowering::Legal:
3366 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003367 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003368 break;
3369 }
3370 }
3371 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00003372
Chris Lattner13fe99c2005-04-02 05:00:07 +00003373 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00003374 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003375 case ISD::UINT_TO_FP: {
3376 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00003377 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3378 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00003379 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003380 Node->getOperand(0).getValueType())) {
3381 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003382 case TargetLowering::Custom:
3383 isCustom = true;
3384 // FALLTHROUGH
3385 case TargetLowering::Legal:
3386 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003387 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003388 if (isCustom) {
3389 Tmp1 = TLI.LowerOperation(Result, DAG);
3390 if (Tmp1.Val) Result = Tmp1;
3391 }
3392 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003393 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00003394 Result = ExpandLegalINT_TO_FP(isSigned,
3395 LegalizeOp(Node->getOperand(0)),
3396 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003397 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003398 case TargetLowering::Promote:
3399 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3400 Node->getValueType(0),
3401 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003402 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00003403 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003404 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00003405 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003406 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3407 Node->getValueType(0), Node->getOperand(0));
3408 break;
3409 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003410 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003411 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003412 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3413 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003414 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003415 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3416 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003417 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003418 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3419 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003420 break;
3421 }
3422 break;
3423 }
3424 case ISD::TRUNCATE:
3425 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3426 case Legal:
3427 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003428 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003429 break;
3430 case Expand:
3431 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3432
3433 // Since the result is legal, we should just be able to truncate the low
3434 // part of the source.
3435 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3436 break;
3437 case Promote:
3438 Result = PromoteOp(Node->getOperand(0));
3439 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3440 break;
3441 }
3442 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003443
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003444 case ISD::FP_TO_SINT:
3445 case ISD::FP_TO_UINT:
3446 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3447 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00003448 Tmp1 = LegalizeOp(Node->getOperand(0));
3449
Chris Lattner44fe26f2005-07-29 00:11:56 +00003450 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3451 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003452 case TargetLowering::Custom:
3453 isCustom = true;
3454 // FALLTHROUGH
3455 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003456 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003457 if (isCustom) {
3458 Tmp1 = TLI.LowerOperation(Result, DAG);
3459 if (Tmp1.Val) Result = Tmp1;
3460 }
3461 break;
3462 case TargetLowering::Promote:
3463 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3464 Node->getOpcode() == ISD::FP_TO_SINT);
3465 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00003466 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00003467 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3468 SDOperand True, False;
3469 MVT::ValueType VT = Node->getOperand(0).getValueType();
3470 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenb59d25f2007-09-19 17:53:26 +00003471 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003472 const uint64_t zero[] = {0, 0};
3473 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3474 uint64_t x = 1ULL << ShiftAmt;
Neil Booth5f009732007-10-07 11:45:55 +00003475 (void)apf.convertFromZeroExtendedInteger
3476 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003477 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begeman36853ee2005-08-14 01:20:53 +00003478 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3479 Node->getOperand(0), Tmp2, ISD::SETLT);
3480 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3481 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00003482 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00003483 Tmp2));
3484 False = DAG.getNode(ISD::XOR, NVT, False,
3485 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003486 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3487 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00003488 } else {
3489 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3490 }
3491 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003492 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003493 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003494 case Expand: {
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003495 MVT::ValueType VT = Op.getValueType();
Dale Johannesen666323e2007-10-10 01:01:31 +00003496 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesen6472eb62007-10-11 23:32:15 +00003497 // Convert ppcf128 to i32
Dale Johannesen666323e2007-10-10 01:01:31 +00003498 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Dale Johannesen6472eb62007-10-11 23:32:15 +00003499 if (Node->getOpcode()==ISD::FP_TO_SINT)
3500 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
Dale Johannesen666323e2007-10-10 01:01:31 +00003501 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3502 (DAG.getNode(ISD::FP_ROUND_INREG,
3503 MVT::ppcf128, Node->getOperand(0),
3504 DAG.getValueType(MVT::f64)))));
Dale Johannesen6472eb62007-10-11 23:32:15 +00003505 else {
3506 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3507 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3508 Tmp2 = DAG.getConstantFP(apf, OVT);
3509 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3510 // FIXME: generated code sucks.
3511 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3512 DAG.getNode(ISD::ADD, MVT::i32,
3513 DAG.getNode(ISD::FP_TO_SINT, VT,
3514 DAG.getNode(ISD::FSUB, OVT,
3515 Node->getOperand(0), Tmp2)),
3516 DAG.getConstant(0x80000000, MVT::i32)),
3517 DAG.getNode(ISD::FP_TO_SINT, VT,
3518 Node->getOperand(0)),
3519 DAG.getCondCode(ISD::SETGE));
3520 }
Dale Johannesen666323e2007-10-10 01:01:31 +00003521 break;
3522 }
Dale Johannesen6472eb62007-10-11 23:32:15 +00003523 // Convert f32 / f64 to i32 / i64.
Evan Cheng31cbddf2007-01-12 02:11:51 +00003524 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003525 switch (Node->getOpcode()) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003526 case ISD::FP_TO_SINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003527 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003528 LC = (VT == MVT::i32)
3529 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003530 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003531 LC = (VT == MVT::i32)
3532 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003533 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003534 assert(VT == MVT::i64);
Dale Johannesenc0154c02007-10-05 20:04:43 +00003535 LC = RTLIB::FPTOSINT_F80_I64;
3536 }
3537 else if (OVT == MVT::ppcf128) {
3538 assert(VT == MVT::i64);
3539 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003540 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003541 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003542 }
3543 case ISD::FP_TO_UINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003544 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003545 LC = (VT == MVT::i32)
3546 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003547 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003548 LC = (VT == MVT::i32)
3549 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003550 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003551 LC = (VT == MVT::i32)
Dale Johannesenc0154c02007-10-05 20:04:43 +00003552 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3553 }
3554 else if (OVT == MVT::ppcf128) {
3555 assert(VT == MVT::i64);
3556 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003557 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003558 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003559 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003560 default: assert(0 && "Unreachable!");
3561 }
3562 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003563 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3564 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003565 break;
3566 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003567 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003568 Tmp1 = PromoteOp(Node->getOperand(0));
3569 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3570 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003571 break;
3572 }
3573 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003574
Chris Lattner91d86242008-01-16 06:57:07 +00003575 case ISD::FP_EXTEND: {
Chris Lattner7ca4d5b2008-01-16 07:51:34 +00003576 MVT::ValueType DstVT = Op.getValueType();
3577 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3578 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
Chris Lattner91d86242008-01-16 06:57:07 +00003579 // The only other way we can lower this is to turn it into a STORE,
3580 // LOAD pair, targetting a temporary location (a stack slot).
Chris Lattner7ca4d5b2008-01-16 07:51:34 +00003581 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
Chris Lattner91d86242008-01-16 06:57:07 +00003582 break;
3583 }
3584 }
3585 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3586 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3587 case Legal:
3588 Tmp1 = LegalizeOp(Node->getOperand(0));
3589 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3590 break;
3591 case Promote:
3592 Tmp1 = PromoteOp(Node->getOperand(0));
3593 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3594 break;
3595 }
3596 break;
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003597 case ISD::FP_ROUND: {
Chris Lattner7ca4d5b2008-01-16 07:51:34 +00003598 MVT::ValueType DstVT = Op.getValueType();
3599 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3600 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3601 if (SrcVT == MVT::ppcf128) {
Dale Johannesenf864ac92007-10-06 01:24:11 +00003602 SDOperand Lo, Hi;
3603 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner7ca4d5b2008-01-16 07:51:34 +00003604 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Hi);
Dale Johannesenf864ac92007-10-06 01:24:11 +00003605 break;
Dale Johannesenc339e452007-08-09 17:27:48 +00003606 } else {
Dale Johannesenf864ac92007-10-06 01:24:11 +00003607 // The only other way we can lower this is to turn it into a STORE,
3608 // LOAD pair, targetting a temporary location (a stack slot).
Chris Lattner7ca4d5b2008-01-16 07:51:34 +00003609 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
Dale Johannesenf864ac92007-10-06 01:24:11 +00003610 break;
Dale Johannesenc339e452007-08-09 17:27:48 +00003611 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003612 }
Dale Johannesena2b3c172007-07-03 00:53:03 +00003613 }
Chris Lattner91d86242008-01-16 06:57:07 +00003614 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3615 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3616 case Legal:
3617 Tmp1 = LegalizeOp(Node->getOperand(0));
3618 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3619 break;
3620 case Promote:
3621 Tmp1 = PromoteOp(Node->getOperand(0));
3622 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1);
3623 break;
3624 }
3625 break;
Chris Lattner7753f172005-09-02 00:18:10 +00003626 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003627 case ISD::ZERO_EXTEND:
3628 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003629 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003630 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003631 case Legal:
3632 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003633 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003634 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003635 case Promote:
3636 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003637 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003638 Tmp1 = PromoteOp(Node->getOperand(0));
3639 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003640 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003641 case ISD::ZERO_EXTEND:
3642 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003643 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003644 Result = DAG.getZeroExtendInReg(Result,
3645 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003646 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003647 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003648 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003649 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003650 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003651 Result,
3652 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003653 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003654 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003655 }
3656 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003657 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003658 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003659 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003660 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003661
3662 // If this operation is not supported, convert it to a shl/shr or load/store
3663 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003664 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3665 default: assert(0 && "This action not supported for this op yet!");
3666 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003667 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003668 break;
3669 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003670 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003671 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003672 // NOTE: we could fall back on load/store here too for targets without
3673 // SAR. However, it is doubtful that any exist.
3674 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3675 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003676 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003677 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3678 Node->getOperand(0), ShiftCst);
3679 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3680 Result, ShiftCst);
3681 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003682 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003683 // EXTLOAD pair, targetting a temporary location (a stack slot).
3684
3685 // NOTE: there is a choice here between constantly creating new stack
3686 // slots and always reusing the same one. We currently always create
3687 // new ones, as reuse may inhibit scheduling.
Chris Lattner7ca4d5b2008-01-16 07:51:34 +00003688 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
3689 Node->getValueType(0));
Chris Lattner99222f72005-01-15 07:15:18 +00003690 } else {
3691 assert(0 && "Unknown op");
3692 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003693 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003694 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003695 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003696 }
Duncan Sands644f9172007-07-27 12:58:54 +00003697 case ISD::TRAMPOLINE: {
3698 SDOperand Ops[6];
3699 for (unsigned i = 0; i != 6; ++i)
3700 Ops[i] = LegalizeOp(Node->getOperand(i));
3701 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3702 // The only option for this node is to custom lower it.
3703 Result = TLI.LowerOperation(Result, DAG);
3704 assert(Result.Val && "Should always custom lower!");
Duncan Sands86e01192007-09-11 14:10:23 +00003705
3706 // Since trampoline produces two values, make sure to remember that we
3707 // legalized both of them.
3708 Tmp1 = LegalizeOp(Result.getValue(1));
3709 Result = LegalizeOp(Result);
3710 AddLegalizedOperand(SDOperand(Node, 0), Result);
3711 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3712 return Op.ResNo ? Tmp1 : Result;
Duncan Sands644f9172007-07-27 12:58:54 +00003713 }
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +00003714 case ISD::FLT_ROUNDS: {
3715 MVT::ValueType VT = Node->getValueType(0);
3716 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3717 default: assert(0 && "This action not supported for this op yet!");
3718 case TargetLowering::Custom:
3719 Result = TLI.LowerOperation(Op, DAG);
3720 if (Result.Val) break;
3721 // Fall Thru
3722 case TargetLowering::Legal:
3723 // If this operation is not supported, lower it to constant 1
3724 Result = DAG.getConstant(1, VT);
3725 break;
3726 }
3727 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00003728 case ISD::TRAP: {
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003729 MVT::ValueType VT = Node->getValueType(0);
3730 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
3731 default: assert(0 && "This action not supported for this op yet!");
Chris Lattneree8df1f2008-01-15 21:58:08 +00003732 case TargetLowering::Legal:
3733 Tmp1 = LegalizeOp(Node->getOperand(0));
3734 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3735 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003736 case TargetLowering::Custom:
3737 Result = TLI.LowerOperation(Op, DAG);
3738 if (Result.Val) break;
3739 // Fall Thru
Chris Lattneree8df1f2008-01-15 21:58:08 +00003740 case TargetLowering::Expand:
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003741 // If this operation is not supported, lower it to 'abort()' call
Chris Lattneree8df1f2008-01-15 21:58:08 +00003742 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003743 TargetLowering::ArgListTy Args;
3744 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneree8df1f2008-01-15 21:58:08 +00003745 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattnerec224882008-01-15 22:09:33 +00003746 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
3747 Args, DAG);
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003748 Result = CallResult.second;
3749 break;
3750 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00003751 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00003752 }
Chris Lattner99222f72005-01-15 07:15:18 +00003753 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003754
Chris Lattner101ea662006-04-08 04:13:17 +00003755 assert(Result.getValueType() == Op.getValueType() &&
3756 "Bad legalization!");
3757
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003758 // Make sure that the generated code is itself legal.
3759 if (Result != Op)
3760 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003761
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003762 // Note that LegalizeOp may be reentered even from single-use nodes, which
3763 // means that we always must cache transformed nodes.
3764 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003765 return Result;
3766}
3767
Chris Lattner4d978642005-01-15 22:16:26 +00003768/// PromoteOp - Given an operation that produces a value in an invalid type,
3769/// promote it to compute the value into a larger type. The produced value will
3770/// have the correct bits for the low portion of the register, but no guarantee
3771/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003772SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3773 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003774 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003775 assert(getTypeAction(VT) == Promote &&
3776 "Caller should expand or legalize operands that are not promotable!");
3777 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3778 "Cannot promote to smaller type!");
3779
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003780 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003781 SDOperand Result;
3782 SDNode *Node = Op.Val;
3783
Chris Lattner4b0ddb22007-02-04 01:17:38 +00003784 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00003785 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003786
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003787 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003788 case ISD::CopyFromReg:
3789 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003790 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003791#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00003792 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003793#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003794 assert(0 && "Do not know how to promote this operator!");
3795 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003796 case ISD::UNDEF:
3797 Result = DAG.getNode(ISD::UNDEF, NVT);
3798 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003799 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003800 if (VT != MVT::i1)
3801 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3802 else
3803 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003804 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3805 break;
3806 case ISD::ConstantFP:
3807 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3808 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3809 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003810
Chris Lattner2cb338d2005-01-18 02:59:52 +00003811 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003812 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003813 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3814 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003815 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003816
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003817 case ISD::TRUNCATE:
3818 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3819 case Legal:
3820 Result = LegalizeOp(Node->getOperand(0));
3821 assert(Result.getValueType() >= NVT &&
3822 "This truncation doesn't make sense!");
3823 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3824 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3825 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003826 case Promote:
3827 // The truncation is not required, because we don't guarantee anything
3828 // about high bits anyway.
3829 Result = PromoteOp(Node->getOperand(0));
3830 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003831 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003832 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3833 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003834 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003835 }
3836 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003837 case ISD::SIGN_EXTEND:
3838 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003839 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003840 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3841 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3842 case Legal:
3843 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003844 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003845 break;
3846 case Promote:
3847 // Promote the reg if it's smaller.
3848 Result = PromoteOp(Node->getOperand(0));
3849 // The high bits are not guaranteed to be anything. Insert an extend.
3850 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003851 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003852 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003853 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003854 Result = DAG.getZeroExtendInReg(Result,
3855 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003856 break;
3857 }
3858 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003859 case ISD::BIT_CONVERT:
Chris Lattner87bc3e72008-01-16 07:45:30 +00003860 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3861 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00003862 Result = PromoteOp(Result);
3863 break;
3864
Chris Lattner4d978642005-01-15 22:16:26 +00003865 case ISD::FP_EXTEND:
3866 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3867 case ISD::FP_ROUND:
3868 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3869 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3870 case Promote: assert(0 && "Unreachable with 2 FP types!");
3871 case Legal:
3872 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003873 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003874 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003875 break;
3876 }
3877 break;
3878
3879 case ISD::SINT_TO_FP:
3880 case ISD::UINT_TO_FP:
3881 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3882 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003883 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003884 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003885 break;
3886
3887 case Promote:
3888 Result = PromoteOp(Node->getOperand(0));
3889 if (Node->getOpcode() == ISD::SINT_TO_FP)
3890 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003891 Result,
3892 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003893 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003894 Result = DAG.getZeroExtendInReg(Result,
3895 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003896 // No extra round required here.
3897 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003898 break;
3899 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003900 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3901 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003902 // Round if we cannot tolerate excess precision.
3903 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003904 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3905 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003906 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003907 }
Chris Lattner4d978642005-01-15 22:16:26 +00003908 break;
3909
Chris Lattner268d4572005-12-09 17:32:47 +00003910 case ISD::SIGN_EXTEND_INREG:
3911 Result = PromoteOp(Node->getOperand(0));
3912 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3913 Node->getOperand(1));
3914 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003915 case ISD::FP_TO_SINT:
3916 case ISD::FP_TO_UINT:
3917 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3918 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003919 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003920 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003921 break;
3922 case Promote:
3923 // The input result is prerounded, so we don't have to do anything
3924 // special.
3925 Tmp1 = PromoteOp(Node->getOperand(0));
3926 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003927 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003928 // If we're promoting a UINT to a larger size, check to see if the new node
3929 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3930 // we can use that instead. This allows us to generate better code for
3931 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3932 // legal, such as PowerPC.
3933 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003934 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003935 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3936 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003937 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3938 } else {
3939 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3940 }
Chris Lattner4d978642005-01-15 22:16:26 +00003941 break;
3942
Chris Lattner13fe99c2005-04-02 05:00:07 +00003943 case ISD::FABS:
3944 case ISD::FNEG:
3945 Tmp1 = PromoteOp(Node->getOperand(0));
3946 assert(Tmp1.getValueType() == NVT);
3947 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3948 // NOTE: we do not have to do any extra rounding here for
3949 // NoExcessFPPrecision, because we know the input will have the appropriate
3950 // precision, and these operations don't modify precision at all.
3951 break;
3952
Chris Lattner9d6fa982005-04-28 21:44:33 +00003953 case ISD::FSQRT:
3954 case ISD::FSIN:
3955 case ISD::FCOS:
3956 Tmp1 = PromoteOp(Node->getOperand(0));
3957 assert(Tmp1.getValueType() == NVT);
3958 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003959 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003960 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3961 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003962 break;
3963
Chris Lattnerca401aa2007-03-03 23:43:21 +00003964 case ISD::FPOWI: {
3965 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3966 // directly as well, which may be better.
3967 Tmp1 = PromoteOp(Node->getOperand(0));
3968 assert(Tmp1.getValueType() == NVT);
3969 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3970 if (NoExcessFPPrecision)
3971 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3972 DAG.getValueType(VT));
3973 break;
3974 }
3975
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003976 case ISD::AND:
3977 case ISD::OR:
3978 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003979 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003980 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003981 case ISD::MUL:
3982 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003983 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003984 // that too is okay if they are integer operations.
3985 Tmp1 = PromoteOp(Node->getOperand(0));
3986 Tmp2 = PromoteOp(Node->getOperand(1));
3987 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3988 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003989 break;
3990 case ISD::FADD:
3991 case ISD::FSUB:
3992 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003993 Tmp1 = PromoteOp(Node->getOperand(0));
3994 Tmp2 = PromoteOp(Node->getOperand(1));
3995 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3996 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3997
3998 // Floating point operations will give excess precision that we may not be
3999 // able to tolerate. If we DO allow excess precision, just leave it,
4000 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00004001 // FIXME: Why would we need to round FP ops more than integer ones?
4002 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00004003 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004004 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4005 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004006 break;
4007
Chris Lattner4d978642005-01-15 22:16:26 +00004008 case ISD::SDIV:
4009 case ISD::SREM:
4010 // These operators require that their input be sign extended.
4011 Tmp1 = PromoteOp(Node->getOperand(0));
4012 Tmp2 = PromoteOp(Node->getOperand(1));
4013 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00004014 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4015 DAG.getValueType(VT));
4016 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4017 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004018 }
4019 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4020
4021 // Perform FP_ROUND: this is probably overly pessimistic.
4022 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004023 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4024 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004025 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00004026 case ISD::FDIV:
4027 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004028 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00004029 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00004030 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4031 case Legal:
4032 Tmp1 = LegalizeOp(Node->getOperand(0));
4033 break;
4034 case Promote:
4035 Tmp1 = PromoteOp(Node->getOperand(0));
4036 break;
4037 case Expand:
4038 assert(0 && "not implemented");
4039 }
4040 switch (getTypeAction(Node->getOperand(1).getValueType())) {
4041 case Legal:
4042 Tmp2 = LegalizeOp(Node->getOperand(1));
4043 break;
4044 case Promote:
4045 Tmp2 = PromoteOp(Node->getOperand(1));
4046 break;
4047 case Expand:
4048 assert(0 && "not implemented");
4049 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00004050 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4051
4052 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004053 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00004054 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4055 DAG.getValueType(VT));
4056 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004057
4058 case ISD::UDIV:
4059 case ISD::UREM:
4060 // These operators require that their input be zero extended.
4061 Tmp1 = PromoteOp(Node->getOperand(0));
4062 Tmp2 = PromoteOp(Node->getOperand(1));
4063 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00004064 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4065 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00004066 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4067 break;
4068
4069 case ISD::SHL:
4070 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004071 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004072 break;
4073 case ISD::SRA:
4074 // The input value must be properly sign extended.
4075 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00004076 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4077 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004078 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004079 break;
4080 case ISD::SRL:
4081 // The input value must be properly zero extended.
4082 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00004083 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004084 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004085 break;
Nate Begeman595ec732006-01-28 03:14:31 +00004086
4087 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004088 Tmp1 = Node->getOperand(0); // Get the chain.
4089 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00004090 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4091 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4092 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4093 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00004094 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00004095 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00004096 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00004097 // Increment the pointer, VAList, to the next vaarg
4098 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4099 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4100 TLI.getPointerTy()));
4101 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00004102 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
4103 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00004104 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00004105 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00004106 }
4107 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004108 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00004109 break;
4110
Evan Chenge71fe34d2006-10-09 20:57:25 +00004111 case ISD::LOAD: {
4112 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00004113 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4114 ? ISD::EXTLOAD : LD->getExtensionType();
4115 Result = DAG.getExtLoad(ExtType, NVT,
4116 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00004117 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00004118 LD->getLoadedVT(),
4119 LD->isVolatile(),
4120 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004121 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004122 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004123 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00004124 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004125 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004126 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4127 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004128 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004129 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00004130 case ISD::SELECT_CC:
4131 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4132 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4133 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004134 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004135 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00004136 case ISD::BSWAP:
4137 Tmp1 = Node->getOperand(0);
4138 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4139 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4140 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004141 DAG.getConstant(MVT::getSizeInBits(NVT) -
4142 MVT::getSizeInBits(VT),
Nate Begeman2fba8a32006-01-14 03:14:10 +00004143 TLI.getShiftAmountTy()));
4144 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004145 case ISD::CTPOP:
4146 case ISD::CTTZ:
4147 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004148 // Zero extend the argument
4149 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004150 // Perform the larger operation, then subtract if needed.
4151 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004152 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004153 case ISD::CTPOP:
4154 Result = Tmp1;
4155 break;
4156 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004157 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00004158 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004159 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4160 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004161 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004162 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004163 break;
4164 case ISD::CTLZ:
4165 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004166 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004167 DAG.getConstant(MVT::getSizeInBits(NVT) -
4168 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004169 break;
4170 }
4171 break;
Dan Gohmana8665142007-06-25 16:23:39 +00004172 case ISD::EXTRACT_SUBVECTOR:
4173 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00004174 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00004175 case ISD::EXTRACT_VECTOR_ELT:
4176 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4177 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004178 }
4179
4180 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004181
4182 // Make sure the result is itself legal.
4183 Result = LegalizeOp(Result);
4184
4185 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004186 AddPromotedOperand(Op, Result);
4187 return Result;
4188}
Chris Lattnerdc750592005-01-07 07:47:09 +00004189
Dan Gohmana8665142007-06-25 16:23:39 +00004190/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4191/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4192/// based on the vector type. The return type of this matches the element type
4193/// of the vector, which may not be legal for the target.
4194SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00004195 // We know that operand #0 is the Vec vector. If the index is a constant
4196 // or if the invec is a supported hardware type, we can use it. Otherwise,
4197 // lower to a store then an indexed load.
4198 SDOperand Vec = Op.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00004199 SDOperand Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00004200
Dan Gohman60028182007-09-24 15:54:53 +00004201 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +00004202 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner6f423252006-03-31 17:55:51 +00004203
Dan Gohmana8665142007-06-25 16:23:39 +00004204 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4205 default: assert(0 && "This action is not supported yet!");
4206 case TargetLowering::Custom: {
4207 Vec = LegalizeOp(Vec);
4208 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4209 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4210 if (Tmp3.Val)
4211 return Tmp3;
4212 break;
4213 }
4214 case TargetLowering::Legal:
4215 if (isTypeLegal(TVT)) {
4216 Vec = LegalizeOp(Vec);
4217 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00004218 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00004219 }
4220 break;
4221 case TargetLowering::Expand:
4222 break;
4223 }
4224
4225 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00004226 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004227 Op = ScalarizeVectorOp(Vec);
4228 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4229 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner6f423252006-03-31 17:55:51 +00004230 SDOperand Lo, Hi;
4231 SplitVectorOp(Vec, Lo, Hi);
4232 if (CIdx->getValue() < NumElems/2) {
4233 Vec = Lo;
4234 } else {
4235 Vec = Hi;
Dan Gohmana8665142007-06-25 16:23:39 +00004236 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4237 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00004238 }
Dan Gohmana8665142007-06-25 16:23:39 +00004239
Chris Lattner6f423252006-03-31 17:55:51 +00004240 // It's now an extract from the appropriate high or low part. Recurse.
4241 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004242 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00004243 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00004244 // Store the value to a temporary stack slot, then LOAD the scalar
4245 // element back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004246 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmana8665142007-06-25 16:23:39 +00004247 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4248
4249 // Add the offset to the index.
4250 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4251 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4252 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling070aca52007-10-18 08:32:37 +00004253
4254 if (MVT::getSizeInBits(Idx.getValueType()) >
4255 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattner064c31e2007-10-19 16:47:35 +00004256 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004257 else
Chris Lattner064c31e2007-10-19 16:47:35 +00004258 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004259
Dan Gohmana8665142007-06-25 16:23:39 +00004260 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4261
4262 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00004263 }
Dan Gohmana8665142007-06-25 16:23:39 +00004264 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00004265}
4266
Dan Gohmana8665142007-06-25 16:23:39 +00004267/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00004268/// we assume the operation can be split if it is not already legal.
Dan Gohmana8665142007-06-25 16:23:39 +00004269SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00004270 // We know that operand #0 is the Vec vector. For now we assume the index
4271 // is a constant and that the extracted result is a supported hardware type.
4272 SDOperand Vec = Op.getOperand(0);
4273 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4274
Dan Gohmana8665142007-06-25 16:23:39 +00004275 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00004276
4277 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4278 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004279 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00004280 }
4281
4282 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4283 SDOperand Lo, Hi;
4284 SplitVectorOp(Vec, Lo, Hi);
4285 if (CIdx->getValue() < NumElems/2) {
4286 Vec = Lo;
4287 } else {
4288 Vec = Hi;
4289 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4290 }
4291
4292 // It's now an extract from the appropriate high or low part. Recurse.
4293 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004294 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00004295}
4296
Nate Begeman7e7f4392006-02-01 07:19:44 +00004297/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4298/// with condition CC on the current target. This usually involves legalizing
4299/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4300/// there may be no choice but to create a new SetCC node to represent the
4301/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4302/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4303void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4304 SDOperand &RHS,
4305 SDOperand &CC) {
Dale Johannesenf864ac92007-10-06 01:24:11 +00004306 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman7e7f4392006-02-01 07:19:44 +00004307
4308 switch (getTypeAction(LHS.getValueType())) {
4309 case Legal:
4310 Tmp1 = LegalizeOp(LHS); // LHS
4311 Tmp2 = LegalizeOp(RHS); // RHS
4312 break;
4313 case Promote:
4314 Tmp1 = PromoteOp(LHS); // LHS
4315 Tmp2 = PromoteOp(RHS); // RHS
4316
4317 // If this is an FP compare, the operands have already been extended.
4318 if (MVT::isInteger(LHS.getValueType())) {
4319 MVT::ValueType VT = LHS.getValueType();
4320 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4321
4322 // Otherwise, we have to insert explicit sign or zero extends. Note
4323 // that we could insert sign extends for ALL conditions, but zero extend
4324 // is cheaper on many machines (an AND instead of two shifts), so prefer
4325 // it.
4326 switch (cast<CondCodeSDNode>(CC)->get()) {
4327 default: assert(0 && "Unknown integer comparison!");
4328 case ISD::SETEQ:
4329 case ISD::SETNE:
4330 case ISD::SETUGE:
4331 case ISD::SETUGT:
4332 case ISD::SETULE:
4333 case ISD::SETULT:
4334 // ALL of these operations will work if we either sign or zero extend
4335 // the operands (including the unsigned comparisons!). Zero extend is
4336 // usually a simpler/cheaper operation, so prefer it.
4337 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4338 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4339 break;
4340 case ISD::SETGE:
4341 case ISD::SETGT:
4342 case ISD::SETLT:
4343 case ISD::SETLE:
4344 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4345 DAG.getValueType(VT));
4346 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4347 DAG.getValueType(VT));
4348 break;
4349 }
4350 }
4351 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004352 case Expand: {
4353 MVT::ValueType VT = LHS.getValueType();
4354 if (VT == MVT::f32 || VT == MVT::f64) {
4355 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00004356 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004357 switch (cast<CondCodeSDNode>(CC)->get()) {
4358 case ISD::SETEQ:
4359 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004360 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004361 break;
4362 case ISD::SETNE:
4363 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004364 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004365 break;
4366 case ISD::SETGE:
4367 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004368 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004369 break;
4370 case ISD::SETLT:
4371 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004372 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004373 break;
4374 case ISD::SETLE:
4375 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004376 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004377 break;
4378 case ISD::SETGT:
4379 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004380 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004381 break;
4382 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00004383 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4384 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004385 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00004386 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004387 break;
4388 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004389 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004390 switch (cast<CondCodeSDNode>(CC)->get()) {
4391 case ISD::SETONE:
4392 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00004393 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004394 // Fallthrough
4395 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004396 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004397 break;
4398 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004399 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004400 break;
4401 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004402 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004403 break;
4404 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004405 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004406 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004407 case ISD::SETUEQ:
4408 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004409 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004410 default: assert(0 && "Unsupported FP setcc!");
4411 }
4412 }
4413
4414 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004415 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00004416 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004417 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004418 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00004419 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00004420 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004421 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00004422 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00004423 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004424 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004425 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00004426 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004427 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4428 Tmp2 = SDOperand();
4429 }
4430 LHS = Tmp1;
4431 RHS = Tmp2;
4432 return;
4433 }
4434
Nate Begeman7e7f4392006-02-01 07:19:44 +00004435 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4436 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004437 ExpandOp(RHS, RHSLo, RHSHi);
4438 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4439
4440 if (VT==MVT::ppcf128) {
4441 // FIXME: This generated code sucks. We want to generate
4442 // FCMP crN, hi1, hi2
4443 // BNE crN, L:
4444 // FCMP crN, lo1, lo2
4445 // The following can be improved, but not that much.
4446 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4447 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4448 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4449 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4450 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4451 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4452 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4453 Tmp2 = SDOperand();
4454 break;
4455 }
4456
4457 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004458 case ISD::SETEQ:
4459 case ISD::SETNE:
4460 if (RHSLo == RHSHi)
4461 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4462 if (RHSCST->isAllOnesValue()) {
4463 // Comparison to -1.
4464 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4465 Tmp2 = RHSLo;
4466 break;
4467 }
4468
4469 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4470 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4471 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4472 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4473 break;
4474 default:
4475 // If this is a comparison of the sign bit, just look at the top part.
4476 // X > -1, x < 0
4477 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4478 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4479 CST->getValue() == 0) || // X < 0
4480 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4481 CST->isAllOnesValue())) { // X > -1
4482 Tmp1 = LHSHi;
4483 Tmp2 = RHSHi;
4484 break;
4485 }
4486
4487 // FIXME: This generated code sucks.
4488 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00004489 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004490 default: assert(0 && "Unknown integer setcc!");
4491 case ISD::SETLT:
4492 case ISD::SETULT: LowCC = ISD::SETULT; break;
4493 case ISD::SETGT:
4494 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4495 case ISD::SETLE:
4496 case ISD::SETULE: LowCC = ISD::SETULE; break;
4497 case ISD::SETGE:
4498 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4499 }
4500
4501 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4502 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4503 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4504
4505 // NOTE: on targets without efficient SELECT of bools, we can always use
4506 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00004507 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4508 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4509 false, DagCombineInfo);
4510 if (!Tmp1.Val)
4511 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4512 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4513 CCCode, false, DagCombineInfo);
4514 if (!Tmp2.Val)
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004515 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng93049452007-02-08 22:16:19 +00004516
4517 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4518 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4519 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4520 (Tmp2C && Tmp2C->getValue() == 0 &&
4521 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4522 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4523 (Tmp2C && Tmp2C->getValue() == 1 &&
4524 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4525 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4526 // low part is known false, returns high part.
4527 // For LE / GE, if high part is known false, ignore the low part.
4528 // For LT / GT, if high part is known true, ignore the low part.
4529 Tmp1 = Tmp2;
4530 Tmp2 = SDOperand();
4531 } else {
4532 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4533 ISD::SETEQ, false, DagCombineInfo);
4534 if (!Result.Val)
4535 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4536 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4537 Result, Tmp1, Tmp2));
4538 Tmp1 = Result;
4539 Tmp2 = SDOperand();
4540 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004541 }
4542 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004543 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004544 LHS = Tmp1;
4545 RHS = Tmp2;
4546}
4547
Chris Lattner87bc3e72008-01-16 07:45:30 +00004548/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4549/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4550/// a load from the stack slot to DestVT, extending it if needed.
4551/// The resultant code need not be legal.
4552SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4553 MVT::ValueType SlotVT,
4554 MVT::ValueType DestVT) {
Chris Lattner36e663d2005-12-23 00:16:34 +00004555 // Create the stack frame object.
Chris Lattner87bc3e72008-01-16 07:45:30 +00004556 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4557
4558 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4559 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4560 unsigned DestSize = MVT::getSizeInBits(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004561
Chris Lattner87bc3e72008-01-16 07:45:30 +00004562 // Emit a store to the stack slot. Use a truncstore if the input value is
4563 // later than DestVT.
4564 SDOperand Store;
4565 if (SrcSize > SlotSize)
4566 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0,SlotVT);
4567 else {
4568 assert(SrcSize == SlotSize && "Invalid store");
4569 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
4570 }
4571
Chris Lattner36e663d2005-12-23 00:16:34 +00004572 // Result is a load from the stack slot.
Chris Lattner87bc3e72008-01-16 07:45:30 +00004573 if (SlotSize == DestSize)
4574 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4575
4576 assert(SlotSize < DestSize && "Unknown extension!");
4577 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004578}
4579
Chris Lattner6be79822006-04-04 17:23:26 +00004580SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4581 // Create a vector sized/aligned stack slot, store the value to element #0,
4582 // then load the whole vector back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004583 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00004584 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00004585 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004586 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00004587}
4588
4589
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004590/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00004591/// support the operation, but do support the resultant vector type.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004592SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4593
4594 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00004595 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004596 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004597 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00004598 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004599 std::map<SDOperand, std::vector<unsigned> > Values;
4600 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004601 bool isConstant = true;
4602 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4603 SplatValue.getOpcode() != ISD::UNDEF)
4604 isConstant = false;
4605
Evan Cheng1d2e9952006-03-24 01:17:21 +00004606 for (unsigned i = 1; i < NumElems; ++i) {
4607 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00004608 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004609 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004610 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004611 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00004612 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004613
4614 // If this isn't a constant element or an undef, we can't use a constant
4615 // pool load.
4616 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4617 V.getOpcode() != ISD::UNDEF)
4618 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004619 }
4620
4621 if (isOnlyLowElement) {
4622 // If the low element is an undef too, then this whole things is an undef.
4623 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4624 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4625 // Otherwise, turn this into a scalar_to_vector node.
4626 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4627 Node->getOperand(0));
4628 }
4629
Chris Lattner77e271c2006-03-24 07:29:17 +00004630 // If all elements are constants, create a load from the constant pool.
4631 if (isConstant) {
4632 MVT::ValueType VT = Node->getValueType(0);
4633 const Type *OpNTy =
4634 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4635 std::vector<Constant*> CV;
4636 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4637 if (ConstantFPSDNode *V =
4638 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00004639 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004640 } else if (ConstantSDNode *V =
4641 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00004642 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004643 } else {
4644 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4645 CV.push_back(UndefValue::get(OpNTy));
4646 }
4647 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00004648 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00004649 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00004650 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004651 }
4652
Chris Lattner21e68c82006-03-20 01:52:29 +00004653 if (SplatValue.Val) { // Splat of one value?
4654 // Build the shuffle constant vector: <0, 0, 0, 0>
4655 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00004656 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00004657 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004658 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004659 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4660 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00004661
4662 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004663 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00004664 // Get the splatted value into the low element of a vector register.
4665 SDOperand LowValVec =
4666 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4667
4668 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4669 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4670 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4671 SplatMask);
4672 }
4673 }
4674
Evan Cheng1d2e9952006-03-24 01:17:21 +00004675 // If there are only two unique elements, we may be able to turn this into a
4676 // vector shuffle.
4677 if (Values.size() == 2) {
4678 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4679 MVT::ValueType MaskVT =
4680 MVT::getIntVectorWithNumElements(NumElems);
4681 std::vector<SDOperand> MaskVec(NumElems);
4682 unsigned i = 0;
4683 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4684 E = Values.end(); I != E; ++I) {
4685 for (std::vector<unsigned>::iterator II = I->second.begin(),
4686 EE = I->second.end(); II != EE; ++II)
Dan Gohman5c441312007-06-14 22:58:02 +00004687 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004688 i += NumElems;
4689 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004690 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4691 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004692
4693 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004694 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4695 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004696 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004697 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4698 E = Values.end(); I != E; ++I) {
4699 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4700 I->first);
4701 Ops.push_back(Op);
4702 }
4703 Ops.push_back(ShuffleMask);
4704
4705 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004706 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4707 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004708 }
4709 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004710
4711 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4712 // aligned object on the stack, store each element into it, then load
4713 // the result as a vector.
4714 MVT::ValueType VT = Node->getValueType(0);
4715 // Create the stack frame object.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004716 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004717
4718 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004719 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004720 unsigned TypeByteSize =
4721 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004722 // Store (in the right endianness) the elements to memory.
4723 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4724 // Ignore undef elements.
4725 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4726
Chris Lattner8fa445a2006-03-22 01:46:54 +00004727 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004728
4729 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4730 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4731
Evan Chengdf9ac472006-10-05 23:01:46 +00004732 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00004733 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004734 }
4735
4736 SDOperand StoreChain;
4737 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004738 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4739 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004740 else
4741 StoreChain = DAG.getEntryNode();
4742
4743 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004744 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004745}
4746
Chris Lattner4157c412005-04-02 04:00:59 +00004747void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4748 SDOperand Op, SDOperand Amt,
4749 SDOperand &Lo, SDOperand &Hi) {
4750 // Expand the subcomponents.
4751 SDOperand LHSL, LHSH;
4752 ExpandOp(Op, LHSL, LHSH);
4753
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004754 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00004755 MVT::ValueType VT = LHSL.getValueType();
4756 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00004757 Hi = Lo.getValue(1);
4758}
4759
4760
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004761/// ExpandShift - Try to find a clever way to expand this shift operation out to
4762/// smaller elements. If we can't find a way that is more efficient than a
4763/// libcall on this target, return false. Otherwise, return true with the
4764/// low-parts expanded into Lo and Hi.
4765bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4766 SDOperand &Lo, SDOperand &Hi) {
4767 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4768 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00004769
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004770 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00004771 SDOperand ShAmt = LegalizeOp(Amt);
4772 MVT::ValueType ShTy = ShAmt.getValueType();
4773 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4774 unsigned NVTBits = MVT::getSizeInBits(NVT);
4775
Chris Lattnerfbbe5702007-10-14 20:35:12 +00004776 // Handle the case when Amt is an immediate.
Nate Begemanb0674922005-04-06 21:13:14 +00004777 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4778 unsigned Cst = CN->getValue();
4779 // Expand the incoming operand to be shifted, so that we have its parts
4780 SDOperand InL, InH;
4781 ExpandOp(Op, InL, InH);
4782 switch(Opc) {
4783 case ISD::SHL:
4784 if (Cst > VTBits) {
4785 Lo = DAG.getConstant(0, NVT);
4786 Hi = DAG.getConstant(0, NVT);
4787 } else if (Cst > NVTBits) {
4788 Lo = DAG.getConstant(0, NVT);
4789 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004790 } else if (Cst == NVTBits) {
4791 Lo = DAG.getConstant(0, NVT);
4792 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00004793 } else {
4794 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4795 Hi = DAG.getNode(ISD::OR, NVT,
4796 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4797 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4798 }
4799 return true;
4800 case ISD::SRL:
4801 if (Cst > VTBits) {
4802 Lo = DAG.getConstant(0, NVT);
4803 Hi = DAG.getConstant(0, NVT);
4804 } else if (Cst > NVTBits) {
4805 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4806 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00004807 } else if (Cst == NVTBits) {
4808 Lo = InH;
4809 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00004810 } else {
4811 Lo = DAG.getNode(ISD::OR, NVT,
4812 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4813 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4814 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4815 }
4816 return true;
4817 case ISD::SRA:
4818 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004819 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004820 DAG.getConstant(NVTBits-1, ShTy));
4821 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004822 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004823 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004824 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004825 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004826 } else if (Cst == NVTBits) {
4827 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004828 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004829 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004830 } else {
4831 Lo = DAG.getNode(ISD::OR, NVT,
4832 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4833 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4834 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4835 }
4836 return true;
4837 }
4838 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004839
4840 // Okay, the shift amount isn't constant. However, if we can tell that it is
4841 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4842 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00004843 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00004844
4845 // If we know that the high bit of the shift amount is one, then we can do
4846 // this as a couple of simple shifts.
4847 if (KnownOne & Mask) {
4848 // Mask out the high bit, which we know is set.
4849 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4850 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4851
4852 // Expand the incoming operand to be shifted, so that we have its parts
4853 SDOperand InL, InH;
4854 ExpandOp(Op, InL, InH);
4855 switch(Opc) {
4856 case ISD::SHL:
4857 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4858 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4859 return true;
4860 case ISD::SRL:
4861 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4862 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4863 return true;
4864 case ISD::SRA:
4865 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4866 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4867 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4868 return true;
4869 }
4870 }
4871
4872 // If we know that the high bit of the shift amount is zero, then we can do
4873 // this as a couple of simple shifts.
4874 if (KnownZero & Mask) {
4875 // Compute 32-amt.
4876 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4877 DAG.getConstant(NVTBits, Amt.getValueType()),
4878 Amt);
4879
4880 // Expand the incoming operand to be shifted, so that we have its parts
4881 SDOperand InL, InH;
4882 ExpandOp(Op, InL, InH);
4883 switch(Opc) {
4884 case ISD::SHL:
4885 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4886 Hi = DAG.getNode(ISD::OR, NVT,
4887 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4888 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4889 return true;
4890 case ISD::SRL:
4891 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4892 Lo = DAG.getNode(ISD::OR, NVT,
4893 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4894 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4895 return true;
4896 case ISD::SRA:
4897 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4898 Lo = DAG.getNode(ISD::OR, NVT,
4899 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4900 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4901 return true;
4902 }
4903 }
4904
Nate Begemanb0674922005-04-06 21:13:14 +00004905 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004906}
Chris Lattneraac464e2005-01-21 06:05:23 +00004907
Chris Lattner4add7e32005-01-23 04:42:50 +00004908
Chris Lattneraac464e2005-01-21 06:05:23 +00004909// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4910// does not fit into a register, return the lo part and set the hi part to the
4911// by-reg argument. If it does fit into a single register, return the result
4912// and leave the Hi part unset.
4913SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004914 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004915 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4916 // The input chain to this libcall is the entry node of the function.
4917 // Legalizing the call will automatically add the previous call to the
4918 // dependence.
4919 SDOperand InChain = DAG.getEntryNode();
4920
Chris Lattneraac464e2005-01-21 06:05:23 +00004921 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004922 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004923 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4924 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4925 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004926 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00004927 Entry.isSExt = isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00004928 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004929 }
4930 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004931
Chris Lattner06bbeb62005-05-11 19:02:11 +00004932 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004933 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004934 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004935 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004936 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004937
Chris Lattner462505f2006-02-13 09:18:02 +00004938 // Legalize the call sequence, starting with the chain. This will advance
4939 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4940 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4941 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004942 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004943 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004944 default: assert(0 && "Unknown thing");
4945 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004946 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004947 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004948 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004949 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004950 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004951 }
Chris Lattner63022662005-09-02 20:26:58 +00004952 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004953}
4954
Chris Lattner4add7e32005-01-23 04:42:50 +00004955
Evan Chengbf535fc2007-04-27 07:33:31 +00004956/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4957///
Chris Lattneraac464e2005-01-21 06:05:23 +00004958SDOperand SelectionDAGLegalize::
4959ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004960 assert(getTypeAction(Source.getValueType()) == Expand &&
4961 "This is not an expansion!");
4962 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4963
Chris Lattner06bbeb62005-05-11 19:02:11 +00004964 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004965 assert(Source.getValueType() == MVT::i64 &&
4966 "This only works for 64-bit -> FP");
4967 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4968 // incoming integer is set. To handle this, we dynamically test to see if
4969 // it is set, and, if so, add a fudge factor.
4970 SDOperand Lo, Hi;
4971 ExpandOp(Source, Lo, Hi);
4972
Chris Lattner2a4f7312005-05-13 04:45:13 +00004973 // If this is unsigned, and not supported, first perform the conversion to
4974 // signed, then adjust the result if the sign bit is set.
4975 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4976 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4977
Chris Lattnerd47675e2005-08-09 20:20:18 +00004978 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4979 DAG.getConstant(0, Hi.getValueType()),
4980 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004981 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4982 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4983 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004984 uint64_t FF = 0x5f800000ULL;
4985 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004986 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004987
Chris Lattnerc30405e2005-08-26 17:15:30 +00004988 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004989 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4990 SDOperand FudgeInReg;
4991 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004992 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen7f724e92007-09-16 16:51:49 +00004993 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Chengbf535fc2007-04-27 07:33:31 +00004994 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen7f724e92007-09-16 16:51:49 +00004995 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00004996 CPIdx, NULL, 0, MVT::f32);
4997 else
4998 assert(0 && "Unexpected conversion");
4999
Evan Chengbf535fc2007-04-27 07:33:31 +00005000 MVT::ValueType SCVT = SignedConv.getValueType();
5001 if (SCVT != DestTy) {
5002 // Destination type needs to be expanded as well. The FADD now we are
5003 // constructing will be expanded into a libcall.
5004 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
5005 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
5006 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
5007 SignedConv, SignedConv.getValue(1));
5008 }
5009 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5010 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00005011 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00005012 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00005013
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005014 // Check to see if the target has a custom way to lower this. If so, use it.
5015 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
5016 default: assert(0 && "This action not implemented for this operation!");
5017 case TargetLowering::Legal:
5018 case TargetLowering::Expand:
5019 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005020 case TargetLowering::Custom: {
5021 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5022 Source), DAG);
5023 if (NV.Val)
5024 return LegalizeOp(NV);
5025 break; // The target decided this was legal after all
5026 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005027 }
5028
Chris Lattner153587e2005-05-12 07:00:44 +00005029 // Expand the source, then glue it back together for the call. We must expand
5030 // the source in case it is shared (this pass of legalize must traverse it).
5031 SDOperand SrcLo, SrcHi;
5032 ExpandOp(Source, SrcLo, SrcHi);
5033 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
5034
Evan Cheng31cbddf2007-01-12 02:11:51 +00005035 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00005036 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005037 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00005038 else {
5039 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00005040 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00005041 }
Chris Lattner462505f2006-02-13 09:18:02 +00005042
Evan Chengbf535fc2007-04-27 07:33:31 +00005043 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner462505f2006-02-13 09:18:02 +00005044 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5045 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005046 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5047 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00005048}
Misha Brukman835702a2005-04-21 22:36:52 +00005049
Chris Lattner689bdcc2006-01-28 08:25:58 +00005050/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5051/// INT_TO_FP operation of the specified operand when the target requests that
5052/// we expand it. At this point, we know that the result and operand types are
5053/// legal for the target.
5054SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5055 SDOperand Op0,
5056 MVT::ValueType DestVT) {
5057 if (Op0.getValueType() == MVT::i32) {
5058 // simple 32-bit [signed|unsigned] integer to float/double expansion
5059
Chris Lattnera2c7ff32008-01-16 07:03:22 +00005060 // Get the stack frame index of a 8 byte buffer.
5061 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5062
Chris Lattner689bdcc2006-01-28 08:25:58 +00005063 // word offset constant for Hi/Lo address computation
5064 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5065 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00005066 SDOperand Hi = StackSlot;
5067 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5068 if (TLI.isLittleEndian())
5069 std::swap(Hi, Lo);
5070
Chris Lattner689bdcc2006-01-28 08:25:58 +00005071 // if signed map to unsigned space
5072 SDOperand Op0Mapped;
5073 if (isSigned) {
5074 // constant used to invert sign bit (signed to unsigned mapping)
5075 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5076 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5077 } else {
5078 Op0Mapped = Op0;
5079 }
5080 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00005081 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00005082 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005083 // initial hi portion of constructed double
5084 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5085 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00005086 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005087 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00005088 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005089 // FP constant to bias correct the final result
5090 SDOperand Bias = DAG.getConstantFP(isSigned ?
5091 BitsToDouble(0x4330000080000000ULL)
5092 : BitsToDouble(0x4330000000000000ULL),
5093 MVT::f64);
5094 // subtract the bias
5095 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5096 // final result
5097 SDOperand Result;
5098 // handle final rounding
5099 if (DestVT == MVT::f64) {
5100 // do nothing
5101 Result = Sub;
Dale Johannesen7f724e92007-09-16 16:51:49 +00005102 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
5103 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
5104 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5105 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005106 }
5107 return Result;
5108 }
5109 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5110 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5111
5112 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
5113 DAG.getConstant(0, Op0.getValueType()),
5114 ISD::SETLT);
5115 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
5116 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5117 SignSet, Four, Zero);
5118
5119 // If the sign bit of the integer is set, the large number will be treated
5120 // as a negative number. To counteract this, the dynamic code adds an
5121 // offset depending on the data type.
5122 uint64_t FF;
5123 switch (Op0.getValueType()) {
5124 default: assert(0 && "Unsupported integer type!");
5125 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5126 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5127 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5128 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5129 }
5130 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00005131 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005132
5133 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5134 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5135 SDOperand FudgeInReg;
5136 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00005137 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005138 else {
Dale Johannesen7d67e542007-09-19 23:55:34 +00005139 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner689bdcc2006-01-28 08:25:58 +00005140 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00005141 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00005142 }
5143
5144 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5145}
5146
5147/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5148/// *INT_TO_FP operation of the specified operand when the target requests that
5149/// we promote it. At this point, we know that the result and operand types are
5150/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5151/// operation that takes a larger input.
5152SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5153 MVT::ValueType DestVT,
5154 bool isSigned) {
5155 // First step, figure out the appropriate *INT_TO_FP operation to use.
5156 MVT::ValueType NewInTy = LegalOp.getValueType();
5157
5158 unsigned OpToUse = 0;
5159
5160 // Scan for the appropriate larger type to use.
5161 while (1) {
5162 NewInTy = (MVT::ValueType)(NewInTy+1);
5163 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5164
5165 // If the target supports SINT_TO_FP of this type, use it.
5166 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5167 default: break;
5168 case TargetLowering::Legal:
5169 if (!TLI.isTypeLegal(NewInTy))
5170 break; // Can't use this datatype.
5171 // FALL THROUGH.
5172 case TargetLowering::Custom:
5173 OpToUse = ISD::SINT_TO_FP;
5174 break;
5175 }
5176 if (OpToUse) break;
5177 if (isSigned) continue;
5178
5179 // If the target supports UINT_TO_FP of this type, use it.
5180 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5181 default: break;
5182 case TargetLowering::Legal:
5183 if (!TLI.isTypeLegal(NewInTy))
5184 break; // Can't use this datatype.
5185 // FALL THROUGH.
5186 case TargetLowering::Custom:
5187 OpToUse = ISD::UINT_TO_FP;
5188 break;
5189 }
5190 if (OpToUse) break;
5191
5192 // Otherwise, try a larger type.
5193 }
5194
5195 // Okay, we found the operation and type to use. Zero extend our input to the
5196 // desired type then run the operation on it.
5197 return DAG.getNode(OpToUse, DestVT,
5198 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5199 NewInTy, LegalOp));
5200}
5201
5202/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5203/// FP_TO_*INT operation of the specified operand when the target requests that
5204/// we promote it. At this point, we know that the result and operand types are
5205/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5206/// operation that returns a larger result.
5207SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5208 MVT::ValueType DestVT,
5209 bool isSigned) {
5210 // First step, figure out the appropriate FP_TO*INT operation to use.
5211 MVT::ValueType NewOutTy = DestVT;
5212
5213 unsigned OpToUse = 0;
5214
5215 // Scan for the appropriate larger type to use.
5216 while (1) {
5217 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5218 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5219
5220 // If the target supports FP_TO_SINT returning this type, use it.
5221 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5222 default: break;
5223 case TargetLowering::Legal:
5224 if (!TLI.isTypeLegal(NewOutTy))
5225 break; // Can't use this datatype.
5226 // FALL THROUGH.
5227 case TargetLowering::Custom:
5228 OpToUse = ISD::FP_TO_SINT;
5229 break;
5230 }
5231 if (OpToUse) break;
5232
5233 // If the target supports FP_TO_UINT of this type, use it.
5234 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5235 default: break;
5236 case TargetLowering::Legal:
5237 if (!TLI.isTypeLegal(NewOutTy))
5238 break; // Can't use this datatype.
5239 // FALL THROUGH.
5240 case TargetLowering::Custom:
5241 OpToUse = ISD::FP_TO_UINT;
5242 break;
5243 }
5244 if (OpToUse) break;
5245
5246 // Otherwise, try a larger type.
5247 }
5248
Chris Lattnerf81d5882007-11-24 07:07:01 +00005249
5250 // Okay, we found the operation and type to use.
5251 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5252
5253 // If the operation produces an invalid type, it must be custom lowered. Use
5254 // the target lowering hooks to expand it. Just keep the low part of the
5255 // expanded operation, we know that we're truncating anyway.
5256 if (getTypeAction(NewOutTy) == Expand) {
5257 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5258 assert(Operation.Val && "Didn't return anything");
5259 }
5260
5261 // Truncate the result of the extended FP_TO_*INT operation to the desired
5262 // size.
5263 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005264}
5265
5266/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5267///
5268SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5269 MVT::ValueType VT = Op.getValueType();
5270 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5271 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5272 switch (VT) {
5273 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5274 case MVT::i16:
5275 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5276 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5277 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5278 case MVT::i32:
5279 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5280 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5281 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5282 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5283 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5284 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5285 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5286 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5287 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5288 case MVT::i64:
5289 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5290 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5291 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5292 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5293 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5294 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5295 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5296 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5297 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5298 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5299 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5300 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5301 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5302 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5303 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5304 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5305 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5306 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5307 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5308 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5309 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5310 }
5311}
5312
5313/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5314///
5315SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5316 switch (Opc) {
5317 default: assert(0 && "Cannot expand this yet!");
5318 case ISD::CTPOP: {
5319 static const uint64_t mask[6] = {
5320 0x5555555555555555ULL, 0x3333333333333333ULL,
5321 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5322 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5323 };
5324 MVT::ValueType VT = Op.getValueType();
5325 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005326 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005327 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5328 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5329 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5330 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5331 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5332 DAG.getNode(ISD::AND, VT,
5333 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5334 }
5335 return Op;
5336 }
5337 case ISD::CTLZ: {
5338 // for now, we do this:
5339 // x = x | (x >> 1);
5340 // x = x | (x >> 2);
5341 // ...
5342 // x = x | (x >>16);
5343 // x = x | (x >>32); // for 64-bit input
5344 // return popcount(~x);
5345 //
5346 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5347 MVT::ValueType VT = Op.getValueType();
5348 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005349 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005350 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5351 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5352 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5353 }
5354 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5355 return DAG.getNode(ISD::CTPOP, VT, Op);
5356 }
5357 case ISD::CTTZ: {
5358 // for now, we use: { return popcount(~x & (x - 1)); }
5359 // unless the target has ctlz but not ctpop, in which case we use:
5360 // { return 32 - nlz(~x & (x-1)); }
5361 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5362 MVT::ValueType VT = Op.getValueType();
5363 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5364 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5365 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5366 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5367 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5368 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5369 TLI.isOperationLegal(ISD::CTLZ, VT))
5370 return DAG.getNode(ISD::SUB, VT,
Dan Gohman1796f1f2007-05-18 17:52:13 +00005371 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00005372 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5373 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5374 }
5375 }
5376}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005377
Chris Lattnerdc750592005-01-07 07:47:09 +00005378/// ExpandOp - Expand the specified SDOperand into its two component pieces
5379/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5380/// LegalizeNodes map is filled in for any results that are not expanded, the
5381/// ExpandedNodes map is filled in for any results that are expanded, and the
5382/// Lo/Hi values are returned.
5383void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5384 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00005385 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00005386 SDNode *Node = Op.Val;
5387 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00005388 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohmana8665142007-06-25 16:23:39 +00005389 MVT::isVector(VT)) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00005390 "Cannot expand to FP value or to larger int value!");
5391
Chris Lattner1a570f12005-09-02 20:32:45 +00005392 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005393 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00005394 = ExpandedNodes.find(Op);
5395 if (I != ExpandedNodes.end()) {
5396 Lo = I->second.first;
5397 Hi = I->second.second;
5398 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00005399 }
5400
Chris Lattnerdc750592005-01-07 07:47:09 +00005401 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00005402 case ISD::CopyFromReg:
5403 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen666323e2007-10-10 01:01:31 +00005404 case ISD::FP_ROUND_INREG:
5405 if (VT == MVT::ppcf128 &&
5406 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5407 TargetLowering::Custom) {
Dale Johannesen6472eb62007-10-11 23:32:15 +00005408 SDOperand SrcLo, SrcHi, Src;
5409 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5410 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5411 SDOperand Result = TLI.LowerOperation(
5412 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen666323e2007-10-10 01:01:31 +00005413 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5414 Lo = Result.Val->getOperand(0);
5415 Hi = Result.Val->getOperand(1);
5416 break;
5417 }
5418 // fall through
Chris Lattner44cab002006-01-21 04:27:00 +00005419 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005420#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005421 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005422#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00005423 assert(0 && "Do not know how to expand this operator!");
5424 abort();
Dale Johannesenb066c1f2007-10-31 00:32:36 +00005425 case ISD::EXTRACT_VECTOR_ELT:
5426 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5427 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5428 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5429 return ExpandOp(Lo, Lo, Hi);
Nate Begemancda9aa72005-04-01 22:34:39 +00005430 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00005431 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00005432 Lo = DAG.getNode(ISD::UNDEF, NVT);
5433 Hi = DAG.getNode(ISD::UNDEF, NVT);
5434 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005435 case ISD::Constant: {
5436 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5437 Lo = DAG.getConstant(Cst, NVT);
5438 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5439 break;
5440 }
Evan Cheng47833a12006-12-12 21:32:44 +00005441 case ISD::ConstantFP: {
5442 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen007aa372007-10-11 18:07:22 +00005443 if (CFP->getValueType(0) == MVT::ppcf128) {
5444 APInt api = CFP->getValueAPF().convertToAPInt();
5445 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5446 MVT::f64);
5447 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5448 MVT::f64);
5449 break;
5450 }
Evan Cheng3766fc602006-12-12 22:19:28 +00005451 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00005452 if (getTypeAction(Lo.getValueType()) == Expand)
5453 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005454 break;
5455 }
Chris Lattner32e08b72005-03-28 22:03:13 +00005456 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005457 // Return the operands.
5458 Lo = Node->getOperand(0);
5459 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00005460 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00005461
5462 case ISD::MERGE_VALUES:
Chris Lattnercab915f2007-11-24 19:12:15 +00005463 if (Node->getNumValues() == 1) {
5464 ExpandOp(Op.getOperand(0), Lo, Hi);
5465 break;
5466 }
Chris Lattnerf81d5882007-11-24 07:07:01 +00005467 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5468 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5469 Op.getValue(1).getValueType() == MVT::Other &&
5470 "unhandled MERGE_VALUES");
5471 ExpandOp(Op.getOperand(0), Lo, Hi);
5472 // Remember that we legalized the chain.
5473 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5474 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005475
5476 case ISD::SIGN_EXTEND_INREG:
5477 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00005478 // sext_inreg the low part if needed.
5479 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5480
5481 // The high part gets the sign extension from the lo-part. This handles
5482 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005483 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5484 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5485 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005486 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00005487
Nate Begeman2fba8a32006-01-14 03:14:10 +00005488 case ISD::BSWAP: {
5489 ExpandOp(Node->getOperand(0), Lo, Hi);
5490 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5491 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5492 Lo = TempLo;
5493 break;
5494 }
5495
Chris Lattner55e9cde2005-05-11 04:51:16 +00005496 case ISD::CTPOP:
5497 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00005498 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5499 DAG.getNode(ISD::CTPOP, NVT, Lo),
5500 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00005501 Hi = DAG.getConstant(0, NVT);
5502 break;
5503
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005504 case ISD::CTLZ: {
5505 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005506 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005507 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5508 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005509 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5510 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005511 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5512 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5513
5514 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5515 Hi = DAG.getConstant(0, NVT);
5516 break;
5517 }
5518
5519 case ISD::CTTZ: {
5520 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005521 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005522 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5523 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005524 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5525 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005526 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5527 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5528
5529 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5530 Hi = DAG.getConstant(0, NVT);
5531 break;
5532 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00005533
Nate Begemane74795c2006-01-25 18:21:52 +00005534 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005535 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5536 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00005537 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5538 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5539
5540 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005541 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00005542 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5543 if (!TLI.isLittleEndian())
5544 std::swap(Lo, Hi);
5545 break;
5546 }
5547
Chris Lattnerdc750592005-01-07 07:47:09 +00005548 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005549 LoadSDNode *LD = cast<LoadSDNode>(Node);
5550 SDOperand Ch = LD->getChain(); // Legalize the chain.
5551 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5552 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman2af30632007-07-09 22:18:38 +00005553 int SVOffset = LD->getSrcValueOffset();
5554 unsigned Alignment = LD->getAlignment();
5555 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00005556
Evan Chenge71fe34d2006-10-09 20:57:25 +00005557 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman2af30632007-07-09 22:18:38 +00005558 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5559 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00005560 if (VT == MVT::f32 || VT == MVT::f64) {
5561 // f32->i32 or f64->i64 one to one expansion.
5562 // Remember that we legalized the chain.
5563 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00005564 // Recursively expand the new load.
5565 if (getTypeAction(NVT) == Expand)
5566 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005567 break;
5568 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00005569
Evan Chenge71fe34d2006-10-09 20:57:25 +00005570 // Increment the pointer to the other half.
5571 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5572 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5573 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005574 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00005575 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman2af30632007-07-09 22:18:38 +00005576 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5577 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00005578
Evan Chenge71fe34d2006-10-09 20:57:25 +00005579 // Build a factor node to remember that this load is independent of the
5580 // other one.
5581 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5582 Hi.getValue(1));
5583
5584 // Remember that we legalized the chain.
5585 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5586 if (!TLI.isLittleEndian())
5587 std::swap(Lo, Hi);
5588 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00005589 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00005590
Dale Johannesen6802d0c2007-10-19 20:29:00 +00005591 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5592 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Chenge370e0e2006-12-13 03:19:57 +00005593 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5594 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005595 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00005596 // Remember that we legalized the chain.
5597 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5598 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5599 break;
5600 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00005601
5602 if (EVT == NVT)
5603 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005604 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005605 else
5606 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005607 SVOffset, EVT, isVolatile,
5608 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005609
5610 // Remember that we legalized the chain.
5611 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5612
5613 if (ExtType == ISD::SEXTLOAD) {
5614 // The high part is obtained by SRA'ing all but one of the bits of the
5615 // lo part.
5616 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5617 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5618 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5619 } else if (ExtType == ISD::ZEXTLOAD) {
5620 // The high part is just a zero.
5621 Hi = DAG.getConstant(0, NVT);
5622 } else /* if (ExtType == ISD::EXTLOAD) */ {
5623 // The high part is undefined.
5624 Hi = DAG.getNode(ISD::UNDEF, NVT);
5625 }
5626 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005627 break;
5628 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005629 case ISD::AND:
5630 case ISD::OR:
5631 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5632 SDOperand LL, LH, RL, RH;
5633 ExpandOp(Node->getOperand(0), LL, LH);
5634 ExpandOp(Node->getOperand(1), RL, RH);
5635 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5636 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5637 break;
5638 }
5639 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005640 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00005641 ExpandOp(Node->getOperand(1), LL, LH);
5642 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00005643 if (getTypeAction(NVT) == Expand)
5644 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005645 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00005646 if (VT != MVT::f32)
5647 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00005648 break;
5649 }
Nate Begemane5b86d72005-08-10 20:51:12 +00005650 case ISD::SELECT_CC: {
5651 SDOperand TL, TH, FL, FH;
5652 ExpandOp(Node->getOperand(2), TL, TH);
5653 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00005654 if (getTypeAction(NVT) == Expand)
5655 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00005656 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5657 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00005658 if (VT != MVT::f32)
5659 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5660 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00005661 break;
5662 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005663 case ISD::ANY_EXTEND:
5664 // The low part is any extension of the input (which degenerates to a copy).
5665 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5666 // The high part is undefined.
5667 Hi = DAG.getNode(ISD::UNDEF, NVT);
5668 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005669 case ISD::SIGN_EXTEND: {
5670 // The low part is just a sign extension of the input (which degenerates to
5671 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005672 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005673
Chris Lattnerdc750592005-01-07 07:47:09 +00005674 // The high part is obtained by SRA'ing all but one of the bits of the lo
5675 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00005676 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005677 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5678 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00005679 break;
5680 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005681 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00005682 // The low part is just a zero extension of the input (which degenerates to
5683 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005684 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005685
Chris Lattnerdc750592005-01-07 07:47:09 +00005686 // The high part is just a zero.
5687 Hi = DAG.getConstant(0, NVT);
5688 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00005689
Chris Lattner59b27fa372007-02-13 23:55:16 +00005690 case ISD::TRUNCATE: {
5691 // The input value must be larger than this value. Expand *it*.
5692 SDOperand NewLo;
5693 ExpandOp(Node->getOperand(0), NewLo, Hi);
5694
5695 // The low part is now either the right size, or it is closer. If not the
5696 // right size, make an illegal truncate so we recursively expand it.
5697 if (NewLo.getValueType() != Node->getValueType(0))
5698 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5699 ExpandOp(NewLo, Lo, Hi);
5700 break;
5701 }
5702
Chris Lattner36e663d2005-12-23 00:16:34 +00005703 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005704 SDOperand Tmp;
5705 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5706 // If the target wants to, allow it to lower this itself.
5707 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5708 case Expand: assert(0 && "cannot expand FP!");
5709 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5710 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5711 }
5712 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5713 }
5714
Evan Cheng4eee7242006-12-09 02:42:38 +00005715 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00005716 if (VT == MVT::f32 || VT == MVT::f64) {
5717 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00005718 if (getTypeAction(NVT) == Expand)
5719 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00005720 break;
5721 }
5722
5723 // If source operand will be expanded to the same type as VT, i.e.
5724 // i64 <- f64, i32 <- f32, expand the source operand instead.
5725 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5726 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5727 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005728 break;
5729 }
5730
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005731 // Turn this into a load/store pair by default.
5732 if (Tmp.Val == 0)
Chris Lattner87bc3e72008-01-16 07:45:30 +00005733 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005734
Chris Lattner36e663d2005-12-23 00:16:34 +00005735 ExpandOp(Tmp, Lo, Hi);
5736 break;
5737 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005738
Chris Lattnerf81d5882007-11-24 07:07:01 +00005739 case ISD::READCYCLECOUNTER: {
Chris Lattner44c28c22005-11-20 22:56:56 +00005740 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5741 TargetLowering::Custom &&
5742 "Must custom expand ReadCycleCounter");
Chris Lattnerf81d5882007-11-24 07:07:01 +00005743 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
5744 assert(Tmp.Val && "Node must be custom expanded!");
5745 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner44c28c22005-11-20 22:56:56 +00005746 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerf81d5882007-11-24 07:07:01 +00005747 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005748 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00005749 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005750
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005751 // These operators cannot be expanded directly, emit them as calls to
5752 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005753 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005754 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00005755 SDOperand Op;
5756 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5757 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005758 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5759 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00005760 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005761
Chris Lattner941d84a2005-07-30 01:40:57 +00005762 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5763
Chris Lattnerfe68d752005-07-29 00:33:32 +00005764 // Now that the custom expander is done, expand the result, which is still
5765 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005766 if (Op.Val) {
5767 ExpandOp(Op, Lo, Hi);
5768 break;
5769 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005770 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005771
Dale Johannesenc0154c02007-10-05 20:04:43 +00005772 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005773 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005774 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00005775 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005776 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00005777 else if (Node->getOperand(0).getValueType() == MVT::f80)
5778 LC = RTLIB::FPTOSINT_F80_I64;
5779 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5780 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005781 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5782 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005783 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005784 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005785
Evan Cheng31cbddf2007-01-12 02:11:51 +00005786 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005787 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005788 SDOperand Op;
5789 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5790 case Expand: assert(0 && "cannot expand FP!");
5791 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5792 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5793 }
5794
5795 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5796
5797 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005798 if (Op.Val) {
5799 ExpandOp(Op, Lo, Hi);
5800 break;
5801 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005802 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005803
Evan Chengfd11ef42007-10-05 01:09:32 +00005804 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005805 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005806 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen789b5a52007-09-28 18:44:17 +00005807 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005808 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00005809 else if (Node->getOperand(0).getValueType() == MVT::f80)
5810 LC = RTLIB::FPTOUINT_F80_I64;
5811 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5812 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005813 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5814 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005815 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005816 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005817
Evan Cheng870e4f82006-01-09 18:31:59 +00005818 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005819 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005820 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005821 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005822 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005823 Op = TLI.LowerOperation(Op, DAG);
5824 if (Op.Val) {
5825 // Now that the custom expander is done, expand the result, which is
5826 // still VT.
5827 ExpandOp(Op, Lo, Hi);
5828 break;
5829 }
5830 }
5831
Chris Lattner72b503b2006-09-13 03:50:39 +00005832 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5833 // this X << 1 as X+X.
5834 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5835 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5836 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5837 SDOperand LoOps[2], HiOps[3];
5838 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5839 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5840 LoOps[1] = LoOps[0];
5841 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5842
5843 HiOps[1] = HiOps[0];
5844 HiOps[2] = Lo.getValue(1);
5845 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5846 break;
5847 }
5848 }
5849
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005850 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005851 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005852 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005853
5854 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005855 TargetLowering::LegalizeAction Action =
5856 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5857 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5858 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005859 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005860 break;
5861 }
5862
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005863 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005864 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5865 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005866 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005867 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005868
Evan Cheng870e4f82006-01-09 18:31:59 +00005869 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005870 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005871 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005872 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005873 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005874 Op = TLI.LowerOperation(Op, DAG);
5875 if (Op.Val) {
5876 // Now that the custom expander is done, expand the result, which is
5877 // still VT.
5878 ExpandOp(Op, Lo, Hi);
5879 break;
5880 }
5881 }
5882
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005883 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005884 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005885 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005886
5887 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005888 TargetLowering::LegalizeAction Action =
5889 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5890 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5891 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005892 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005893 break;
5894 }
5895
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005896 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005897 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5898 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005899 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005900 }
5901
5902 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005903 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005904 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005905 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005906 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005907 Op = TLI.LowerOperation(Op, DAG);
5908 if (Op.Val) {
5909 // Now that the custom expander is done, expand the result, which is
5910 // still VT.
5911 ExpandOp(Op, Lo, Hi);
5912 break;
5913 }
5914 }
5915
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005916 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005917 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005918 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005919
5920 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005921 TargetLowering::LegalizeAction Action =
5922 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5923 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5924 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005925 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005926 break;
5927 }
5928
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005929 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005930 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5931 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005932 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005933 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005934
Misha Brukman835702a2005-04-21 22:36:52 +00005935 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005936 case ISD::SUB: {
5937 // If the target wants to custom expand this, let them.
5938 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5939 TargetLowering::Custom) {
5940 Op = TLI.LowerOperation(Op, DAG);
5941 if (Op.Val) {
5942 ExpandOp(Op, Lo, Hi);
5943 break;
5944 }
5945 }
5946
5947 // Expand the subcomponents.
5948 SDOperand LHSL, LHSH, RHSL, RHSH;
5949 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5950 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005951 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5952 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005953 LoOps[0] = LHSL;
5954 LoOps[1] = RHSL;
5955 HiOps[0] = LHSH;
5956 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005957 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005958 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005959 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005960 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005961 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005962 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005963 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005964 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005965 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005966 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005967 }
Chris Lattner2135bc02007-05-17 18:15:41 +00005968
5969 case ISD::ADDC:
5970 case ISD::SUBC: {
5971 // Expand the subcomponents.
5972 SDOperand LHSL, LHSH, RHSL, RHSH;
5973 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5974 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5975 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5976 SDOperand LoOps[2] = { LHSL, RHSL };
5977 SDOperand HiOps[3] = { LHSH, RHSH };
5978
5979 if (Node->getOpcode() == ISD::ADDC) {
5980 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5981 HiOps[2] = Lo.getValue(1);
5982 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5983 } else {
5984 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5985 HiOps[2] = Lo.getValue(1);
5986 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5987 }
5988 // Remember that we legalized the flag.
5989 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5990 break;
5991 }
5992 case ISD::ADDE:
5993 case ISD::SUBE: {
5994 // Expand the subcomponents.
5995 SDOperand LHSL, LHSH, RHSL, RHSH;
5996 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5997 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5998 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5999 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6000 SDOperand HiOps[3] = { LHSH, RHSH };
6001
6002 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6003 HiOps[2] = Lo.getValue(1);
6004 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6005
6006 // Remember that we legalized the flag.
6007 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6008 break;
6009 }
Nate Begemanadd0c632005-04-11 03:01:51 +00006010 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00006011 // If the target wants to custom expand this, let them.
6012 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00006013 SDOperand New = TLI.LowerOperation(Op, DAG);
6014 if (New.Val) {
6015 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00006016 break;
6017 }
6018 }
6019
Evan Chenge93762d2006-09-01 18:17:58 +00006020 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6021 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohmana1603612007-10-08 18:33:35 +00006022 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6023 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6024 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanadd0c632005-04-11 03:01:51 +00006025 SDOperand LL, LH, RL, RH;
6026 ExpandOp(Node->getOperand(0), LL, LH);
6027 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohmana1603612007-10-08 18:33:35 +00006028 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
6029 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6030 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
6031 // FIXME: generalize this to handle other bit sizes
6032 if (LHSSB == 32 && RHSSB == 32 &&
6033 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
6034 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
6035 // The inputs are both zero-extended.
6036 if (HasUMUL_LOHI) {
6037 // We can emit a umul_lohi.
6038 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6039 Hi = SDOperand(Lo.Val, 1);
6040 break;
6041 }
6042 if (HasMULHU) {
6043 // We can emit a mulhu+mul.
6044 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6045 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6046 break;
6047 }
Dan Gohmana1603612007-10-08 18:33:35 +00006048 }
6049 if (LHSSB > BitSize && RHSSB > BitSize) {
6050 // The input values are both sign-extended.
6051 if (HasSMUL_LOHI) {
6052 // We can emit a smul_lohi.
6053 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6054 Hi = SDOperand(Lo.Val, 1);
6055 break;
6056 }
6057 if (HasMULHS) {
6058 // We can emit a mulhs+mul.
6059 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6060 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6061 break;
6062 }
6063 }
6064 if (HasUMUL_LOHI) {
6065 // Lo,Hi = umul LHS, RHS.
6066 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6067 DAG.getVTList(NVT, NVT), LL, RL);
6068 Lo = UMulLOHI;
6069 Hi = UMulLOHI.getValue(1);
Nate Begeman43144a22005-08-30 02:44:00 +00006070 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6071 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6072 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6073 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00006074 break;
Nate Begeman43144a22005-08-30 02:44:00 +00006075 }
Dale Johannesena4a972e2007-10-24 22:26:08 +00006076 if (HasMULHU) {
6077 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6078 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6079 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6080 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6081 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6082 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6083 break;
6084 }
Nate Begemanadd0c632005-04-11 03:01:51 +00006085 }
Evan Chenge93762d2006-09-01 18:17:58 +00006086
Dan Gohmana1603612007-10-08 18:33:35 +00006087 // If nothing else, we can make a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006088 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6089 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00006090 break;
6091 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006092 case ISD::SDIV:
6093 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6094 break;
6095 case ISD::UDIV:
6096 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6097 break;
6098 case ISD::SREM:
6099 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6100 break;
6101 case ISD::UREM:
6102 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6103 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00006104
Evan Cheng4eee7242006-12-09 02:42:38 +00006105 case ISD::FADD:
Duncan Sands53c954f2008-01-10 10:28:30 +00006106 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6107 RTLIB::ADD_F64,
6108 RTLIB::ADD_F80,
6109 RTLIB::ADD_PPCF128)),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006110 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006111 break;
6112 case ISD::FSUB:
Duncan Sands53c954f2008-01-10 10:28:30 +00006113 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6114 RTLIB::SUB_F64,
6115 RTLIB::SUB_F80,
6116 RTLIB::SUB_PPCF128)),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006117 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006118 break;
6119 case ISD::FMUL:
Duncan Sands53c954f2008-01-10 10:28:30 +00006120 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6121 RTLIB::MUL_F64,
6122 RTLIB::MUL_F80,
6123 RTLIB::MUL_PPCF128)),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006124 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006125 break;
6126 case ISD::FDIV:
Duncan Sands53c954f2008-01-10 10:28:30 +00006127 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6128 RTLIB::DIV_F64,
6129 RTLIB::DIV_F80,
6130 RTLIB::DIV_PPCF128)),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006131 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006132 break;
6133 case ISD::FP_EXTEND:
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006134 if (VT == MVT::ppcf128) {
6135 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6136 Node->getOperand(0).getValueType()==MVT::f64);
6137 const uint64_t zero = 0;
6138 if (Node->getOperand(0).getValueType()==MVT::f32)
6139 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6140 else
6141 Hi = Node->getOperand(0);
6142 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6143 break;
6144 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006145 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006146 break;
6147 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00006148 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006149 break;
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006150 case ISD::FPOWI:
Duncan Sands53c954f2008-01-10 10:28:30 +00006151 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6152 RTLIB::POWI_F64,
6153 RTLIB::POWI_F80,
6154 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006155 Node, false, Hi);
6156 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006157 case ISD::FSQRT:
6158 case ISD::FSIN:
6159 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006160 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00006161 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006162 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00006163 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6164 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006165 break;
6166 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00006167 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6168 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006169 break;
6170 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00006171 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6172 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006173 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006174 default: assert(0 && "Unreachable!");
6175 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006176 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00006177 break;
6178 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006179 case ISD::FABS: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006180 if (VT == MVT::ppcf128) {
6181 SDOperand Tmp;
6182 ExpandOp(Node->getOperand(0), Lo, Tmp);
6183 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6184 // lo = hi==fabs(hi) ? lo : -lo;
6185 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6186 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6187 DAG.getCondCode(ISD::SETEQ));
6188 break;
6189 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006190 SDOperand Mask = (VT == MVT::f64)
6191 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6192 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6193 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6194 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6195 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6196 if (getTypeAction(NVT) == Expand)
6197 ExpandOp(Lo, Lo, Hi);
6198 break;
6199 }
6200 case ISD::FNEG: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006201 if (VT == MVT::ppcf128) {
6202 ExpandOp(Node->getOperand(0), Lo, Hi);
6203 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6204 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6205 break;
6206 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006207 SDOperand Mask = (VT == MVT::f64)
6208 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6209 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6210 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6211 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6212 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6213 if (getTypeAction(NVT) == Expand)
6214 ExpandOp(Lo, Lo, Hi);
6215 break;
6216 }
Evan Cheng003feb02007-01-04 21:56:39 +00006217 case ISD::FCOPYSIGN: {
6218 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6219 if (getTypeAction(NVT) == Expand)
6220 ExpandOp(Lo, Lo, Hi);
6221 break;
6222 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006223 case ISD::SINT_TO_FP:
6224 case ISD::UINT_TO_FP: {
6225 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6226 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006227 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006228 static uint64_t zero = 0;
6229 if (isSigned) {
6230 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6231 Node->getOperand(0)));
6232 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6233 } else {
6234 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6235 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6236 Node->getOperand(0)));
6237 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6238 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006239 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006240 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6241 DAG.getConstant(0, MVT::i32),
6242 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6243 DAG.getConstantFP(
6244 APFloat(APInt(128, 2, TwoE32)),
6245 MVT::ppcf128)),
6246 Hi,
6247 DAG.getCondCode(ISD::SETLT)),
6248 Lo, Hi);
6249 }
6250 break;
6251 }
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006252 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6253 // si64->ppcf128 done by libcall, below
6254 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6255 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6256 Lo, Hi);
6257 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6258 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6259 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6260 DAG.getConstant(0, MVT::i64),
6261 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6262 DAG.getConstantFP(
6263 APFloat(APInt(128, 2, TwoE64)),
6264 MVT::ppcf128)),
6265 Hi,
6266 DAG.getCondCode(ISD::SETLT)),
6267 Lo, Hi);
6268 break;
6269 }
Evan Cheng75439b32007-09-27 07:35:39 +00006270 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006271 if (Node->getOperand(0).getValueType() == MVT::i64) {
6272 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006273 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006274 else if (VT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006275 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00006276 else if (VT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00006277 assert(isSigned);
Dale Johannesenc0154c02007-10-05 20:04:43 +00006278 LC = RTLIB::SINTTOFP_I64_F80;
6279 }
6280 else if (VT == MVT::ppcf128) {
6281 assert(isSigned);
6282 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006283 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006284 } else {
6285 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006286 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006287 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00006288 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006289 }
6290
6291 // Promote the operand if needed.
6292 if (getTypeAction(SrcVT) == Promote) {
6293 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6294 Tmp = isSigned
6295 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6296 DAG.getValueType(SrcVT))
6297 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6298 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6299 }
Evan Chengbf535fc2007-04-27 07:33:31 +00006300
6301 const char *LibCall = TLI.getLibcallName(LC);
6302 if (LibCall)
6303 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6304 else {
6305 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6306 Node->getOperand(0));
6307 if (getTypeAction(Lo.getValueType()) == Expand)
6308 ExpandOp(Lo, Lo, Hi);
6309 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006310 break;
6311 }
Evan Chengf3a80c62006-12-13 02:38:13 +00006312 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006313
Chris Lattnerac12f682005-12-21 18:02:52 +00006314 // Make sure the resultant values have been legalized themselves, unless this
6315 // is a type that requires multi-step expansion.
6316 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6317 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00006318 if (Hi.Val)
6319 // Don't legalize the high part if it is expanded to a single node.
6320 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00006321 }
Evan Cheng870e4f82006-01-09 18:31:59 +00006322
6323 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006324 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00006325 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00006326}
6327
Dan Gohmana8665142007-06-25 16:23:39 +00006328/// SplitVectorOp - Given an operand of vector type, break it down into
6329/// two smaller values, still of vector type.
Chris Lattner32206f52006-03-18 01:44:44 +00006330void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6331 SDOperand &Hi) {
Dan Gohmana8665142007-06-25 16:23:39 +00006332 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00006333 SDNode *Node = Op.Val;
Dan Gohman60028182007-09-24 15:54:53 +00006334 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattner32206f52006-03-18 01:44:44 +00006335 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begemanbd117f02007-11-15 21:15:26 +00006336
Dan Gohman60028182007-09-24 15:54:53 +00006337 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begemanbd117f02007-11-15 21:15:26 +00006338
6339 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6340 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6341
6342 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6343 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6344
Chris Lattner32206f52006-03-18 01:44:44 +00006345 // See if we already split it.
6346 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6347 = SplitNodes.find(Op);
6348 if (I != SplitNodes.end()) {
6349 Lo = I->second.first;
6350 Hi = I->second.second;
6351 return;
6352 }
6353
6354 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006355 default:
6356#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006357 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006358#endif
6359 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner67d77942007-11-19 20:21:32 +00006360 case ISD::UNDEF:
6361 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6362 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6363 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006364 case ISD::BUILD_PAIR:
6365 Lo = Node->getOperand(0);
6366 Hi = Node->getOperand(1);
6367 break;
Dan Gohmana90183e2007-09-28 23:53:40 +00006368 case ISD::INSERT_VECTOR_ELT: {
6369 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6370 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6371 SDOperand ScalarOp = Node->getOperand(1);
Nate Begemanbd117f02007-11-15 21:15:26 +00006372 if (Index < NewNumElts_Lo)
6373 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohmana90183e2007-09-28 23:53:40 +00006374 DAG.getConstant(Index, TLI.getPointerTy()));
6375 else
Nate Begemanbd117f02007-11-15 21:15:26 +00006376 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6377 DAG.getConstant(Index - NewNumElts_Lo,
6378 TLI.getPointerTy()));
Dan Gohmana90183e2007-09-28 23:53:40 +00006379 break;
6380 }
Chris Lattner6fa95ec2007-11-19 21:16:54 +00006381 case ISD::VECTOR_SHUFFLE: {
6382 // Build the low part.
6383 SDOperand Mask = Node->getOperand(2);
6384 SmallVector<SDOperand, 8> Ops;
6385 MVT::ValueType PtrVT = TLI.getPointerTy();
6386
6387 // Insert all of the elements from the input that are needed. We use
6388 // buildvector of extractelement here because the input vectors will have
6389 // to be legalized, so this makes the code simpler.
6390 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6391 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6392 SDOperand InVec = Node->getOperand(0);
6393 if (Idx >= NumElements) {
6394 InVec = Node->getOperand(1);
6395 Idx -= NumElements;
6396 }
6397 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6398 DAG.getConstant(Idx, PtrVT)));
6399 }
6400 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6401 Ops.clear();
6402
6403 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6404 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6405 SDOperand InVec = Node->getOperand(0);
6406 if (Idx >= NumElements) {
6407 InVec = Node->getOperand(1);
6408 Idx -= NumElements;
6409 }
6410 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6411 DAG.getConstant(Idx, PtrVT)));
6412 }
6413 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6414 break;
6415 }
Dan Gohmana8665142007-06-25 16:23:39 +00006416 case ISD::BUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006417 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begemanbd117f02007-11-15 21:15:26 +00006418 Node->op_begin()+NewNumElts_Lo);
6419 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006420
Nate Begemanbd117f02007-11-15 21:15:26 +00006421 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmana8665142007-06-25 16:23:39 +00006422 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00006423 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006424 break;
6425 }
Dan Gohmana8665142007-06-25 16:23:39 +00006426 case ISD::CONCAT_VECTORS: {
Nate Begemanbd117f02007-11-15 21:15:26 +00006427 // FIXME: Handle non-power-of-two vectors?
Dan Gohmana8665142007-06-25 16:23:39 +00006428 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6429 if (NewNumSubvectors == 1) {
6430 Lo = Node->getOperand(0);
6431 Hi = Node->getOperand(1);
6432 } else {
6433 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6434 Node->op_begin()+NewNumSubvectors);
Nate Begemanbd117f02007-11-15 21:15:26 +00006435 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00006436
Dan Gohmana8665142007-06-25 16:23:39 +00006437 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6438 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00006439 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmana8665142007-06-25 16:23:39 +00006440 }
Dan Gohman26455c42007-06-13 15:12:02 +00006441 break;
6442 }
Dan Gohman8f518b982007-10-17 14:48:28 +00006443 case ISD::SELECT: {
6444 SDOperand Cond = Node->getOperand(0);
6445
6446 SDOperand LL, LH, RL, RH;
6447 SplitVectorOp(Node->getOperand(1), LL, LH);
6448 SplitVectorOp(Node->getOperand(2), RL, RH);
6449
6450 if (MVT::isVector(Cond.getValueType())) {
6451 // Handle a vector merge.
6452 SDOperand CL, CH;
6453 SplitVectorOp(Cond, CL, CH);
Nate Begemanbd117f02007-11-15 21:15:26 +00006454 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6455 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00006456 } else {
6457 // Handle a simple select with vector operands.
Nate Begemanbd117f02007-11-15 21:15:26 +00006458 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6459 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00006460 }
6461 break;
6462 }
Dan Gohmana8665142007-06-25 16:23:39 +00006463 case ISD::ADD:
6464 case ISD::SUB:
6465 case ISD::MUL:
6466 case ISD::FADD:
6467 case ISD::FSUB:
6468 case ISD::FMUL:
6469 case ISD::SDIV:
6470 case ISD::UDIV:
6471 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00006472 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006473 case ISD::AND:
6474 case ISD::OR:
Dan Gohman36347a22007-11-19 15:15:03 +00006475 case ISD::XOR:
6476 case ISD::UREM:
6477 case ISD::SREM:
6478 case ISD::FREM: {
Chris Lattner32206f52006-03-18 01:44:44 +00006479 SDOperand LL, LH, RL, RH;
6480 SplitVectorOp(Node->getOperand(0), LL, LH);
6481 SplitVectorOp(Node->getOperand(1), RL, RH);
6482
Nate Begemanbd117f02007-11-15 21:15:26 +00006483 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6484 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00006485 break;
6486 }
Dan Gohman2a7de412007-10-11 23:57:53 +00006487 case ISD::FPOWI: {
6488 SDOperand L, H;
6489 SplitVectorOp(Node->getOperand(0), L, H);
6490
Nate Begemanbd117f02007-11-15 21:15:26 +00006491 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6492 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman2a7de412007-10-11 23:57:53 +00006493 break;
6494 }
6495 case ISD::CTTZ:
6496 case ISD::CTLZ:
6497 case ISD::CTPOP:
6498 case ISD::FNEG:
6499 case ISD::FABS:
6500 case ISD::FSQRT:
6501 case ISD::FSIN:
Nate Begemand4d45c22007-11-17 03:58:34 +00006502 case ISD::FCOS:
6503 case ISD::FP_TO_SINT:
6504 case ISD::FP_TO_UINT:
6505 case ISD::SINT_TO_FP:
6506 case ISD::UINT_TO_FP: {
Dan Gohman2a7de412007-10-11 23:57:53 +00006507 SDOperand L, H;
6508 SplitVectorOp(Node->getOperand(0), L, H);
6509
Nate Begemanbd117f02007-11-15 21:15:26 +00006510 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6511 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman2a7de412007-10-11 23:57:53 +00006512 break;
6513 }
Dan Gohmana8665142007-06-25 16:23:39 +00006514 case ISD::LOAD: {
6515 LoadSDNode *LD = cast<LoadSDNode>(Node);
6516 SDOperand Ch = LD->getChain();
6517 SDOperand Ptr = LD->getBasePtr();
6518 const Value *SV = LD->getSrcValue();
6519 int SVOffset = LD->getSrcValueOffset();
6520 unsigned Alignment = LD->getAlignment();
6521 bool isVolatile = LD->isVolatile();
6522
Nate Begemanbd117f02007-11-15 21:15:26 +00006523 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6524 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattner32206f52006-03-18 01:44:44 +00006525 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6526 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00006527 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00006528 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begemanbd117f02007-11-15 21:15:26 +00006529 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00006530
6531 // Build a factor node to remember that this load is independent of the
6532 // other one.
6533 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6534 Hi.getValue(1));
6535
6536 // Remember that we legalized the chain.
6537 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00006538 break;
6539 }
Dan Gohmana8665142007-06-25 16:23:39 +00006540 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006541 // We know the result is a vector. The input may be either a vector or a
6542 // scalar value.
Dan Gohman0de76942007-06-29 00:09:08 +00006543 SDOperand InOp = Node->getOperand(0);
6544 if (!MVT::isVector(InOp.getValueType()) ||
6545 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6546 // The input is a scalar or single-element vector.
6547 // Lower to a store/load so that it can be split.
6548 // FIXME: this could be improved probably.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00006549 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006550
Evan Chengdf9ac472006-10-05 23:01:46 +00006551 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman0de76942007-06-29 00:09:08 +00006552 InOp, Ptr, NULL, 0);
6553 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006554 }
Dan Gohman0de76942007-06-29 00:09:08 +00006555 // Split the vector and convert each of the pieces now.
6556 SplitVectorOp(InOp, Lo, Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00006557 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6558 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman0de76942007-06-29 00:09:08 +00006559 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006560 }
Chris Lattner32206f52006-03-18 01:44:44 +00006561 }
6562
6563 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006564 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00006565 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00006566 assert(isNew && "Value already split?!?");
Chris Lattner32206f52006-03-18 01:44:44 +00006567}
6568
6569
Dan Gohmanf4e86da2007-06-27 14:06:22 +00006570/// ScalarizeVectorOp - Given an operand of single-element vector type
6571/// (e.g. v1f32), convert it into the equivalent operation that returns a
6572/// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +00006573SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6574 assert(MVT::isVector(Op.getValueType()) &&
6575 "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00006576 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00006577 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6578 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00006579
Dan Gohmana8665142007-06-25 16:23:39 +00006580 // See if we already scalarized it.
6581 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6582 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00006583
6584 SDOperand Result;
6585 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00006586 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006587#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006588 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006589#endif
Dan Gohmana8665142007-06-25 16:23:39 +00006590 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6591 case ISD::ADD:
6592 case ISD::FADD:
6593 case ISD::SUB:
6594 case ISD::FSUB:
6595 case ISD::MUL:
6596 case ISD::FMUL:
6597 case ISD::SDIV:
6598 case ISD::UDIV:
6599 case ISD::FDIV:
6600 case ISD::SREM:
6601 case ISD::UREM:
6602 case ISD::FREM:
Dan Gohman2a7de412007-10-11 23:57:53 +00006603 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006604 case ISD::AND:
6605 case ISD::OR:
6606 case ISD::XOR:
6607 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00006608 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00006609 ScalarizeVectorOp(Node->getOperand(0)),
6610 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00006611 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006612 case ISD::FNEG:
6613 case ISD::FABS:
6614 case ISD::FSQRT:
6615 case ISD::FSIN:
6616 case ISD::FCOS:
6617 Result = DAG.getNode(Node->getOpcode(),
6618 NewVT,
6619 ScalarizeVectorOp(Node->getOperand(0)));
6620 break;
Dan Gohman4f056f32007-10-12 14:13:46 +00006621 case ISD::FPOWI:
6622 Result = DAG.getNode(Node->getOpcode(),
6623 NewVT,
6624 ScalarizeVectorOp(Node->getOperand(0)),
6625 Node->getOperand(1));
6626 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006627 case ISD::LOAD: {
6628 LoadSDNode *LD = cast<LoadSDNode>(Node);
6629 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6630 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00006631
Dan Gohmana8665142007-06-25 16:23:39 +00006632 const Value *SV = LD->getSrcValue();
6633 int SVOffset = LD->getSrcValueOffset();
6634 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6635 LD->isVolatile(), LD->getAlignment());
6636
Chris Lattner32206f52006-03-18 01:44:44 +00006637 // Remember that we legalized the chain.
6638 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6639 break;
6640 }
Dan Gohmana8665142007-06-25 16:23:39 +00006641 case ISD::BUILD_VECTOR:
6642 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00006643 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006644 case ISD::INSERT_VECTOR_ELT:
6645 // Returning the inserted scalar element.
6646 Result = Node->getOperand(1);
6647 break;
6648 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00006649 assert(Node->getOperand(0).getValueType() == NewVT &&
6650 "Concat of non-legal vectors not yet supported!");
6651 Result = Node->getOperand(0);
6652 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006653 case ISD::VECTOR_SHUFFLE: {
6654 // Figure out if the scalar is the LHS or RHS and return it.
6655 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6656 if (cast<ConstantSDNode>(EltNum)->getValue())
6657 Result = ScalarizeVectorOp(Node->getOperand(1));
6658 else
6659 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00006660 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006661 }
6662 case ISD::EXTRACT_SUBVECTOR:
6663 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00006664 assert(Result.getValueType() == NewVT);
6665 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006666 case ISD::BIT_CONVERT:
6667 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattnerf6f94d32006-03-28 20:24:43 +00006668 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006669 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00006670 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00006671 ScalarizeVectorOp(Op.getOperand(1)),
6672 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00006673 break;
Chris Lattner32206f52006-03-18 01:44:44 +00006674 }
6675
6676 if (TLI.isTypeLegal(NewVT))
6677 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00006678 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6679 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00006680 return Result;
6681}
6682
Chris Lattnerdc750592005-01-07 07:47:09 +00006683
6684// SelectionDAG::Legalize - This is the entry point for the file.
6685//
Chris Lattner4add7e32005-01-23 04:42:50 +00006686void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00006687 if (ViewLegalizeDAGs) viewGraph();
6688
Chris Lattnerdc750592005-01-07 07:47:09 +00006689 /// run - This is the main entry point to this class.
6690 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00006691 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00006692}
6693