blob: 1ad8e80258549fb6012588437b5a221f6ba8e77f [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey70323a82006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Cheng631ccc62007-08-16 23:50:06 +000018#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000019#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000020#include "llvm/Target/TargetData.h"
Evan Cheng84a28d42006-10-30 08:00:44 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000022#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000023#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000024#include "llvm/Constants.h"
Reid Spencera94d3942007-01-19 21:13:56 +000025#include "llvm/DerivedTypes.h"
Chris Lattneref598052006-04-02 03:07:27 +000026#include "llvm/Support/MathExtras.h"
27#include "llvm/Support/CommandLine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattnere83030b2007-02-03 01:12:36 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattner97af9d52006-08-08 01:09:31 +000030#include "llvm/ADT/SmallVector.h"
Chris Lattnerebeb48d2007-02-04 00:27:56 +000031#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng1d2e9952006-03-24 01:17:21 +000032#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000033using namespace llvm;
34
Chris Lattneref598052006-04-02 03:07:27 +000035#ifndef NDEBUG
36static cl::opt<bool>
37ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
38 cl::desc("Pop up a window to show dags before legalize"));
39#else
40static const bool ViewLegalizeDAGs = 0;
41#endif
42
Chris Lattnerdc750592005-01-07 07:47:09 +000043//===----------------------------------------------------------------------===//
44/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
45/// hacks on it until the target machine can handle it. This involves
46/// eliminating value sizes the machine cannot handle (promoting small sizes to
47/// large sizes or splitting up large values into small values) as well as
48/// eliminating operations the machine cannot handle.
49///
50/// This code also does a small amount of optimization and recognition of idioms
51/// as part of its processing. For example, if a target does not support a
52/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
53/// will attempt merge setcc and brc instructions into brcc's.
54///
55namespace {
Chris Lattner54a34cd2006-06-28 21:58:30 +000056class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattnerdc750592005-01-07 07:47:09 +000057 TargetLowering &TLI;
58 SelectionDAG &DAG;
59
Chris Lattner462505f2006-02-13 09:18:02 +000060 // Libcall insertion helpers.
61
62 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
63 /// legalized. We use this to ensure that calls are properly serialized
64 /// against each other, including inserted libcalls.
65 SDOperand LastCALLSEQ_END;
66
67 /// IsLegalizingCall - This member is used *only* for purposes of providing
68 /// helpful assertions that a libcall isn't created while another call is
69 /// being legalized (which could lead to non-serialized call sequences).
70 bool IsLegalizingCall;
71
Chris Lattnerdc750592005-01-07 07:47:09 +000072 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000073 Legal, // The target natively supports this operation.
74 Promote, // This operation should be executed in a larger type.
Chris Lattneraa2372562006-05-24 17:04:05 +000075 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000076 };
Chris Lattner462505f2006-02-13 09:18:02 +000077
Chris Lattnerdc750592005-01-07 07:47:09 +000078 /// ValueTypeActions - This is a bitvector that contains two bits for each
79 /// value type, where the two bits correspond to the LegalizeAction enum.
80 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000081 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000082
Chris Lattnerdc750592005-01-07 07:47:09 +000083 /// LegalizedNodes - For nodes that are of legal width, and that have more
84 /// than one use, this map indicates what regularized operand to use. This
85 /// allows us to avoid legalizing the same thing more than once.
Chris Lattnered39c862007-02-04 00:50:02 +000086 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000087
Chris Lattner1f2c9d82005-01-15 05:21:40 +000088 /// PromotedNodes - For nodes that are below legal width, and that have more
89 /// than one use, this map indicates what promoted value to use. This allows
90 /// us to avoid promoting the same thing more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +000091 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner1f2c9d82005-01-15 05:21:40 +000092
Chris Lattner32206f52006-03-18 01:44:44 +000093 /// ExpandedNodes - For nodes that need to be expanded this map indicates
94 /// which which operands are the expanded version of the input. This allows
95 /// us to avoid expanding the same node more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +000096 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000097
Chris Lattner32206f52006-03-18 01:44:44 +000098 /// SplitNodes - For vector nodes that need to be split, this map indicates
99 /// which which operands are the split version of the input. This allows us
100 /// to avoid splitting the same node more than once.
101 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
102
Dan Gohmana8665142007-06-25 16:23:39 +0000103 /// ScalarizedNodes - For nodes that need to be converted from vector types to
104 /// scalar types, this contains the mapping of ones we have already
Chris Lattner32206f52006-03-18 01:44:44 +0000105 /// processed to the result.
Dan Gohmana8665142007-06-25 16:23:39 +0000106 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattner32206f52006-03-18 01:44:44 +0000107
Chris Lattnerea4ca942005-01-07 22:28:47 +0000108 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +0000109 LegalizedNodes.insert(std::make_pair(From, To));
110 // If someone requests legalization of the new node, return itself.
111 if (From != To)
112 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000113 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000114 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner4b0ddb22007-02-04 01:17:38 +0000115 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000116 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +0000117 // If someone requests legalization of the new node, return itself.
118 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000119 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000120
Chris Lattnerdc750592005-01-07 07:47:09 +0000121public:
122
Chris Lattner4add7e32005-01-23 04:42:50 +0000123 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000124
Chris Lattnerdc750592005-01-07 07:47:09 +0000125 /// getTypeAction - Return how we should legalize values of this type, either
126 /// it is already legal or we need to expand it into multiple registers of
127 /// smaller integer type, or we need to promote it to a larger type.
128 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +0000129 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +0000130 }
131
132 /// isTypeLegal - Return true if this type is legal on this target.
133 ///
134 bool isTypeLegal(MVT::ValueType VT) const {
135 return getTypeAction(VT) == Legal;
136 }
137
Chris Lattnerdc750592005-01-07 07:47:09 +0000138 void LegalizeDAG();
139
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000140private:
Dan Gohmana8665142007-06-25 16:23:39 +0000141 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000142 /// appropriate for its type.
143 void HandleOp(SDOperand Op);
144
145 /// LegalizeOp - We know that the specified value has a legal type.
146 /// Recursively ensure that the operands have legal types, then return the
147 /// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000148 SDOperand LegalizeOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000149
Dan Gohman2a7de412007-10-11 23:57:53 +0000150 /// UnrollVectorOp - We know that the given vector has a legal type, however
151 /// the operation it performs is not legal and is an operation that we have
152 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
153 /// operating on each element individually.
154 SDOperand UnrollVectorOp(SDOperand O);
155
Chris Lattner32206f52006-03-18 01:44:44 +0000156 /// PromoteOp - Given an operation that produces a value in an invalid type,
157 /// promote it to compute the value into a larger type. The produced value
158 /// will have the correct bits for the low portion of the register, but no
159 /// guarantee is made about the top bits: it may be zero, sign-extended, or
160 /// garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000161 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000162
Chris Lattner32206f52006-03-18 01:44:44 +0000163 /// ExpandOp - Expand the specified SDOperand into its two component pieces
164 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
165 /// the LegalizeNodes map is filled in for any results that are not expanded,
166 /// the ExpandedNodes map is filled in for any results that are expanded, and
167 /// the Lo/Hi values are returned. This applies to integer types and Vector
168 /// types.
169 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
170
Dan Gohmana8665142007-06-25 16:23:39 +0000171 /// SplitVectorOp - Given an operand of vector type, break it down into
172 /// two smaller values.
Chris Lattner32206f52006-03-18 01:44:44 +0000173 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
174
Dan Gohmanf4e86da2007-06-27 14:06:22 +0000175 /// ScalarizeVectorOp - Given an operand of single-element vector type
176 /// (e.g. v1f32), convert it into the equivalent operation that returns a
177 /// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +0000178 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000179
Chris Lattner6be79822006-04-04 17:23:26 +0000180 /// isShuffleLegal - Return true if a vector shuffle is legal with the
181 /// specified mask and type. Targets can specify exactly which masks they
182 /// support and the code generator is tasked with not creating illegal masks.
183 ///
184 /// Note that this will also return true for shuffles that are promoted to a
185 /// different type.
186 ///
187 /// If this is a legal shuffle, this method returns the (possibly promoted)
188 /// build_vector Mask. If it's not a legal shuffle, it returns null.
189 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
190
Chris Lattner4488f0c2006-07-26 23:55:56 +0000191 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000192 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000193
Nate Begeman7e7f4392006-02-01 07:19:44 +0000194 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
195
Reid Spencere63b6512006-12-31 05:55:36 +0000196 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattneraac464e2005-01-21 06:05:23 +0000197 SDOperand &Hi);
198 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
199 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000200
Chris Lattner36e663d2005-12-23 00:16:34 +0000201 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000202 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner6be79822006-04-04 17:23:26 +0000203 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000204 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
205 SDOperand LegalOp,
206 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000207 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
208 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000209 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
210 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000211
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000212 SDOperand ExpandBSWAP(SDOperand Op);
213 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000214 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
215 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000216 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
217 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000218
Dan Gohmana8665142007-06-25 16:23:39 +0000219 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner42a5fca2006-04-02 05:06:04 +0000220 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattner6f423252006-03-31 17:55:51 +0000221
Chris Lattnerdc750592005-01-07 07:47:09 +0000222 SDOperand getIntPtrConstant(uint64_t Val) {
223 return DAG.getConstant(Val, TLI.getPointerTy());
224 }
225};
226}
227
Chris Lattner6be79822006-04-04 17:23:26 +0000228/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
229/// specified mask and type. Targets can specify exactly which masks they
230/// support and the code generator is tasked with not creating illegal masks.
231///
232/// Note that this will also return true for shuffles that are promoted to a
233/// different type.
234SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
235 SDOperand Mask) const {
236 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
237 default: return 0;
238 case TargetLowering::Legal:
239 case TargetLowering::Custom:
240 break;
241 case TargetLowering::Promote: {
242 // If this is promoted to a different type, convert the shuffle mask and
243 // ask if it is legal in the promoted type!
244 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
245
246 // If we changed # elements, change the shuffle mask.
247 unsigned NumEltsGrowth =
248 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
249 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
250 if (NumEltsGrowth > 1) {
251 // Renumber the elements.
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000252 SmallVector<SDOperand, 8> Ops;
Chris Lattner6be79822006-04-04 17:23:26 +0000253 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
254 SDOperand InOp = Mask.getOperand(i);
255 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
256 if (InOp.getOpcode() == ISD::UNDEF)
257 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
258 else {
259 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
260 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
261 }
262 }
263 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000264 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +0000265 }
266 VT = NVT;
267 break;
268 }
269 }
270 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
271}
272
Chris Lattner4add7e32005-01-23 04:42:50 +0000273SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
274 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
275 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000276 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000277 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000278}
279
Chris Lattnere31adc82007-06-18 21:28:10 +0000280/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
281/// contains all of a nodes operands before it contains the node.
282static void ComputeTopDownOrdering(SelectionDAG &DAG,
283 SmallVector<SDNode*, 64> &Order) {
284
285 DenseMap<SDNode*, unsigned> Visited;
286 std::vector<SDNode*> Worklist;
287 Worklist.reserve(128);
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000288
Chris Lattnere31adc82007-06-18 21:28:10 +0000289 // Compute ordering from all of the leaves in the graphs, those (like the
290 // entry node) that have no operands.
291 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
292 E = DAG.allnodes_end(); I != E; ++I) {
293 if (I->getNumOperands() == 0) {
294 Visited[I] = 0 - 1U;
295 Worklist.push_back(I);
296 }
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000297 }
298
Chris Lattnere31adc82007-06-18 21:28:10 +0000299 while (!Worklist.empty()) {
300 SDNode *N = Worklist.back();
301 Worklist.pop_back();
302
303 if (++Visited[N] != N->getNumOperands())
304 continue; // Haven't visited all operands yet
305
306 Order.push_back(N);
307
308 // Now that we have N in, add anything that uses it if all of their operands
309 // are now done.
310 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
311 UI != E; ++UI)
312 Worklist.push_back(*UI);
313 }
314
315 assert(Order.size() == Visited.size() &&
316 Order.size() ==
317 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
318 "Error: DAG is cyclic!");
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000319}
320
Chris Lattner44fe26f2005-07-29 00:11:56 +0000321
Chris Lattnerdc750592005-01-07 07:47:09 +0000322void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000323 LastCALLSEQ_END = DAG.getEntryNode();
324 IsLegalizingCall = false;
325
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000326 // The legalize process is inherently a bottom-up recursive process (users
327 // legalize their uses before themselves). Given infinite stack space, we
328 // could just start legalizing on the root and traverse the whole graph. In
329 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000330 // blocks. To avoid this problem, compute an ordering of the nodes where each
331 // node is only legalized after all of its operands are legalized.
Chris Lattner94c44c92007-02-04 01:20:02 +0000332 SmallVector<SDNode*, 64> Order;
Chris Lattnere31adc82007-06-18 21:28:10 +0000333 ComputeTopDownOrdering(DAG, Order);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000334
Chris Lattner32206f52006-03-18 01:44:44 +0000335 for (unsigned i = 0, e = Order.size(); i != e; ++i)
336 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000337
338 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000339 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000340 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
341 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000342
343 ExpandedNodes.clear();
344 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000345 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000346 SplitNodes.clear();
Dan Gohmana8665142007-06-25 16:23:39 +0000347 ScalarizedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000348
349 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000350 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000351}
352
Chris Lattner462505f2006-02-13 09:18:02 +0000353
354/// FindCallEndFromCallStart - Given a chained node that is part of a call
355/// sequence, find the CALLSEQ_END node that terminates the call sequence.
356static SDNode *FindCallEndFromCallStart(SDNode *Node) {
357 if (Node->getOpcode() == ISD::CALLSEQ_END)
358 return Node;
359 if (Node->use_empty())
360 return 0; // No CallSeqEnd
361
362 // The chain is usually at the end.
363 SDOperand TheChain(Node, Node->getNumValues()-1);
364 if (TheChain.getValueType() != MVT::Other) {
365 // Sometimes it's at the beginning.
366 TheChain = SDOperand(Node, 0);
367 if (TheChain.getValueType() != MVT::Other) {
368 // Otherwise, hunt for it.
369 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
370 if (Node->getValueType(i) == MVT::Other) {
371 TheChain = SDOperand(Node, i);
372 break;
373 }
374
375 // Otherwise, we walked into a node without a chain.
376 if (TheChain.getValueType() != MVT::Other)
377 return 0;
378 }
379 }
380
381 for (SDNode::use_iterator UI = Node->use_begin(),
382 E = Node->use_end(); UI != E; ++UI) {
383
384 // Make sure to only follow users of our token chain.
385 SDNode *User = *UI;
386 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
387 if (User->getOperand(i) == TheChain)
388 if (SDNode *Result = FindCallEndFromCallStart(User))
389 return Result;
390 }
391 return 0;
392}
393
394/// FindCallStartFromCallEnd - Given a chained node that is part of a call
395/// sequence, find the CALLSEQ_START node that initiates the call sequence.
396static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
397 assert(Node && "Didn't find callseq_start for a call??");
398 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
399
400 assert(Node->getOperand(0).getValueType() == MVT::Other &&
401 "Node doesn't have a token chain argument!");
402 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
403}
404
405/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
406/// see if any uses can reach Dest. If no dest operands can get to dest,
407/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000408///
409/// Keep track of the nodes we fine that actually do lead to Dest in
410/// NodesLeadingTo. This avoids retraversing them exponential number of times.
411///
412bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000413 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000414 if (N == Dest) return true; // N certainly leads to Dest :)
415
Chris Lattner4488f0c2006-07-26 23:55:56 +0000416 // If we've already processed this node and it does lead to Dest, there is no
417 // need to reprocess it.
418 if (NodesLeadingTo.count(N)) return true;
419
Chris Lattner462505f2006-02-13 09:18:02 +0000420 // If the first result of this node has been already legalized, then it cannot
421 // reach N.
422 switch (getTypeAction(N->getValueType(0))) {
423 case Legal:
424 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
425 break;
426 case Promote:
427 if (PromotedNodes.count(SDOperand(N, 0))) return false;
428 break;
429 case Expand:
430 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
431 break;
432 }
433
434 // Okay, this node has not already been legalized. Check and legalize all
435 // operands. If none lead to Dest, then we can legalize this node.
436 bool OperandsLeadToDest = false;
437 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
438 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000439 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000440
Chris Lattner4488f0c2006-07-26 23:55:56 +0000441 if (OperandsLeadToDest) {
442 NodesLeadingTo.insert(N);
443 return true;
444 }
Chris Lattner462505f2006-02-13 09:18:02 +0000445
446 // Okay, this node looks safe, legalize it and return false.
Chris Lattner916ae072006-04-17 22:10:08 +0000447 HandleOp(SDOperand(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000448 return false;
449}
450
Dan Gohmana8665142007-06-25 16:23:39 +0000451/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000452/// appropriate for its type.
453void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohmana8665142007-06-25 16:23:39 +0000454 MVT::ValueType VT = Op.getValueType();
455 switch (getTypeAction(VT)) {
Chris Lattner32206f52006-03-18 01:44:44 +0000456 default: assert(0 && "Bad type action!");
Dan Gohmana8665142007-06-25 16:23:39 +0000457 case Legal: (void)LegalizeOp(Op); break;
458 case Promote: (void)PromoteOp(Op); break;
Chris Lattner32206f52006-03-18 01:44:44 +0000459 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +0000460 if (!MVT::isVector(VT)) {
461 // If this is an illegal scalar, expand it into its two component
462 // pieces.
Chris Lattner32206f52006-03-18 01:44:44 +0000463 SDOperand X, Y;
Chris Lattner2ed652f2007-08-25 01:00:22 +0000464 if (Op.getOpcode() == ISD::TargetConstant)
465 break; // Allow illegal target nodes.
Chris Lattner32206f52006-03-18 01:44:44 +0000466 ExpandOp(Op, X, Y);
Dan Gohmana8665142007-06-25 16:23:39 +0000467 } else if (MVT::getVectorNumElements(VT) == 1) {
468 // If this is an illegal single element vector, convert it to a
469 // scalar operation.
470 (void)ScalarizeVectorOp(Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000471 } else {
Dan Gohmana8665142007-06-25 16:23:39 +0000472 // Otherwise, this is an illegal multiple element vector.
473 // Split it in half and legalize both parts.
474 SDOperand X, Y;
475 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000476 }
477 break;
478 }
479}
Chris Lattner462505f2006-02-13 09:18:02 +0000480
Evan Cheng22cf8992006-12-13 20:57:08 +0000481/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
482/// a load from the constant pool.
483static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000484 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000485 bool Extend = false;
486
487 // If a FP immediate is precise when represented as a float and if the
488 // target can do an extending load from float to double, we put it into
489 // the constant pool as a float, even if it's is statically typed as a
490 // double.
491 MVT::ValueType VT = CFP->getValueType(0);
492 bool isDouble = VT == MVT::f64;
Dale Johannesen7f724e92007-09-16 16:51:49 +0000493 ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT),
Dale Johannesen98d3a082007-09-14 22:26:36 +0000494 CFP->getValueAPF());
Evan Cheng22cf8992006-12-13 20:57:08 +0000495 if (!UseCP) {
Dale Johannesen98d3a082007-09-14 22:26:36 +0000496 if (VT!=MVT::f64 && VT!=MVT::f32)
497 assert(0 && "Invalid type expansion");
Dale Johannesen028084e2007-09-12 03:30:33 +0000498 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
499 isDouble ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000500 }
501
Dale Johannesend246b2c2007-08-30 00:23:21 +0000502 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
Evan Cheng47833a12006-12-12 21:32:44 +0000503 // Only do this if the target has a native EXTLOAD instruction from f32.
Dale Johannesen98d3a082007-09-14 22:26:36 +0000504 // Do not try to be clever about long doubles (so far)
Evan Cheng47833a12006-12-12 21:32:44 +0000505 TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
506 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
507 VT = MVT::f32;
508 Extend = true;
509 }
510
511 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
512 if (Extend) {
513 return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
514 CPIdx, NULL, 0, MVT::f32);
515 } else {
516 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
517 }
518}
519
Chris Lattner462505f2006-02-13 09:18:02 +0000520
Evan Cheng003feb02007-01-04 21:56:39 +0000521/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
522/// operations.
523static
524SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
525 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng3b841dd2007-01-05 21:31:51 +0000526 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000527 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000528 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
529 "fcopysign expansion only supported for f32 and f64");
Evan Cheng003feb02007-01-04 21:56:39 +0000530 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000531
Evan Cheng003feb02007-01-04 21:56:39 +0000532 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000533 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000534 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
535 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000536 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000537 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000538 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000539 // Shift right or sign-extend it if the two operands have different types.
540 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
541 if (SizeDiff > 0) {
542 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
543 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
544 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
545 } else if (SizeDiff < 0)
546 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000547
548 // Clear the sign bit of first operand.
549 SDOperand Mask2 = (VT == MVT::f64)
550 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
551 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
552 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng003feb02007-01-04 21:56:39 +0000553 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000554 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
555
556 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000557 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
558 return Result;
559}
560
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000561/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
562static
563SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
564 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000565 SDOperand Chain = ST->getChain();
566 SDOperand Ptr = ST->getBasePtr();
567 SDOperand Val = ST->getValue();
568 MVT::ValueType VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000569 int Alignment = ST->getAlignment();
570 int SVOffset = ST->getSrcValueOffset();
571 if (MVT::isFloatingPoint(ST->getStoredVT())) {
572 // Expand to a bitconvert of the value to the integer type of the
573 // same size, then a (misaligned) int store.
574 MVT::ValueType intVT;
575 if (VT==MVT::f64)
576 intVT = MVT::i64;
577 else if (VT==MVT::f32)
578 intVT = MVT::i32;
579 else
580 assert(0 && "Unaligned load of unsupported floating point type");
581
582 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
583 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
584 SVOffset, ST->isVolatile(), Alignment);
585 }
586 assert(MVT::isInteger(ST->getStoredVT()) &&
587 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000588 // Get the half-size VT
589 MVT::ValueType NewStoredVT = ST->getStoredVT() - 1;
590 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000591 int IncrementSize = NumBits / 8;
592
593 // Divide the stored value in two parts.
594 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
595 SDOperand Lo = Val;
596 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
597
598 // Store the two parts
599 SDOperand Store1, Store2;
600 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
601 ST->getSrcValue(), SVOffset, NewStoredVT,
602 ST->isVolatile(), Alignment);
603 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
604 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
605 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
606 ST->getSrcValue(), SVOffset + IncrementSize,
607 NewStoredVT, ST->isVolatile(), Alignment);
608
609 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
610}
611
612/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
613static
614SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
615 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000616 int SVOffset = LD->getSrcValueOffset();
617 SDOperand Chain = LD->getChain();
618 SDOperand Ptr = LD->getBasePtr();
619 MVT::ValueType VT = LD->getValueType(0);
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000620 MVT::ValueType LoadedVT = LD->getLoadedVT();
621 if (MVT::isFloatingPoint(VT)) {
622 // Expand to a (misaligned) integer load of the same size,
623 // then bitconvert to floating point.
624 MVT::ValueType intVT;
625 if (LoadedVT==MVT::f64)
626 intVT = MVT::i64;
627 else if (LoadedVT==MVT::f32)
628 intVT = MVT::i32;
629 else
630 assert(0 && "Unaligned load of unsupported floating point type");
631
632 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
633 SVOffset, LD->isVolatile(),
634 LD->getAlignment());
635 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
636 if (LoadedVT != VT)
637 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
638
639 SDOperand Ops[] = { Result, Chain };
640 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
641 Ops, 2);
642 }
643 assert(MVT::isInteger(LoadedVT) && "Unaligned load of unsupported type.");
644 MVT::ValueType NewLoadedVT = LoadedVT - 1;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000645 int NumBits = MVT::getSizeInBits(NewLoadedVT);
646 int Alignment = LD->getAlignment();
647 int IncrementSize = NumBits / 8;
648 ISD::LoadExtType HiExtType = LD->getExtensionType();
649
650 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
651 if (HiExtType == ISD::NON_EXTLOAD)
652 HiExtType = ISD::ZEXTLOAD;
653
654 // Load the value in two parts
655 SDOperand Lo, Hi;
656 if (TLI.isLittleEndian()) {
657 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
658 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
659 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
660 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
661 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
662 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
663 Alignment);
664 } else {
665 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
666 NewLoadedVT,LD->isVolatile(), Alignment);
667 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
668 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
669 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
670 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
671 Alignment);
672 }
673
674 // aggregate the two parts
675 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
676 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
677 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
678
679 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
680 Hi.getValue(1));
681
682 SDOperand Ops[] = { Result, TF };
683 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
684}
Evan Cheng003feb02007-01-04 21:56:39 +0000685
Dan Gohman2a7de412007-10-11 23:57:53 +0000686/// UnrollVectorOp - We know that the given vector has a legal type, however
687/// the operation it performs is not legal and is an operation that we have
688/// no way of lowering. "Unroll" the vector, splitting out the scalars and
689/// operating on each element individually.
690SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
691 MVT::ValueType VT = Op.getValueType();
692 assert(isTypeLegal(VT) &&
693 "Caller should expand or promote operands that are not legal!");
694 assert(Op.Val->getNumValues() == 1 &&
695 "Can't unroll a vector with multiple results!");
696 unsigned NE = MVT::getVectorNumElements(VT);
697 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
698
699 SmallVector<SDOperand, 8> Scalars;
700 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
701 for (unsigned i = 0; i != NE; ++i) {
702 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
703 SDOperand Operand = Op.getOperand(j);
704 MVT::ValueType OperandVT = Operand.getValueType();
705 if (MVT::isVector(OperandVT)) {
706 // A vector operand; extract a single element.
707 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
708 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
709 OperandEltVT,
710 Operand,
711 DAG.getConstant(i, MVT::i32));
712 } else {
713 // A scalar operand; just use it as is.
714 Operands[j] = Operand;
715 }
716 }
717 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
718 &Operands[0], Operands.size()));
719 }
720
721 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
722}
723
Dan Gohmanff727882007-07-13 20:14:11 +0000724/// LegalizeOp - We know that the specified value has a legal type, and
725/// that its operands are legal. Now ensure that the operation itself
726/// is legal, recursively ensuring that the operands' operations remain
727/// legal.
Chris Lattnerdc750592005-01-07 07:47:09 +0000728SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner2ed652f2007-08-25 01:00:22 +0000729 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
730 return Op;
731
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000732 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000733 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000734 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000735
Chris Lattnerdc750592005-01-07 07:47:09 +0000736 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000737 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000738 if (Node->getNumValues() > 1) {
739 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000740 if (getTypeAction(Node->getValueType(i)) != Legal) {
741 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000742 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000743 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000744 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000745 }
746 }
747
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000748 // Note that LegalizeOp may be reentered even from single-use nodes, which
749 // means that we always must cache transformed nodes.
Chris Lattnered39c862007-02-04 00:50:02 +0000750 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000751 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000752
Nate Begemane5b86d72005-08-10 20:51:12 +0000753 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000754 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000755 bool isCustom = false;
756
Chris Lattnerdc750592005-01-07 07:47:09 +0000757 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000758 case ISD::FrameIndex:
759 case ISD::EntryToken:
760 case ISD::Register:
761 case ISD::BasicBlock:
762 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000763 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000764 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000765 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000766 case ISD::TargetConstantPool:
767 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000768 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000769 case ISD::TargetExternalSymbol:
770 case ISD::VALUETYPE:
771 case ISD::SRCVALUE:
772 case ISD::STRING:
773 case ISD::CONDCODE:
774 // Primitives must all be legal.
Duncan Sands052c8432007-10-16 09:07:20 +0000775 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattnereb637512006-01-28 08:31:04 +0000776 "This must be legal!");
777 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000778 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000779 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
780 // If this is a target node, legalize it by legalizing the operands then
781 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000782 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000783 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000784 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000785
Chris Lattner97af9d52006-08-08 01:09:31 +0000786 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000787
788 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
789 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
790 return Result.getValue(Op.ResNo);
791 }
792 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000793#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000794 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000795#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000796 assert(0 && "Do not know how to legalize this operator!");
797 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000798 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000799 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000800 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000801 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000802 case ISD::ConstantPool:
803 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000804 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
805 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000806 case TargetLowering::Custom:
807 Tmp1 = TLI.LowerOperation(Op, DAG);
808 if (Tmp1.Val) Result = Tmp1;
809 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000810 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000811 break;
812 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000813 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000814 case ISD::FRAMEADDR:
815 case ISD::RETURNADDR:
816 // The only option for these nodes is to custom lower them. If the target
817 // does not custom lower them, then return zero.
818 Tmp1 = TLI.LowerOperation(Op, DAG);
819 if (Tmp1.Val)
820 Result = Tmp1;
821 else
822 Result = DAG.getConstant(0, TLI.getPointerTy());
823 break;
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000824 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000825 MVT::ValueType VT = Node->getValueType(0);
826 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
827 default: assert(0 && "This action is not supported yet!");
828 case TargetLowering::Custom:
829 Result = TLI.LowerOperation(Op, DAG);
830 if (Result.Val) break;
831 // Fall Thru
832 case TargetLowering::Legal:
833 Result = DAG.getConstant(0, VT);
834 break;
835 }
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000836 }
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000837 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000838 case ISD::EXCEPTIONADDR: {
839 Tmp1 = LegalizeOp(Node->getOperand(0));
840 MVT::ValueType VT = Node->getValueType(0);
841 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
842 default: assert(0 && "This action is not supported yet!");
843 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000844 unsigned Reg = TLI.getExceptionAddressRegister();
Jim Laskey7f5872c2007-02-22 15:37:19 +0000845 Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo);
846 }
847 break;
848 case TargetLowering::Custom:
849 Result = TLI.LowerOperation(Op, DAG);
850 if (Result.Val) break;
851 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000852 case TargetLowering::Legal: {
853 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
854 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
855 Ops, 2).getValue(Op.ResNo);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000856 break;
857 }
858 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000859 }
Jim Laskey7f5872c2007-02-22 15:37:19 +0000860 break;
Jim Laskey644af6b2007-02-28 20:43:58 +0000861 case ISD::EHSELECTION: {
862 Tmp1 = LegalizeOp(Node->getOperand(0));
863 Tmp2 = LegalizeOp(Node->getOperand(1));
864 MVT::ValueType VT = Node->getValueType(0);
865 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
866 default: assert(0 && "This action is not supported yet!");
867 case TargetLowering::Expand: {
868 unsigned Reg = TLI.getExceptionSelectorRegister();
869 Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo);
870 }
871 break;
872 case TargetLowering::Custom:
873 Result = TLI.LowerOperation(Op, DAG);
874 if (Result.Val) break;
875 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000876 case TargetLowering::Legal: {
877 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
878 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
879 Ops, 2).getValue(Op.ResNo);
Jim Laskey644af6b2007-02-28 20:43:58 +0000880 break;
881 }
882 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000883 }
Jim Laskey644af6b2007-02-28 20:43:58 +0000884 break;
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000885 case ISD::EH_RETURN: {
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000886 MVT::ValueType VT = Node->getValueType(0);
887 // The only "good" option for this node is to custom lower it.
888 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
889 default: assert(0 && "This action is not supported at all!");
890 case TargetLowering::Custom:
891 Result = TLI.LowerOperation(Op, DAG);
892 if (Result.Val) break;
893 // Fall Thru
894 case TargetLowering::Legal:
895 // Target does not know, how to lower this, lower to noop
896 Result = LegalizeOp(Node->getOperand(0));
897 break;
898 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000899 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000900 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000901 case ISD::AssertSext:
902 case ISD::AssertZext:
903 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000904 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000905 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000906 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000907 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000908 Result = Node->getOperand(Op.ResNo);
909 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000910 case ISD::CopyFromReg:
911 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000912 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000913 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000914 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000915 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000916 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000917 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000918 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000919 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
920 } else {
921 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
922 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000923 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
924 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000925 // Since CopyFromReg produces two values, make sure to remember that we
926 // legalized both of them.
927 AddLegalizedOperand(Op.getValue(0), Result);
928 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
929 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000930 case ISD::UNDEF: {
931 MVT::ValueType VT = Op.getValueType();
932 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000933 default: assert(0 && "This action is not supported yet!");
934 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000935 if (MVT::isInteger(VT))
936 Result = DAG.getConstant(0, VT);
937 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf04d37d2007-09-26 17:26:49 +0000938 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
939 VT);
Nate Begemancda9aa72005-04-01 22:34:39 +0000940 else
941 assert(0 && "Unknown value type!");
942 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000943 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000944 break;
945 }
946 break;
947 }
Chris Lattnera4f68052006-03-24 02:26:29 +0000948
Chris Lattnere55d1712006-03-28 00:40:33 +0000949 case ISD::INTRINSIC_W_CHAIN:
950 case ISD::INTRINSIC_WO_CHAIN:
951 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +0000952 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +0000953 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
954 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +0000955 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +0000956
957 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +0000958 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +0000959 TargetLowering::Custom) {
960 Tmp3 = TLI.LowerOperation(Result, DAG);
961 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +0000962 }
963
964 if (Result.Val->getNumValues() == 1) break;
965
966 // Must have return value and chain result.
967 assert(Result.Val->getNumValues() == 2 &&
968 "Cannot return more than two values!");
969
970 // Since loads produce two values, make sure to remember that we
971 // legalized both of them.
972 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
973 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
974 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +0000975 }
Chris Lattner435b4022005-11-29 06:21:05 +0000976
977 case ISD::LOCATION:
978 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
979 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
980
981 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
982 case TargetLowering::Promote:
983 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +0000984 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +0000985 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000986 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +0000987 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000988
Jim Laskeyc56315c2007-01-26 21:22:28 +0000989 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +0000990 const std::string &FName =
991 cast<StringSDNode>(Node->getOperand(3))->getValue();
992 const std::string &DirName =
993 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +0000994 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000995
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000996 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +0000997 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000998 SDOperand LineOp = Node->getOperand(1);
999 SDOperand ColOp = Node->getOperand(2);
1000
1001 if (useDEBUG_LOC) {
1002 Ops.push_back(LineOp); // line #
1003 Ops.push_back(ColOp); // col #
1004 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001005 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001006 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +00001007 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1008 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +00001009 unsigned ID = MMI->RecordLabel(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001010 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Jim Laskeyf9e54452007-01-26 14:34:52 +00001011 Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001012 }
Jim Laskey9e296be2005-12-21 20:51:37 +00001013 } else {
1014 Result = Tmp1; // chain
1015 }
Chris Lattner435b4022005-11-29 06:21:05 +00001016 break;
Chris Lattnerc06da622005-12-18 23:54:29 +00001017 }
Chris Lattner435b4022005-11-29 06:21:05 +00001018 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +00001019 if (Tmp1 != Node->getOperand(0) ||
1020 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001021 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +00001022 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +00001023 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1024 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1025 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1026 } else {
1027 // Otherwise promote them.
1028 Ops.push_back(PromoteOp(Node->getOperand(1)));
1029 Ops.push_back(PromoteOp(Node->getOperand(2)));
1030 }
Chris Lattner435b4022005-11-29 06:21:05 +00001031 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1032 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +00001033 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +00001034 }
1035 break;
1036 }
1037 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001038
1039 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +00001040 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +00001041 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +00001042 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +00001043 case TargetLowering::Legal:
1044 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1045 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1046 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1047 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001048 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001049 break;
1050 }
1051 break;
1052
Jim Laskeyf9e54452007-01-26 14:34:52 +00001053 case ISD::LABEL:
1054 assert(Node->getNumOperands() == 2 && "Invalid LABEL node!");
1055 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001056 default: assert(0 && "This action is not supported yet!");
1057 case TargetLowering::Legal:
1058 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1059 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001060 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +00001061 break;
Chris Lattner567b9252007-03-03 19:21:38 +00001062 case TargetLowering::Expand:
1063 Result = LegalizeOp(Node->getOperand(0));
1064 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001065 }
Nate Begeman5965bd12006-02-17 05:43:56 +00001066 break;
Chris Lattner435b4022005-11-29 06:21:05 +00001067
Scott Michel9d09c5c2007-08-08 23:23:31 +00001068 case ISD::Constant: {
1069 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1070 unsigned opAction =
1071 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1072
Chris Lattnerdc750592005-01-07 07:47:09 +00001073 // We know we don't need to expand constants here, constants only have one
1074 // value and we check that it is fine above.
1075
Scott Michel9d09c5c2007-08-08 23:23:31 +00001076 if (opAction == TargetLowering::Custom) {
1077 Tmp1 = TLI.LowerOperation(Result, DAG);
1078 if (Tmp1.Val)
1079 Result = Tmp1;
1080 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001081 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +00001082 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001083 case ISD::ConstantFP: {
1084 // Spill FP immediates to the constant pool if the target cannot directly
1085 // codegen them. Targets often have some immediate values that can be
1086 // efficiently generated into an FP register without a load. We explicitly
1087 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +00001088 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1089
1090 // Check to see if this FP immediate is already legal.
1091 bool isLegal = false;
1092 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1093 E = TLI.legal_fpimm_end(); I != E; ++I)
1094 if (CFP->isExactlyValue(*I)) {
1095 isLegal = true;
1096 break;
1097 }
1098
Chris Lattner758b0ac2006-01-29 06:26:56 +00001099 // If this is a legal constant, turn it into a TargetConstantFP node.
1100 if (isLegal) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00001101 Result = DAG.getTargetConstantFP(CFP->getValueAPF(),
1102 CFP->getValueType(0));
Chris Lattner758b0ac2006-01-29 06:26:56 +00001103 break;
1104 }
1105
1106 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1107 default: assert(0 && "This action is not supported yet!");
1108 case TargetLowering::Custom:
1109 Tmp3 = TLI.LowerOperation(Result, DAG);
1110 if (Tmp3.Val) {
1111 Result = Tmp3;
1112 break;
1113 }
1114 // FALLTHROUGH
1115 case TargetLowering::Expand:
Evan Cheng3766fc602006-12-12 22:19:28 +00001116 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001117 }
1118 break;
1119 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001120 case ISD::TokenFactor:
1121 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001122 Tmp1 = LegalizeOp(Node->getOperand(0));
1123 Tmp2 = LegalizeOp(Node->getOperand(1));
1124 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1125 } else if (Node->getNumOperands() == 3) {
1126 Tmp1 = LegalizeOp(Node->getOperand(0));
1127 Tmp2 = LegalizeOp(Node->getOperand(1));
1128 Tmp3 = LegalizeOp(Node->getOperand(2));
1129 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001130 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +00001131 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001132 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001133 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1134 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001135 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001136 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001137 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001138
1139 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001140 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001141 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001142 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1143 assert(Tmp3.Val && "Target didn't custom lower this node!");
1144 assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() &&
1145 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001146
Chris Lattneraaa23d92006-05-16 22:53:20 +00001147 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001148 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +00001149 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
1150 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001151 if (Op.ResNo == i)
1152 Tmp2 = Tmp1;
1153 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1154 }
1155 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001156 case ISD::EXTRACT_SUBREG: {
1157 Tmp1 = LegalizeOp(Node->getOperand(0));
1158 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1159 assert(idx && "Operand must be a constant");
1160 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1161 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1162 }
1163 break;
1164 case ISD::INSERT_SUBREG: {
1165 Tmp1 = LegalizeOp(Node->getOperand(0));
1166 Tmp2 = LegalizeOp(Node->getOperand(1));
1167 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1168 assert(idx && "Operand must be a constant");
1169 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1170 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1171 }
1172 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001173 case ISD::BUILD_VECTOR:
1174 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001175 default: assert(0 && "This action is not supported yet!");
1176 case TargetLowering::Custom:
1177 Tmp3 = TLI.LowerOperation(Result, DAG);
1178 if (Tmp3.Val) {
1179 Result = Tmp3;
1180 break;
1181 }
1182 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001183 case TargetLowering::Expand:
1184 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001185 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001186 }
Chris Lattner29b23012006-03-19 01:17:20 +00001187 break;
1188 case ISD::INSERT_VECTOR_ELT:
1189 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
1190 Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal
1191 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
1192 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1193
1194 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1195 Node->getValueType(0))) {
1196 default: assert(0 && "This action is not supported yet!");
1197 case TargetLowering::Legal:
1198 break;
1199 case TargetLowering::Custom:
1200 Tmp3 = TLI.LowerOperation(Result, DAG);
1201 if (Tmp3.Val) {
1202 Result = Tmp3;
1203 break;
1204 }
1205 // FALLTHROUGH
1206 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001207 // If the insert index is a constant, codegen this as a scalar_to_vector,
1208 // then a shuffle that inserts it into the right position in the vector.
1209 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
1210 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1211 Tmp1.getValueType(), Tmp2);
1212
1213 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1214 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
Dan Gohman5c441312007-06-14 22:58:02 +00001215 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
Chris Lattner326870b2006-04-17 19:21:01 +00001216
1217 // We generate a shuffle of InVec and ScVec, so the shuffle mask should
1218 // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of
1219 // the RHS.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001220 SmallVector<SDOperand, 8> ShufOps;
Chris Lattner326870b2006-04-17 19:21:01 +00001221 for (unsigned i = 0; i != NumElts; ++i) {
1222 if (i != InsertPos->getValue())
1223 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1224 else
1225 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1226 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001227 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1228 &ShufOps[0], ShufOps.size());
Chris Lattner326870b2006-04-17 19:21:01 +00001229
1230 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1231 Tmp1, ScVec, ShufMask);
1232 Result = LegalizeOp(Result);
1233 break;
1234 }
1235
Chris Lattner29b23012006-03-19 01:17:20 +00001236 // If the target doesn't support this, we have to spill the input vector
1237 // to a temporary stack slot, update the element, then reload it. This is
1238 // badness. We could also load the value into a vector register (either
1239 // with a "move to register" or "extload into register" instruction, then
1240 // permute it into place, if the idx is a constant and if the idx is
1241 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001242 MVT::ValueType VT = Tmp1.getValueType();
1243 MVT::ValueType EltVT = Tmp2.getValueType();
1244 MVT::ValueType IdxVT = Tmp3.getValueType();
1245 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattnerd6f7d442007-10-15 17:48:57 +00001246 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001247 // Store the vector.
Evan Chengab51cf22006-10-13 21:14:26 +00001248 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001249
1250 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001251 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1252 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001253 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001254 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1255 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1256 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001257 // Store the scalar value.
Evan Chengab51cf22006-10-13 21:14:26 +00001258 Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0);
Evan Cheng168e45b2006-03-31 01:27:51 +00001259 // Load the updated vector.
Evan Chenge71fe34d2006-10-09 20:57:25 +00001260 Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0);
Chris Lattner29b23012006-03-19 01:17:20 +00001261 break;
1262 }
1263 }
1264 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001265 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001266 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1267 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1268 break;
1269 }
1270
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001271 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1272 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1273 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1274 Node->getValueType(0))) {
1275 default: assert(0 && "This action is not supported yet!");
1276 case TargetLowering::Legal:
1277 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001278 case TargetLowering::Custom:
1279 Tmp3 = TLI.LowerOperation(Result, DAG);
1280 if (Tmp3.Val) {
1281 Result = Tmp3;
1282 break;
1283 }
1284 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001285 case TargetLowering::Expand:
1286 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001287 break;
1288 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001289 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001290 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001291 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1292 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1293 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1294
1295 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001296 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1297 default: assert(0 && "Unknown operation action!");
1298 case TargetLowering::Legal:
1299 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1300 "vector shuffle should not be created if not legal!");
1301 break;
1302 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001303 Tmp3 = TLI.LowerOperation(Result, DAG);
1304 if (Tmp3.Val) {
1305 Result = Tmp3;
1306 break;
1307 }
1308 // FALLTHROUGH
1309 case TargetLowering::Expand: {
1310 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00001311 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng9fa89592006-04-05 06:07:11 +00001312 MVT::ValueType PtrVT = TLI.getPointerTy();
1313 SDOperand Mask = Node->getOperand(2);
1314 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001315 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001316 for (unsigned i = 0; i != NumElems; ++i) {
1317 SDOperand Arg = Mask.getOperand(i);
1318 if (Arg.getOpcode() == ISD::UNDEF) {
1319 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1320 } else {
1321 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1322 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1323 if (Idx < NumElems)
1324 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1325 DAG.getConstant(Idx, PtrVT)));
1326 else
1327 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1328 DAG.getConstant(Idx - NumElems, PtrVT)));
1329 }
1330 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001331 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001332 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001333 }
Chris Lattner6be79822006-04-04 17:23:26 +00001334 case TargetLowering::Promote: {
1335 // Change base type to a different vector type.
1336 MVT::ValueType OVT = Node->getValueType(0);
1337 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1338
1339 // Cast the two input vectors.
1340 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1341 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1342
1343 // Convert the shuffle mask to the right # elements.
1344 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1345 assert(Tmp3.Val && "Shuffle not legal?");
1346 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1347 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1348 break;
1349 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001350 }
1351 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001352
1353 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001354 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001355 Tmp2 = LegalizeOp(Node->getOperand(1));
1356 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001357 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001358 break;
1359
Dan Gohmana8665142007-06-25 16:23:39 +00001360 case ISD::EXTRACT_SUBVECTOR:
1361 Tmp1 = Node->getOperand(0);
1362 Tmp2 = LegalizeOp(Node->getOperand(1));
1363 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1364 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001365 break;
1366
Chris Lattner462505f2006-02-13 09:18:02 +00001367 case ISD::CALLSEQ_START: {
1368 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1369
1370 // Recursively Legalize all of the inputs of the call end that do not lead
1371 // to this call start. This ensures that any libcalls that need be inserted
1372 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001373 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001374 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001375 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1376 NodesLeadingTo);
1377 }
Chris Lattner462505f2006-02-13 09:18:02 +00001378
1379 // Now that we legalized all of the inputs (which may have inserted
1380 // libcalls) create the new CALLSEQ_START node.
1381 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1382
1383 // Merge in the last call, to ensure that this call start after the last
1384 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001385 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001386 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1387 Tmp1 = LegalizeOp(Tmp1);
1388 }
Chris Lattner462505f2006-02-13 09:18:02 +00001389
1390 // Do not try to legalize the target-specific arguments (#1+).
1391 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001392 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001393 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001394 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001395 }
1396
1397 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001398 AddLegalizedOperand(Op.getValue(0), Result);
1399 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1400 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1401
Chris Lattner462505f2006-02-13 09:18:02 +00001402 // Now that the callseq_start and all of the non-call nodes above this call
1403 // sequence have been legalized, legalize the call itself. During this
1404 // process, no libcalls can/will be inserted, guaranteeing that no calls
1405 // can overlap.
1406 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1407 SDOperand InCallSEQ = LastCALLSEQ_END;
1408 // Note that we are selecting this call!
1409 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1410 IsLegalizingCall = true;
1411
1412 // Legalize the call, starting from the CALLSEQ_END.
1413 LegalizeOp(LastCALLSEQ_END);
1414 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1415 return Result;
1416 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001417 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001418 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1419 // will cause this node to be legalized as well as handling libcalls right.
1420 if (LastCALLSEQ_END.Val != Node) {
1421 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001422 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001423 assert(I != LegalizedNodes.end() &&
1424 "Legalizing the call start should have legalized this node!");
1425 return I->second;
1426 }
1427
1428 // Otherwise, the call start has been legalized and everything is going
1429 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001430 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001431 // Do not try to legalize the target-specific arguments (#1+), except for
1432 // an optional flag input.
1433 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1434 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001435 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001436 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001437 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001438 }
1439 } else {
1440 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1441 if (Tmp1 != Node->getOperand(0) ||
1442 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001443 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001444 Ops[0] = Tmp1;
1445 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001446 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001447 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001448 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001449 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001450 // This finishes up call legalization.
1451 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001452
1453 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1454 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1455 if (Node->getNumValues() == 2)
1456 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1457 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001458 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng631ccc62007-08-16 23:50:06 +00001459 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerec26b482005-01-09 19:03:49 +00001460 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1461 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1462 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001463 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001464
Chris Lattnerd02b0542006-01-28 10:58:55 +00001465 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001466 Tmp2 = Result.getValue(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001467 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Cheng7f4ec822006-01-11 22:14:47 +00001468 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001469 case TargetLowering::Expand: {
1470 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1471 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1472 " not tell us which reg is the stack pointer!");
1473 SDOperand Chain = Tmp1.getOperand(0);
1474 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001475 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1476 Chain = SP.getValue(1);
1477 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1478 unsigned StackAlign =
1479 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1480 if (Align > StackAlign)
Evan Chengcb6d65e2007-08-17 18:02:22 +00001481 SP = DAG.getNode(ISD::AND, VT, SP,
1482 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng631ccc62007-08-16 23:50:06 +00001483 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
1484 Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001485 Tmp1 = LegalizeOp(Tmp1);
1486 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001487 break;
1488 }
1489 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001490 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1491 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001492 Tmp1 = LegalizeOp(Tmp3);
1493 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001494 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001495 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001496 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001497 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001498 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001499 // Since this op produce two values, make sure to remember that we
1500 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001501 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1502 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001503 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001504 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001505 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001506 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001507 bool Changed = false;
1508 // Legalize all of the operands of the inline asm, in case they are nodes
1509 // that need to be expanded or something. Note we skip the asm string and
1510 // all of the TargetConstant flags.
1511 SDOperand Op = LegalizeOp(Ops[0]);
1512 Changed = Op != Ops[0];
1513 Ops[0] = Op;
1514
1515 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1516 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1517 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1518 for (++i; NumVals; ++i, --NumVals) {
1519 SDOperand Op = LegalizeOp(Ops[i]);
1520 if (Op != Ops[i]) {
1521 Changed = true;
1522 Ops[i] = Op;
1523 }
1524 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001525 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001526
1527 if (HasInFlag) {
1528 Op = LegalizeOp(Ops.back());
1529 Changed |= Op != Ops.back();
1530 Ops.back() = Op;
1531 }
1532
1533 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001534 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001535
1536 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001537 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001538 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1539 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001540 }
Chris Lattner68a12142005-01-07 22:12:08 +00001541 case ISD::BR:
1542 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001543 // Ensure that libcalls are emitted before a branch.
1544 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1545 Tmp1 = LegalizeOp(Tmp1);
1546 LastCALLSEQ_END = DAG.getEntryNode();
1547
Chris Lattnerd02b0542006-01-28 10:58:55 +00001548 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001549 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001550 case ISD::BRIND:
1551 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1552 // Ensure that libcalls are emitted before a branch.
1553 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1554 Tmp1 = LegalizeOp(Tmp1);
1555 LastCALLSEQ_END = DAG.getEntryNode();
1556
1557 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1558 default: assert(0 && "Indirect target must be legal type (pointer)!");
1559 case Legal:
1560 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1561 break;
1562 }
1563 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1564 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001565 case ISD::BR_JT:
1566 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1567 // Ensure that libcalls are emitted before a branch.
1568 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1569 Tmp1 = LegalizeOp(Tmp1);
1570 LastCALLSEQ_END = DAG.getEntryNode();
1571
1572 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1573 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1574
1575 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1576 default: assert(0 && "This action is not supported yet!");
1577 case TargetLowering::Legal: break;
1578 case TargetLowering::Custom:
1579 Tmp1 = TLI.LowerOperation(Result, DAG);
1580 if (Tmp1.Val) Result = Tmp1;
1581 break;
1582 case TargetLowering::Expand: {
1583 SDOperand Chain = Result.getOperand(0);
1584 SDOperand Table = Result.getOperand(1);
1585 SDOperand Index = Result.getOperand(2);
1586
1587 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001588 MachineFunction &MF = DAG.getMachineFunction();
1589 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001590 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1591 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001592
1593 SDOperand LD;
1594 switch (EntrySize) {
1595 default: assert(0 && "Size of jump table not supported yet."); break;
1596 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break;
1597 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break;
1598 }
1599
1600 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001601 // For PIC, the sequence is:
1602 // BRIND(load(Jumptable + index) + RelocBase)
1603 // RelocBase is the JumpTable on PPC and X86, GOT on Alpha
1604 SDOperand Reloc;
1605 if (TLI.usesGlobalOffsetTable())
1606 Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy);
1607 else
1608 Reloc = Table;
Evan Chenge6d58472006-10-31 02:31:00 +00001609 Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD;
Evan Cheng84a28d42006-10-30 08:00:44 +00001610 Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc);
1611 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
1612 } else {
1613 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD);
1614 }
1615 }
1616 }
1617 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001618 case ISD::BRCOND:
1619 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001620 // Ensure that libcalls are emitted before a return.
1621 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1622 Tmp1 = LegalizeOp(Tmp1);
1623 LastCALLSEQ_END = DAG.getEntryNode();
1624
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001625 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1626 case Expand: assert(0 && "It's impossible to expand bools");
1627 case Legal:
1628 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1629 break;
1630 case Promote:
1631 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001632
1633 // The top bits of the promoted condition are not necessarily zero, ensure
1634 // that the value is properly zero extended.
Dan Gohman309d3d52007-06-22 14:59:07 +00001635 if (!DAG.MaskedValueIsZero(Tmp2,
Chris Lattnerdb189382006-11-27 04:39:56 +00001636 MVT::getIntVTBitMask(Tmp2.getValueType())^1))
1637 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001638 break;
1639 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001640
1641 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001642 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001643
1644 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1645 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001646 case TargetLowering::Legal: break;
1647 case TargetLowering::Custom:
1648 Tmp1 = TLI.LowerOperation(Result, DAG);
1649 if (Tmp1.Val) Result = Tmp1;
1650 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001651 case TargetLowering::Expand:
1652 // Expand brcond's setcc into its constituent parts and create a BR_CC
1653 // Node.
1654 if (Tmp2.getOpcode() == ISD::SETCC) {
1655 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1656 Tmp2.getOperand(0), Tmp2.getOperand(1),
1657 Node->getOperand(2));
1658 } else {
1659 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1660 DAG.getCondCode(ISD::SETNE), Tmp2,
1661 DAG.getConstant(0, Tmp2.getValueType()),
1662 Node->getOperand(2));
1663 }
1664 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001665 }
1666 break;
1667 case ISD::BR_CC:
1668 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001669 // Ensure that libcalls are emitted before a branch.
1670 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1671 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001672 Tmp2 = Node->getOperand(2); // LHS
1673 Tmp3 = Node->getOperand(3); // RHS
1674 Tmp4 = Node->getOperand(1); // CC
1675
1676 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001677 LastCALLSEQ_END = DAG.getEntryNode();
1678
Nate Begeman7e7f4392006-02-01 07:19:44 +00001679 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1680 // the LHS is a legal SETCC itself. In this case, we need to compare
1681 // the result against zero to select between true and false values.
1682 if (Tmp3.Val == 0) {
1683 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1684 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001685 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001686
1687 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1688 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001689
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001690 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1691 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001692 case TargetLowering::Legal: break;
1693 case TargetLowering::Custom:
1694 Tmp4 = TLI.LowerOperation(Result, DAG);
1695 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001696 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001697 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001698 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001699 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001700 LoadSDNode *LD = cast<LoadSDNode>(Node);
1701 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1702 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001703
Evan Chenge71fe34d2006-10-09 20:57:25 +00001704 ISD::LoadExtType ExtType = LD->getExtensionType();
1705 if (ExtType == ISD::NON_EXTLOAD) {
1706 MVT::ValueType VT = Node->getValueType(0);
1707 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1708 Tmp3 = Result.getValue(0);
1709 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001710
Evan Chenge71fe34d2006-10-09 20:57:25 +00001711 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1712 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001713 case TargetLowering::Legal:
1714 // If this is an unaligned load and the target doesn't support it,
1715 // expand it.
1716 if (!TLI.allowsUnalignedMemoryAccesses()) {
1717 unsigned ABIAlignment = TLI.getTargetData()->
1718 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1719 if (LD->getAlignment() < ABIAlignment){
1720 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1721 TLI);
1722 Tmp3 = Result.getOperand(0);
1723 Tmp4 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001724 Tmp3 = LegalizeOp(Tmp3);
1725 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001726 }
1727 }
1728 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001729 case TargetLowering::Custom:
1730 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1731 if (Tmp1.Val) {
1732 Tmp3 = LegalizeOp(Tmp1);
1733 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001734 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001735 break;
1736 case TargetLowering::Promote: {
1737 // Only promote a load of vector type to another.
1738 assert(MVT::isVector(VT) && "Cannot promote this load!");
1739 // Change base type to a different vector type.
1740 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1741
1742 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001743 LD->getSrcValueOffset(),
1744 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001745 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1746 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001747 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001748 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001749 }
1750 // Since loads produce two values, make sure to remember that we
1751 // legalized both of them.
1752 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1753 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1754 return Op.ResNo ? Tmp4 : Tmp3;
1755 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00001756 MVT::ValueType SrcVT = LD->getLoadedVT();
Evan Chenge71fe34d2006-10-09 20:57:25 +00001757 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
1758 default: assert(0 && "This action is not supported yet!");
1759 case TargetLowering::Promote:
1760 assert(SrcVT == MVT::i1 &&
1761 "Can only promote extending LOAD from i1 -> i8!");
1762 Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
1763 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00001764 MVT::i8, LD->isVolatile(), LD->getAlignment());
Duncan Sandsd42c8122007-10-17 13:49:58 +00001765 Tmp1 = Result.getValue(0);
1766 Tmp2 = Result.getValue(1);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001767 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001768 case TargetLowering::Custom:
1769 isCustom = true;
1770 // FALLTHROUGH
1771 case TargetLowering::Legal:
1772 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1773 Tmp1 = Result.getValue(0);
1774 Tmp2 = Result.getValue(1);
1775
1776 if (isCustom) {
1777 Tmp3 = TLI.LowerOperation(Result, DAG);
1778 if (Tmp3.Val) {
1779 Tmp1 = LegalizeOp(Tmp3);
1780 Tmp2 = LegalizeOp(Tmp3.getValue(1));
1781 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001782 } else {
1783 // If this is an unaligned load and the target doesn't support it,
1784 // expand it.
1785 if (!TLI.allowsUnalignedMemoryAccesses()) {
1786 unsigned ABIAlignment = TLI.getTargetData()->
1787 getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT()));
1788 if (LD->getAlignment() < ABIAlignment){
1789 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1790 TLI);
1791 Tmp1 = Result.getOperand(0);
1792 Tmp2 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001793 Tmp1 = LegalizeOp(Tmp1);
1794 Tmp2 = LegalizeOp(Tmp2);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001795 }
1796 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001797 }
1798 break;
1799 case TargetLowering::Expand:
1800 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
1801 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
1802 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001803 LD->getSrcValueOffset(),
1804 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001805 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
1806 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
1807 Tmp2 = LegalizeOp(Load.getValue(1));
1808 break;
1809 }
Chris Lattner296a83c2007-02-01 04:55:59 +00001810 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
Evan Chenge71fe34d2006-10-09 20:57:25 +00001811 // Turn the unsupported load into an EXTLOAD followed by an explicit
1812 // zero/sign extend inreg.
1813 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1814 Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001815 LD->getSrcValueOffset(), SrcVT,
1816 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001817 SDOperand ValRes;
1818 if (ExtType == ISD::SEXTLOAD)
1819 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1820 Result, DAG.getValueType(SrcVT));
1821 else
1822 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
1823 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
1824 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
1825 break;
1826 }
1827 // Since loads produce two values, make sure to remember that we legalized
1828 // both of them.
1829 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1830 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1831 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001832 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00001833 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001834 case ISD::EXTRACT_ELEMENT: {
1835 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1836 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001837 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00001838 case Legal:
1839 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1840 // 1 -> Hi
1841 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1842 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1843 TLI.getShiftAmountTy()));
1844 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1845 } else {
1846 // 0 -> Lo
1847 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1848 Node->getOperand(0));
1849 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001850 break;
1851 case Expand:
1852 // Get both the low and high parts.
1853 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1854 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1855 Result = Tmp2; // 1 -> Hi
1856 else
1857 Result = Tmp1; // 0 -> Lo
1858 break;
1859 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001860 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001861 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001862
1863 case ISD::CopyToReg:
1864 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001865
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001866 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001867 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00001868 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00001869 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001870 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001871 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001872 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001873 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001874 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00001875 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001876 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
1877 Tmp3);
1878 } else {
1879 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00001880 }
1881
1882 // Since this produces two values, make sure to remember that we legalized
1883 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001884 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00001885 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001886 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00001887 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001888 break;
1889
1890 case ISD::RET:
1891 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001892
1893 // Ensure that libcalls are emitted before a return.
1894 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1895 Tmp1 = LegalizeOp(Tmp1);
1896 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001897
Chris Lattnerdc750592005-01-07 07:47:09 +00001898 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00001899 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00001900 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00001901 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001902 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001903 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00001904 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00001905 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001906 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +00001907 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001908 SDOperand Lo, Hi;
1909 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00001910
1911 // Big endian systems want the hi reg first.
1912 if (!TLI.isLittleEndian())
1913 std::swap(Lo, Hi);
1914
Evan Cheng3432ab92006-12-11 19:27:14 +00001915 if (Hi.Val)
1916 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
1917 else
1918 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00001919 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001920 } else {
1921 SDNode *InVal = Tmp2.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00001922 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
1923 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001924
Dan Gohmana8665142007-06-25 16:23:39 +00001925 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00001926 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001927 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00001928 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00001929 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00001930 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001931 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001932 } else if (NumElems == 1) {
1933 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00001934 Tmp2 = ScalarizeVectorOp(Tmp2);
1935 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00001936 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001937
1938 // FIXME: Returns of gcc generic vectors smaller than a legal type
1939 // should be returned in integer registers!
1940
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001941 // The scalarized value type may not be legal, e.g. it might require
1942 // promotion or expansion. Relegalize the return.
1943 Result = LegalizeOp(Result);
1944 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001945 // FIXME: Returns of gcc generic vectors larger than a legal vector
1946 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001947 SDOperand Lo, Hi;
1948 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00001949 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00001950 Result = LegalizeOp(Result);
1951 }
1952 }
Misha Brukman835702a2005-04-21 22:36:52 +00001953 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001954 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001955 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00001956 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00001957 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001958 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001959 }
1960 break;
1961 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001962 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001963 break;
1964 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00001965 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00001966 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00001967 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00001968 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1969 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001970 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00001971 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001972 break;
1973 case Expand: {
1974 SDOperand Lo, Hi;
Dan Gohman3b62d722007-06-27 16:08:04 +00001975 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00001976 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001977 ExpandOp(Node->getOperand(i), Lo, Hi);
1978 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00001979 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00001980 if (Hi.Val) {
1981 NewValues.push_back(Hi);
1982 NewValues.push_back(Node->getOperand(i+1));
1983 }
Misha Brukman835702a2005-04-21 22:36:52 +00001984 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001985 }
1986 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001987 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001988 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001989
1990 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00001991 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00001992 else
Chris Lattner97af9d52006-08-08 01:09:31 +00001993 Result = DAG.getNode(ISD::RET, MVT::Other,
1994 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00001995 break;
1996 }
1997 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001998
Chris Lattner4d1ea712006-01-29 21:02:23 +00001999 if (Result.getOpcode() == ISD::RET) {
2000 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2001 default: assert(0 && "This action is not supported yet!");
2002 case TargetLowering::Legal: break;
2003 case TargetLowering::Custom:
2004 Tmp1 = TLI.LowerOperation(Result, DAG);
2005 if (Tmp1.Val) Result = Tmp1;
2006 break;
2007 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002008 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002009 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002010 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00002011 StoreSDNode *ST = cast<StoreSDNode>(Node);
2012 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2013 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00002014 int SVOffset = ST->getSrcValueOffset();
2015 unsigned Alignment = ST->getAlignment();
2016 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00002017
Evan Chengab51cf22006-10-13 21:14:26 +00002018 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002019 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2020 // FIXME: We shouldn't do this for TargetConstantFP's.
2021 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2022 // to phase ordering between legalized code and the dag combiner. This
2023 // probably means that we need to integrate dag combiner and legalizer
2024 // together.
Dale Johannesen98d3a082007-09-14 22:26:36 +00002025 // We generally can't do this one for long doubles.
Chris Lattner5e6fe052007-10-13 06:35:54 +00002026 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002027 if (CFP->getValueType(0) == MVT::f32 &&
2028 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen028084e2007-09-12 03:30:33 +00002029 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2030 convertToAPInt().getZExtValue(),
Dale Johannesen245dceb2007-09-11 18:32:33 +00002031 MVT::i32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00002032 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2033 SVOffset, isVolatile, Alignment);
2034 break;
2035 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002036 // If this target supports 64-bit registers, do a single 64-bit store.
2037 if (getTypeAction(MVT::i64) == Legal) {
2038 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2039 getZExtValue(), MVT::i64);
2040 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2041 SVOffset, isVolatile, Alignment);
2042 break;
2043 } else if (getTypeAction(MVT::i32) == Legal) {
2044 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2045 // stores. If the target supports neither 32- nor 64-bits, this
2046 // xform is certainly not worth it.
2047 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2048 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2049 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
2050 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
2051
2052 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2053 SVOffset, isVolatile, Alignment);
2054 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2055 getIntPtrConstant(4));
2056 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
2057 isVolatile, std::max(Alignment, 4U));
2058
2059 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2060 break;
2061 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002062 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002063 }
2064
Evan Chengab51cf22006-10-13 21:14:26 +00002065 switch (getTypeAction(ST->getStoredVT())) {
2066 case Legal: {
2067 Tmp3 = LegalizeOp(ST->getValue());
2068 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2069 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00002070
Evan Chengab51cf22006-10-13 21:14:26 +00002071 MVT::ValueType VT = Tmp3.getValueType();
2072 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2073 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002074 case TargetLowering::Legal:
2075 // If this is an unaligned store and the target doesn't support it,
2076 // expand it.
2077 if (!TLI.allowsUnalignedMemoryAccesses()) {
2078 unsigned ABIAlignment = TLI.getTargetData()->
2079 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2080 if (ST->getAlignment() < ABIAlignment)
2081 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2082 TLI);
2083 }
2084 break;
Evan Chengab51cf22006-10-13 21:14:26 +00002085 case TargetLowering::Custom:
2086 Tmp1 = TLI.LowerOperation(Result, DAG);
2087 if (Tmp1.Val) Result = Tmp1;
2088 break;
2089 case TargetLowering::Promote:
2090 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2091 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2092 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2093 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00002094 ST->getSrcValue(), SVOffset, isVolatile,
2095 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002096 break;
2097 }
2098 break;
2099 }
2100 case Promote:
2101 // Truncate the value and store the result.
2102 Tmp3 = PromoteOp(ST->getValue());
2103 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002104 SVOffset, ST->getStoredVT(),
2105 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002106 break;
2107
2108 case Expand:
2109 unsigned IncrementSize = 0;
2110 SDOperand Lo, Hi;
2111
2112 // If this is a vector type, then we have to calculate the increment as
2113 // the product of the element size in bytes, and the number of elements
2114 // in the high half of the vector.
Dan Gohmana8665142007-06-25 16:23:39 +00002115 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002116 SDNode *InVal = ST->getValue().Val;
Dan Gohmana8665142007-06-25 16:23:39 +00002117 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
2118 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
Evan Chengab51cf22006-10-13 21:14:26 +00002119
Dan Gohmana8665142007-06-25 16:23:39 +00002120 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002121 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00002122 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002123 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002124 // Turn this into a normal store of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002125 Tmp3 = LegalizeOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00002126 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002127 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002128 Result = LegalizeOp(Result);
2129 break;
2130 } else if (NumElems == 1) {
2131 // Turn this into a normal store of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002132 Tmp3 = ScalarizeVectorOp(Node->getOperand(1));
Evan Chengab51cf22006-10-13 21:14:26 +00002133 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002134 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002135 // The scalarized value type may not be legal, e.g. it might require
2136 // promotion or expansion. Relegalize the scalar store.
2137 Result = LegalizeOp(Result);
2138 break;
2139 } else {
2140 SplitVectorOp(Node->getOperand(1), Lo, Hi);
2141 IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8;
2142 }
2143 } else {
2144 ExpandOp(Node->getOperand(1), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00002145 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002146
2147 if (!TLI.isLittleEndian())
2148 std::swap(Lo, Hi);
2149 }
2150
2151 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002152 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002153
2154 if (Hi.Val == NULL) {
2155 // Must be int <-> float one-to-one expansion.
2156 Result = Lo;
2157 break;
2158 }
2159
Evan Chengab51cf22006-10-13 21:14:26 +00002160 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2161 getIntPtrConstant(IncrementSize));
2162 assert(isTypeLegal(Tmp2.getValueType()) &&
2163 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002164 SVOffset += IncrementSize;
2165 if (Alignment > IncrementSize)
2166 Alignment = IncrementSize;
Evan Chengab51cf22006-10-13 21:14:26 +00002167 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002168 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002169 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2170 break;
2171 }
2172 } else {
2173 // Truncating store
2174 assert(isTypeLegal(ST->getValue().getValueType()) &&
2175 "Cannot handle illegal TRUNCSTORE yet!");
2176 Tmp3 = LegalizeOp(ST->getValue());
2177
2178 // The only promote case we handle is TRUNCSTORE:i1 X into
2179 // -> TRUNCSTORE:i8 (and X, 1)
2180 if (ST->getStoredVT() == MVT::i1 &&
2181 TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) {
2182 // Promote the bool to a mask then store.
2183 Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3,
2184 DAG.getConstant(1, Tmp3.getValueType()));
2185 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002186 SVOffset, MVT::i8,
2187 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002188 } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2189 Tmp2 != ST->getBasePtr()) {
2190 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2191 ST->getOffset());
2192 }
2193
2194 MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT();
2195 switch (TLI.getStoreXAction(StVT)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002196 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002197 case TargetLowering::Legal:
2198 // If this is an unaligned store and the target doesn't support it,
2199 // expand it.
2200 if (!TLI.allowsUnalignedMemoryAccesses()) {
2201 unsigned ABIAlignment = TLI.getTargetData()->
2202 getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT()));
2203 if (ST->getAlignment() < ABIAlignment)
2204 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2205 TLI);
2206 }
2207 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002208 case TargetLowering::Custom:
2209 Tmp1 = TLI.LowerOperation(Result, DAG);
2210 if (Tmp1.Val) Result = Tmp1;
2211 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002212 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002213 }
2214 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002215 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002216 case ISD::PCMARKER:
2217 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002218 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002219 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002220 case ISD::STACKSAVE:
2221 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002222 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2223 Tmp1 = Result.getValue(0);
2224 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002225
Chris Lattnerb3266452006-01-13 02:50:02 +00002226 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2227 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002228 case TargetLowering::Legal: break;
2229 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002230 Tmp3 = TLI.LowerOperation(Result, DAG);
2231 if (Tmp3.Val) {
2232 Tmp1 = LegalizeOp(Tmp3);
2233 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002234 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002235 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002236 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002237 // Expand to CopyFromReg if the target set
2238 // StackPointerRegisterToSaveRestore.
2239 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002240 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002241 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002242 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002243 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002244 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2245 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002246 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002247 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002248 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002249
2250 // Since stacksave produce two values, make sure to remember that we
2251 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002252 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2253 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2254 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002255
Chris Lattnerb3266452006-01-13 02:50:02 +00002256 case ISD::STACKRESTORE:
2257 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2258 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002259 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002260
2261 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2262 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002263 case TargetLowering::Legal: break;
2264 case TargetLowering::Custom:
2265 Tmp1 = TLI.LowerOperation(Result, DAG);
2266 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002267 break;
2268 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002269 // Expand to CopyToReg if the target set
2270 // StackPointerRegisterToSaveRestore.
2271 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2272 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2273 } else {
2274 Result = Tmp1;
2275 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002276 break;
2277 }
2278 break;
2279
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002280 case ISD::READCYCLECOUNTER:
2281 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002282 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002283 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2284 Node->getValueType(0))) {
2285 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002286 case TargetLowering::Legal:
2287 Tmp1 = Result.getValue(0);
2288 Tmp2 = Result.getValue(1);
2289 break;
Evan Cheng69739932006-11-29 08:26:18 +00002290 case TargetLowering::Custom:
2291 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002292 Tmp1 = LegalizeOp(Result.getValue(0));
2293 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002294 break;
2295 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002296
2297 // Since rdcc produce two values, make sure to remember that we legalized
2298 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002299 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2300 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002301 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002302
Chris Lattner39c67442005-01-14 22:08:15 +00002303 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002304 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2305 case Expand: assert(0 && "It's impossible to expand bools");
2306 case Legal:
2307 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2308 break;
2309 case Promote:
2310 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002311 // Make sure the condition is either zero or one.
Dan Gohman309d3d52007-06-22 14:59:07 +00002312 if (!DAG.MaskedValueIsZero(Tmp1,
Chris Lattner3abb6362006-11-28 01:03:30 +00002313 MVT::getIntVTBitMask(Tmp1.getValueType())^1))
2314 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002315 break;
2316 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002317 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002318 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002319
Chris Lattnerd02b0542006-01-28 10:58:55 +00002320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002321
Nate Begeman987121a2005-08-23 04:29:48 +00002322 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002323 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002324 case TargetLowering::Legal: break;
2325 case TargetLowering::Custom: {
2326 Tmp1 = TLI.LowerOperation(Result, DAG);
2327 if (Tmp1.Val) Result = Tmp1;
2328 break;
2329 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002330 case TargetLowering::Expand:
2331 if (Tmp1.getOpcode() == ISD::SETCC) {
2332 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2333 Tmp2, Tmp3,
2334 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2335 } else {
2336 Result = DAG.getSelectCC(Tmp1,
2337 DAG.getConstant(0, Tmp1.getValueType()),
2338 Tmp2, Tmp3, ISD::SETNE);
2339 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002340 break;
2341 case TargetLowering::Promote: {
2342 MVT::ValueType NVT =
2343 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2344 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002345 if (MVT::isVector(Tmp2.getValueType())) {
2346 ExtOp = ISD::BIT_CONVERT;
2347 TruncOp = ISD::BIT_CONVERT;
2348 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002349 ExtOp = ISD::ANY_EXTEND;
2350 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002351 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002352 ExtOp = ISD::FP_EXTEND;
2353 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002354 }
2355 // Promote each of the values to the new type.
2356 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2357 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2358 // Perform the larger operation, then round down.
2359 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
2360 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2361 break;
2362 }
2363 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002364 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002365 case ISD::SELECT_CC: {
2366 Tmp1 = Node->getOperand(0); // LHS
2367 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002368 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2369 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002370 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002371
Nate Begeman7e7f4392006-02-01 07:19:44 +00002372 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2373
2374 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2375 // the LHS is a legal SETCC itself. In this case, we need to compare
2376 // the result against zero to select between true and false values.
2377 if (Tmp2.Val == 0) {
2378 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2379 CC = DAG.getCondCode(ISD::SETNE);
2380 }
2381 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2382
2383 // Everything is legal, see if we should expand this op or something.
2384 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2385 default: assert(0 && "This action is not supported yet!");
2386 case TargetLowering::Legal: break;
2387 case TargetLowering::Custom:
2388 Tmp1 = TLI.LowerOperation(Result, DAG);
2389 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002390 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002391 }
2392 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002393 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002394 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002395 Tmp1 = Node->getOperand(0);
2396 Tmp2 = Node->getOperand(1);
2397 Tmp3 = Node->getOperand(2);
2398 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2399
2400 // If we had to Expand the SetCC operands into a SELECT node, then it may
2401 // not always be possible to return a true LHS & RHS. In this case, just
2402 // return the value we legalized, returned in the LHS
2403 if (Tmp2.Val == 0) {
2404 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002405 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002406 }
Nate Begeman987121a2005-08-23 04:29:48 +00002407
Chris Lattnerf263a232006-01-30 22:43:50 +00002408 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002409 default: assert(0 && "Cannot handle this action for SETCC yet!");
2410 case TargetLowering::Custom:
2411 isCustom = true;
2412 // FALLTHROUGH.
2413 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002414 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002415 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002416 Tmp4 = TLI.LowerOperation(Result, DAG);
2417 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002418 }
Nate Begeman987121a2005-08-23 04:29:48 +00002419 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002420 case TargetLowering::Promote: {
2421 // First step, figure out the appropriate operation to use.
2422 // Allow SETCC to not be supported for all legal data types
2423 // Mostly this targets FP
2424 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002425 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002426
2427 // Scan for the appropriate larger type to use.
2428 while (1) {
2429 NewInTy = (MVT::ValueType)(NewInTy+1);
2430
2431 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2432 "Fell off of the edge of the integer world");
2433 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2434 "Fell off of the edge of the floating point world");
2435
2436 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002437 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002438 break;
2439 }
2440 if (MVT::isInteger(NewInTy))
2441 assert(0 && "Cannot promote Legal Integer SETCC yet");
2442 else {
2443 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2444 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2445 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002446 Tmp1 = LegalizeOp(Tmp1);
2447 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002448 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002449 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002450 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002451 }
Nate Begeman987121a2005-08-23 04:29:48 +00002452 case TargetLowering::Expand:
2453 // Expand a setcc node into a select_cc of the same condition, lhs, and
2454 // rhs that selects between const 1 (true) and const 0 (false).
2455 MVT::ValueType VT = Node->getValueType(0);
2456 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2457 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002458 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002459 break;
2460 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002461 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002462 case ISD::MEMSET:
2463 case ISD::MEMCPY:
2464 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002465 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002466 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2467
2468 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2469 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2470 case Expand: assert(0 && "Cannot expand a byte!");
2471 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002472 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002473 break;
2474 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002475 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002476 break;
2477 }
2478 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002479 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002480 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002481
2482 SDOperand Tmp4;
2483 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002484 case Expand: {
2485 // Length is too big, just take the lo-part of the length.
2486 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002487 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002488 break;
2489 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002490 case Legal:
2491 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002492 break;
2493 case Promote:
2494 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002495 break;
2496 }
2497
2498 SDOperand Tmp5;
2499 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2500 case Expand: assert(0 && "Cannot expand this yet!");
2501 case Legal:
2502 Tmp5 = LegalizeOp(Node->getOperand(4));
2503 break;
2504 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002505 Tmp5 = PromoteOp(Node->getOperand(4));
2506 break;
2507 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002508
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002509 SDOperand Tmp6;
2510 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2511 case Expand: assert(0 && "Cannot expand this yet!");
2512 case Legal:
2513 Tmp6 = LegalizeOp(Node->getOperand(5));
2514 break;
2515 case Promote:
2516 Tmp6 = PromoteOp(Node->getOperand(5));
2517 break;
2518 }
2519
Chris Lattner3c0dd462005-01-16 07:29:19 +00002520 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2521 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002522 case TargetLowering::Custom:
2523 isCustom = true;
2524 // FALLTHROUGH
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002525 case TargetLowering::Legal: {
2526 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2527 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002528 if (isCustom) {
2529 Tmp1 = TLI.LowerOperation(Result, DAG);
2530 if (Tmp1.Val) Result = Tmp1;
2531 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002532 break;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002533 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002534 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002535 // Otherwise, the target does not support this operation. Lower the
2536 // operation to an explicit libcall as appropriate.
2537 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002538 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002539 TargetLowering::ArgListTy Args;
2540 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002541
Reid Spencer6dced922005-01-12 14:53:45 +00002542 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002543 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002544 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00002545 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002546 // Extend the (previously legalized) ubyte argument to be an int value
2547 // for the call.
2548 if (Tmp3.getValueType() > MVT::i32)
2549 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2550 else
2551 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002552 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencere63b6512006-12-31 05:55:36 +00002553 Args.push_back(Entry);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002554 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002555 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002556
2557 FnName = "memset";
2558 } else if (Node->getOpcode() == ISD::MEMCPY ||
2559 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002560 Entry.Ty = IntPtrTy;
Reid Spencer791864c2007-01-03 04:22:32 +00002561 Entry.Node = Tmp2; Args.push_back(Entry);
2562 Entry.Node = Tmp3; Args.push_back(Entry);
2563 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002564 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2565 } else {
2566 assert(0 && "Unknown op!");
2567 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002568
Chris Lattner85d70c62005-01-11 05:57:22 +00002569 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00002570 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002571 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002572 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002573 break;
2574 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002575 }
2576 break;
2577 }
Chris Lattner5385db52005-05-09 20:23:03 +00002578
Chris Lattner4157c412005-04-02 04:00:59 +00002579 case ISD::SHL_PARTS:
2580 case ISD::SRA_PARTS:
2581 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002582 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002583 bool Changed = false;
2584 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2585 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2586 Changed |= Ops.back() != Node->getOperand(i);
2587 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002588 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002589 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002590
Evan Cheng870e4f82006-01-09 18:31:59 +00002591 switch (TLI.getOperationAction(Node->getOpcode(),
2592 Node->getValueType(0))) {
2593 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002594 case TargetLowering::Legal: break;
2595 case TargetLowering::Custom:
2596 Tmp1 = TLI.LowerOperation(Result, DAG);
2597 if (Tmp1.Val) {
2598 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002599 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002600 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002601 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2602 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002603 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002604 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002605 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002606 return RetVal;
2607 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002608 break;
2609 }
2610
Chris Lattner13fe99c2005-04-02 05:00:07 +00002611 // Since these produce multiple values, make sure to remember that we
2612 // legalized all of them.
2613 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2614 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2615 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002616 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002617
2618 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00002619 case ISD::ADD:
2620 case ISD::SUB:
2621 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00002622 case ISD::MULHS:
2623 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00002624 case ISD::UDIV:
2625 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00002626 case ISD::AND:
2627 case ISD::OR:
2628 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00002629 case ISD::SHL:
2630 case ISD::SRL:
2631 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002632 case ISD::FADD:
2633 case ISD::FSUB:
2634 case ISD::FMUL:
2635 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00002636 case ISD::FPOW:
Chris Lattnerdc750592005-01-07 07:47:09 +00002637 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00002638 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2639 case Expand: assert(0 && "Not possible");
2640 case Legal:
2641 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2642 break;
2643 case Promote:
2644 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2645 break;
2646 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002647
2648 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002649
Andrew Lenharth72594262005-12-24 23:42:32 +00002650 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00002651 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002652 case TargetLowering::Legal: break;
2653 case TargetLowering::Custom:
2654 Tmp1 = TLI.LowerOperation(Result, DAG);
2655 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00002656 break;
Chris Lattner87f08092006-04-02 03:57:31 +00002657 case TargetLowering::Expand: {
Dan Gohmana1603612007-10-08 18:33:35 +00002658 MVT::ValueType VT = Op.getValueType();
2659
2660 // See if multiply or divide can be lowered using two-result operations.
2661 SDVTList VTs = DAG.getVTList(VT, VT);
2662 if (Node->getOpcode() == ISD::MUL) {
2663 // We just need the low half of the multiply; try both the signed
2664 // and unsigned forms. If the target supports both SMUL_LOHI and
2665 // UMUL_LOHI, form a preference by checking which forms of plain
2666 // MULH it supports.
2667 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
2668 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
2669 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
2670 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
2671 unsigned OpToUse = 0;
2672 if (HasSMUL_LOHI && !HasMULHS) {
2673 OpToUse = ISD::SMUL_LOHI;
2674 } else if (HasUMUL_LOHI && !HasMULHU) {
2675 OpToUse = ISD::UMUL_LOHI;
2676 } else if (HasSMUL_LOHI) {
2677 OpToUse = ISD::SMUL_LOHI;
2678 } else if (HasUMUL_LOHI) {
2679 OpToUse = ISD::UMUL_LOHI;
2680 }
2681 if (OpToUse) {
2682 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
2683 break;
2684 }
2685 }
2686 if (Node->getOpcode() == ISD::MULHS &&
2687 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
2688 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2689 break;
2690 }
2691 if (Node->getOpcode() == ISD::MULHU &&
2692 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
2693 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
2694 break;
2695 }
2696 if (Node->getOpcode() == ISD::SDIV &&
2697 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2698 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2699 break;
2700 }
2701 if (Node->getOpcode() == ISD::UDIV &&
2702 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2703 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
2704 break;
2705 }
2706
Dan Gohman2a7de412007-10-11 23:57:53 +00002707 // Check to see if we have a libcall for this operator.
2708 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2709 bool isSigned = false;
2710 switch (Node->getOpcode()) {
2711 case ISD::UDIV:
2712 case ISD::SDIV:
2713 if (VT == MVT::i32) {
2714 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng31cbddf2007-01-12 02:11:51 +00002715 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman2a7de412007-10-11 23:57:53 +00002716 isSigned = Node->getOpcode() == ISD::SDIV;
2717 }
2718 break;
2719 case ISD::FPOW:
2720 LC = VT == MVT::f32 ? RTLIB::POW_F32 :
2721 VT == MVT::f64 ? RTLIB::POW_F64 :
2722 VT == MVT::f80 ? RTLIB::POW_F80 :
2723 VT == MVT::ppcf128 ? RTLIB::POW_PPCF128 :
2724 RTLIB::UNKNOWN_LIBCALL;
2725 break;
2726 default: break;
2727 }
2728 if (LC != RTLIB::UNKNOWN_LIBCALL) {
2729 SDOperand Dummy;
2730 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002731 break;
2732 }
2733
Chris Lattner87f08092006-04-02 03:57:31 +00002734 assert(MVT::isVector(Node->getValueType(0)) &&
2735 "Cannot expand this binary operator!");
2736 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman2a7de412007-10-11 23:57:53 +00002737 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattner87f08092006-04-02 03:57:31 +00002738 break;
2739 }
Evan Cheng119266e2006-04-12 21:20:24 +00002740 case TargetLowering::Promote: {
2741 switch (Node->getOpcode()) {
2742 default: assert(0 && "Do not know how to promote this BinOp!");
2743 case ISD::AND:
2744 case ISD::OR:
2745 case ISD::XOR: {
2746 MVT::ValueType OVT = Node->getValueType(0);
2747 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
2748 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
2749 // Bit convert each of the values to the new type.
2750 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
2751 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
2752 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2753 // Bit convert the result back the original type.
2754 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
2755 break;
2756 }
2757 }
2758 }
Andrew Lenharth72594262005-12-24 23:42:32 +00002759 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002760 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002761
Dan Gohman12334ac2007-10-05 14:17:22 +00002762 case ISD::SMUL_LOHI:
2763 case ISD::UMUL_LOHI:
2764 case ISD::SDIVREM:
2765 case ISD::UDIVREM:
2766 // These nodes will only be produced by target-specific lowering, so
2767 // they shouldn't be here if they aren't legal.
Duncan Sands052c8432007-10-16 09:07:20 +00002768 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman12334ac2007-10-05 14:17:22 +00002769 "This must be legal!");
Dan Gohmana1603612007-10-08 18:33:35 +00002770
2771 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2772 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
2773 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman12334ac2007-10-05 14:17:22 +00002774 break;
2775
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002776 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
2777 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2778 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2779 case Expand: assert(0 && "Not possible");
2780 case Legal:
2781 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
2782 break;
2783 case Promote:
2784 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
2785 break;
2786 }
2787
2788 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2789
2790 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
2791 default: assert(0 && "Operation not supported");
2792 case TargetLowering::Custom:
2793 Tmp1 = TLI.LowerOperation(Result, DAG);
2794 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00002795 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002796 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00002797 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00002798 // If this target supports fabs/fneg natively and select is cheap,
2799 // do this efficiently.
2800 if (!TLI.isSelectExpensive() &&
2801 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
2802 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00002803 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00002804 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00002805 // Get the sign bit of the RHS.
2806 MVT::ValueType IVT =
2807 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
2808 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
2809 SignBit = DAG.getSetCC(TLI.getSetCCResultTy(),
2810 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
2811 // Get the absolute value of the result.
2812 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
2813 // Select between the nabs and abs value based on the sign bit of
2814 // the input.
2815 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
2816 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
2817 AbsVal),
2818 AbsVal);
2819 Result = LegalizeOp(Result);
2820 break;
2821 }
2822
2823 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00002824 MVT::ValueType NVT =
2825 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
2826 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
2827 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
2828 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002829 break;
2830 }
Evan Cheng003feb02007-01-04 21:56:39 +00002831 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002832 break;
2833
Nate Begeman5965bd12006-02-17 05:43:56 +00002834 case ISD::ADDC:
2835 case ISD::SUBC:
2836 Tmp1 = LegalizeOp(Node->getOperand(0));
2837 Tmp2 = LegalizeOp(Node->getOperand(1));
2838 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2839 // Since this produces two values, make sure to remember that we legalized
2840 // both of them.
2841 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2842 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2843 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00002844
Nate Begeman5965bd12006-02-17 05:43:56 +00002845 case ISD::ADDE:
2846 case ISD::SUBE:
2847 Tmp1 = LegalizeOp(Node->getOperand(0));
2848 Tmp2 = LegalizeOp(Node->getOperand(1));
2849 Tmp3 = LegalizeOp(Node->getOperand(2));
2850 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
2851 // Since this produces two values, make sure to remember that we legalized
2852 // both of them.
2853 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
2854 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
2855 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00002856
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002857 case ISD::BUILD_PAIR: {
2858 MVT::ValueType PairTy = Node->getValueType(0);
2859 // TODO: handle the case where the Lo and Hi operands are not of legal type
2860 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
2861 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
2862 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002863 case TargetLowering::Promote:
2864 case TargetLowering::Custom:
2865 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002866 case TargetLowering::Legal:
2867 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
2868 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
2869 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002870 case TargetLowering::Expand:
2871 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
2872 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
2873 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
2874 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
2875 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002876 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00002877 break;
2878 }
2879 break;
2880 }
2881
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002882 case ISD::UREM:
2883 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002884 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002885 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
2886 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002887
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002888 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002889 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
2890 case TargetLowering::Custom:
2891 isCustom = true;
2892 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002893 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002894 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002895 if (isCustom) {
2896 Tmp1 = TLI.LowerOperation(Result, DAG);
2897 if (Tmp1.Val) Result = Tmp1;
2898 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002899 break;
Dan Gohmana1603612007-10-08 18:33:35 +00002900 case TargetLowering::Expand: {
Evan Cheng1fc7c362006-09-18 23:28:33 +00002901 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00002902 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohmana1603612007-10-08 18:33:35 +00002903 MVT::ValueType VT = Node->getValueType(0);
2904
2905 // See if remainder can be lowered using two-result operations.
2906 SDVTList VTs = DAG.getVTList(VT, VT);
2907 if (Node->getOpcode() == ISD::SREM &&
2908 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
2909 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2910 break;
2911 }
2912 if (Node->getOpcode() == ISD::UREM &&
2913 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
2914 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
2915 break;
2916 }
2917
2918 if (MVT::isInteger(VT)) {
2919 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002920 TargetLowering::Legal) {
2921 // X % Y -> X-X/Y*Y
Evan Cheng1fc7c362006-09-18 23:28:33 +00002922 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002923 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
2924 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
2925 } else {
Dan Gohmana1603612007-10-08 18:33:35 +00002926 assert(VT == MVT::i32 &&
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002927 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00002928 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
2929 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002930 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002931 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00002932 }
Chris Lattner81914422005-08-03 20:31:37 +00002933 } else {
2934 // Floating point mod -> fmod libcall.
Dan Gohmana1603612007-10-08 18:33:35 +00002935 RTLIB::Libcall LC = VT == MVT::f32
Evan Cheng31cbddf2007-01-12 02:11:51 +00002936 ? RTLIB::REM_F32 : RTLIB::REM_F64;
Chris Lattner81914422005-08-03 20:31:37 +00002937 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00002938 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
2939 false/*sign irrelevant*/, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002940 }
2941 break;
2942 }
Dan Gohmana1603612007-10-08 18:33:35 +00002943 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00002944 break;
Nate Begemane74795c2006-01-25 18:21:52 +00002945 case ISD::VAARG: {
2946 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2947 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
2948
Chris Lattner364b89a2006-01-28 07:42:08 +00002949 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00002950 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2951 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002952 case TargetLowering::Custom:
2953 isCustom = true;
2954 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00002955 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002956 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2957 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002958 Tmp1 = Result.getValue(1);
2959
2960 if (isCustom) {
2961 Tmp2 = TLI.LowerOperation(Result, DAG);
2962 if (Tmp2.Val) {
2963 Result = LegalizeOp(Tmp2);
2964 Tmp1 = LegalizeOp(Tmp2.getValue(1));
2965 }
2966 }
Nate Begemane74795c2006-01-25 18:21:52 +00002967 break;
2968 case TargetLowering::Expand: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002969 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begemane74795c2006-01-25 18:21:52 +00002970 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00002971 SV->getValue(), SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002972 // Increment the pointer, VAList, to the next vaarg
2973 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2974 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2975 TLI.getPointerTy()));
2976 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00002977 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
2978 SV->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00002979 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00002980 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002981 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00002982 Result = LegalizeOp(Result);
2983 break;
2984 }
2985 }
2986 // Since VAARG produces two values, make sure to remember that we
2987 // legalized both of them.
2988 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002989 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
2990 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00002991 }
2992
2993 case ISD::VACOPY:
2994 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2995 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
2996 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
2997
2998 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
2999 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003000 case TargetLowering::Custom:
3001 isCustom = true;
3002 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003003 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003004 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3005 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003006 if (isCustom) {
3007 Tmp1 = TLI.LowerOperation(Result, DAG);
3008 if (Tmp1.Val) Result = Tmp1;
3009 }
Nate Begemane74795c2006-01-25 18:21:52 +00003010 break;
3011 case TargetLowering::Expand:
3012 // This defaults to loading a pointer from the input and storing it to the
3013 // output, returning the chain.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003014 SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3));
Evan Chengab51cf22006-10-13 21:14:26 +00003015 SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4));
Evan Chenge71fe34d2006-10-09 20:57:25 +00003016 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(),
3017 SVD->getOffset());
Evan Chengab51cf22006-10-13 21:14:26 +00003018 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(),
3019 SVS->getOffset());
Nate Begemane74795c2006-01-25 18:21:52 +00003020 break;
3021 }
3022 break;
3023
3024 case ISD::VAEND:
3025 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3026 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3027
3028 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3029 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003030 case TargetLowering::Custom:
3031 isCustom = true;
3032 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003033 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003034 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003035 if (isCustom) {
3036 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3037 if (Tmp1.Val) Result = Tmp1;
3038 }
Nate Begemane74795c2006-01-25 18:21:52 +00003039 break;
3040 case TargetLowering::Expand:
3041 Result = Tmp1; // Default to a no-op, return the chain
3042 break;
3043 }
3044 break;
3045
3046 case ISD::VASTART:
3047 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3048 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3049
Chris Lattnerd02b0542006-01-28 10:58:55 +00003050 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3051
Nate Begemane74795c2006-01-25 18:21:52 +00003052 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3053 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003054 case TargetLowering::Legal: break;
3055 case TargetLowering::Custom:
3056 Tmp1 = TLI.LowerOperation(Result, DAG);
3057 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00003058 break;
3059 }
3060 break;
3061
Nate Begeman1b8121b2006-01-11 21:21:00 +00003062 case ISD::ROTL:
3063 case ISD::ROTR:
3064 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3065 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00003066 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00003067 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3068 default:
3069 assert(0 && "ROTL/ROTR legalize operation not supported");
3070 break;
3071 case TargetLowering::Legal:
3072 break;
3073 case TargetLowering::Custom:
3074 Tmp1 = TLI.LowerOperation(Result, DAG);
3075 if (Tmp1.Val) Result = Tmp1;
3076 break;
3077 case TargetLowering::Promote:
3078 assert(0 && "Do not know how to promote ROTL/ROTR");
3079 break;
3080 case TargetLowering::Expand:
3081 assert(0 && "Do not know how to expand ROTL/ROTR");
3082 break;
3083 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00003084 break;
3085
Nate Begeman2fba8a32006-01-14 03:14:10 +00003086 case ISD::BSWAP:
3087 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3088 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003089 case TargetLowering::Custom:
3090 assert(0 && "Cannot custom legalize this yet!");
3091 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003092 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003093 break;
3094 case TargetLowering::Promote: {
3095 MVT::ValueType OVT = Tmp1.getValueType();
3096 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohman1796f1f2007-05-18 17:52:13 +00003097 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00003098
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003099 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3100 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3101 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3102 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3103 break;
3104 }
3105 case TargetLowering::Expand:
3106 Result = ExpandBSWAP(Tmp1);
3107 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003108 }
3109 break;
3110
Andrew Lenharth5e177822005-05-03 17:19:30 +00003111 case ISD::CTPOP:
3112 case ISD::CTTZ:
3113 case ISD::CTLZ:
3114 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3115 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00003116 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00003117 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003118 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00003119 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00003120 TargetLowering::Custom) {
3121 Tmp1 = TLI.LowerOperation(Result, DAG);
3122 if (Tmp1.Val) {
3123 Result = Tmp1;
3124 }
Scott Michel34e2d222007-07-30 21:00:31 +00003125 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003126 break;
3127 case TargetLowering::Promote: {
3128 MVT::ValueType OVT = Tmp1.getValueType();
3129 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00003130
3131 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00003132 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3133 // Perform the larger operation, then subtract if needed.
3134 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003135 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00003136 case ISD::CTPOP:
3137 Result = Tmp1;
3138 break;
3139 case ISD::CTTZ:
3140 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00003141 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003142 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00003143 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003144 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel34e2d222007-07-30 21:00:31 +00003145 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003146 break;
3147 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003148 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003149 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003150 DAG.getConstant(MVT::getSizeInBits(NVT) -
3151 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00003152 break;
3153 }
3154 break;
3155 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003156 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003157 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003158 break;
3159 }
3160 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003161
Chris Lattner13fe99c2005-04-02 05:00:07 +00003162 // Unary operators
3163 case ISD::FABS:
3164 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00003165 case ISD::FSQRT:
3166 case ISD::FSIN:
3167 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003168 Tmp1 = LegalizeOp(Node->getOperand(0));
3169 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003170 case TargetLowering::Promote:
3171 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00003172 isCustom = true;
3173 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00003174 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003175 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00003176 if (isCustom) {
3177 Tmp1 = TLI.LowerOperation(Result, DAG);
3178 if (Tmp1.Val) Result = Tmp1;
3179 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003180 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00003181 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003182 switch (Node->getOpcode()) {
3183 default: assert(0 && "Unreachable!");
3184 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003185 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3186 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003187 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00003188 break;
Chris Lattner80026402005-04-30 04:43:14 +00003189 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003190 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3191 MVT::ValueType VT = Node->getValueType(0);
3192 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003193 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003194 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3195 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00003196 break;
3197 }
3198 case ISD::FSQRT:
3199 case ISD::FSIN:
3200 case ISD::FCOS: {
3201 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman2a7de412007-10-11 23:57:53 +00003202
3203 // Expand unsupported unary vector operators by unrolling them.
3204 if (MVT::isVector(VT)) {
3205 Result = LegalizeOp(UnrollVectorOp(Op));
3206 break;
3207 }
3208
Evan Cheng31cbddf2007-01-12 02:11:51 +00003209 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00003210 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00003211 case ISD::FSQRT:
Dale Johannesen25a00a62007-09-28 01:08:20 +00003212 LC = VT == MVT::f32 ? RTLIB::SQRT_F32 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00003213 VT == MVT::f64 ? RTLIB::SQRT_F64 :
3214 VT == MVT::f80 ? RTLIB::SQRT_F80 :
3215 VT == MVT::ppcf128 ? RTLIB::SQRT_PPCF128 :
3216 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003217 break;
3218 case ISD::FSIN:
3219 LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
3220 break;
3221 case ISD::FCOS:
3222 LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64;
3223 break;
Chris Lattner80026402005-04-30 04:43:14 +00003224 default: assert(0 && "Unreachable!");
3225 }
Nate Begeman77558da2005-08-04 21:43:28 +00003226 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003227 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3228 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00003229 break;
3230 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003231 }
3232 break;
3233 }
3234 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003235 case ISD::FPOWI: {
Dan Gohman2a7de412007-10-11 23:57:53 +00003236 MVT::ValueType VT = Node->getValueType(0);
3237
3238 // Expand unsupported unary vector operators by unrolling them.
3239 if (MVT::isVector(VT)) {
3240 Result = LegalizeOp(UnrollVectorOp(Op));
3241 break;
3242 }
3243
3244 // We always lower FPOWI into a libcall. No target support for it yet.
Dale Johannesen25a00a62007-09-28 01:08:20 +00003245 RTLIB::Libcall LC =
Dan Gohman2a7de412007-10-11 23:57:53 +00003246 VT == MVT::f32 ? RTLIB::POWI_F32 :
3247 VT == MVT::f64 ? RTLIB::POWI_F64 :
3248 VT == MVT::f80 ? RTLIB::POWI_F80 :
3249 VT == MVT::ppcf128 ? RTLIB::POWI_PPCF128 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00003250 RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003251 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003252 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3253 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003254 break;
3255 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003256 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003257 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00003258 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Dan Gohmana8665142007-06-25 16:23:39 +00003259 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3260 // The input has to be a vector type, we have to either scalarize it, pack
3261 // it, or convert it based on whether the input vector type is legal.
3262 SDNode *InVal = Node->getOperand(0).Val;
3263 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0));
3264 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0));
3265
3266 // Figure out if there is a simple type corresponding to this Vector
3267 // type. If so, convert to the vector type.
3268 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3269 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003270 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003271 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3272 LegalizeOp(Node->getOperand(0)));
3273 break;
3274 } else if (NumElems == 1) {
3275 // Turn this into a bit convert of the scalar input.
3276 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3277 ScalarizeVectorOp(Node->getOperand(0)));
3278 break;
3279 } else {
3280 // FIXME: UNIMP! Store then reload
3281 assert(0 && "Cast from unsupported vector type not implemented yet!");
3282 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003283 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003284 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3285 Node->getOperand(0).getValueType())) {
3286 default: assert(0 && "Unknown operation action!");
3287 case TargetLowering::Expand:
3288 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3289 break;
3290 case TargetLowering::Legal:
3291 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003292 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003293 break;
3294 }
3295 }
3296 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00003297
Chris Lattner13fe99c2005-04-02 05:00:07 +00003298 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00003299 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003300 case ISD::UINT_TO_FP: {
3301 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00003302 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3303 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00003304 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003305 Node->getOperand(0).getValueType())) {
3306 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003307 case TargetLowering::Custom:
3308 isCustom = true;
3309 // FALLTHROUGH
3310 case TargetLowering::Legal:
3311 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003312 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003313 if (isCustom) {
3314 Tmp1 = TLI.LowerOperation(Result, DAG);
3315 if (Tmp1.Val) Result = Tmp1;
3316 }
3317 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003318 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00003319 Result = ExpandLegalINT_TO_FP(isSigned,
3320 LegalizeOp(Node->getOperand(0)),
3321 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003322 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003323 case TargetLowering::Promote:
3324 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3325 Node->getValueType(0),
3326 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003327 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00003328 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003329 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00003330 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003331 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3332 Node->getValueType(0), Node->getOperand(0));
3333 break;
3334 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003335 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003336 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003337 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3338 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003339 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003340 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3341 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003342 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003343 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3344 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003345 break;
3346 }
3347 break;
3348 }
3349 case ISD::TRUNCATE:
3350 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3351 case Legal:
3352 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003353 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003354 break;
3355 case Expand:
3356 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3357
3358 // Since the result is legal, we should just be able to truncate the low
3359 // part of the source.
3360 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3361 break;
3362 case Promote:
3363 Result = PromoteOp(Node->getOperand(0));
3364 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3365 break;
3366 }
3367 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003368
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003369 case ISD::FP_TO_SINT:
3370 case ISD::FP_TO_UINT:
3371 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3372 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00003373 Tmp1 = LegalizeOp(Node->getOperand(0));
3374
Chris Lattner44fe26f2005-07-29 00:11:56 +00003375 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3376 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003377 case TargetLowering::Custom:
3378 isCustom = true;
3379 // FALLTHROUGH
3380 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003381 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003382 if (isCustom) {
3383 Tmp1 = TLI.LowerOperation(Result, DAG);
3384 if (Tmp1.Val) Result = Tmp1;
3385 }
3386 break;
3387 case TargetLowering::Promote:
3388 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3389 Node->getOpcode() == ISD::FP_TO_SINT);
3390 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00003391 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00003392 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3393 SDOperand True, False;
3394 MVT::ValueType VT = Node->getOperand(0).getValueType();
3395 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesenb59d25f2007-09-19 17:53:26 +00003396 unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003397 const uint64_t zero[] = {0, 0};
3398 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
3399 uint64_t x = 1ULL << ShiftAmt;
Neil Booth5f009732007-10-07 11:45:55 +00003400 (void)apf.convertFromZeroExtendedInteger
3401 (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003402 Tmp2 = DAG.getConstantFP(apf, VT);
Nate Begeman36853ee2005-08-14 01:20:53 +00003403 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
3404 Node->getOperand(0), Tmp2, ISD::SETLT);
3405 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3406 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00003407 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00003408 Tmp2));
3409 False = DAG.getNode(ISD::XOR, NVT, False,
3410 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003411 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3412 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00003413 } else {
3414 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3415 }
3416 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003417 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003418 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003419 case Expand: {
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003420 MVT::ValueType VT = Op.getValueType();
Dale Johannesen666323e2007-10-10 01:01:31 +00003421 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesen6472eb62007-10-11 23:32:15 +00003422 // Convert ppcf128 to i32
Dale Johannesen666323e2007-10-10 01:01:31 +00003423 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Dale Johannesen6472eb62007-10-11 23:32:15 +00003424 if (Node->getOpcode()==ISD::FP_TO_SINT)
3425 Result = DAG.getNode(ISD::FP_TO_SINT, VT,
Dale Johannesen666323e2007-10-10 01:01:31 +00003426 DAG.getNode(ISD::FP_ROUND, MVT::f64,
3427 (DAG.getNode(ISD::FP_ROUND_INREG,
3428 MVT::ppcf128, Node->getOperand(0),
3429 DAG.getValueType(MVT::f64)))));
Dale Johannesen6472eb62007-10-11 23:32:15 +00003430 else {
3431 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3432 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3433 Tmp2 = DAG.getConstantFP(apf, OVT);
3434 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3435 // FIXME: generated code sucks.
3436 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3437 DAG.getNode(ISD::ADD, MVT::i32,
3438 DAG.getNode(ISD::FP_TO_SINT, VT,
3439 DAG.getNode(ISD::FSUB, OVT,
3440 Node->getOperand(0), Tmp2)),
3441 DAG.getConstant(0x80000000, MVT::i32)),
3442 DAG.getNode(ISD::FP_TO_SINT, VT,
3443 Node->getOperand(0)),
3444 DAG.getCondCode(ISD::SETGE));
3445 }
Dale Johannesen666323e2007-10-10 01:01:31 +00003446 break;
3447 }
Dale Johannesen6472eb62007-10-11 23:32:15 +00003448 // Convert f32 / f64 to i32 / i64.
Evan Cheng31cbddf2007-01-12 02:11:51 +00003449 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003450 switch (Node->getOpcode()) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003451 case ISD::FP_TO_SINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003452 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003453 LC = (VT == MVT::i32)
3454 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003455 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003456 LC = (VT == MVT::i32)
3457 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003458 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003459 assert(VT == MVT::i64);
Dale Johannesenc0154c02007-10-05 20:04:43 +00003460 LC = RTLIB::FPTOSINT_F80_I64;
3461 }
3462 else if (OVT == MVT::ppcf128) {
3463 assert(VT == MVT::i64);
3464 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003465 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003466 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003467 }
3468 case ISD::FP_TO_UINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003469 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003470 LC = (VT == MVT::i32)
3471 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003472 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003473 LC = (VT == MVT::i32)
3474 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003475 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003476 LC = (VT == MVT::i32)
Dale Johannesenc0154c02007-10-05 20:04:43 +00003477 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3478 }
3479 else if (OVT == MVT::ppcf128) {
3480 assert(VT == MVT::i64);
3481 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003482 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003483 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003484 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003485 default: assert(0 && "Unreachable!");
3486 }
3487 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003488 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3489 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003490 break;
3491 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003492 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003493 Tmp1 = PromoteOp(Node->getOperand(0));
3494 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3495 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003496 break;
3497 }
3498 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003499
Dale Johannesenc339e452007-08-09 17:27:48 +00003500 case ISD::FP_EXTEND:
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003501 case ISD::FP_ROUND: {
3502 MVT::ValueType newVT = Op.getValueType();
3503 MVT::ValueType oldVT = Op.getOperand(0).getValueType();
3504 if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) {
Dale Johannesenf864ac92007-10-06 01:24:11 +00003505 if (Node->getOpcode() == ISD::FP_ROUND && oldVT == MVT::ppcf128) {
3506 SDOperand Lo, Hi;
3507 ExpandOp(Node->getOperand(0), Lo, Hi);
3508 if (newVT == MVT::f64)
3509 Result = Hi;
3510 else
3511 Result = DAG.getNode(ISD::FP_ROUND, newVT, Hi);
3512 break;
Dale Johannesenc339e452007-08-09 17:27:48 +00003513 } else {
Dale Johannesenf864ac92007-10-06 01:24:11 +00003514 // The only other way we can lower this is to turn it into a STORE,
3515 // LOAD pair, targetting a temporary location (a stack slot).
3516
3517 // NOTE: there is a choice here between constantly creating new stack
3518 // slots and always reusing the same one. We currently always create
3519 // new ones, as reuse may inhibit scheduling.
3520 MVT::ValueType slotVT =
3521 (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT;
3522 const Type *Ty = MVT::getTypeForValueType(slotVT);
3523 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
3524 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
3525 MachineFunction &MF = DAG.getMachineFunction();
3526 int SSFI =
3527 MF.getFrameInfo()->CreateStackObject(TySize, Align);
3528 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3529 if (Node->getOpcode() == ISD::FP_EXTEND) {
3530 Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0),
3531 StackSlot, NULL, 0);
3532 Result = DAG.getExtLoad(ISD::EXTLOAD, newVT,
3533 Result, StackSlot, NULL, 0, oldVT);
3534 } else {
3535 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3536 StackSlot, NULL, 0, newVT);
Duncan Sands052c8432007-10-16 09:07:20 +00003537 Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0);
Dale Johannesenf864ac92007-10-06 01:24:11 +00003538 }
3539 break;
Dale Johannesenc339e452007-08-09 17:27:48 +00003540 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003541 }
Dale Johannesena2b3c172007-07-03 00:53:03 +00003542 }
3543 // FALL THROUGH
Chris Lattner7753f172005-09-02 00:18:10 +00003544 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003545 case ISD::ZERO_EXTEND:
3546 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003547 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003548 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003549 case Legal:
3550 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003551 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003552 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003553 case Promote:
3554 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003555 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003556 Tmp1 = PromoteOp(Node->getOperand(0));
3557 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003558 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003559 case ISD::ZERO_EXTEND:
3560 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003561 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003562 Result = DAG.getZeroExtendInReg(Result,
3563 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003564 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003565 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003566 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003567 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003568 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003569 Result,
3570 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003571 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003572 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003573 Result = PromoteOp(Node->getOperand(0));
3574 if (Result.getValueType() != Op.getValueType())
3575 // Dynamically dead while we have only 2 FP types.
3576 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
3577 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003578 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00003579 Result = PromoteOp(Node->getOperand(0));
3580 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
3581 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003582 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003583 }
3584 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003585 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003586 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003587 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003588 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003589
3590 // If this operation is not supported, convert it to a shl/shr or load/store
3591 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003592 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3593 default: assert(0 && "This action not supported for this op yet!");
3594 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003595 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003596 break;
3597 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003598 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003599 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003600 // NOTE: we could fall back on load/store here too for targets without
3601 // SAR. However, it is doubtful that any exist.
3602 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3603 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003604 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003605 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3606 Node->getOperand(0), ShiftCst);
3607 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3608 Result, ShiftCst);
3609 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003610 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003611 // EXTLOAD pair, targetting a temporary location (a stack slot).
3612
3613 // NOTE: there is a choice here between constantly creating new stack
3614 // slots and always reusing the same one. We currently always create
3615 // new ones, as reuse may inhibit scheduling.
3616 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
Chris Lattner1deacd62007-04-28 06:42:38 +00003617 uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty);
Chris Lattner945e4372007-02-14 05:52:17 +00003618 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
Chris Lattner99222f72005-01-15 07:15:18 +00003619 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00003620 int SSFI =
Chris Lattner1deacd62007-04-28 06:42:38 +00003621 MF.getFrameInfo()->CreateStackObject(TySize, Align);
Chris Lattner99222f72005-01-15 07:15:18 +00003622 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003623 Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0),
3624 StackSlot, NULL, 0, ExtraVT);
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003625 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003626 Result, StackSlot, NULL, 0, ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00003627 } else {
3628 assert(0 && "Unknown op");
3629 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00003630 break;
Chris Lattner99222f72005-01-15 07:15:18 +00003631 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003632 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003633 }
Duncan Sands644f9172007-07-27 12:58:54 +00003634 case ISD::TRAMPOLINE: {
3635 SDOperand Ops[6];
3636 for (unsigned i = 0; i != 6; ++i)
3637 Ops[i] = LegalizeOp(Node->getOperand(i));
3638 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
3639 // The only option for this node is to custom lower it.
3640 Result = TLI.LowerOperation(Result, DAG);
3641 assert(Result.Val && "Should always custom lower!");
Duncan Sands86e01192007-09-11 14:10:23 +00003642
3643 // Since trampoline produces two values, make sure to remember that we
3644 // legalized both of them.
3645 Tmp1 = LegalizeOp(Result.getValue(1));
3646 Result = LegalizeOp(Result);
3647 AddLegalizedOperand(SDOperand(Node, 0), Result);
3648 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3649 return Op.ResNo ? Tmp1 : Result;
Duncan Sands644f9172007-07-27 12:58:54 +00003650 }
Chris Lattner99222f72005-01-15 07:15:18 +00003651 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003652
Chris Lattner101ea662006-04-08 04:13:17 +00003653 assert(Result.getValueType() == Op.getValueType() &&
3654 "Bad legalization!");
3655
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003656 // Make sure that the generated code is itself legal.
3657 if (Result != Op)
3658 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003659
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003660 // Note that LegalizeOp may be reentered even from single-use nodes, which
3661 // means that we always must cache transformed nodes.
3662 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00003663 return Result;
3664}
3665
Chris Lattner4d978642005-01-15 22:16:26 +00003666/// PromoteOp - Given an operation that produces a value in an invalid type,
3667/// promote it to compute the value into a larger type. The produced value will
3668/// have the correct bits for the low portion of the register, but no guarantee
3669/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003670SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
3671 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003672 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003673 assert(getTypeAction(VT) == Promote &&
3674 "Caller should expand or legalize operands that are not promotable!");
3675 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
3676 "Cannot promote to smaller type!");
3677
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003678 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003679 SDOperand Result;
3680 SDNode *Node = Op.Val;
3681
Chris Lattner4b0ddb22007-02-04 01:17:38 +00003682 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00003683 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00003684
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003685 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003686 case ISD::CopyFromReg:
3687 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003688 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003689#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00003690 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00003691#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003692 assert(0 && "Do not know how to promote this operator!");
3693 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003694 case ISD::UNDEF:
3695 Result = DAG.getNode(ISD::UNDEF, NVT);
3696 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003697 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00003698 if (VT != MVT::i1)
3699 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
3700 else
3701 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003702 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
3703 break;
3704 case ISD::ConstantFP:
3705 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
3706 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
3707 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00003708
Chris Lattner2cb338d2005-01-18 02:59:52 +00003709 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003710 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00003711 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
3712 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00003713 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00003714
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003715 case ISD::TRUNCATE:
3716 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3717 case Legal:
3718 Result = LegalizeOp(Node->getOperand(0));
3719 assert(Result.getValueType() >= NVT &&
3720 "This truncation doesn't make sense!");
3721 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
3722 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
3723 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00003724 case Promote:
3725 // The truncation is not required, because we don't guarantee anything
3726 // about high bits anyway.
3727 Result = PromoteOp(Node->getOperand(0));
3728 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003729 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00003730 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3731 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00003732 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003733 }
3734 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003735 case ISD::SIGN_EXTEND:
3736 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00003737 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00003738 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3739 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
3740 case Legal:
3741 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003742 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003743 break;
3744 case Promote:
3745 // Promote the reg if it's smaller.
3746 Result = PromoteOp(Node->getOperand(0));
3747 // The high bits are not guaranteed to be anything. Insert an extend.
3748 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00003749 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003750 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00003751 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00003752 Result = DAG.getZeroExtendInReg(Result,
3753 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00003754 break;
3755 }
3756 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003757 case ISD::BIT_CONVERT:
3758 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
3759 Result = PromoteOp(Result);
3760 break;
3761
Chris Lattner4d978642005-01-15 22:16:26 +00003762 case ISD::FP_EXTEND:
3763 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
3764 case ISD::FP_ROUND:
3765 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3766 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
3767 case Promote: assert(0 && "Unreachable with 2 FP types!");
3768 case Legal:
3769 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003770 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003771 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003772 break;
3773 }
3774 break;
3775
3776 case ISD::SINT_TO_FP:
3777 case ISD::UINT_TO_FP:
3778 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3779 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00003780 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003781 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00003782 break;
3783
3784 case Promote:
3785 Result = PromoteOp(Node->getOperand(0));
3786 if (Node->getOpcode() == ISD::SINT_TO_FP)
3787 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003788 Result,
3789 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00003790 else
Chris Lattner0e852af2005-04-13 02:38:47 +00003791 Result = DAG.getZeroExtendInReg(Result,
3792 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003793 // No extra round required here.
3794 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00003795 break;
3796 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00003797 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
3798 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00003799 // Round if we cannot tolerate excess precision.
3800 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003801 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3802 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00003803 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003804 }
Chris Lattner4d978642005-01-15 22:16:26 +00003805 break;
3806
Chris Lattner268d4572005-12-09 17:32:47 +00003807 case ISD::SIGN_EXTEND_INREG:
3808 Result = PromoteOp(Node->getOperand(0));
3809 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
3810 Node->getOperand(1));
3811 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003812 case ISD::FP_TO_SINT:
3813 case ISD::FP_TO_UINT:
3814 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3815 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00003816 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003817 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00003818 break;
3819 case Promote:
3820 // The input result is prerounded, so we don't have to do anything
3821 // special.
3822 Tmp1 = PromoteOp(Node->getOperand(0));
3823 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003824 }
Nate Begeman36853ee2005-08-14 01:20:53 +00003825 // If we're promoting a UINT to a larger size, check to see if the new node
3826 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
3827 // we can use that instead. This allows us to generate better code for
3828 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
3829 // legal, such as PowerPC.
3830 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003831 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00003832 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
3833 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00003834 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
3835 } else {
3836 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3837 }
Chris Lattner4d978642005-01-15 22:16:26 +00003838 break;
3839
Chris Lattner13fe99c2005-04-02 05:00:07 +00003840 case ISD::FABS:
3841 case ISD::FNEG:
3842 Tmp1 = PromoteOp(Node->getOperand(0));
3843 assert(Tmp1.getValueType() == NVT);
3844 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
3845 // NOTE: we do not have to do any extra rounding here for
3846 // NoExcessFPPrecision, because we know the input will have the appropriate
3847 // precision, and these operations don't modify precision at all.
3848 break;
3849
Chris Lattner9d6fa982005-04-28 21:44:33 +00003850 case ISD::FSQRT:
3851 case ISD::FSIN:
3852 case ISD::FCOS:
3853 Tmp1 = PromoteOp(Node->getOperand(0));
3854 assert(Tmp1.getValueType() == NVT);
3855 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003856 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003857 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3858 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00003859 break;
3860
Chris Lattnerca401aa2007-03-03 23:43:21 +00003861 case ISD::FPOWI: {
3862 // Promote f32 powi to f64 powi. Note that this could insert a libcall
3863 // directly as well, which may be better.
3864 Tmp1 = PromoteOp(Node->getOperand(0));
3865 assert(Tmp1.getValueType() == NVT);
3866 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
3867 if (NoExcessFPPrecision)
3868 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3869 DAG.getValueType(VT));
3870 break;
3871 }
3872
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003873 case ISD::AND:
3874 case ISD::OR:
3875 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003876 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00003877 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003878 case ISD::MUL:
3879 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00003880 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003881 // that too is okay if they are integer operations.
3882 Tmp1 = PromoteOp(Node->getOperand(0));
3883 Tmp2 = PromoteOp(Node->getOperand(1));
3884 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3885 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00003886 break;
3887 case ISD::FADD:
3888 case ISD::FSUB:
3889 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003890 Tmp1 = PromoteOp(Node->getOperand(0));
3891 Tmp2 = PromoteOp(Node->getOperand(1));
3892 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
3893 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3894
3895 // Floating point operations will give excess precision that we may not be
3896 // able to tolerate. If we DO allow excess precision, just leave it,
3897 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00003898 // FIXME: Why would we need to round FP ops more than integer ones?
3899 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00003900 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003901 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3902 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003903 break;
3904
Chris Lattner4d978642005-01-15 22:16:26 +00003905 case ISD::SDIV:
3906 case ISD::SREM:
3907 // These operators require that their input be sign extended.
3908 Tmp1 = PromoteOp(Node->getOperand(0));
3909 Tmp2 = PromoteOp(Node->getOperand(1));
3910 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00003911 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3912 DAG.getValueType(VT));
3913 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
3914 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003915 }
3916 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3917
3918 // Perform FP_ROUND: this is probably overly pessimistic.
3919 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00003920 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3921 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00003922 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003923 case ISD::FDIV:
3924 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003925 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003926 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00003927 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3928 case Legal:
3929 Tmp1 = LegalizeOp(Node->getOperand(0));
3930 break;
3931 case Promote:
3932 Tmp1 = PromoteOp(Node->getOperand(0));
3933 break;
3934 case Expand:
3935 assert(0 && "not implemented");
3936 }
3937 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3938 case Legal:
3939 Tmp2 = LegalizeOp(Node->getOperand(1));
3940 break;
3941 case Promote:
3942 Tmp2 = PromoteOp(Node->getOperand(1));
3943 break;
3944 case Expand:
3945 assert(0 && "not implemented");
3946 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00003947 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3948
3949 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003950 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00003951 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
3952 DAG.getValueType(VT));
3953 break;
Chris Lattner4d978642005-01-15 22:16:26 +00003954
3955 case ISD::UDIV:
3956 case ISD::UREM:
3957 // These operators require that their input be zero extended.
3958 Tmp1 = PromoteOp(Node->getOperand(0));
3959 Tmp2 = PromoteOp(Node->getOperand(1));
3960 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00003961 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
3962 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00003963 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3964 break;
3965
3966 case ISD::SHL:
3967 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003968 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003969 break;
3970 case ISD::SRA:
3971 // The input value must be properly sign extended.
3972 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003973 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
3974 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003975 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003976 break;
3977 case ISD::SRL:
3978 // The input value must be properly zero extended.
3979 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00003980 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003981 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00003982 break;
Nate Begeman595ec732006-01-28 03:14:31 +00003983
3984 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003985 Tmp1 = Node->getOperand(0); // Get the chain.
3986 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00003987 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
3988 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
3989 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
3990 } else {
Evan Chenge71fe34d2006-10-09 20:57:25 +00003991 SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2));
Nate Begeman595ec732006-01-28 03:14:31 +00003992 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
Evan Chenge71fe34d2006-10-09 20:57:25 +00003993 SV->getValue(), SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00003994 // Increment the pointer, VAList, to the next vaarg
3995 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3996 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3997 TLI.getPointerTy()));
3998 // Store the incremented VAList to the legalized pointer
Evan Chengab51cf22006-10-13 21:14:26 +00003999 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(),
4000 SV->getOffset());
Nate Begeman595ec732006-01-28 03:14:31 +00004001 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00004002 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00004003 }
4004 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004005 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00004006 break;
4007
Evan Chenge71fe34d2006-10-09 20:57:25 +00004008 case ISD::LOAD: {
4009 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00004010 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4011 ? ISD::EXTLOAD : LD->getExtensionType();
4012 Result = DAG.getExtLoad(ExtType, NVT,
4013 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00004014 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman2af30632007-07-09 22:18:38 +00004015 LD->getLoadedVT(),
4016 LD->isVolatile(),
4017 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004018 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004019 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004020 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00004021 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004022 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004023 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4024 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004025 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004026 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00004027 case ISD::SELECT_CC:
4028 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4029 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4030 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004031 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004032 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00004033 case ISD::BSWAP:
4034 Tmp1 = Node->getOperand(0);
4035 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4036 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4037 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004038 DAG.getConstant(MVT::getSizeInBits(NVT) -
4039 MVT::getSizeInBits(VT),
Nate Begeman2fba8a32006-01-14 03:14:10 +00004040 TLI.getShiftAmountTy()));
4041 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004042 case ISD::CTPOP:
4043 case ISD::CTTZ:
4044 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004045 // Zero extend the argument
4046 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004047 // Perform the larger operation, then subtract if needed.
4048 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004049 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004050 case ISD::CTPOP:
4051 Result = Tmp1;
4052 break;
4053 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004054 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00004055 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004056 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4057 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004058 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004059 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004060 break;
4061 case ISD::CTLZ:
4062 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004063 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004064 DAG.getConstant(MVT::getSizeInBits(NVT) -
4065 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004066 break;
4067 }
4068 break;
Dan Gohmana8665142007-06-25 16:23:39 +00004069 case ISD::EXTRACT_SUBVECTOR:
4070 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00004071 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00004072 case ISD::EXTRACT_VECTOR_ELT:
4073 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4074 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004075 }
4076
4077 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004078
4079 // Make sure the result is itself legal.
4080 Result = LegalizeOp(Result);
4081
4082 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004083 AddPromotedOperand(Op, Result);
4084 return Result;
4085}
Chris Lattnerdc750592005-01-07 07:47:09 +00004086
Dan Gohmana8665142007-06-25 16:23:39 +00004087/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4088/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4089/// based on the vector type. The return type of this matches the element type
4090/// of the vector, which may not be legal for the target.
4091SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00004092 // We know that operand #0 is the Vec vector. If the index is a constant
4093 // or if the invec is a supported hardware type, we can use it. Otherwise,
4094 // lower to a store then an indexed load.
4095 SDOperand Vec = Op.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00004096 SDOperand Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00004097
Dan Gohman60028182007-09-24 15:54:53 +00004098 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +00004099 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner6f423252006-03-31 17:55:51 +00004100
Dan Gohmana8665142007-06-25 16:23:39 +00004101 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4102 default: assert(0 && "This action is not supported yet!");
4103 case TargetLowering::Custom: {
4104 Vec = LegalizeOp(Vec);
4105 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4106 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4107 if (Tmp3.Val)
4108 return Tmp3;
4109 break;
4110 }
4111 case TargetLowering::Legal:
4112 if (isTypeLegal(TVT)) {
4113 Vec = LegalizeOp(Vec);
4114 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00004115 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00004116 }
4117 break;
4118 case TargetLowering::Expand:
4119 break;
4120 }
4121
4122 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00004123 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004124 Op = ScalarizeVectorOp(Vec);
4125 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
4126 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner6f423252006-03-31 17:55:51 +00004127 SDOperand Lo, Hi;
4128 SplitVectorOp(Vec, Lo, Hi);
4129 if (CIdx->getValue() < NumElems/2) {
4130 Vec = Lo;
4131 } else {
4132 Vec = Hi;
Dan Gohmana8665142007-06-25 16:23:39 +00004133 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2,
4134 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00004135 }
Dan Gohmana8665142007-06-25 16:23:39 +00004136
Chris Lattner6f423252006-03-31 17:55:51 +00004137 // It's now an extract from the appropriate high or low part. Recurse.
4138 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004139 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00004140 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00004141 // Store the value to a temporary stack slot, then LOAD the scalar
4142 // element back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004143 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmana8665142007-06-25 16:23:39 +00004144 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4145
4146 // Add the offset to the index.
4147 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4148 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4149 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling070aca52007-10-18 08:32:37 +00004150
4151 if (MVT::getSizeInBits(Idx.getValueType()) >
4152 MVT::getSizeInBits(TLI.getPointerTy()))
4153 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), StackPtr);
4154 else
Bill Wendlingde16ad12007-10-19 01:10:49 +00004155 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), StackPtr);
Bill Wendling070aca52007-10-18 08:32:37 +00004156
Dan Gohmana8665142007-06-25 16:23:39 +00004157 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4158
4159 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00004160 }
Dan Gohmana8665142007-06-25 16:23:39 +00004161 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00004162}
4163
Dan Gohmana8665142007-06-25 16:23:39 +00004164/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00004165/// we assume the operation can be split if it is not already legal.
Dan Gohmana8665142007-06-25 16:23:39 +00004166SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00004167 // We know that operand #0 is the Vec vector. For now we assume the index
4168 // is a constant and that the extracted result is a supported hardware type.
4169 SDOperand Vec = Op.getOperand(0);
4170 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4171
Dan Gohmana8665142007-06-25 16:23:39 +00004172 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00004173
4174 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4175 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004176 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00004177 }
4178
4179 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4180 SDOperand Lo, Hi;
4181 SplitVectorOp(Vec, Lo, Hi);
4182 if (CIdx->getValue() < NumElems/2) {
4183 Vec = Lo;
4184 } else {
4185 Vec = Hi;
4186 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4187 }
4188
4189 // It's now an extract from the appropriate high or low part. Recurse.
4190 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004191 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00004192}
4193
Nate Begeman7e7f4392006-02-01 07:19:44 +00004194/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4195/// with condition CC on the current target. This usually involves legalizing
4196/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4197/// there may be no choice but to create a new SetCC node to represent the
4198/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4199/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4200void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4201 SDOperand &RHS,
4202 SDOperand &CC) {
Dale Johannesenf864ac92007-10-06 01:24:11 +00004203 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman7e7f4392006-02-01 07:19:44 +00004204
4205 switch (getTypeAction(LHS.getValueType())) {
4206 case Legal:
4207 Tmp1 = LegalizeOp(LHS); // LHS
4208 Tmp2 = LegalizeOp(RHS); // RHS
4209 break;
4210 case Promote:
4211 Tmp1 = PromoteOp(LHS); // LHS
4212 Tmp2 = PromoteOp(RHS); // RHS
4213
4214 // If this is an FP compare, the operands have already been extended.
4215 if (MVT::isInteger(LHS.getValueType())) {
4216 MVT::ValueType VT = LHS.getValueType();
4217 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4218
4219 // Otherwise, we have to insert explicit sign or zero extends. Note
4220 // that we could insert sign extends for ALL conditions, but zero extend
4221 // is cheaper on many machines (an AND instead of two shifts), so prefer
4222 // it.
4223 switch (cast<CondCodeSDNode>(CC)->get()) {
4224 default: assert(0 && "Unknown integer comparison!");
4225 case ISD::SETEQ:
4226 case ISD::SETNE:
4227 case ISD::SETUGE:
4228 case ISD::SETUGT:
4229 case ISD::SETULE:
4230 case ISD::SETULT:
4231 // ALL of these operations will work if we either sign or zero extend
4232 // the operands (including the unsigned comparisons!). Zero extend is
4233 // usually a simpler/cheaper operation, so prefer it.
4234 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4235 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4236 break;
4237 case ISD::SETGE:
4238 case ISD::SETGT:
4239 case ISD::SETLT:
4240 case ISD::SETLE:
4241 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4242 DAG.getValueType(VT));
4243 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4244 DAG.getValueType(VT));
4245 break;
4246 }
4247 }
4248 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004249 case Expand: {
4250 MVT::ValueType VT = LHS.getValueType();
4251 if (VT == MVT::f32 || VT == MVT::f64) {
4252 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00004253 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004254 switch (cast<CondCodeSDNode>(CC)->get()) {
4255 case ISD::SETEQ:
4256 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004257 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004258 break;
4259 case ISD::SETNE:
4260 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004261 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004262 break;
4263 case ISD::SETGE:
4264 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004265 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004266 break;
4267 case ISD::SETLT:
4268 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004269 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004270 break;
4271 case ISD::SETLE:
4272 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004273 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004274 break;
4275 case ISD::SETGT:
4276 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004277 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004278 break;
4279 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00004280 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4281 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004282 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00004283 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004284 break;
4285 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004286 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004287 switch (cast<CondCodeSDNode>(CC)->get()) {
4288 case ISD::SETONE:
4289 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00004290 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004291 // Fallthrough
4292 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004293 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004294 break;
4295 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004296 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004297 break;
4298 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004299 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004300 break;
4301 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004302 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004303 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004304 case ISD::SETUEQ:
4305 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004306 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004307 default: assert(0 && "Unsupported FP setcc!");
4308 }
4309 }
4310
4311 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004312 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00004313 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004314 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004315 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00004316 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00004317 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004318 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00004319 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00004320 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004321 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004322 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00004323 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004324 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4325 Tmp2 = SDOperand();
4326 }
4327 LHS = Tmp1;
4328 RHS = Tmp2;
4329 return;
4330 }
4331
Nate Begeman7e7f4392006-02-01 07:19:44 +00004332 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4333 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004334 ExpandOp(RHS, RHSLo, RHSHi);
4335 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4336
4337 if (VT==MVT::ppcf128) {
4338 // FIXME: This generated code sucks. We want to generate
4339 // FCMP crN, hi1, hi2
4340 // BNE crN, L:
4341 // FCMP crN, lo1, lo2
4342 // The following can be improved, but not that much.
4343 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4344 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
4345 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4346 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
4347 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
4348 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4349 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4350 Tmp2 = SDOperand();
4351 break;
4352 }
4353
4354 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004355 case ISD::SETEQ:
4356 case ISD::SETNE:
4357 if (RHSLo == RHSHi)
4358 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4359 if (RHSCST->isAllOnesValue()) {
4360 // Comparison to -1.
4361 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4362 Tmp2 = RHSLo;
4363 break;
4364 }
4365
4366 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4367 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4368 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4369 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4370 break;
4371 default:
4372 // If this is a comparison of the sign bit, just look at the top part.
4373 // X > -1, x < 0
4374 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4375 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4376 CST->getValue() == 0) || // X < 0
4377 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4378 CST->isAllOnesValue())) { // X > -1
4379 Tmp1 = LHSHi;
4380 Tmp2 = RHSHi;
4381 break;
4382 }
4383
4384 // FIXME: This generated code sucks.
4385 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00004386 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004387 default: assert(0 && "Unknown integer setcc!");
4388 case ISD::SETLT:
4389 case ISD::SETULT: LowCC = ISD::SETULT; break;
4390 case ISD::SETGT:
4391 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4392 case ISD::SETLE:
4393 case ISD::SETULE: LowCC = ISD::SETULE; break;
4394 case ISD::SETGE:
4395 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4396 }
4397
4398 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4399 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4400 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4401
4402 // NOTE: on targets without efficient SELECT of bools, we can always use
4403 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00004404 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
4405 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
4406 false, DagCombineInfo);
4407 if (!Tmp1.Val)
4408 Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
4409 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4410 CCCode, false, DagCombineInfo);
4411 if (!Tmp2.Val)
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004412 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC);
Evan Cheng93049452007-02-08 22:16:19 +00004413
4414 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4415 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4416 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4417 (Tmp2C && Tmp2C->getValue() == 0 &&
4418 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4419 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4420 (Tmp2C && Tmp2C->getValue() == 1 &&
4421 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4422 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4423 // low part is known false, returns high part.
4424 // For LE / GE, if high part is known false, ignore the low part.
4425 // For LT / GT, if high part is known true, ignore the low part.
4426 Tmp1 = Tmp2;
4427 Tmp2 = SDOperand();
4428 } else {
4429 Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
4430 ISD::SETEQ, false, DagCombineInfo);
4431 if (!Result.Val)
4432 Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
4433 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4434 Result, Tmp1, Tmp2));
4435 Tmp1 = Result;
4436 Tmp2 = SDOperand();
4437 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004438 }
4439 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004440 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004441 LHS = Tmp1;
4442 RHS = Tmp2;
4443}
4444
Chris Lattner36e663d2005-12-23 00:16:34 +00004445/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00004446/// The resultant code need not be legal. Note that SrcOp is the input operand
4447/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00004448SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
4449 SDOperand SrcOp) {
4450 // Create the stack frame object.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004451 SDOperand FIPtr = DAG.CreateStackTemporary(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004452
4453 // Emit a store to the stack slot.
Evan Chengab51cf22006-10-13 21:14:26 +00004454 SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004455 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004456 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
Chris Lattner36e663d2005-12-23 00:16:34 +00004457}
4458
Chris Lattner6be79822006-04-04 17:23:26 +00004459SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4460 // Create a vector sized/aligned stack slot, store the value to element #0,
4461 // then load the whole vector back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004462 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Evan Chengdf9ac472006-10-05 23:01:46 +00004463 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Evan Chengab51cf22006-10-13 21:14:26 +00004464 NULL, 0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004465 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0);
Chris Lattner6be79822006-04-04 17:23:26 +00004466}
4467
4468
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004469/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00004470/// support the operation, but do support the resultant vector type.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004471SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4472
4473 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00004474 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004475 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004476 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00004477 SDOperand SplatValue = Node->getOperand(0);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004478 std::map<SDOperand, std::vector<unsigned> > Values;
4479 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004480 bool isConstant = true;
4481 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4482 SplatValue.getOpcode() != ISD::UNDEF)
4483 isConstant = false;
4484
Evan Cheng1d2e9952006-03-24 01:17:21 +00004485 for (unsigned i = 1; i < NumElems; ++i) {
4486 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00004487 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004488 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004489 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004490 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00004491 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004492
4493 // If this isn't a constant element or an undef, we can't use a constant
4494 // pool load.
4495 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4496 V.getOpcode() != ISD::UNDEF)
4497 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004498 }
4499
4500 if (isOnlyLowElement) {
4501 // If the low element is an undef too, then this whole things is an undef.
4502 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4503 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4504 // Otherwise, turn this into a scalar_to_vector node.
4505 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4506 Node->getOperand(0));
4507 }
4508
Chris Lattner77e271c2006-03-24 07:29:17 +00004509 // If all elements are constants, create a load from the constant pool.
4510 if (isConstant) {
4511 MVT::ValueType VT = Node->getValueType(0);
4512 const Type *OpNTy =
4513 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4514 std::vector<Constant*> CV;
4515 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4516 if (ConstantFPSDNode *V =
4517 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00004518 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004519 } else if (ConstantSDNode *V =
4520 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00004521 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004522 } else {
4523 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4524 CV.push_back(UndefValue::get(OpNTy));
4525 }
4526 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00004527 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00004528 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Evan Chenge71fe34d2006-10-09 20:57:25 +00004529 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004530 }
4531
Chris Lattner21e68c82006-03-20 01:52:29 +00004532 if (SplatValue.Val) { // Splat of one value?
4533 // Build the shuffle constant vector: <0, 0, 0, 0>
4534 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00004535 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00004536 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004537 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004538 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4539 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00004540
4541 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004542 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00004543 // Get the splatted value into the low element of a vector register.
4544 SDOperand LowValVec =
4545 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
4546
4547 // Return shuffle(LowValVec, undef, <0,0,0,0>)
4548 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
4549 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
4550 SplatMask);
4551 }
4552 }
4553
Evan Cheng1d2e9952006-03-24 01:17:21 +00004554 // If there are only two unique elements, we may be able to turn this into a
4555 // vector shuffle.
4556 if (Values.size() == 2) {
4557 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
4558 MVT::ValueType MaskVT =
4559 MVT::getIntVectorWithNumElements(NumElems);
4560 std::vector<SDOperand> MaskVec(NumElems);
4561 unsigned i = 0;
4562 for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4563 E = Values.end(); I != E; ++I) {
4564 for (std::vector<unsigned>::iterator II = I->second.begin(),
4565 EE = I->second.end(); II != EE; ++II)
Dan Gohman5c441312007-06-14 22:58:02 +00004566 MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00004567 i += NumElems;
4568 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004569 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4570 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004571
4572 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00004573 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
4574 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004575 SmallVector<SDOperand, 8> Ops;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004576 for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(),
4577 E = Values.end(); I != E; ++I) {
4578 SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4579 I->first);
4580 Ops.push_back(Op);
4581 }
4582 Ops.push_back(ShuffleMask);
4583
4584 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004585 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0),
4586 &Ops[0], Ops.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00004587 }
4588 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004589
4590 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
4591 // aligned object on the stack, store each element into it, then load
4592 // the result as a vector.
4593 MVT::ValueType VT = Node->getValueType(0);
4594 // Create the stack frame object.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004595 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004596
4597 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004598 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004599 unsigned TypeByteSize =
4600 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004601 // Store (in the right endianness) the elements to memory.
4602 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4603 // Ignore undef elements.
4604 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4605
Chris Lattner8fa445a2006-03-22 01:46:54 +00004606 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004607
4608 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
4609 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
4610
Evan Chengdf9ac472006-10-05 23:01:46 +00004611 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00004612 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004613 }
4614
4615 SDOperand StoreChain;
4616 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004617 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
4618 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004619 else
4620 StoreChain = DAG.getEntryNode();
4621
4622 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00004623 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004624}
4625
Chris Lattner4157c412005-04-02 04:00:59 +00004626void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
4627 SDOperand Op, SDOperand Amt,
4628 SDOperand &Lo, SDOperand &Hi) {
4629 // Expand the subcomponents.
4630 SDOperand LHSL, LHSH;
4631 ExpandOp(Op, LHSL, LHSH);
4632
Chris Lattnerc24a1d32006-08-08 02:23:42 +00004633 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00004634 MVT::ValueType VT = LHSL.getValueType();
4635 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00004636 Hi = Lo.getValue(1);
4637}
4638
4639
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004640/// ExpandShift - Try to find a clever way to expand this shift operation out to
4641/// smaller elements. If we can't find a way that is more efficient than a
4642/// libcall on this target, return false. Otherwise, return true with the
4643/// low-parts expanded into Lo and Hi.
4644bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
4645 SDOperand &Lo, SDOperand &Hi) {
4646 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
4647 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00004648
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004649 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00004650 SDOperand ShAmt = LegalizeOp(Amt);
4651 MVT::ValueType ShTy = ShAmt.getValueType();
4652 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
4653 unsigned NVTBits = MVT::getSizeInBits(NVT);
4654
Chris Lattnerfbbe5702007-10-14 20:35:12 +00004655 // Handle the case when Amt is an immediate.
Nate Begemanb0674922005-04-06 21:13:14 +00004656 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
4657 unsigned Cst = CN->getValue();
4658 // Expand the incoming operand to be shifted, so that we have its parts
4659 SDOperand InL, InH;
4660 ExpandOp(Op, InL, InH);
4661 switch(Opc) {
4662 case ISD::SHL:
4663 if (Cst > VTBits) {
4664 Lo = DAG.getConstant(0, NVT);
4665 Hi = DAG.getConstant(0, NVT);
4666 } else if (Cst > NVTBits) {
4667 Lo = DAG.getConstant(0, NVT);
4668 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004669 } else if (Cst == NVTBits) {
4670 Lo = DAG.getConstant(0, NVT);
4671 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00004672 } else {
4673 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
4674 Hi = DAG.getNode(ISD::OR, NVT,
4675 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
4676 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
4677 }
4678 return true;
4679 case ISD::SRL:
4680 if (Cst > VTBits) {
4681 Lo = DAG.getConstant(0, NVT);
4682 Hi = DAG.getConstant(0, NVT);
4683 } else if (Cst > NVTBits) {
4684 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
4685 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00004686 } else if (Cst == NVTBits) {
4687 Lo = InH;
4688 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00004689 } else {
4690 Lo = DAG.getNode(ISD::OR, NVT,
4691 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4692 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4693 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
4694 }
4695 return true;
4696 case ISD::SRA:
4697 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004698 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004699 DAG.getConstant(NVTBits-1, ShTy));
4700 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00004701 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004702 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00004703 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00004704 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00004705 } else if (Cst == NVTBits) {
4706 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00004707 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00004708 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00004709 } else {
4710 Lo = DAG.getNode(ISD::OR, NVT,
4711 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
4712 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
4713 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
4714 }
4715 return true;
4716 }
4717 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00004718
4719 // Okay, the shift amount isn't constant. However, if we can tell that it is
4720 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
4721 uint64_t Mask = NVTBits, KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00004722 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00004723
4724 // If we know that the high bit of the shift amount is one, then we can do
4725 // this as a couple of simple shifts.
4726 if (KnownOne & Mask) {
4727 // Mask out the high bit, which we know is set.
4728 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
4729 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4730
4731 // Expand the incoming operand to be shifted, so that we have its parts
4732 SDOperand InL, InH;
4733 ExpandOp(Op, InL, InH);
4734 switch(Opc) {
4735 case ISD::SHL:
4736 Lo = DAG.getConstant(0, NVT); // Low part is zero.
4737 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
4738 return true;
4739 case ISD::SRL:
4740 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
4741 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
4742 return true;
4743 case ISD::SRA:
4744 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
4745 DAG.getConstant(NVTBits-1, Amt.getValueType()));
4746 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
4747 return true;
4748 }
4749 }
4750
4751 // If we know that the high bit of the shift amount is zero, then we can do
4752 // this as a couple of simple shifts.
4753 if (KnownZero & Mask) {
4754 // Compute 32-amt.
4755 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
4756 DAG.getConstant(NVTBits, Amt.getValueType()),
4757 Amt);
4758
4759 // Expand the incoming operand to be shifted, so that we have its parts
4760 SDOperand InL, InH;
4761 ExpandOp(Op, InL, InH);
4762 switch(Opc) {
4763 case ISD::SHL:
4764 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
4765 Hi = DAG.getNode(ISD::OR, NVT,
4766 DAG.getNode(ISD::SHL, NVT, InH, Amt),
4767 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
4768 return true;
4769 case ISD::SRL:
4770 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
4771 Lo = DAG.getNode(ISD::OR, NVT,
4772 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4773 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4774 return true;
4775 case ISD::SRA:
4776 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
4777 Lo = DAG.getNode(ISD::OR, NVT,
4778 DAG.getNode(ISD::SRL, NVT, InL, Amt),
4779 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
4780 return true;
4781 }
4782 }
4783
Nate Begemanb0674922005-04-06 21:13:14 +00004784 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00004785}
Chris Lattneraac464e2005-01-21 06:05:23 +00004786
Chris Lattner4add7e32005-01-23 04:42:50 +00004787
Chris Lattneraac464e2005-01-21 06:05:23 +00004788// ExpandLibCall - Expand a node into a call to a libcall. If the result value
4789// does not fit into a register, return the lo part and set the hi part to the
4790// by-reg argument. If it does fit into a single register, return the result
4791// and leave the Hi part unset.
4792SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00004793 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00004794 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
4795 // The input chain to this libcall is the entry node of the function.
4796 // Legalizing the call will automatically add the previous call to the
4797 // dependence.
4798 SDOperand InChain = DAG.getEntryNode();
4799
Chris Lattneraac464e2005-01-21 06:05:23 +00004800 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00004801 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00004802 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
4803 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
4804 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00004805 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00004806 Entry.isSExt = isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00004807 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00004808 }
4809 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00004810
Chris Lattner06bbeb62005-05-11 19:02:11 +00004811 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00004812 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00004813 std::pair<SDOperand,SDOperand> CallInfo =
Reid Spencere63b6512006-12-31 05:55:36 +00004814 TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false,
Chris Lattner2e77db62005-05-13 18:50:42 +00004815 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00004816
Chris Lattner462505f2006-02-13 09:18:02 +00004817 // Legalize the call sequence, starting with the chain. This will advance
4818 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
4819 // was added by LowerCallTo (guaranteeing proper serialization of calls).
4820 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00004821 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004822 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004823 default: assert(0 && "Unknown thing");
4824 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004825 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00004826 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004827 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00004828 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00004829 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00004830 }
Chris Lattner63022662005-09-02 20:26:58 +00004831 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00004832}
4833
Chris Lattner4add7e32005-01-23 04:42:50 +00004834
Evan Chengbf535fc2007-04-27 07:33:31 +00004835/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
4836///
Chris Lattneraac464e2005-01-21 06:05:23 +00004837SDOperand SelectionDAGLegalize::
4838ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattneraac464e2005-01-21 06:05:23 +00004839 assert(getTypeAction(Source.getValueType()) == Expand &&
4840 "This is not an expansion!");
4841 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
4842
Chris Lattner06bbeb62005-05-11 19:02:11 +00004843 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004844 assert(Source.getValueType() == MVT::i64 &&
4845 "This only works for 64-bit -> FP");
4846 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
4847 // incoming integer is set. To handle this, we dynamically test to see if
4848 // it is set, and, if so, add a fudge factor.
4849 SDOperand Lo, Hi;
4850 ExpandOp(Source, Lo, Hi);
4851
Chris Lattner2a4f7312005-05-13 04:45:13 +00004852 // If this is unsigned, and not supported, first perform the conversion to
4853 // signed, then adjust the result if the sign bit is set.
4854 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
4855 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
4856
Chris Lattnerd47675e2005-08-09 20:20:18 +00004857 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
4858 DAG.getConstant(0, Hi.getValueType()),
4859 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004860 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
4861 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
4862 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00004863 uint64_t FF = 0x5f800000ULL;
4864 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00004865 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004866
Chris Lattnerc30405e2005-08-26 17:15:30 +00004867 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00004868 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
4869 SDOperand FudgeInReg;
4870 if (DestTy == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00004871 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Dale Johannesen7f724e92007-09-16 16:51:49 +00004872 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Chengbf535fc2007-04-27 07:33:31 +00004873 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen7f724e92007-09-16 16:51:49 +00004874 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dale Johannesen98d3a082007-09-14 22:26:36 +00004875 CPIdx, NULL, 0, MVT::f32);
4876 else
4877 assert(0 && "Unexpected conversion");
4878
Evan Chengbf535fc2007-04-27 07:33:31 +00004879 MVT::ValueType SCVT = SignedConv.getValueType();
4880 if (SCVT != DestTy) {
4881 // Destination type needs to be expanded as well. The FADD now we are
4882 // constructing will be expanded into a libcall.
4883 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
4884 assert(SCVT == MVT::i32 && DestTy == MVT::f64);
4885 SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64,
4886 SignedConv, SignedConv.getValue(1));
4887 }
4888 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
4889 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00004890 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00004891 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00004892
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004893 // Check to see if the target has a custom way to lower this. If so, use it.
4894 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
4895 default: assert(0 && "This action not implemented for this operation!");
4896 case TargetLowering::Legal:
4897 case TargetLowering::Expand:
4898 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00004899 case TargetLowering::Custom: {
4900 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
4901 Source), DAG);
4902 if (NV.Val)
4903 return LegalizeOp(NV);
4904 break; // The target decided this was legal after all
4905 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00004906 }
4907
Chris Lattner153587e2005-05-12 07:00:44 +00004908 // Expand the source, then glue it back together for the call. We must expand
4909 // the source in case it is shared (this pass of legalize must traverse it).
4910 SDOperand SrcLo, SrcHi;
4911 ExpandOp(Source, SrcLo, SrcHi);
4912 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
4913
Evan Cheng31cbddf2007-01-12 02:11:51 +00004914 RTLIB::Libcall LC;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004915 if (DestTy == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00004916 LC = RTLIB::SINTTOFP_I64_F32;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004917 else {
4918 assert(DestTy == MVT::f64 && "Unknown fp value type!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00004919 LC = RTLIB::SINTTOFP_I64_F64;
Chris Lattner06bbeb62005-05-11 19:02:11 +00004920 }
Chris Lattner462505f2006-02-13 09:18:02 +00004921
Evan Chengbf535fc2007-04-27 07:33:31 +00004922 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner462505f2006-02-13 09:18:02 +00004923 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
4924 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004925 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
4926 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00004927}
Misha Brukman835702a2005-04-21 22:36:52 +00004928
Chris Lattner689bdcc2006-01-28 08:25:58 +00004929/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
4930/// INT_TO_FP operation of the specified operand when the target requests that
4931/// we expand it. At this point, we know that the result and operand types are
4932/// legal for the target.
4933SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
4934 SDOperand Op0,
4935 MVT::ValueType DestVT) {
4936 if (Op0.getValueType() == MVT::i32) {
4937 // simple 32-bit [signed|unsigned] integer to float/double expansion
4938
Chris Lattner50ee0e42007-01-20 22:35:55 +00004939 // get the stack frame index of a 8 byte buffer, pessimistically aligned
Chris Lattner689bdcc2006-01-28 08:25:58 +00004940 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner50ee0e42007-01-20 22:35:55 +00004941 const Type *F64Type = MVT::getTypeForValueType(MVT::f64);
4942 unsigned StackAlign =
Chris Lattner945e4372007-02-14 05:52:17 +00004943 (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type);
Chris Lattner50ee0e42007-01-20 22:35:55 +00004944 int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004945 // get address of 8 byte buffer
4946 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4947 // word offset constant for Hi/Lo address computation
4948 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
4949 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00004950 SDOperand Hi = StackSlot;
4951 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
4952 if (TLI.isLittleEndian())
4953 std::swap(Hi, Lo);
4954
Chris Lattner689bdcc2006-01-28 08:25:58 +00004955 // if signed map to unsigned space
4956 SDOperand Op0Mapped;
4957 if (isSigned) {
4958 // constant used to invert sign bit (signed to unsigned mapping)
4959 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
4960 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
4961 } else {
4962 Op0Mapped = Op0;
4963 }
4964 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00004965 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00004966 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004967 // initial hi portion of constructed double
4968 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
4969 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00004970 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004971 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00004972 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004973 // FP constant to bias correct the final result
4974 SDOperand Bias = DAG.getConstantFP(isSigned ?
4975 BitsToDouble(0x4330000080000000ULL)
4976 : BitsToDouble(0x4330000000000000ULL),
4977 MVT::f64);
4978 // subtract the bias
4979 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
4980 // final result
4981 SDOperand Result;
4982 // handle final rounding
4983 if (DestVT == MVT::f64) {
4984 // do nothing
4985 Result = Sub;
Dale Johannesen7f724e92007-09-16 16:51:49 +00004986 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
4987 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub);
4988 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
4989 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00004990 }
4991 return Result;
4992 }
4993 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
4994 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
4995
4996 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
4997 DAG.getConstant(0, Op0.getValueType()),
4998 ISD::SETLT);
4999 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
5000 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5001 SignSet, Four, Zero);
5002
5003 // If the sign bit of the integer is set, the large number will be treated
5004 // as a negative number. To counteract this, the dynamic code adds an
5005 // offset depending on the data type.
5006 uint64_t FF;
5007 switch (Op0.getValueType()) {
5008 default: assert(0 && "Unsupported integer type!");
5009 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5010 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5011 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5012 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5013 }
5014 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00005015 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005016
5017 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5018 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5019 SDOperand FudgeInReg;
5020 if (DestVT == MVT::f32)
Evan Chenge71fe34d2006-10-09 20:57:25 +00005021 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005022 else {
Dale Johannesen7d67e542007-09-19 23:55:34 +00005023 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
Chris Lattner689bdcc2006-01-28 08:25:58 +00005024 DAG.getEntryNode(), CPIdx,
Evan Chenge71fe34d2006-10-09 20:57:25 +00005025 NULL, 0, MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00005026 }
5027
5028 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5029}
5030
5031/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5032/// *INT_TO_FP operation of the specified operand when the target requests that
5033/// we promote it. At this point, we know that the result and operand types are
5034/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5035/// operation that takes a larger input.
5036SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5037 MVT::ValueType DestVT,
5038 bool isSigned) {
5039 // First step, figure out the appropriate *INT_TO_FP operation to use.
5040 MVT::ValueType NewInTy = LegalOp.getValueType();
5041
5042 unsigned OpToUse = 0;
5043
5044 // Scan for the appropriate larger type to use.
5045 while (1) {
5046 NewInTy = (MVT::ValueType)(NewInTy+1);
5047 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5048
5049 // If the target supports SINT_TO_FP of this type, use it.
5050 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5051 default: break;
5052 case TargetLowering::Legal:
5053 if (!TLI.isTypeLegal(NewInTy))
5054 break; // Can't use this datatype.
5055 // FALL THROUGH.
5056 case TargetLowering::Custom:
5057 OpToUse = ISD::SINT_TO_FP;
5058 break;
5059 }
5060 if (OpToUse) break;
5061 if (isSigned) continue;
5062
5063 // If the target supports UINT_TO_FP of this type, use it.
5064 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5065 default: break;
5066 case TargetLowering::Legal:
5067 if (!TLI.isTypeLegal(NewInTy))
5068 break; // Can't use this datatype.
5069 // FALL THROUGH.
5070 case TargetLowering::Custom:
5071 OpToUse = ISD::UINT_TO_FP;
5072 break;
5073 }
5074 if (OpToUse) break;
5075
5076 // Otherwise, try a larger type.
5077 }
5078
5079 // Okay, we found the operation and type to use. Zero extend our input to the
5080 // desired type then run the operation on it.
5081 return DAG.getNode(OpToUse, DestVT,
5082 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5083 NewInTy, LegalOp));
5084}
5085
5086/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5087/// FP_TO_*INT operation of the specified operand when the target requests that
5088/// we promote it. At this point, we know that the result and operand types are
5089/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5090/// operation that returns a larger result.
5091SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5092 MVT::ValueType DestVT,
5093 bool isSigned) {
5094 // First step, figure out the appropriate FP_TO*INT operation to use.
5095 MVT::ValueType NewOutTy = DestVT;
5096
5097 unsigned OpToUse = 0;
5098
5099 // Scan for the appropriate larger type to use.
5100 while (1) {
5101 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5102 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5103
5104 // If the target supports FP_TO_SINT returning this type, use it.
5105 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5106 default: break;
5107 case TargetLowering::Legal:
5108 if (!TLI.isTypeLegal(NewOutTy))
5109 break; // Can't use this datatype.
5110 // FALL THROUGH.
5111 case TargetLowering::Custom:
5112 OpToUse = ISD::FP_TO_SINT;
5113 break;
5114 }
5115 if (OpToUse) break;
5116
5117 // If the target supports FP_TO_UINT of this type, use it.
5118 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5119 default: break;
5120 case TargetLowering::Legal:
5121 if (!TLI.isTypeLegal(NewOutTy))
5122 break; // Can't use this datatype.
5123 // FALL THROUGH.
5124 case TargetLowering::Custom:
5125 OpToUse = ISD::FP_TO_UINT;
5126 break;
5127 }
5128 if (OpToUse) break;
5129
5130 // Otherwise, try a larger type.
5131 }
5132
5133 // Okay, we found the operation and type to use. Truncate the result of the
5134 // extended FP_TO_*INT operation to the desired size.
5135 return DAG.getNode(ISD::TRUNCATE, DestVT,
5136 DAG.getNode(OpToUse, NewOutTy, LegalOp));
5137}
5138
5139/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5140///
5141SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5142 MVT::ValueType VT = Op.getValueType();
5143 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5144 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5145 switch (VT) {
5146 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5147 case MVT::i16:
5148 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5149 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5150 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5151 case MVT::i32:
5152 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5153 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5154 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5155 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5156 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5157 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5158 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5159 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5160 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5161 case MVT::i64:
5162 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5163 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5164 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5165 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5166 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5167 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5168 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5169 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5170 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5171 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5172 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5173 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5174 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5175 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5176 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5177 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5178 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5179 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5180 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5181 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5182 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5183 }
5184}
5185
5186/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5187///
5188SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5189 switch (Opc) {
5190 default: assert(0 && "Cannot expand this yet!");
5191 case ISD::CTPOP: {
5192 static const uint64_t mask[6] = {
5193 0x5555555555555555ULL, 0x3333333333333333ULL,
5194 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5195 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5196 };
5197 MVT::ValueType VT = Op.getValueType();
5198 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005199 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005200 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5201 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5202 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5203 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5204 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5205 DAG.getNode(ISD::AND, VT,
5206 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5207 }
5208 return Op;
5209 }
5210 case ISD::CTLZ: {
5211 // for now, we do this:
5212 // x = x | (x >> 1);
5213 // x = x | (x >> 2);
5214 // ...
5215 // x = x | (x >>16);
5216 // x = x | (x >>32); // for 64-bit input
5217 // return popcount(~x);
5218 //
5219 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5220 MVT::ValueType VT = Op.getValueType();
5221 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005222 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005223 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5224 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5225 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5226 }
5227 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5228 return DAG.getNode(ISD::CTPOP, VT, Op);
5229 }
5230 case ISD::CTTZ: {
5231 // for now, we use: { return popcount(~x & (x - 1)); }
5232 // unless the target has ctlz but not ctpop, in which case we use:
5233 // { return 32 - nlz(~x & (x-1)); }
5234 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5235 MVT::ValueType VT = Op.getValueType();
5236 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5237 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5238 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5239 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5240 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5241 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5242 TLI.isOperationLegal(ISD::CTLZ, VT))
5243 return DAG.getNode(ISD::SUB, VT,
Dan Gohman1796f1f2007-05-18 17:52:13 +00005244 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00005245 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5246 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5247 }
5248 }
5249}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005250
Chris Lattnerdc750592005-01-07 07:47:09 +00005251/// ExpandOp - Expand the specified SDOperand into its two component pieces
5252/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5253/// LegalizeNodes map is filled in for any results that are not expanded, the
5254/// ExpandedNodes map is filled in for any results that are expanded, and the
5255/// Lo/Hi values are returned.
5256void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5257 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00005258 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00005259 SDNode *Node = Op.Val;
5260 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00005261 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohmana8665142007-06-25 16:23:39 +00005262 MVT::isVector(VT)) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00005263 "Cannot expand to FP value or to larger int value!");
5264
Chris Lattner1a570f12005-09-02 20:32:45 +00005265 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005266 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00005267 = ExpandedNodes.find(Op);
5268 if (I != ExpandedNodes.end()) {
5269 Lo = I->second.first;
5270 Hi = I->second.second;
5271 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00005272 }
5273
Chris Lattnerdc750592005-01-07 07:47:09 +00005274 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00005275 case ISD::CopyFromReg:
5276 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen666323e2007-10-10 01:01:31 +00005277 case ISD::FP_ROUND_INREG:
5278 if (VT == MVT::ppcf128 &&
5279 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5280 TargetLowering::Custom) {
Dale Johannesen6472eb62007-10-11 23:32:15 +00005281 SDOperand SrcLo, SrcHi, Src;
5282 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5283 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5284 SDOperand Result = TLI.LowerOperation(
5285 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen666323e2007-10-10 01:01:31 +00005286 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5287 Lo = Result.Val->getOperand(0);
5288 Hi = Result.Val->getOperand(1);
5289 break;
5290 }
5291 // fall through
Chris Lattner44cab002006-01-21 04:27:00 +00005292 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005293#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005294 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005295#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00005296 assert(0 && "Do not know how to expand this operator!");
5297 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00005298 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00005299 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00005300 Lo = DAG.getNode(ISD::UNDEF, NVT);
5301 Hi = DAG.getNode(ISD::UNDEF, NVT);
5302 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005303 case ISD::Constant: {
5304 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
5305 Lo = DAG.getConstant(Cst, NVT);
5306 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
5307 break;
5308 }
Evan Cheng47833a12006-12-12 21:32:44 +00005309 case ISD::ConstantFP: {
5310 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen007aa372007-10-11 18:07:22 +00005311 if (CFP->getValueType(0) == MVT::ppcf128) {
5312 APInt api = CFP->getValueAPF().convertToAPInt();
5313 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5314 MVT::f64);
5315 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5316 MVT::f64);
5317 break;
5318 }
Evan Cheng3766fc602006-12-12 22:19:28 +00005319 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00005320 if (getTypeAction(Lo.getValueType()) == Expand)
5321 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005322 break;
5323 }
Chris Lattner32e08b72005-03-28 22:03:13 +00005324 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005325 // Return the operands.
5326 Lo = Node->getOperand(0);
5327 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00005328 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005329
5330 case ISD::SIGN_EXTEND_INREG:
5331 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00005332 // sext_inreg the low part if needed.
5333 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5334
5335 // The high part gets the sign extension from the lo-part. This handles
5336 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005337 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5338 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5339 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005340 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00005341
Nate Begeman2fba8a32006-01-14 03:14:10 +00005342 case ISD::BSWAP: {
5343 ExpandOp(Node->getOperand(0), Lo, Hi);
5344 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5345 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5346 Lo = TempLo;
5347 break;
5348 }
5349
Chris Lattner55e9cde2005-05-11 04:51:16 +00005350 case ISD::CTPOP:
5351 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00005352 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5353 DAG.getNode(ISD::CTPOP, NVT, Lo),
5354 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00005355 Hi = DAG.getConstant(0, NVT);
5356 break;
5357
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005358 case ISD::CTLZ: {
5359 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005360 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005361 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5362 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005363 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
5364 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005365 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5366 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5367
5368 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5369 Hi = DAG.getConstant(0, NVT);
5370 break;
5371 }
5372
5373 case ISD::CTTZ: {
5374 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005375 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005376 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5377 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00005378 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
5379 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005380 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5381 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5382
5383 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5384 Hi = DAG.getConstant(0, NVT);
5385 break;
5386 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00005387
Nate Begemane74795c2006-01-25 18:21:52 +00005388 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005389 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5390 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00005391 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5392 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5393
5394 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005395 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00005396 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
5397 if (!TLI.isLittleEndian())
5398 std::swap(Lo, Hi);
5399 break;
5400 }
5401
Chris Lattnerdc750592005-01-07 07:47:09 +00005402 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005403 LoadSDNode *LD = cast<LoadSDNode>(Node);
5404 SDOperand Ch = LD->getChain(); // Legalize the chain.
5405 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5406 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman2af30632007-07-09 22:18:38 +00005407 int SVOffset = LD->getSrcValueOffset();
5408 unsigned Alignment = LD->getAlignment();
5409 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00005410
Evan Chenge71fe34d2006-10-09 20:57:25 +00005411 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman2af30632007-07-09 22:18:38 +00005412 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5413 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00005414 if (VT == MVT::f32 || VT == MVT::f64) {
5415 // f32->i32 or f64->i64 one to one expansion.
5416 // Remember that we legalized the chain.
5417 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00005418 // Recursively expand the new load.
5419 if (getTypeAction(NVT) == Expand)
5420 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005421 break;
5422 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00005423
Evan Chenge71fe34d2006-10-09 20:57:25 +00005424 // Increment the pointer to the other half.
5425 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5426 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
5427 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005428 SVOffset += IncrementSize;
Dan Gohman2af30632007-07-09 22:18:38 +00005429 if (Alignment > IncrementSize)
5430 Alignment = IncrementSize;
5431 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5432 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00005433
Evan Chenge71fe34d2006-10-09 20:57:25 +00005434 // Build a factor node to remember that this load is independent of the
5435 // other one.
5436 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5437 Hi.getValue(1));
5438
5439 // Remember that we legalized the chain.
5440 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
5441 if (!TLI.isLittleEndian())
5442 std::swap(Lo, Hi);
5443 } else {
Evan Chengd35734b2006-10-11 07:10:22 +00005444 MVT::ValueType EVT = LD->getLoadedVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00005445
5446 if (VT == MVT::f64 && EVT == MVT::f32) {
5447 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5448 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005449 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00005450 // Remember that we legalized the chain.
5451 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5452 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5453 break;
5454 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00005455
5456 if (EVT == NVT)
5457 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005458 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005459 else
5460 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005461 SVOffset, EVT, isVolatile,
5462 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00005463
5464 // Remember that we legalized the chain.
5465 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
5466
5467 if (ExtType == ISD::SEXTLOAD) {
5468 // The high part is obtained by SRA'ing all but one of the bits of the
5469 // lo part.
5470 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
5471 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5472 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
5473 } else if (ExtType == ISD::ZEXTLOAD) {
5474 // The high part is just a zero.
5475 Hi = DAG.getConstant(0, NVT);
5476 } else /* if (ExtType == ISD::EXTLOAD) */ {
5477 // The high part is undefined.
5478 Hi = DAG.getNode(ISD::UNDEF, NVT);
5479 }
5480 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005481 break;
5482 }
Chris Lattnerdc750592005-01-07 07:47:09 +00005483 case ISD::AND:
5484 case ISD::OR:
5485 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
5486 SDOperand LL, LH, RL, RH;
5487 ExpandOp(Node->getOperand(0), LL, LH);
5488 ExpandOp(Node->getOperand(1), RL, RH);
5489 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
5490 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
5491 break;
5492 }
5493 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005494 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00005495 ExpandOp(Node->getOperand(1), LL, LH);
5496 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00005497 if (getTypeAction(NVT) == Expand)
5498 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005499 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00005500 if (VT != MVT::f32)
5501 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00005502 break;
5503 }
Nate Begemane5b86d72005-08-10 20:51:12 +00005504 case ISD::SELECT_CC: {
5505 SDOperand TL, TH, FL, FH;
5506 ExpandOp(Node->getOperand(2), TL, TH);
5507 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00005508 if (getTypeAction(NVT) == Expand)
5509 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00005510 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5511 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00005512 if (VT != MVT::f32)
5513 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
5514 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00005515 break;
5516 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005517 case ISD::ANY_EXTEND:
5518 // The low part is any extension of the input (which degenerates to a copy).
5519 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
5520 // The high part is undefined.
5521 Hi = DAG.getNode(ISD::UNDEF, NVT);
5522 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005523 case ISD::SIGN_EXTEND: {
5524 // The low part is just a sign extension of the input (which degenerates to
5525 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005526 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005527
Chris Lattnerdc750592005-01-07 07:47:09 +00005528 // The high part is obtained by SRA'ing all but one of the bits of the lo
5529 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00005530 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005531 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5532 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00005533 break;
5534 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005535 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00005536 // The low part is just a zero extension of the input (which degenerates to
5537 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005538 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00005539
Chris Lattnerdc750592005-01-07 07:47:09 +00005540 // The high part is just a zero.
5541 Hi = DAG.getConstant(0, NVT);
5542 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00005543
Chris Lattner59b27fa372007-02-13 23:55:16 +00005544 case ISD::TRUNCATE: {
5545 // The input value must be larger than this value. Expand *it*.
5546 SDOperand NewLo;
5547 ExpandOp(Node->getOperand(0), NewLo, Hi);
5548
5549 // The low part is now either the right size, or it is closer. If not the
5550 // right size, make an illegal truncate so we recursively expand it.
5551 if (NewLo.getValueType() != Node->getValueType(0))
5552 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
5553 ExpandOp(NewLo, Lo, Hi);
5554 break;
5555 }
5556
Chris Lattner36e663d2005-12-23 00:16:34 +00005557 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005558 SDOperand Tmp;
5559 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
5560 // If the target wants to, allow it to lower this itself.
5561 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5562 case Expand: assert(0 && "cannot expand FP!");
5563 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
5564 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
5565 }
5566 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
5567 }
5568
Evan Cheng4eee7242006-12-09 02:42:38 +00005569 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00005570 if (VT == MVT::f32 || VT == MVT::f64) {
5571 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00005572 if (getTypeAction(NVT) == Expand)
5573 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00005574 break;
5575 }
5576
5577 // If source operand will be expanded to the same type as VT, i.e.
5578 // i64 <- f64, i32 <- f32, expand the source operand instead.
5579 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
5580 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
5581 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005582 break;
5583 }
5584
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005585 // Turn this into a load/store pair by default.
5586 if (Tmp.Val == 0)
Evan Cheng3432ab92006-12-11 19:27:14 +00005587 Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0));
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00005588
Chris Lattner36e663d2005-12-23 00:16:34 +00005589 ExpandOp(Tmp, Lo, Hi);
5590 break;
5591 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005592
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005593 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00005594 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
5595 TargetLowering::Custom &&
5596 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005597 Lo = TLI.LowerOperation(Op, DAG);
5598 assert(Lo.Val && "Node must be custom expanded!");
5599 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00005600 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005601 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00005602 break;
5603
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005604 // These operators cannot be expanded directly, emit them as calls to
5605 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005606 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005607 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00005608 SDOperand Op;
5609 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5610 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005611 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5612 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00005613 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005614
Chris Lattner941d84a2005-07-30 01:40:57 +00005615 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
5616
Chris Lattnerfe68d752005-07-29 00:33:32 +00005617 // Now that the custom expander is done, expand the result, which is still
5618 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005619 if (Op.Val) {
5620 ExpandOp(Op, Lo, Hi);
5621 break;
5622 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005623 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005624
Dale Johannesenc0154c02007-10-05 20:04:43 +00005625 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005626 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005627 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00005628 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005629 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00005630 else if (Node->getOperand(0).getValueType() == MVT::f80)
5631 LC = RTLIB::FPTOSINT_F80_I64;
5632 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5633 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005634 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5635 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005636 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005637 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005638
Evan Cheng31cbddf2007-01-12 02:11:51 +00005639 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00005640 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005641 SDOperand Op;
5642 switch (getTypeAction(Node->getOperand(0).getValueType())) {
5643 case Expand: assert(0 && "cannot expand FP!");
5644 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
5645 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
5646 }
5647
5648 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
5649
5650 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005651 if (Op.Val) {
5652 ExpandOp(Op, Lo, Hi);
5653 break;
5654 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00005655 }
Jeff Cohen546fd592005-07-30 18:33:25 +00005656
Evan Chengfd11ef42007-10-05 01:09:32 +00005657 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005658 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005659 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen789b5a52007-09-28 18:44:17 +00005660 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00005661 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00005662 else if (Node->getOperand(0).getValueType() == MVT::f80)
5663 LC = RTLIB::FPTOUINT_F80_I64;
5664 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
5665 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005666 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
5667 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005668 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005669 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00005670
Evan Cheng870e4f82006-01-09 18:31:59 +00005671 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005672 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005673 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005674 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005675 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005676 Op = TLI.LowerOperation(Op, DAG);
5677 if (Op.Val) {
5678 // Now that the custom expander is done, expand the result, which is
5679 // still VT.
5680 ExpandOp(Op, Lo, Hi);
5681 break;
5682 }
5683 }
5684
Chris Lattner72b503b2006-09-13 03:50:39 +00005685 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
5686 // this X << 1 as X+X.
5687 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
5688 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
5689 TLI.isOperationLegal(ISD::ADDE, NVT)) {
5690 SDOperand LoOps[2], HiOps[3];
5691 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
5692 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
5693 LoOps[1] = LoOps[0];
5694 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5695
5696 HiOps[1] = HiOps[0];
5697 HiOps[2] = Lo.getValue(1);
5698 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5699 break;
5700 }
5701 }
5702
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005703 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005704 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005705 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005706
5707 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005708 TargetLowering::LegalizeAction Action =
5709 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
5710 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5711 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005712 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005713 break;
5714 }
5715
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005716 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005717 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
5718 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005719 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005720 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005721
Evan Cheng870e4f82006-01-09 18:31:59 +00005722 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005723 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005724 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005725 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005726 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005727 Op = TLI.LowerOperation(Op, DAG);
5728 if (Op.Val) {
5729 // Now that the custom expander is done, expand the result, which is
5730 // still VT.
5731 ExpandOp(Op, Lo, Hi);
5732 break;
5733 }
5734 }
5735
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005736 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005737 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005738 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005739
5740 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005741 TargetLowering::LegalizeAction Action =
5742 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
5743 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5744 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005745 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005746 break;
5747 }
5748
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005749 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005750 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
5751 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005752 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005753 }
5754
5755 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005756 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00005757 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005758 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005759 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00005760 Op = TLI.LowerOperation(Op, DAG);
5761 if (Op.Val) {
5762 // Now that the custom expander is done, expand the result, which is
5763 // still VT.
5764 ExpandOp(Op, Lo, Hi);
5765 break;
5766 }
5767 }
5768
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005769 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00005770 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005771 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00005772
5773 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00005774 TargetLowering::LegalizeAction Action =
5775 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
5776 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
5777 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00005778 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00005779 break;
5780 }
5781
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005782 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005783 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
5784 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005785 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00005786 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005787
Misha Brukman835702a2005-04-21 22:36:52 +00005788 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005789 case ISD::SUB: {
5790 // If the target wants to custom expand this, let them.
5791 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
5792 TargetLowering::Custom) {
5793 Op = TLI.LowerOperation(Op, DAG);
5794 if (Op.Val) {
5795 ExpandOp(Op, Lo, Hi);
5796 break;
5797 }
5798 }
5799
5800 // Expand the subcomponents.
5801 SDOperand LHSL, LHSH, RHSL, RHSH;
5802 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5803 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00005804 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5805 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005806 LoOps[0] = LHSL;
5807 LoOps[1] = RHSL;
5808 HiOps[0] = LHSH;
5809 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00005810 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00005811 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005812 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005813 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005814 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00005815 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005816 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00005817 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00005818 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00005819 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005820 }
Chris Lattner2135bc02007-05-17 18:15:41 +00005821
5822 case ISD::ADDC:
5823 case ISD::SUBC: {
5824 // Expand the subcomponents.
5825 SDOperand LHSL, LHSH, RHSL, RHSH;
5826 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5827 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5828 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5829 SDOperand LoOps[2] = { LHSL, RHSL };
5830 SDOperand HiOps[3] = { LHSH, RHSH };
5831
5832 if (Node->getOpcode() == ISD::ADDC) {
5833 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
5834 HiOps[2] = Lo.getValue(1);
5835 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
5836 } else {
5837 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
5838 HiOps[2] = Lo.getValue(1);
5839 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
5840 }
5841 // Remember that we legalized the flag.
5842 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5843 break;
5844 }
5845 case ISD::ADDE:
5846 case ISD::SUBE: {
5847 // Expand the subcomponents.
5848 SDOperand LHSL, LHSH, RHSL, RHSH;
5849 ExpandOp(Node->getOperand(0), LHSL, LHSH);
5850 ExpandOp(Node->getOperand(1), RHSL, RHSH);
5851 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
5852 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
5853 SDOperand HiOps[3] = { LHSH, RHSH };
5854
5855 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
5856 HiOps[2] = Lo.getValue(1);
5857 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
5858
5859 // Remember that we legalized the flag.
5860 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
5861 break;
5862 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005863 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005864 // If the target wants to custom expand this, let them.
5865 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00005866 SDOperand New = TLI.LowerOperation(Op, DAG);
5867 if (New.Val) {
5868 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00005869 break;
5870 }
5871 }
5872
Evan Chenge93762d2006-09-01 18:17:58 +00005873 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
5874 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohmana1603612007-10-08 18:33:35 +00005875 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
5876 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
5877 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanadd0c632005-04-11 03:01:51 +00005878 SDOperand LL, LH, RL, RH;
5879 ExpandOp(Node->getOperand(0), LL, LH);
5880 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohmana1603612007-10-08 18:33:35 +00005881 unsigned BitSize = MVT::getSizeInBits(RH.getValueType());
5882 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
5883 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
5884 // FIXME: generalize this to handle other bit sizes
5885 if (LHSSB == 32 && RHSSB == 32 &&
5886 DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) &&
5887 DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) {
5888 // The inputs are both zero-extended.
5889 if (HasUMUL_LOHI) {
5890 // We can emit a umul_lohi.
5891 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5892 Hi = SDOperand(Lo.Val, 1);
5893 break;
5894 }
5895 if (HasMULHU) {
5896 // We can emit a mulhu+mul.
5897 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5898 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
5899 break;
5900 }
Dan Gohmana1603612007-10-08 18:33:35 +00005901 }
5902 if (LHSSB > BitSize && RHSSB > BitSize) {
5903 // The input values are both sign-extended.
5904 if (HasSMUL_LOHI) {
5905 // We can emit a smul_lohi.
5906 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
5907 Hi = SDOperand(Lo.Val, 1);
5908 break;
5909 }
5910 if (HasMULHS) {
5911 // We can emit a mulhs+mul.
5912 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
5913 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
5914 break;
5915 }
5916 }
5917 if (HasUMUL_LOHI) {
5918 // Lo,Hi = umul LHS, RHS.
5919 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
5920 DAG.getVTList(NVT, NVT), LL, RL);
5921 Lo = UMulLOHI;
5922 Hi = UMulLOHI.getValue(1);
Nate Begeman43144a22005-08-30 02:44:00 +00005923 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
5924 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
5925 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
5926 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00005927 break;
Nate Begeman43144a22005-08-30 02:44:00 +00005928 }
Nate Begemanadd0c632005-04-11 03:01:51 +00005929 }
Evan Chenge93762d2006-09-01 18:17:58 +00005930
Dan Gohmana1603612007-10-08 18:33:35 +00005931 // If nothing else, we can make a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00005932 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
5933 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00005934 break;
5935 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005936 case ISD::SDIV:
5937 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
5938 break;
5939 case ISD::UDIV:
5940 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
5941 break;
5942 case ISD::SREM:
5943 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
5944 break;
5945 case ISD::UREM:
5946 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
5947 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00005948
Evan Cheng4eee7242006-12-09 02:42:38 +00005949 case ISD::FADD:
Dale Johannesenc0154c02007-10-05 20:04:43 +00005950 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::ADD_F32 :
5951 VT == MVT::f64 ? RTLIB::ADD_F64 :
5952 VT == MVT::ppcf128 ?
5953 RTLIB::ADD_PPCF128 :
5954 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00005955 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005956 break;
5957 case ISD::FSUB:
Dale Johannesenc0154c02007-10-05 20:04:43 +00005958 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::SUB_F32 :
5959 VT == MVT::f64 ? RTLIB::SUB_F64 :
5960 VT == MVT::ppcf128 ?
5961 RTLIB::SUB_PPCF128 :
5962 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00005963 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005964 break;
5965 case ISD::FMUL:
Dale Johannesenc0154c02007-10-05 20:04:43 +00005966 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::MUL_F32 :
5967 VT == MVT::f64 ? RTLIB::MUL_F64 :
5968 VT == MVT::ppcf128 ?
5969 RTLIB::MUL_PPCF128 :
5970 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00005971 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005972 break;
5973 case ISD::FDIV:
Dale Johannesenc0154c02007-10-05 20:04:43 +00005974 Lo = ExpandLibCall(TLI.getLibcallName(VT == MVT::f32 ? RTLIB::DIV_F32 :
5975 VT == MVT::f64 ? RTLIB::DIV_F64 :
5976 VT == MVT::ppcf128 ?
5977 RTLIB::DIV_PPCF128 :
5978 RTLIB::UNKNOWN_LIBCALL),
Evan Cheng31cbddf2007-01-12 02:11:51 +00005979 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005980 break;
5981 case ISD::FP_EXTEND:
Dale Johannesen05ff9e82007-10-12 01:37:08 +00005982 if (VT == MVT::ppcf128) {
5983 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
5984 Node->getOperand(0).getValueType()==MVT::f64);
5985 const uint64_t zero = 0;
5986 if (Node->getOperand(0).getValueType()==MVT::f32)
5987 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
5988 else
5989 Hi = Node->getOperand(0);
5990 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
5991 break;
5992 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00005993 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005994 break;
5995 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00005996 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00005997 break;
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00005998 case ISD::FPOWI:
Dale Johannesen25a00a62007-09-28 01:08:20 +00005999 Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) ? RTLIB::POWI_F32 :
6000 (VT == MVT::f64) ? RTLIB::POWI_F64 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00006001 (VT == MVT::f80) ? RTLIB::POWI_F80 :
6002 (VT == MVT::ppcf128) ?
6003 RTLIB::POWI_PPCF128 :
6004 RTLIB::UNKNOWN_LIBCALL),
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006005 Node, false, Hi);
6006 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006007 case ISD::FSQRT:
6008 case ISD::FSIN:
6009 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006010 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00006011 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006012 case ISD::FSQRT:
Dale Johannesen25a00a62007-09-28 01:08:20 +00006013 LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 :
Dale Johannesenc0154c02007-10-05 20:04:43 +00006014 (VT == MVT::f64) ? RTLIB::SQRT_F64 :
6015 (VT == MVT::f80) ? RTLIB::SQRT_F80 :
6016 (VT == MVT::ppcf128) ? RTLIB::SQRT_PPCF128 :
6017 RTLIB::UNKNOWN_LIBCALL;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006018 break;
6019 case ISD::FSIN:
6020 LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64;
6021 break;
6022 case ISD::FCOS:
6023 LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64;
6024 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006025 default: assert(0 && "Unreachable!");
6026 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006027 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00006028 break;
6029 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006030 case ISD::FABS: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006031 if (VT == MVT::ppcf128) {
6032 SDOperand Tmp;
6033 ExpandOp(Node->getOperand(0), Lo, Tmp);
6034 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6035 // lo = hi==fabs(hi) ? lo : -lo;
6036 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6037 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6038 DAG.getCondCode(ISD::SETEQ));
6039 break;
6040 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006041 SDOperand Mask = (VT == MVT::f64)
6042 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6043 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6044 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6045 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6046 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6047 if (getTypeAction(NVT) == Expand)
6048 ExpandOp(Lo, Lo, Hi);
6049 break;
6050 }
6051 case ISD::FNEG: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006052 if (VT == MVT::ppcf128) {
6053 ExpandOp(Node->getOperand(0), Lo, Hi);
6054 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6055 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6056 break;
6057 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006058 SDOperand Mask = (VT == MVT::f64)
6059 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6060 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6061 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6062 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6063 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6064 if (getTypeAction(NVT) == Expand)
6065 ExpandOp(Lo, Lo, Hi);
6066 break;
6067 }
Evan Cheng003feb02007-01-04 21:56:39 +00006068 case ISD::FCOPYSIGN: {
6069 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6070 if (getTypeAction(NVT) == Expand)
6071 ExpandOp(Lo, Lo, Hi);
6072 break;
6073 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006074 case ISD::SINT_TO_FP:
6075 case ISD::UINT_TO_FP: {
6076 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6077 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006078 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006079 static uint64_t zero = 0;
6080 if (isSigned) {
6081 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6082 Node->getOperand(0)));
6083 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6084 } else {
6085 static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
6086 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6087 Node->getOperand(0)));
6088 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6089 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006090 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006091 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6092 DAG.getConstant(0, MVT::i32),
6093 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6094 DAG.getConstantFP(
6095 APFloat(APInt(128, 2, TwoE32)),
6096 MVT::ppcf128)),
6097 Hi,
6098 DAG.getCondCode(ISD::SETLT)),
6099 Lo, Hi);
6100 }
6101 break;
6102 }
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006103 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6104 // si64->ppcf128 done by libcall, below
6105 static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
6106 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6107 Lo, Hi);
6108 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6109 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6110 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6111 DAG.getConstant(0, MVT::i64),
6112 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6113 DAG.getConstantFP(
6114 APFloat(APInt(128, 2, TwoE64)),
6115 MVT::ppcf128)),
6116 Hi,
6117 DAG.getCondCode(ISD::SETLT)),
6118 Lo, Hi);
6119 break;
6120 }
Evan Cheng75439b32007-09-27 07:35:39 +00006121 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006122 if (Node->getOperand(0).getValueType() == MVT::i64) {
6123 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006124 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006125 else if (VT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006126 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00006127 else if (VT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00006128 assert(isSigned);
Dale Johannesenc0154c02007-10-05 20:04:43 +00006129 LC = RTLIB::SINTTOFP_I64_F80;
6130 }
6131 else if (VT == MVT::ppcf128) {
6132 assert(isSigned);
6133 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006134 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006135 } else {
6136 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006137 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006138 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00006139 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006140 }
6141
6142 // Promote the operand if needed.
6143 if (getTypeAction(SrcVT) == Promote) {
6144 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6145 Tmp = isSigned
6146 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6147 DAG.getValueType(SrcVT))
6148 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6149 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6150 }
Evan Chengbf535fc2007-04-27 07:33:31 +00006151
6152 const char *LibCall = TLI.getLibcallName(LC);
6153 if (LibCall)
6154 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6155 else {
6156 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6157 Node->getOperand(0));
6158 if (getTypeAction(Lo.getValueType()) == Expand)
6159 ExpandOp(Lo, Lo, Hi);
6160 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006161 break;
6162 }
Evan Chengf3a80c62006-12-13 02:38:13 +00006163 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006164
Chris Lattnerac12f682005-12-21 18:02:52 +00006165 // Make sure the resultant values have been legalized themselves, unless this
6166 // is a type that requires multi-step expansion.
6167 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6168 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00006169 if (Hi.Val)
6170 // Don't legalize the high part if it is expanded to a single node.
6171 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00006172 }
Evan Cheng870e4f82006-01-09 18:31:59 +00006173
6174 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006175 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00006176 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00006177}
6178
Dan Gohmana8665142007-06-25 16:23:39 +00006179/// SplitVectorOp - Given an operand of vector type, break it down into
6180/// two smaller values, still of vector type.
Chris Lattner32206f52006-03-18 01:44:44 +00006181void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6182 SDOperand &Hi) {
Dan Gohmana8665142007-06-25 16:23:39 +00006183 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00006184 SDNode *Node = Op.Val;
Dan Gohman60028182007-09-24 15:54:53 +00006185 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattner32206f52006-03-18 01:44:44 +00006186 assert(NumElements > 1 && "Cannot split a single element vector!");
6187 unsigned NewNumElts = NumElements/2;
Dan Gohman60028182007-09-24 15:54:53 +00006188 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Dan Gohmana8665142007-06-25 16:23:39 +00006189 MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts);
Chris Lattner32206f52006-03-18 01:44:44 +00006190
6191 // See if we already split it.
6192 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6193 = SplitNodes.find(Op);
6194 if (I != SplitNodes.end()) {
6195 Lo = I->second.first;
6196 Hi = I->second.second;
6197 return;
6198 }
6199
6200 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006201 default:
6202#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006203 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006204#endif
6205 assert(0 && "Unhandled operation in SplitVectorOp!");
Dan Gohmana8665142007-06-25 16:23:39 +00006206 case ISD::BUILD_PAIR:
6207 Lo = Node->getOperand(0);
6208 Hi = Node->getOperand(1);
6209 break;
Dan Gohmana90183e2007-09-28 23:53:40 +00006210 case ISD::INSERT_VECTOR_ELT: {
6211 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6212 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6213 SDOperand ScalarOp = Node->getOperand(1);
6214 if (Index < NewNumElts)
6215 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Lo, ScalarOp,
6216 DAG.getConstant(Index, TLI.getPointerTy()));
6217 else
6218 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Hi, ScalarOp,
6219 DAG.getConstant(Index - NewNumElts, TLI.getPointerTy()));
6220 break;
6221 }
Dan Gohmana8665142007-06-25 16:23:39 +00006222 case ISD::BUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006223 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6224 Node->op_begin()+NewNumElts);
Dan Gohmana8665142007-06-25 16:23:39 +00006225 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006226
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006227 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts,
Dan Gohmana8665142007-06-25 16:23:39 +00006228 Node->op_end());
6229 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006230 break;
6231 }
Dan Gohmana8665142007-06-25 16:23:39 +00006232 case ISD::CONCAT_VECTORS: {
6233 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6234 if (NewNumSubvectors == 1) {
6235 Lo = Node->getOperand(0);
6236 Hi = Node->getOperand(1);
6237 } else {
6238 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6239 Node->op_begin()+NewNumSubvectors);
6240 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00006241
Dan Gohmana8665142007-06-25 16:23:39 +00006242 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6243 Node->op_end());
6244 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size());
6245 }
Dan Gohman26455c42007-06-13 15:12:02 +00006246 break;
6247 }
Dan Gohman8f518b982007-10-17 14:48:28 +00006248 case ISD::SELECT: {
6249 SDOperand Cond = Node->getOperand(0);
6250
6251 SDOperand LL, LH, RL, RH;
6252 SplitVectorOp(Node->getOperand(1), LL, LH);
6253 SplitVectorOp(Node->getOperand(2), RL, RH);
6254
6255 if (MVT::isVector(Cond.getValueType())) {
6256 // Handle a vector merge.
6257 SDOperand CL, CH;
6258 SplitVectorOp(Cond, CL, CH);
6259 Lo = DAG.getNode(Node->getOpcode(), NewVT, CL, LL, RL);
6260 Hi = DAG.getNode(Node->getOpcode(), NewVT, CH, LH, RH);
6261 } else {
6262 // Handle a simple select with vector operands.
6263 Lo = DAG.getNode(Node->getOpcode(), NewVT, Cond, LL, RL);
6264 Hi = DAG.getNode(Node->getOpcode(), NewVT, Cond, LH, RH);
6265 }
6266 break;
6267 }
Dan Gohmana8665142007-06-25 16:23:39 +00006268 case ISD::ADD:
6269 case ISD::SUB:
6270 case ISD::MUL:
6271 case ISD::FADD:
6272 case ISD::FSUB:
6273 case ISD::FMUL:
6274 case ISD::SDIV:
6275 case ISD::UDIV:
6276 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00006277 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006278 case ISD::AND:
6279 case ISD::OR:
6280 case ISD::XOR: {
Chris Lattner32206f52006-03-18 01:44:44 +00006281 SDOperand LL, LH, RL, RH;
6282 SplitVectorOp(Node->getOperand(0), LL, LH);
6283 SplitVectorOp(Node->getOperand(1), RL, RH);
6284
Dan Gohmana8665142007-06-25 16:23:39 +00006285 Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL);
6286 Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00006287 break;
6288 }
Dan Gohman2a7de412007-10-11 23:57:53 +00006289 case ISD::FPOWI: {
6290 SDOperand L, H;
6291 SplitVectorOp(Node->getOperand(0), L, H);
6292
6293 Lo = DAG.getNode(Node->getOpcode(), NewVT, L, Node->getOperand(1));
6294 Hi = DAG.getNode(Node->getOpcode(), NewVT, H, Node->getOperand(1));
6295 break;
6296 }
6297 case ISD::CTTZ:
6298 case ISD::CTLZ:
6299 case ISD::CTPOP:
6300 case ISD::FNEG:
6301 case ISD::FABS:
6302 case ISD::FSQRT:
6303 case ISD::FSIN:
6304 case ISD::FCOS: {
6305 SDOperand L, H;
6306 SplitVectorOp(Node->getOperand(0), L, H);
6307
6308 Lo = DAG.getNode(Node->getOpcode(), NewVT, L);
6309 Hi = DAG.getNode(Node->getOpcode(), NewVT, H);
6310 break;
6311 }
Dan Gohmana8665142007-06-25 16:23:39 +00006312 case ISD::LOAD: {
6313 LoadSDNode *LD = cast<LoadSDNode>(Node);
6314 SDOperand Ch = LD->getChain();
6315 SDOperand Ptr = LD->getBasePtr();
6316 const Value *SV = LD->getSrcValue();
6317 int SVOffset = LD->getSrcValueOffset();
6318 unsigned Alignment = LD->getAlignment();
6319 bool isVolatile = LD->isVolatile();
6320
6321 Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6322 unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattner32206f52006-03-18 01:44:44 +00006323 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
6324 getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00006325 SVOffset += IncrementSize;
6326 if (Alignment > IncrementSize)
6327 Alignment = IncrementSize;
6328 Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00006329
6330 // Build a factor node to remember that this load is independent of the
6331 // other one.
6332 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6333 Hi.getValue(1));
6334
6335 // Remember that we legalized the chain.
6336 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00006337 break;
6338 }
Dan Gohmana8665142007-06-25 16:23:39 +00006339 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006340 // We know the result is a vector. The input may be either a vector or a
6341 // scalar value.
Dan Gohman0de76942007-06-29 00:09:08 +00006342 SDOperand InOp = Node->getOperand(0);
6343 if (!MVT::isVector(InOp.getValueType()) ||
6344 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6345 // The input is a scalar or single-element vector.
6346 // Lower to a store/load so that it can be split.
6347 // FIXME: this could be improved probably.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00006348 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006349
Evan Chengdf9ac472006-10-05 23:01:46 +00006350 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman0de76942007-06-29 00:09:08 +00006351 InOp, Ptr, NULL, 0);
6352 InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006353 }
Dan Gohman0de76942007-06-29 00:09:08 +00006354 // Split the vector and convert each of the pieces now.
6355 SplitVectorOp(InOp, Lo, Hi);
6356 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo);
6357 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi);
6358 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006359 }
Chris Lattner32206f52006-03-18 01:44:44 +00006360 }
6361
6362 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006363 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00006364 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00006365 assert(isNew && "Value already split?!?");
Chris Lattner32206f52006-03-18 01:44:44 +00006366}
6367
6368
Dan Gohmanf4e86da2007-06-27 14:06:22 +00006369/// ScalarizeVectorOp - Given an operand of single-element vector type
6370/// (e.g. v1f32), convert it into the equivalent operation that returns a
6371/// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +00006372SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6373 assert(MVT::isVector(Op.getValueType()) &&
6374 "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00006375 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00006376 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6377 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00006378
Dan Gohmana8665142007-06-25 16:23:39 +00006379 // See if we already scalarized it.
6380 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6381 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00006382
6383 SDOperand Result;
6384 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00006385 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006386#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006387 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006388#endif
Dan Gohmana8665142007-06-25 16:23:39 +00006389 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
6390 case ISD::ADD:
6391 case ISD::FADD:
6392 case ISD::SUB:
6393 case ISD::FSUB:
6394 case ISD::MUL:
6395 case ISD::FMUL:
6396 case ISD::SDIV:
6397 case ISD::UDIV:
6398 case ISD::FDIV:
6399 case ISD::SREM:
6400 case ISD::UREM:
6401 case ISD::FREM:
Dan Gohman2a7de412007-10-11 23:57:53 +00006402 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006403 case ISD::AND:
6404 case ISD::OR:
6405 case ISD::XOR:
6406 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00006407 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00006408 ScalarizeVectorOp(Node->getOperand(0)),
6409 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00006410 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006411 case ISD::FNEG:
6412 case ISD::FABS:
6413 case ISD::FSQRT:
6414 case ISD::FSIN:
6415 case ISD::FCOS:
6416 Result = DAG.getNode(Node->getOpcode(),
6417 NewVT,
6418 ScalarizeVectorOp(Node->getOperand(0)));
6419 break;
Dan Gohman4f056f32007-10-12 14:13:46 +00006420 case ISD::FPOWI:
6421 Result = DAG.getNode(Node->getOpcode(),
6422 NewVT,
6423 ScalarizeVectorOp(Node->getOperand(0)),
6424 Node->getOperand(1));
6425 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006426 case ISD::LOAD: {
6427 LoadSDNode *LD = cast<LoadSDNode>(Node);
6428 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
6429 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00006430
Dan Gohmana8665142007-06-25 16:23:39 +00006431 const Value *SV = LD->getSrcValue();
6432 int SVOffset = LD->getSrcValueOffset();
6433 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
6434 LD->isVolatile(), LD->getAlignment());
6435
Chris Lattner32206f52006-03-18 01:44:44 +00006436 // Remember that we legalized the chain.
6437 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
6438 break;
6439 }
Dan Gohmana8665142007-06-25 16:23:39 +00006440 case ISD::BUILD_VECTOR:
6441 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00006442 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006443 case ISD::INSERT_VECTOR_ELT:
6444 // Returning the inserted scalar element.
6445 Result = Node->getOperand(1);
6446 break;
6447 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00006448 assert(Node->getOperand(0).getValueType() == NewVT &&
6449 "Concat of non-legal vectors not yet supported!");
6450 Result = Node->getOperand(0);
6451 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006452 case ISD::VECTOR_SHUFFLE: {
6453 // Figure out if the scalar is the LHS or RHS and return it.
6454 SDOperand EltNum = Node->getOperand(2).getOperand(0);
6455 if (cast<ConstantSDNode>(EltNum)->getValue())
6456 Result = ScalarizeVectorOp(Node->getOperand(1));
6457 else
6458 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00006459 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006460 }
6461 case ISD::EXTRACT_SUBVECTOR:
6462 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00006463 assert(Result.getValueType() == NewVT);
6464 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006465 case ISD::BIT_CONVERT:
6466 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattnerf6f94d32006-03-28 20:24:43 +00006467 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006468 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00006469 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00006470 ScalarizeVectorOp(Op.getOperand(1)),
6471 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00006472 break;
Chris Lattner32206f52006-03-18 01:44:44 +00006473 }
6474
6475 if (TLI.isTypeLegal(NewVT))
6476 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00006477 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
6478 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00006479 return Result;
6480}
6481
Chris Lattnerdc750592005-01-07 07:47:09 +00006482
6483// SelectionDAG::Legalize - This is the entry point for the file.
6484//
Chris Lattner4add7e32005-01-23 04:42:50 +00006485void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00006486 if (ViewLegalizeDAGs) viewGraph();
6487
Chris Lattnerdc750592005-01-07 07:47:09 +00006488 /// run - This is the main entry point to this class.
6489 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00006490 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00006491}
6492