blob: abd569b208edaa9ae6a217bfd1d5d4f475f06616 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey70323a82006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman2d489b52008-02-06 22:27:42 +000019#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng631ccc62007-08-16 23:50:06 +000020#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000022#include "llvm/Target/TargetData.h"
Evan Cheng84a28d42006-10-30 08:00:44 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000025#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000026#include "llvm/Constants.h"
Reid Spencera94d3942007-01-19 21:13:56 +000027#include "llvm/DerivedTypes.h"
Chris Lattneref598052006-04-02 03:07:27 +000028#include "llvm/Support/CommandLine.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000029#include "llvm/Support/Compiler.h"
Duncan Sands1826ded2007-10-28 12:59:45 +000030#include "llvm/Support/MathExtras.h"
Chris Lattnere83030b2007-02-03 01:12:36 +000031#include "llvm/ADT/DenseMap.h"
Chris Lattner97af9d52006-08-08 01:09:31 +000032#include "llvm/ADT/SmallVector.h"
Chris Lattnerebeb48d2007-02-04 00:27:56 +000033#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng1d2e9952006-03-24 01:17:21 +000034#include <map>
Chris Lattnerdc750592005-01-07 07:47:09 +000035using namespace llvm;
36
Chris Lattneref598052006-04-02 03:07:27 +000037#ifndef NDEBUG
38static cl::opt<bool>
39ViewLegalizeDAGs("view-legalize-dags", cl::Hidden,
40 cl::desc("Pop up a window to show dags before legalize"));
41#else
42static const bool ViewLegalizeDAGs = 0;
43#endif
44
Chris Lattnerdc750592005-01-07 07:47:09 +000045//===----------------------------------------------------------------------===//
46/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
47/// hacks on it until the target machine can handle it. This involves
48/// eliminating value sizes the machine cannot handle (promoting small sizes to
49/// large sizes or splitting up large values into small values) as well as
50/// eliminating operations the machine cannot handle.
51///
52/// This code also does a small amount of optimization and recognition of idioms
53/// as part of its processing. For example, if a target does not support a
54/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
55/// will attempt merge setcc and brc instructions into brcc's.
56///
57namespace {
Chris Lattner54a34cd2006-06-28 21:58:30 +000058class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattnerdc750592005-01-07 07:47:09 +000059 TargetLowering &TLI;
60 SelectionDAG &DAG;
61
Chris Lattner462505f2006-02-13 09:18:02 +000062 // Libcall insertion helpers.
63
64 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
65 /// legalized. We use this to ensure that calls are properly serialized
66 /// against each other, including inserted libcalls.
67 SDOperand LastCALLSEQ_END;
68
69 /// IsLegalizingCall - This member is used *only* for purposes of providing
70 /// helpful assertions that a libcall isn't created while another call is
71 /// being legalized (which could lead to non-serialized call sequences).
72 bool IsLegalizingCall;
73
Chris Lattnerdc750592005-01-07 07:47:09 +000074 enum LegalizeAction {
Chris Lattner2c748af2006-01-29 08:42:06 +000075 Legal, // The target natively supports this operation.
76 Promote, // This operation should be executed in a larger type.
Chris Lattneraa2372562006-05-24 17:04:05 +000077 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattnerdc750592005-01-07 07:47:09 +000078 };
Chris Lattner462505f2006-02-13 09:18:02 +000079
Chris Lattnerdc750592005-01-07 07:47:09 +000080 /// ValueTypeActions - This is a bitvector that contains two bits for each
81 /// value type, where the two bits correspond to the LegalizeAction enum.
82 /// This can be queried with "getTypeAction(VT)".
Chris Lattner2c748af2006-01-29 08:42:06 +000083 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000084
Chris Lattnerdc750592005-01-07 07:47:09 +000085 /// LegalizedNodes - For nodes that are of legal width, and that have more
86 /// than one use, this map indicates what regularized operand to use. This
87 /// allows us to avoid legalizing the same thing more than once.
Chris Lattnered39c862007-02-04 00:50:02 +000088 DenseMap<SDOperand, SDOperand> LegalizedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000089
Chris Lattner1f2c9d82005-01-15 05:21:40 +000090 /// PromotedNodes - For nodes that are below legal width, and that have more
91 /// than one use, this map indicates what promoted value to use. This allows
92 /// us to avoid promoting the same thing more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +000093 DenseMap<SDOperand, SDOperand> PromotedNodes;
Chris Lattner1f2c9d82005-01-15 05:21:40 +000094
Chris Lattner32206f52006-03-18 01:44:44 +000095 /// ExpandedNodes - For nodes that need to be expanded this map indicates
96 /// which which operands are the expanded version of the input. This allows
97 /// us to avoid expanding the same node more than once.
Chris Lattner4b0ddb22007-02-04 01:17:38 +000098 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
Chris Lattnerdc750592005-01-07 07:47:09 +000099
Chris Lattner32206f52006-03-18 01:44:44 +0000100 /// SplitNodes - For vector nodes that need to be split, this map indicates
101 /// which which operands are the split version of the input. This allows us
102 /// to avoid splitting the same node more than once.
103 std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes;
104
Dan Gohmana8665142007-06-25 16:23:39 +0000105 /// ScalarizedNodes - For nodes that need to be converted from vector types to
106 /// scalar types, this contains the mapping of ones we have already
Chris Lattner32206f52006-03-18 01:44:44 +0000107 /// processed to the result.
Dan Gohmana8665142007-06-25 16:23:39 +0000108 std::map<SDOperand, SDOperand> ScalarizedNodes;
Chris Lattner32206f52006-03-18 01:44:44 +0000109
Chris Lattnerea4ca942005-01-07 22:28:47 +0000110 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +0000111 LegalizedNodes.insert(std::make_pair(From, To));
112 // If someone requests legalization of the new node, return itself.
113 if (From != To)
114 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000115 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000116 void AddPromotedOperand(SDOperand From, SDOperand To) {
Chris Lattner4b0ddb22007-02-04 01:17:38 +0000117 bool isNew = PromotedNodes.insert(std::make_pair(From, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000118 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +0000119 // If someone requests legalization of the new node, return itself.
120 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000121 }
Chris Lattnerea4ca942005-01-07 22:28:47 +0000122
Chris Lattnerdc750592005-01-07 07:47:09 +0000123public:
124
Chris Lattner4add7e32005-01-23 04:42:50 +0000125 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +0000126
Chris Lattnerdc750592005-01-07 07:47:09 +0000127 /// getTypeAction - Return how we should legalize values of this type, either
128 /// it is already legal or we need to expand it into multiple registers of
129 /// smaller integer type, or we need to promote it to a larger type.
130 LegalizeAction getTypeAction(MVT::ValueType VT) const {
Chris Lattner2c748af2006-01-29 08:42:06 +0000131 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +0000132 }
133
134 /// isTypeLegal - Return true if this type is legal on this target.
135 ///
136 bool isTypeLegal(MVT::ValueType VT) const {
137 return getTypeAction(VT) == Legal;
138 }
139
Chris Lattnerdc750592005-01-07 07:47:09 +0000140 void LegalizeDAG();
141
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000142private:
Dan Gohmana8665142007-06-25 16:23:39 +0000143 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000144 /// appropriate for its type.
145 void HandleOp(SDOperand Op);
146
147 /// LegalizeOp - We know that the specified value has a legal type.
148 /// Recursively ensure that the operands have legal types, then return the
149 /// result.
Chris Lattnerdc750592005-01-07 07:47:09 +0000150 SDOperand LegalizeOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000151
Dan Gohman2a7de412007-10-11 23:57:53 +0000152 /// UnrollVectorOp - We know that the given vector has a legal type, however
153 /// the operation it performs is not legal and is an operation that we have
154 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
155 /// operating on each element individually.
156 SDOperand UnrollVectorOp(SDOperand O);
157
Chris Lattner32206f52006-03-18 01:44:44 +0000158 /// PromoteOp - Given an operation that produces a value in an invalid type,
159 /// promote it to compute the value into a larger type. The produced value
160 /// will have the correct bits for the low portion of the register, but no
161 /// guarantee is made about the top bits: it may be zero, sign-extended, or
162 /// garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000163 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000164
Chris Lattner32206f52006-03-18 01:44:44 +0000165 /// ExpandOp - Expand the specified SDOperand into its two component pieces
166 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
167 /// the LegalizeNodes map is filled in for any results that are not expanded,
168 /// the ExpandedNodes map is filled in for any results that are expanded, and
169 /// the Lo/Hi values are returned. This applies to integer types and Vector
170 /// types.
171 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
172
Dan Gohmana8665142007-06-25 16:23:39 +0000173 /// SplitVectorOp - Given an operand of vector type, break it down into
174 /// two smaller values.
Chris Lattner32206f52006-03-18 01:44:44 +0000175 void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
176
Dan Gohmanf4e86da2007-06-27 14:06:22 +0000177 /// ScalarizeVectorOp - Given an operand of single-element vector type
178 /// (e.g. v1f32), convert it into the equivalent operation that returns a
179 /// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +0000180 SDOperand ScalarizeVectorOp(SDOperand O);
Chris Lattner32206f52006-03-18 01:44:44 +0000181
Chris Lattner6be79822006-04-04 17:23:26 +0000182 /// isShuffleLegal - Return true if a vector shuffle is legal with the
183 /// specified mask and type. Targets can specify exactly which masks they
184 /// support and the code generator is tasked with not creating illegal masks.
185 ///
186 /// Note that this will also return true for shuffles that are promoted to a
187 /// different type.
188 ///
189 /// If this is a legal shuffle, this method returns the (possibly promoted)
190 /// build_vector Mask. If it's not a legal shuffle, it returns null.
191 SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const;
192
Chris Lattner4488f0c2006-07-26 23:55:56 +0000193 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000194 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000195
Nate Begeman7e7f4392006-02-01 07:19:44 +0000196 void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC);
197
Reid Spencere63b6512006-12-31 05:55:36 +0000198 SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned,
Chris Lattneraac464e2005-01-21 06:05:23 +0000199 SDOperand &Hi);
200 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
201 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000202
Chris Lattner87bc3e72008-01-16 07:45:30 +0000203 SDOperand EmitStackConvert(SDOperand SrcOp, MVT::ValueType SlotVT,
204 MVT::ValueType DestVT);
Chris Lattner9cdc5a02006-03-19 06:31:19 +0000205 SDOperand ExpandBUILD_VECTOR(SDNode *Node);
Chris Lattner6be79822006-04-04 17:23:26 +0000206 SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000207 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
208 SDOperand LegalOp,
209 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000210 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
211 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000212 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
213 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000214
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000215 SDOperand ExpandBSWAP(SDOperand Op);
216 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000217 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
218 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000219 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
220 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000221
Dan Gohmana8665142007-06-25 16:23:39 +0000222 SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op);
Chris Lattner42a5fca2006-04-02 05:06:04 +0000223 SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op);
Chris Lattnerdc750592005-01-07 07:47:09 +0000224};
225}
226
Chris Lattner6be79822006-04-04 17:23:26 +0000227/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
228/// specified mask and type. Targets can specify exactly which masks they
229/// support and the code generator is tasked with not creating illegal masks.
230///
231/// Note that this will also return true for shuffles that are promoted to a
232/// different type.
233SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT,
234 SDOperand Mask) const {
235 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
236 default: return 0;
237 case TargetLowering::Legal:
238 case TargetLowering::Custom:
239 break;
240 case TargetLowering::Promote: {
241 // If this is promoted to a different type, convert the shuffle mask and
242 // ask if it is legal in the promoted type!
243 MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
244
245 // If we changed # elements, change the shuffle mask.
246 unsigned NumEltsGrowth =
247 MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT);
248 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
249 if (NumEltsGrowth > 1) {
250 // Renumber the elements.
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000251 SmallVector<SDOperand, 8> Ops;
Chris Lattner6be79822006-04-04 17:23:26 +0000252 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
253 SDOperand InOp = Mask.getOperand(i);
254 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
255 if (InOp.getOpcode() == ISD::UNDEF)
256 Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
257 else {
258 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue();
259 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32));
260 }
261 }
262 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000263 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +0000264 }
265 VT = NVT;
266 break;
267 }
268 }
269 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0;
270}
271
Chris Lattner4add7e32005-01-23 04:42:50 +0000272SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
273 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
274 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000275 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000276 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000277}
278
Chris Lattnere31adc82007-06-18 21:28:10 +0000279/// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order
280/// contains all of a nodes operands before it contains the node.
281static void ComputeTopDownOrdering(SelectionDAG &DAG,
282 SmallVector<SDNode*, 64> &Order) {
283
284 DenseMap<SDNode*, unsigned> Visited;
285 std::vector<SDNode*> Worklist;
286 Worklist.reserve(128);
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000287
Chris Lattnere31adc82007-06-18 21:28:10 +0000288 // Compute ordering from all of the leaves in the graphs, those (like the
289 // entry node) that have no operands.
290 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
291 E = DAG.allnodes_end(); I != E; ++I) {
292 if (I->getNumOperands() == 0) {
293 Visited[I] = 0 - 1U;
294 Worklist.push_back(I);
295 }
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000296 }
297
Chris Lattnere31adc82007-06-18 21:28:10 +0000298 while (!Worklist.empty()) {
299 SDNode *N = Worklist.back();
300 Worklist.pop_back();
301
302 if (++Visited[N] != N->getNumOperands())
303 continue; // Haven't visited all operands yet
304
305 Order.push_back(N);
306
307 // Now that we have N in, add anything that uses it if all of their operands
308 // are now done.
309 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
310 UI != E; ++UI)
311 Worklist.push_back(*UI);
312 }
313
314 assert(Order.size() == Visited.size() &&
315 Order.size() ==
316 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
317 "Error: DAG is cyclic!");
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000318}
319
Chris Lattner44fe26f2005-07-29 00:11:56 +0000320
Chris Lattnerdc750592005-01-07 07:47:09 +0000321void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner462505f2006-02-13 09:18:02 +0000322 LastCALLSEQ_END = DAG.getEntryNode();
323 IsLegalizingCall = false;
324
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000325 // The legalize process is inherently a bottom-up recursive process (users
326 // legalize their uses before themselves). Given infinite stack space, we
327 // could just start legalizing on the root and traverse the whole graph. In
328 // practice however, this causes us to run out of stack space on large basic
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000329 // blocks. To avoid this problem, compute an ordering of the nodes where each
330 // node is only legalized after all of its operands are legalized.
Chris Lattner94c44c92007-02-04 01:20:02 +0000331 SmallVector<SDNode*, 64> Order;
Chris Lattnere31adc82007-06-18 21:28:10 +0000332 ComputeTopDownOrdering(DAG, Order);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000333
Chris Lattner32206f52006-03-18 01:44:44 +0000334 for (unsigned i = 0, e = Order.size(); i != e; ++i)
335 HandleOp(SDOperand(Order[i], 0));
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000336
337 // Finally, it's possible the root changed. Get the new root.
Chris Lattnerdc750592005-01-07 07:47:09 +0000338 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000339 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
340 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000341
342 ExpandedNodes.clear();
343 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000344 PromotedNodes.clear();
Chris Lattner32206f52006-03-18 01:44:44 +0000345 SplitNodes.clear();
Dan Gohmana8665142007-06-25 16:23:39 +0000346 ScalarizedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000347
348 // Remove dead nodes now.
Chris Lattner8927c872006-08-04 17:45:20 +0000349 DAG.RemoveDeadNodes();
Chris Lattnerdc750592005-01-07 07:47:09 +0000350}
351
Chris Lattner462505f2006-02-13 09:18:02 +0000352
353/// FindCallEndFromCallStart - Given a chained node that is part of a call
354/// sequence, find the CALLSEQ_END node that terminates the call sequence.
355static SDNode *FindCallEndFromCallStart(SDNode *Node) {
356 if (Node->getOpcode() == ISD::CALLSEQ_END)
357 return Node;
358 if (Node->use_empty())
359 return 0; // No CallSeqEnd
360
361 // The chain is usually at the end.
362 SDOperand TheChain(Node, Node->getNumValues()-1);
363 if (TheChain.getValueType() != MVT::Other) {
364 // Sometimes it's at the beginning.
365 TheChain = SDOperand(Node, 0);
366 if (TheChain.getValueType() != MVT::Other) {
367 // Otherwise, hunt for it.
368 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
369 if (Node->getValueType(i) == MVT::Other) {
370 TheChain = SDOperand(Node, i);
371 break;
372 }
373
374 // Otherwise, we walked into a node without a chain.
375 if (TheChain.getValueType() != MVT::Other)
376 return 0;
377 }
378 }
379
380 for (SDNode::use_iterator UI = Node->use_begin(),
381 E = Node->use_end(); UI != E; ++UI) {
382
383 // Make sure to only follow users of our token chain.
384 SDNode *User = *UI;
385 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
386 if (User->getOperand(i) == TheChain)
387 if (SDNode *Result = FindCallEndFromCallStart(User))
388 return Result;
389 }
390 return 0;
391}
392
393/// FindCallStartFromCallEnd - Given a chained node that is part of a call
394/// sequence, find the CALLSEQ_START node that initiates the call sequence.
395static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
396 assert(Node && "Didn't find callseq_start for a call??");
397 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
398
399 assert(Node->getOperand(0).getValueType() == MVT::Other &&
400 "Node doesn't have a token chain argument!");
401 return FindCallStartFromCallEnd(Node->getOperand(0).Val);
402}
403
404/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
405/// see if any uses can reach Dest. If no dest operands can get to dest,
406/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000407///
408/// Keep track of the nodes we fine that actually do lead to Dest in
409/// NodesLeadingTo. This avoids retraversing them exponential number of times.
410///
411bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattnerebeb48d2007-02-04 00:27:56 +0000412 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner462505f2006-02-13 09:18:02 +0000413 if (N == Dest) return true; // N certainly leads to Dest :)
414
Chris Lattner4488f0c2006-07-26 23:55:56 +0000415 // If we've already processed this node and it does lead to Dest, there is no
416 // need to reprocess it.
417 if (NodesLeadingTo.count(N)) return true;
418
Chris Lattner462505f2006-02-13 09:18:02 +0000419 // If the first result of this node has been already legalized, then it cannot
420 // reach N.
421 switch (getTypeAction(N->getValueType(0))) {
422 case Legal:
423 if (LegalizedNodes.count(SDOperand(N, 0))) return false;
424 break;
425 case Promote:
426 if (PromotedNodes.count(SDOperand(N, 0))) return false;
427 break;
428 case Expand:
429 if (ExpandedNodes.count(SDOperand(N, 0))) return false;
430 break;
431 }
432
433 // Okay, this node has not already been legalized. Check and legalize all
434 // operands. If none lead to Dest, then we can legalize this node.
435 bool OperandsLeadToDest = false;
436 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
437 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Chris Lattner4488f0c2006-07-26 23:55:56 +0000438 LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo);
Chris Lattner462505f2006-02-13 09:18:02 +0000439
Chris Lattner4488f0c2006-07-26 23:55:56 +0000440 if (OperandsLeadToDest) {
441 NodesLeadingTo.insert(N);
442 return true;
443 }
Chris Lattner462505f2006-02-13 09:18:02 +0000444
445 // Okay, this node looks safe, legalize it and return false.
Chris Lattner916ae072006-04-17 22:10:08 +0000446 HandleOp(SDOperand(N, 0));
Chris Lattner462505f2006-02-13 09:18:02 +0000447 return false;
448}
449
Dan Gohmana8665142007-06-25 16:23:39 +0000450/// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattner32206f52006-03-18 01:44:44 +0000451/// appropriate for its type.
452void SelectionDAGLegalize::HandleOp(SDOperand Op) {
Dan Gohmana8665142007-06-25 16:23:39 +0000453 MVT::ValueType VT = Op.getValueType();
454 switch (getTypeAction(VT)) {
Chris Lattner32206f52006-03-18 01:44:44 +0000455 default: assert(0 && "Bad type action!");
Dan Gohmana8665142007-06-25 16:23:39 +0000456 case Legal: (void)LegalizeOp(Op); break;
457 case Promote: (void)PromoteOp(Op); break;
Chris Lattner32206f52006-03-18 01:44:44 +0000458 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +0000459 if (!MVT::isVector(VT)) {
460 // If this is an illegal scalar, expand it into its two component
461 // pieces.
Chris Lattner32206f52006-03-18 01:44:44 +0000462 SDOperand X, Y;
Chris Lattner2ed652f2007-08-25 01:00:22 +0000463 if (Op.getOpcode() == ISD::TargetConstant)
464 break; // Allow illegal target nodes.
Chris Lattner32206f52006-03-18 01:44:44 +0000465 ExpandOp(Op, X, Y);
Dan Gohmana8665142007-06-25 16:23:39 +0000466 } else if (MVT::getVectorNumElements(VT) == 1) {
467 // If this is an illegal single element vector, convert it to a
468 // scalar operation.
469 (void)ScalarizeVectorOp(Op);
Chris Lattner32206f52006-03-18 01:44:44 +0000470 } else {
Dan Gohmana8665142007-06-25 16:23:39 +0000471 // Otherwise, this is an illegal multiple element vector.
472 // Split it in half and legalize both parts.
473 SDOperand X, Y;
474 SplitVectorOp(Op, X, Y);
Chris Lattner32206f52006-03-18 01:44:44 +0000475 }
476 break;
477 }
478}
Chris Lattner462505f2006-02-13 09:18:02 +0000479
Evan Cheng22cf8992006-12-13 20:57:08 +0000480/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
481/// a load from the constant pool.
482static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng3766fc602006-12-12 22:19:28 +0000483 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng47833a12006-12-12 21:32:44 +0000484 bool Extend = false;
485
486 // If a FP immediate is precise when represented as a float and if the
487 // target can do an extending load from float to double, we put it into
488 // the constant pool as a float, even if it's is statically typed as a
Chris Lattner3dc38992008-03-05 06:46:58 +0000489 // double. This shrinks FP constants and canonicalizes them for targets where
490 // an FP extending load is the same cost as a normal load (such as on the x87
491 // fp stack or PPC FP unit).
Evan Cheng47833a12006-12-12 21:32:44 +0000492 MVT::ValueType VT = CFP->getValueType(0);
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(),
Evan Cheng38caf772008-03-04 08:05:30 +0000499 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng3766fc602006-12-12 22:19:28 +0000500 }
501
Evan Cheng38caf772008-03-04 08:05:30 +0000502 MVT::ValueType OrigVT = VT;
503 MVT::ValueType SVT = VT;
504 while (SVT != MVT::f32) {
505 SVT = (unsigned)SVT - 1;
506 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
507 // Only do this if the target has a native EXTLOAD instruction from
508 // smaller type.
Evan Cheng0a62cb42008-03-05 01:30:59 +0000509 TLI.isLoadXLegal(ISD::EXTLOAD, SVT) &&
Chris Lattner3dc38992008-03-05 06:46:58 +0000510 TLI.ShouldShrinkFPConstant(OrigVT)) {
Evan Cheng38caf772008-03-04 08:05:30 +0000511 const Type *SType = MVT::getTypeForValueType(SVT);
512 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
513 VT = SVT;
514 Extend = true;
515 }
Evan Cheng47833a12006-12-12 21:32:44 +0000516 }
517
518 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Evan Cheng38caf772008-03-04 08:05:30 +0000519 if (Extend)
520 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman16d4bc32008-02-07 18:41:25 +0000521 CPIdx, PseudoSourceValue::getConstantPool(),
Evan Cheng38caf772008-03-04 08:05:30 +0000522 0, VT);
523 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
524 PseudoSourceValue::getConstantPool(), 0);
Evan Cheng47833a12006-12-12 21:32:44 +0000525}
526
Chris Lattner462505f2006-02-13 09:18:02 +0000527
Evan Cheng003feb02007-01-04 21:56:39 +0000528/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
529/// operations.
530static
531SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT,
532 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng3b841dd2007-01-05 21:31:51 +0000533 MVT::ValueType VT = Node->getValueType(0);
Evan Cheng003feb02007-01-04 21:56:39 +0000534 MVT::ValueType SrcVT = Node->getOperand(1).getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +0000535 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
536 "fcopysign expansion only supported for f32 and f64");
Evan Cheng003feb02007-01-04 21:56:39 +0000537 MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng3b841dd2007-01-05 21:31:51 +0000538
Evan Cheng003feb02007-01-04 21:56:39 +0000539 // First get the sign bit of second operand.
Evan Cheng3b841dd2007-01-05 21:31:51 +0000540 SDOperand Mask1 = (SrcVT == MVT::f64)
Evan Cheng003feb02007-01-04 21:56:39 +0000541 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
542 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000543 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000544 SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000545 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng003feb02007-01-04 21:56:39 +0000546 // Shift right or sign-extend it if the two operands have different types.
547 int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT);
548 if (SizeDiff > 0) {
549 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
550 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
551 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
552 } else if (SizeDiff < 0)
553 SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit);
Evan Cheng3b841dd2007-01-05 21:31:51 +0000554
555 // Clear the sign bit of first operand.
556 SDOperand Mask2 = (VT == MVT::f64)
557 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
558 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
559 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Evan Cheng003feb02007-01-04 21:56:39 +0000560 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng3b841dd2007-01-05 21:31:51 +0000561 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
562
563 // Or the value with the sign bit.
Evan Cheng003feb02007-01-04 21:56:39 +0000564 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
565 return Result;
566}
567
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000568/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
569static
570SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
571 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000572 SDOperand Chain = ST->getChain();
573 SDOperand Ptr = ST->getBasePtr();
574 SDOperand Val = ST->getValue();
575 MVT::ValueType VT = Val.getValueType();
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000576 int Alignment = ST->getAlignment();
577 int SVOffset = ST->getSrcValueOffset();
Dale Johannesenbf76a082008-02-27 22:36:00 +0000578 if (MVT::isFloatingPoint(ST->getMemoryVT()) ||
579 MVT::isVector(ST->getMemoryVT())) {
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000580 // Expand to a bitconvert of the value to the integer type of the
581 // same size, then a (misaligned) int store.
582 MVT::ValueType intVT;
Dale Johannesen208cc8f2008-03-01 03:40:57 +0000583 if (MVT::is128BitVector(VT) || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesenbf76a082008-02-27 22:36:00 +0000584 intVT = MVT::i128;
Dale Johannesen208cc8f2008-03-01 03:40:57 +0000585 else if (MVT::is64BitVector(VT) || VT==MVT::f64)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000586 intVT = MVT::i64;
587 else if (VT==MVT::f32)
588 intVT = MVT::i32;
589 else
Dale Johannesenc4c3de22008-02-28 18:36:51 +0000590 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000591
592 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
593 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
594 SVOffset, ST->isVolatile(), Alignment);
595 }
Dan Gohman47a7d6f2008-01-30 00:15:11 +0000596 assert(MVT::isInteger(ST->getMemoryVT()) &&
Dale Johannesenbf76a082008-02-27 22:36:00 +0000597 !MVT::isVector(ST->getMemoryVT()) &&
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000598 "Unaligned store of unknown type.");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000599 // Get the half-size VT
Dan Gohman47a7d6f2008-01-30 00:15:11 +0000600 MVT::ValueType NewStoredVT = ST->getMemoryVT() - 1;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000601 int NumBits = MVT::getSizeInBits(NewStoredVT);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000602 int IncrementSize = NumBits / 8;
603
604 // Divide the stored value in two parts.
605 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
606 SDOperand Lo = Val;
607 SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
608
609 // Store the two parts
610 SDOperand Store1, Store2;
611 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
612 ST->getSrcValue(), SVOffset, NewStoredVT,
613 ST->isVolatile(), Alignment);
614 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
615 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sands1826ded2007-10-28 12:59:45 +0000616 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000617 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
618 ST->getSrcValue(), SVOffset + IncrementSize,
619 NewStoredVT, ST->isVolatile(), Alignment);
620
621 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
622}
623
624/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
625static
626SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
627 TargetLowering &TLI) {
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000628 int SVOffset = LD->getSrcValueOffset();
629 SDOperand Chain = LD->getChain();
630 SDOperand Ptr = LD->getBasePtr();
631 MVT::ValueType VT = LD->getValueType(0);
Dan Gohman47a7d6f2008-01-30 00:15:11 +0000632 MVT::ValueType LoadedVT = LD->getMemoryVT();
Dale Johannesenbf76a082008-02-27 22:36:00 +0000633 if (MVT::isFloatingPoint(VT) || MVT::isVector(VT)) {
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000634 // Expand to a (misaligned) integer load of the same size,
Dale Johannesenbf76a082008-02-27 22:36:00 +0000635 // then bitconvert to floating point or vector.
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000636 MVT::ValueType intVT;
Dale Johannesen208cc8f2008-03-01 03:40:57 +0000637 if (MVT::is128BitVector(LoadedVT) ||
638 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesenbf76a082008-02-27 22:36:00 +0000639 intVT = MVT::i128;
Dale Johannesen208cc8f2008-03-01 03:40:57 +0000640 else if (MVT::is64BitVector(LoadedVT) || LoadedVT == MVT::f64)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000641 intVT = MVT::i64;
Chris Lattner09c03932007-11-19 21:38:03 +0000642 else if (LoadedVT == MVT::f32)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000643 intVT = MVT::i32;
644 else
Dale Johannesenbf76a082008-02-27 22:36:00 +0000645 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000646
647 SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
648 SVOffset, LD->isVolatile(),
649 LD->getAlignment());
650 SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Dale Johannesenbf76a082008-02-27 22:36:00 +0000651 if (MVT::isFloatingPoint(VT) && LoadedVT != VT)
Dale Johannesen29e6ac42007-09-08 19:29:23 +0000652 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
653
654 SDOperand Ops[] = { Result, Chain };
655 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
656 Ops, 2);
657 }
Dale Johannesenbf76a082008-02-27 22:36:00 +0000658 assert(MVT::isInteger(LoadedVT) && !MVT::isVector(LoadedVT) &&
Chris Lattner09c03932007-11-19 21:38:03 +0000659 "Unaligned load of unsupported type.");
660
Dale Johannesenbf76a082008-02-27 22:36:00 +0000661 // Compute the new VT that is half the size of the old one. This is an
662 // integer MVT.
Chris Lattner09c03932007-11-19 21:38:03 +0000663 unsigned NumBits = MVT::getSizeInBits(LoadedVT);
664 MVT::ValueType NewLoadedVT;
Dale Johannesenbf76a082008-02-27 22:36:00 +0000665 NewLoadedVT = MVT::getIntegerType(NumBits/2);
Chris Lattner09c03932007-11-19 21:38:03 +0000666 NumBits >>= 1;
667
668 unsigned Alignment = LD->getAlignment();
669 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000670 ISD::LoadExtType HiExtType = LD->getExtensionType();
671
672 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
673 if (HiExtType == ISD::NON_EXTLOAD)
674 HiExtType = ISD::ZEXTLOAD;
675
676 // Load the value in two parts
677 SDOperand Lo, Hi;
678 if (TLI.isLittleEndian()) {
679 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
680 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
681 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
682 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
683 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
684 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000685 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000686 } else {
687 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
688 NewLoadedVT,LD->isVolatile(), Alignment);
689 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
690 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
691 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
692 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sands1826ded2007-10-28 12:59:45 +0000693 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +0000694 }
695
696 // aggregate the two parts
697 SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
698 SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
699 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
700
701 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
702 Hi.getValue(1));
703
704 SDOperand Ops[] = { Result, TF };
705 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2);
706}
Evan Cheng003feb02007-01-04 21:56:39 +0000707
Dan Gohman2a7de412007-10-11 23:57:53 +0000708/// UnrollVectorOp - We know that the given vector has a legal type, however
709/// the operation it performs is not legal and is an operation that we have
710/// no way of lowering. "Unroll" the vector, splitting out the scalars and
711/// operating on each element individually.
712SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) {
713 MVT::ValueType VT = Op.getValueType();
714 assert(isTypeLegal(VT) &&
715 "Caller should expand or promote operands that are not legal!");
716 assert(Op.Val->getNumValues() == 1 &&
717 "Can't unroll a vector with multiple results!");
718 unsigned NE = MVT::getVectorNumElements(VT);
719 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
720
721 SmallVector<SDOperand, 8> Scalars;
722 SmallVector<SDOperand, 4> Operands(Op.getNumOperands());
723 for (unsigned i = 0; i != NE; ++i) {
724 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
725 SDOperand Operand = Op.getOperand(j);
726 MVT::ValueType OperandVT = Operand.getValueType();
727 if (MVT::isVector(OperandVT)) {
728 // A vector operand; extract a single element.
729 MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT);
730 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
731 OperandEltVT,
732 Operand,
733 DAG.getConstant(i, MVT::i32));
734 } else {
735 // A scalar operand; just use it as is.
736 Operands[j] = Operand;
737 }
738 }
739 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
740 &Operands[0], Operands.size()));
741 }
742
743 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
744}
745
Duncan Sands53c954f2008-01-10 10:28:30 +0000746/// GetFPLibCall - Return the right libcall for the given floating point type.
747static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT,
748 RTLIB::Libcall Call_F32,
749 RTLIB::Libcall Call_F64,
750 RTLIB::Libcall Call_F80,
751 RTLIB::Libcall Call_PPCF128) {
752 return
753 VT == MVT::f32 ? Call_F32 :
754 VT == MVT::f64 ? Call_F64 :
755 VT == MVT::f80 ? Call_F80 :
756 VT == MVT::ppcf128 ? Call_PPCF128 :
757 RTLIB::UNKNOWN_LIBCALL;
758}
759
Dan Gohmanff727882007-07-13 20:14:11 +0000760/// LegalizeOp - We know that the specified value has a legal type, and
761/// that its operands are legal. Now ensure that the operation itself
762/// is legal, recursively ensuring that the operands' operations remain
763/// legal.
Chris Lattnerdc750592005-01-07 07:47:09 +0000764SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattner2ed652f2007-08-25 01:00:22 +0000765 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
766 return Op;
767
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000768 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000769 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000770 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000771
Chris Lattnerdc750592005-01-07 07:47:09 +0000772 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000773 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000774 if (Node->getNumValues() > 1) {
775 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattner32206f52006-03-18 01:44:44 +0000776 if (getTypeAction(Node->getValueType(i)) != Legal) {
777 HandleOp(Op.getValue(i));
Chris Lattnerdc750592005-01-07 07:47:09 +0000778 assert(LegalizedNodes.count(Op) &&
Chris Lattner32206f52006-03-18 01:44:44 +0000779 "Handling didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000780 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000781 }
782 }
783
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000784 // Note that LegalizeOp may be reentered even from single-use nodes, which
785 // means that we always must cache transformed nodes.
Chris Lattnered39c862007-02-04 00:50:02 +0000786 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner85d70c62005-01-11 05:57:22 +0000787 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000788
Nate Begemane5b86d72005-08-10 20:51:12 +0000789 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000790 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000791 bool isCustom = false;
792
Chris Lattnerdc750592005-01-07 07:47:09 +0000793 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000794 case ISD::FrameIndex:
795 case ISD::EntryToken:
796 case ISD::Register:
797 case ISD::BasicBlock:
798 case ISD::TargetFrameIndex:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000799 case ISD::TargetJumpTable:
Chris Lattnereb637512006-01-28 08:31:04 +0000800 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000801 case ISD::TargetConstantFP:
Chris Lattnereb637512006-01-28 08:31:04 +0000802 case ISD::TargetConstantPool:
803 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000804 case ISD::TargetGlobalTLSAddress:
Chris Lattnereb637512006-01-28 08:31:04 +0000805 case ISD::TargetExternalSymbol:
806 case ISD::VALUETYPE:
807 case ISD::SRCVALUE:
Dan Gohman2d489b52008-02-06 22:27:42 +0000808 case ISD::MEMOPERAND:
Chris Lattnereb637512006-01-28 08:31:04 +0000809 case ISD::STRING:
810 case ISD::CONDCODE:
811 // Primitives must all be legal.
Duncan Sands052c8432007-10-16 09:07:20 +0000812 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattnereb637512006-01-28 08:31:04 +0000813 "This must be legal!");
814 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000815 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000816 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
817 // If this is a target node, legalize it by legalizing the operands then
818 // passing it through.
Chris Lattner97af9d52006-08-08 01:09:31 +0000819 SmallVector<SDOperand, 8> Ops;
Chris Lattner62f1b832006-05-17 18:00:08 +0000820 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattner3eb86932005-05-14 06:34:48 +0000821 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner62f1b832006-05-17 18:00:08 +0000822
Chris Lattner97af9d52006-08-08 01:09:31 +0000823 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattner3eb86932005-05-14 06:34:48 +0000824
825 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
826 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
827 return Result.getValue(Op.ResNo);
828 }
829 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000830#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +0000831 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +0000832#endif
Chris Lattnerdc750592005-01-07 07:47:09 +0000833 assert(0 && "Do not know how to legalize this operator!");
834 abort();
Lauro Ramos Venancio94314be2007-04-20 23:02:39 +0000835 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattnerdc750592005-01-07 07:47:09 +0000836 case ISD::GlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000837 case ISD::GlobalTLSAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000838 case ISD::ExternalSymbol:
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000839 case ISD::ConstantPool:
840 case ISD::JumpTable: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000841 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
842 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000843 case TargetLowering::Custom:
844 Tmp1 = TLI.LowerOperation(Op, DAG);
845 if (Tmp1.Val) Result = Tmp1;
846 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000847 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000848 break;
849 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000850 break;
Nate Begemaneda59972007-01-29 22:58:52 +0000851 case ISD::FRAMEADDR:
852 case ISD::RETURNADDR:
853 // The only option for these nodes is to custom lower them. If the target
854 // does not custom lower them, then return zero.
855 Tmp1 = TLI.LowerOperation(Op, DAG);
856 if (Tmp1.Val)
857 Result = Tmp1;
858 else
859 Result = DAG.getConstant(0, TLI.getPointerTy());
860 break;
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000861 case ISD::FRAME_TO_ARGS_OFFSET: {
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000862 MVT::ValueType VT = Node->getValueType(0);
863 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
864 default: assert(0 && "This action is not supported yet!");
865 case TargetLowering::Custom:
866 Result = TLI.LowerOperation(Op, DAG);
867 if (Result.Val) break;
868 // Fall Thru
869 case TargetLowering::Legal:
870 Result = DAG.getConstant(0, VT);
871 break;
872 }
Anton Korobeynikov2bdec2a2007-08-29 23:18:48 +0000873 }
Anton Korobeynikov830b1cb2007-08-29 19:28:29 +0000874 break;
Jim Laskey7f5872c2007-02-22 15:37:19 +0000875 case ISD::EXCEPTIONADDR: {
876 Tmp1 = LegalizeOp(Node->getOperand(0));
877 MVT::ValueType VT = Node->getValueType(0);
878 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
879 default: assert(0 && "This action is not supported yet!");
880 case TargetLowering::Expand: {
Jim Laskey644af6b2007-02-28 20:43:58 +0000881 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +0000882 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000883 }
884 break;
885 case TargetLowering::Custom:
886 Result = TLI.LowerOperation(Op, DAG);
887 if (Result.Val) break;
888 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000889 case TargetLowering::Legal: {
890 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 };
891 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sands57a60f02007-12-31 18:35:50 +0000892 Ops, 2);
Jim Laskey7f5872c2007-02-22 15:37:19 +0000893 break;
894 }
895 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000896 }
Duncan Sands57a60f02007-12-31 18:35:50 +0000897 if (Result.Val->getNumValues() == 1) break;
898
899 assert(Result.Val->getNumValues() == 2 &&
900 "Cannot return more than two values!");
901
902 // Since we produced two values, make sure to remember that we
903 // legalized both of them.
904 Tmp1 = LegalizeOp(Result);
905 Tmp2 = LegalizeOp(Result.getValue(1));
906 AddLegalizedOperand(Op.getValue(0), Tmp1);
907 AddLegalizedOperand(Op.getValue(1), Tmp2);
908 return Op.ResNo ? Tmp2 : Tmp1;
Jim Laskey644af6b2007-02-28 20:43:58 +0000909 case ISD::EHSELECTION: {
910 Tmp1 = LegalizeOp(Node->getOperand(0));
911 Tmp2 = LegalizeOp(Node->getOperand(1));
912 MVT::ValueType VT = Node->getValueType(0);
913 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
914 default: assert(0 && "This action is not supported yet!");
915 case TargetLowering::Expand: {
916 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sands57a60f02007-12-31 18:35:50 +0000917 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey644af6b2007-02-28 20:43:58 +0000918 }
919 break;
920 case TargetLowering::Custom:
921 Result = TLI.LowerOperation(Op, DAG);
922 if (Result.Val) break;
923 // Fall Thru
Chris Lattner1cbe2082007-04-27 17:12:52 +0000924 case TargetLowering::Legal: {
925 SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 };
926 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other),
Duncan Sands57a60f02007-12-31 18:35:50 +0000927 Ops, 2);
Jim Laskey644af6b2007-02-28 20:43:58 +0000928 break;
929 }
930 }
Chris Lattner1cbe2082007-04-27 17:12:52 +0000931 }
Duncan Sands57a60f02007-12-31 18:35:50 +0000932 if (Result.Val->getNumValues() == 1) break;
933
934 assert(Result.Val->getNumValues() == 2 &&
935 "Cannot return more than two values!");
936
937 // Since we produced two values, make sure to remember that we
938 // legalized both of them.
939 Tmp1 = LegalizeOp(Result);
940 Tmp2 = LegalizeOp(Result.getValue(1));
941 AddLegalizedOperand(Op.getValue(0), Tmp1);
942 AddLegalizedOperand(Op.getValue(1), Tmp2);
943 return Op.ResNo ? Tmp2 : Tmp1;
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000944 case ISD::EH_RETURN: {
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000945 MVT::ValueType VT = Node->getValueType(0);
946 // The only "good" option for this node is to custom lower it.
947 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
948 default: assert(0 && "This action is not supported at all!");
949 case TargetLowering::Custom:
950 Result = TLI.LowerOperation(Op, DAG);
951 if (Result.Val) break;
952 // Fall Thru
953 case TargetLowering::Legal:
954 // Target does not know, how to lower this, lower to noop
955 Result = LegalizeOp(Node->getOperand(0));
956 break;
957 }
Nick Lewyckyd20f4852007-07-14 15:11:14 +0000958 }
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000959 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000960 case ISD::AssertSext:
961 case ISD::AssertZext:
962 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000963 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000964 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000965 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000966 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000967 Result = Node->getOperand(Op.ResNo);
968 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000969 case ISD::CopyFromReg:
970 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000971 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000972 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000973 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000974 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000975 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000976 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000977 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000978 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
979 } else {
980 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
981 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000982 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
983 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000984 // Since CopyFromReg produces two values, make sure to remember that we
985 // legalized both of them.
986 AddLegalizedOperand(Op.getValue(0), Result);
987 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
988 return Result.getValue(Op.ResNo);
Nate Begemancda9aa72005-04-01 22:34:39 +0000989 case ISD::UNDEF: {
990 MVT::ValueType VT = Op.getValueType();
991 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000992 default: assert(0 && "This action is not supported yet!");
993 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000994 if (MVT::isInteger(VT))
995 Result = DAG.getConstant(0, VT);
996 else if (MVT::isFloatingPoint(VT))
Dale Johannesenf04d37d2007-09-26 17:26:49 +0000997 Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)),
998 VT);
Nate Begemancda9aa72005-04-01 22:34:39 +0000999 else
1000 assert(0 && "Unknown value type!");
1001 break;
Nate Begeman69d39432005-04-02 00:41:14 +00001002 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +00001003 break;
1004 }
1005 break;
1006 }
Chris Lattnera4f68052006-03-24 02:26:29 +00001007
Chris Lattnere55d1712006-03-28 00:40:33 +00001008 case ISD::INTRINSIC_W_CHAIN:
1009 case ISD::INTRINSIC_WO_CHAIN:
1010 case ISD::INTRINSIC_VOID: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001011 SmallVector<SDOperand, 8> Ops;
Chris Lattnera4f68052006-03-24 02:26:29 +00001012 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1013 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001014 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner30ee7252006-03-26 09:12:51 +00001015
1016 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattnere55d1712006-03-28 00:40:33 +00001017 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner30ee7252006-03-26 09:12:51 +00001018 TargetLowering::Custom) {
1019 Tmp3 = TLI.LowerOperation(Result, DAG);
1020 if (Tmp3.Val) Result = Tmp3;
Chris Lattnerd5f94c92006-03-27 20:28:29 +00001021 }
1022
1023 if (Result.Val->getNumValues() == 1) break;
1024
1025 // Must have return value and chain result.
1026 assert(Result.Val->getNumValues() == 2 &&
1027 "Cannot return more than two values!");
1028
1029 // Since loads produce two values, make sure to remember that we
1030 // legalized both of them.
1031 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1032 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1033 return Result.getValue(Op.ResNo);
Chris Lattnera4f68052006-03-24 02:26:29 +00001034 }
Chris Lattner435b4022005-11-29 06:21:05 +00001035
1036 case ISD::LOCATION:
1037 assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!");
1038 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1039
1040 switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) {
1041 case TargetLowering::Promote:
1042 default: assert(0 && "This action is not supported yet!");
Jim Laskey7c462762005-12-16 22:45:29 +00001043 case TargetLowering::Expand: {
Jim Laskeyc56315c2007-01-26 21:22:28 +00001044 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +00001045 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Jim Laskeyf9e54452007-01-26 14:34:52 +00001046 bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001047
Jim Laskeyc56315c2007-01-26 21:22:28 +00001048 if (MMI && (useDEBUG_LOC || useLABEL)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001049 const std::string &FName =
1050 cast<StringSDNode>(Node->getOperand(3))->getValue();
1051 const std::string &DirName =
1052 cast<StringSDNode>(Node->getOperand(4))->getValue();
Jim Laskeyc56315c2007-01-26 21:22:28 +00001053 unsigned SrcFile = MMI->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001054
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001055 SmallVector<SDOperand, 8> Ops;
Jim Laskey9e296be2005-12-21 20:51:37 +00001056 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +00001057 SDOperand LineOp = Node->getOperand(1);
1058 SDOperand ColOp = Node->getOperand(2);
1059
1060 if (useDEBUG_LOC) {
1061 Ops.push_back(LineOp); // line #
1062 Ops.push_back(ColOp); // col #
1063 Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001064 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001065 } else {
Jim Laskey2eea4362006-02-15 19:34:44 +00001066 unsigned Line = cast<ConstantSDNode>(LineOp)->getValue();
1067 unsigned Col = cast<ConstantSDNode>(ColOp)->getValue();
Evan Cheng263070e2008-02-01 02:05:57 +00001068 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001069 Ops.push_back(DAG.getConstant(ID, MVT::i32));
Evan Cheng1c6c16e2008-01-31 09:59:15 +00001070 Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label
1071 Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size());
Jim Laskey762e9ec2006-01-05 01:25:28 +00001072 }
Jim Laskey9e296be2005-12-21 20:51:37 +00001073 } else {
1074 Result = Tmp1; // chain
1075 }
Chris Lattner435b4022005-11-29 06:21:05 +00001076 break;
Chris Lattnerc06da622005-12-18 23:54:29 +00001077 }
Chris Lattner435b4022005-11-29 06:21:05 +00001078 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +00001079 if (Tmp1 != Node->getOperand(0) ||
1080 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001081 SmallVector<SDOperand, 8> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +00001082 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +00001083 if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) {
1084 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1085 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1086 } else {
1087 // Otherwise promote them.
1088 Ops.push_back(PromoteOp(Node->getOperand(1)));
1089 Ops.push_back(PromoteOp(Node->getOperand(2)));
1090 }
Chris Lattner435b4022005-11-29 06:21:05 +00001091 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1092 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattner97af9d52006-08-08 01:09:31 +00001093 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner435b4022005-11-29 06:21:05 +00001094 }
1095 break;
1096 }
1097 break;
Evan Chengefd142a2008-02-02 04:07:54 +00001098
1099 case ISD::DECLARE:
1100 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1101 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1102 default: assert(0 && "This action is not supported yet!");
1103 case TargetLowering::Legal:
1104 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1105 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1106 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1107 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1108 break;
Chris Lattner9824ffe2008-02-28 05:53:40 +00001109 case TargetLowering::Expand:
1110 Result = LegalizeOp(Node->getOperand(0));
1111 break;
Evan Chengefd142a2008-02-02 04:07:54 +00001112 }
1113 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001114
1115 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +00001116 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +00001117 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +00001118 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +00001119 case TargetLowering::Legal:
1120 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1121 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1122 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1123 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001124 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +00001125 break;
1126 }
1127 break;
1128
Jim Laskeyf9e54452007-01-26 14:34:52 +00001129 case ISD::LABEL:
Evan Cheng1c6c16e2008-01-31 09:59:15 +00001130 assert(Node->getNumOperands() == 3 && "Invalid LABEL node!");
Jim Laskeyf9e54452007-01-26 14:34:52 +00001131 switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) {
Jim Laskey762e9ec2006-01-05 01:25:28 +00001132 default: assert(0 && "This action is not supported yet!");
1133 case TargetLowering::Legal:
1134 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1135 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id.
Evan Cheng1c6c16e2008-01-31 09:59:15 +00001136 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand.
1137 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Jim Laskey7c462762005-12-16 22:45:29 +00001138 break;
Chris Lattner567b9252007-03-03 19:21:38 +00001139 case TargetLowering::Expand:
1140 Result = LegalizeOp(Node->getOperand(0));
1141 break;
Jim Laskey7c462762005-12-16 22:45:29 +00001142 }
Nate Begeman5965bd12006-02-17 05:43:56 +00001143 break;
Chris Lattner435b4022005-11-29 06:21:05 +00001144
Evan Cheng95cf6612008-03-08 00:58:38 +00001145 case ISD::PREFETCH:
1146 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1147 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1148 default: assert(0 && "This action is not supported yet!");
1149 case TargetLowering::Legal:
1150 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1151 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1152 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1153 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1154 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1155 break;
1156 case TargetLowering::Expand:
1157 // It's a noop.
1158 Result = LegalizeOp(Node->getOperand(0));
1159 break;
1160 }
1161 break;
1162
Andrew Lenharth9b254ee2008-02-16 01:24:58 +00001163 case ISD::MEMBARRIER: {
1164 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001165 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1166 default: assert(0 && "This action is not supported yet!");
1167 case TargetLowering::Legal: {
1168 SDOperand Ops[6];
1169 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sands96658d02008-02-27 08:53:44 +00001170 for (int x = 1; x < 6; ++x) {
1171 Ops[x] = Node->getOperand(x);
1172 if (!isTypeLegal(Ops[x].getValueType()))
1173 Ops[x] = PromoteOp(Ops[x]);
1174 }
Andrew Lenharthfedcf472008-02-16 14:46:26 +00001175 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1176 break;
1177 }
1178 case TargetLowering::Expand:
1179 //There is no libgcc call for this op
1180 Result = Node->getOperand(0); // Noop
1181 break;
1182 }
Andrew Lenharth9b254ee2008-02-16 01:24:58 +00001183 break;
1184 }
1185
Andrew Lenharth95528942008-02-21 06:45:13 +00001186 case ISD::ATOMIC_LCS:
1187 case ISD::ATOMIC_LAS:
1188 case ISD::ATOMIC_SWAP: {
1189 assert(((Node->getNumOperands() == 4 && Node->getOpcode() == ISD::ATOMIC_LCS) ||
1190 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_LAS) ||
1191 (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_SWAP)) &&
Andrew Lenharthd032c332008-03-01 21:52:34 +00001192 "Invalid Atomic node!");
Andrew Lenharth95528942008-02-21 06:45:13 +00001193 int num = Node->getOpcode() == ISD::ATOMIC_LCS ? 4 : 3;
Andrew Lenharthd032c332008-03-01 21:52:34 +00001194 SDOperand Ops[4];
1195 for (int x = 0; x < num; ++x)
1196 Ops[x] = LegalizeOp(Node->getOperand(x));
1197 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num);
1198
1199 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharth95528942008-02-21 06:45:13 +00001200 default: assert(0 && "This action is not supported yet!");
Andrew Lenharthd032c332008-03-01 21:52:34 +00001201 case TargetLowering::Custom:
1202 Result = TLI.LowerOperation(Result, DAG);
1203 break;
1204 case TargetLowering::Legal:
Andrew Lenharth95528942008-02-21 06:45:13 +00001205 break;
1206 }
Andrew Lenharthd032c332008-03-01 21:52:34 +00001207 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1208 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1209 return Result.getValue(Op.ResNo);
Andrew Lenharth95528942008-02-21 06:45:13 +00001210 }
1211
Scott Michel9d09c5c2007-08-08 23:23:31 +00001212 case ISD::Constant: {
1213 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1214 unsigned opAction =
1215 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1216
Chris Lattnerdc750592005-01-07 07:47:09 +00001217 // We know we don't need to expand constants here, constants only have one
1218 // value and we check that it is fine above.
1219
Scott Michel9d09c5c2007-08-08 23:23:31 +00001220 if (opAction == TargetLowering::Custom) {
1221 Tmp1 = TLI.LowerOperation(Result, DAG);
1222 if (Tmp1.Val)
1223 Result = Tmp1;
1224 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001225 break;
Scott Michel9d09c5c2007-08-08 23:23:31 +00001226 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001227 case ISD::ConstantFP: {
1228 // Spill FP immediates to the constant pool if the target cannot directly
1229 // codegen them. Targets often have some immediate values that can be
1230 // efficiently generated into an FP register without a load. We explicitly
1231 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattnerdc750592005-01-07 07:47:09 +00001232 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1233
Chris Lattner758b0ac2006-01-29 06:26:56 +00001234 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1235 default: assert(0 && "This action is not supported yet!");
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001236 case TargetLowering::Legal:
1237 break;
Chris Lattner758b0ac2006-01-29 06:26:56 +00001238 case TargetLowering::Custom:
1239 Tmp3 = TLI.LowerOperation(Result, DAG);
1240 if (Tmp3.Val) {
1241 Result = Tmp3;
1242 break;
1243 }
1244 // FALLTHROUGH
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001245 case TargetLowering::Expand: {
1246 // Check to see if this FP immediate is already legal.
1247 bool isLegal = false;
1248 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1249 E = TLI.legal_fpimm_end(); I != E; ++I) {
1250 if (CFP->isExactlyValue(*I)) {
1251 isLegal = true;
1252 break;
1253 }
1254 }
1255 // If this is a legal constant, turn it into a TargetConstantFP node.
1256 if (isLegal)
1257 break;
Evan Cheng3766fc602006-12-12 22:19:28 +00001258 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattnerdc750592005-01-07 07:47:09 +00001259 }
Nate Begeman53e1b3f2008-02-14 08:57:00 +00001260 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001261 break;
1262 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001263 case ISD::TokenFactor:
1264 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001265 Tmp1 = LegalizeOp(Node->getOperand(0));
1266 Tmp2 = LegalizeOp(Node->getOperand(1));
1267 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1268 } else if (Node->getNumOperands() == 3) {
1269 Tmp1 = LegalizeOp(Node->getOperand(0));
1270 Tmp2 = LegalizeOp(Node->getOperand(1));
1271 Tmp3 = LegalizeOp(Node->getOperand(2));
1272 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001273 } else {
Chris Lattner97af9d52006-08-08 01:09:31 +00001274 SmallVector<SDOperand, 8> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +00001275 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001276 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1277 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner97af9d52006-08-08 01:09:31 +00001278 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner05b4e372005-01-13 17:59:25 +00001279 }
Chris Lattner05b4e372005-01-13 17:59:25 +00001280 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001281
1282 case ISD::FORMAL_ARGUMENTS:
Chris Lattneraaa23d92006-05-16 22:53:20 +00001283 case ISD::CALL:
Chris Lattnerd3b504a2006-04-12 16:20:43 +00001284 // The only option for this is to custom lower it.
Chris Lattnera1cec012006-05-17 17:55:45 +00001285 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
1286 assert(Tmp3.Val && "Target didn't custom lower this node!");
Dale Johannesen8ee39c62008-03-05 19:14:03 +00001287 // A call within a calling sequence must be legalized to something
1288 // other than the normal CALLSEQ_END. Violating this gets Legalize
1289 // into an infinite loop.
1290 assert ((!IsLegalizingCall ||
1291 Node->getOpcode() != ISD::CALL ||
1292 Tmp3.Val->getOpcode() != ISD::CALLSEQ_END) &&
1293 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendlingf359fed2007-11-13 00:44:25 +00001294
1295 // The number of incoming and outgoing values should match; unless the final
1296 // outgoing value is a flag.
1297 assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() ||
1298 (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 &&
1299 Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) ==
1300 MVT::Flag)) &&
Chris Lattnera1cec012006-05-17 17:55:45 +00001301 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001302
Chris Lattneraaa23d92006-05-16 22:53:20 +00001303 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001304 // remember that we legalized all of them, so it doesn't get relegalized.
Chris Lattnera1cec012006-05-17 17:55:45 +00001305 for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) {
Bill Wendlingf359fed2007-11-13 00:44:25 +00001306 if (Tmp3.Val->getValueType(i) == MVT::Flag)
1307 continue;
Chris Lattnera1cec012006-05-17 17:55:45 +00001308 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Chris Lattner5f0edfb2006-05-16 05:49:56 +00001309 if (Op.ResNo == i)
1310 Tmp2 = Tmp1;
1311 AddLegalizedOperand(SDOperand(Node, i), Tmp1);
1312 }
1313 return Tmp2;
Christopher Lamba8fc0e52007-07-26 07:34:40 +00001314 case ISD::EXTRACT_SUBREG: {
1315 Tmp1 = LegalizeOp(Node->getOperand(0));
1316 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1317 assert(idx && "Operand must be a constant");
1318 Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1319 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1320 }
1321 break;
1322 case ISD::INSERT_SUBREG: {
1323 Tmp1 = LegalizeOp(Node->getOperand(0));
1324 Tmp2 = LegalizeOp(Node->getOperand(1));
1325 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1326 assert(idx && "Operand must be a constant");
1327 Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0));
1328 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1329 }
1330 break;
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001331 case ISD::BUILD_VECTOR:
1332 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner29b23012006-03-19 01:17:20 +00001333 default: assert(0 && "This action is not supported yet!");
1334 case TargetLowering::Custom:
1335 Tmp3 = TLI.LowerOperation(Result, DAG);
1336 if (Tmp3.Val) {
1337 Result = Tmp3;
1338 break;
1339 }
1340 // FALLTHROUGH
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001341 case TargetLowering::Expand:
1342 Result = ExpandBUILD_VECTOR(Result.Val);
Chris Lattnerf4e1a532006-03-19 00:52:58 +00001343 break;
Chris Lattner29b23012006-03-19 01:17:20 +00001344 }
Chris Lattner29b23012006-03-19 01:17:20 +00001345 break;
1346 case ISD::INSERT_VECTOR_ELT:
1347 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner29b23012006-03-19 01:17:20 +00001348 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman735ab3c2008-02-13 06:43:04 +00001349
1350 // The type of the value to insert may not be legal, even though the vector
1351 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1352 // here.
1353 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1354 default: assert(0 && "Cannot expand insert element operand");
1355 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1356 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
1357 }
Chris Lattner29b23012006-03-19 01:17:20 +00001358 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1359
1360 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1361 Node->getValueType(0))) {
1362 default: assert(0 && "This action is not supported yet!");
1363 case TargetLowering::Legal:
1364 break;
1365 case TargetLowering::Custom:
Nate Begeman5743da52008-01-05 20:47:37 +00001366 Tmp4 = TLI.LowerOperation(Result, DAG);
1367 if (Tmp4.Val) {
1368 Result = Tmp4;
Chris Lattner29b23012006-03-19 01:17:20 +00001369 break;
1370 }
1371 // FALLTHROUGH
1372 case TargetLowering::Expand: {
Chris Lattner326870b2006-04-17 19:21:01 +00001373 // If the insert index is a constant, codegen this as a scalar_to_vector,
1374 // then a shuffle that inserts it into the right position in the vector.
1375 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman735ab3c2008-02-13 06:43:04 +00001376 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1377 // match the element type of the vector being created.
1378 if (Tmp2.getValueType() ==
1379 MVT::getVectorElementType(Op.getValueType())) {
1380 SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
1381 Tmp1.getValueType(), Tmp2);
1382
1383 unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType());
1384 MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts);
1385 MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT);
1386
1387 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1388 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1389 // elt 0 of the RHS.
1390 SmallVector<SDOperand, 8> ShufOps;
1391 for (unsigned i = 0; i != NumElts; ++i) {
1392 if (i != InsertPos->getValue())
1393 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1394 else
1395 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1396 }
1397 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
1398 &ShufOps[0], ShufOps.size());
1399
1400 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1401 Tmp1, ScVec, ShufMask);
1402 Result = LegalizeOp(Result);
1403 break;
Chris Lattner326870b2006-04-17 19:21:01 +00001404 }
Chris Lattner326870b2006-04-17 19:21:01 +00001405 }
1406
Chris Lattner29b23012006-03-19 01:17:20 +00001407 // If the target doesn't support this, we have to spill the input vector
1408 // to a temporary stack slot, update the element, then reload it. This is
1409 // badness. We could also load the value into a vector register (either
1410 // with a "move to register" or "extload into register" instruction, then
1411 // permute it into place, if the idx is a constant and if the idx is
1412 // supported by the target.
Evan Cheng78e3d562006-04-08 01:46:37 +00001413 MVT::ValueType VT = Tmp1.getValueType();
Nate Begeman735ab3c2008-02-13 06:43:04 +00001414 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng78e3d562006-04-08 01:46:37 +00001415 MVT::ValueType IdxVT = Tmp3.getValueType();
1416 MVT::ValueType PtrVT = TLI.getPointerTy();
Chris Lattnerd6f7d442007-10-15 17:48:57 +00001417 SDOperand StackPtr = DAG.CreateStackTemporary(VT);
Dan Gohman2d489b52008-02-06 22:27:42 +00001418
Dan Gohman54d3b5a2008-02-11 18:58:42 +00001419 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val);
Dan Gohman2d489b52008-02-06 22:27:42 +00001420 int SPFI = StackPtrFI->getIndex();
1421
Evan Cheng168e45b2006-03-31 01:27:51 +00001422 // Store the vector.
Dan Gohman2d489b52008-02-06 22:27:42 +00001423 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001424 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00001425 SPFI);
Evan Cheng168e45b2006-03-31 01:27:51 +00001426
1427 // Truncate or zero extend offset to target pointer type.
Evan Cheng78e3d562006-04-08 01:46:37 +00001428 unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1429 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
Evan Cheng168e45b2006-03-31 01:27:51 +00001430 // Add the offset to the index.
Evan Cheng78e3d562006-04-08 01:46:37 +00001431 unsigned EltSize = MVT::getSizeInBits(EltVT)/8;
1432 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
1433 SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Evan Cheng168e45b2006-03-31 01:27:51 +00001434 // Store the scalar value.
Nate Begeman735ab3c2008-02-13 06:43:04 +00001435 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
1436 PseudoSourceValue::getFixedStack(), SPFI, EltVT);
Evan Cheng168e45b2006-03-31 01:27:51 +00001437 // Load the updated vector.
Dan Gohman2d489b52008-02-06 22:27:42 +00001438 Result = DAG.getLoad(VT, Ch, StackPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001439 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner29b23012006-03-19 01:17:20 +00001440 break;
1441 }
1442 }
1443 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001444 case ISD::SCALAR_TO_VECTOR:
Chris Lattner6be79822006-04-04 17:23:26 +00001445 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1446 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1447 break;
1448 }
1449
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001450 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1451 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1452 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1453 Node->getValueType(0))) {
1454 default: assert(0 && "This action is not supported yet!");
1455 case TargetLowering::Legal:
1456 break;
Chris Lattner79fb91c2006-03-19 06:47:21 +00001457 case TargetLowering::Custom:
1458 Tmp3 = TLI.LowerOperation(Result, DAG);
1459 if (Tmp3.Val) {
1460 Result = Tmp3;
1461 break;
1462 }
1463 // FALLTHROUGH
Chris Lattner6be79822006-04-04 17:23:26 +00001464 case TargetLowering::Expand:
1465 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001466 break;
1467 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00001468 break;
Chris Lattner21e68c82006-03-20 01:52:29 +00001469 case ISD::VECTOR_SHUFFLE:
Chris Lattner21e68c82006-03-20 01:52:29 +00001470 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1471 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1472 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1473
1474 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner6be79822006-04-04 17:23:26 +00001475 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1476 default: assert(0 && "Unknown operation action!");
1477 case TargetLowering::Legal:
1478 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1479 "vector shuffle should not be created if not legal!");
1480 break;
1481 case TargetLowering::Custom:
Evan Cheng9fa89592006-04-05 06:07:11 +00001482 Tmp3 = TLI.LowerOperation(Result, DAG);
1483 if (Tmp3.Val) {
1484 Result = Tmp3;
1485 break;
1486 }
1487 // FALLTHROUGH
1488 case TargetLowering::Expand: {
1489 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00001490 MVT::ValueType EltVT = MVT::getVectorElementType(VT);
Evan Cheng9fa89592006-04-05 06:07:11 +00001491 MVT::ValueType PtrVT = TLI.getPointerTy();
1492 SDOperand Mask = Node->getOperand(2);
1493 unsigned NumElems = Mask.getNumOperands();
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001494 SmallVector<SDOperand,8> Ops;
Evan Cheng9fa89592006-04-05 06:07:11 +00001495 for (unsigned i = 0; i != NumElems; ++i) {
1496 SDOperand Arg = Mask.getOperand(i);
1497 if (Arg.getOpcode() == ISD::UNDEF) {
1498 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1499 } else {
1500 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1501 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
1502 if (Idx < NumElems)
1503 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1504 DAG.getConstant(Idx, PtrVT)));
1505 else
1506 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1507 DAG.getConstant(Idx - NumElems, PtrVT)));
1508 }
1509 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001510 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6be79822006-04-04 17:23:26 +00001511 break;
Evan Cheng9fa89592006-04-05 06:07:11 +00001512 }
Chris Lattner6be79822006-04-04 17:23:26 +00001513 case TargetLowering::Promote: {
1514 // Change base type to a different vector type.
1515 MVT::ValueType OVT = Node->getValueType(0);
1516 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1517
1518 // Cast the two input vectors.
1519 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1520 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1521
1522 // Convert the shuffle mask to the right # elements.
1523 Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0);
1524 assert(Tmp3.Val && "Shuffle not legal?");
1525 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1526 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1527 break;
1528 }
Chris Lattner21e68c82006-03-20 01:52:29 +00001529 }
1530 break;
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001531
1532 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohmana8665142007-06-25 16:23:39 +00001533 Tmp1 = Node->getOperand(0);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001534 Tmp2 = LegalizeOp(Node->getOperand(1));
1535 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmana8665142007-06-25 16:23:39 +00001536 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner7c0cd8c2006-03-21 20:44:12 +00001537 break;
1538
Dan Gohmana8665142007-06-25 16:23:39 +00001539 case ISD::EXTRACT_SUBVECTOR:
1540 Tmp1 = Node->getOperand(0);
1541 Tmp2 = LegalizeOp(Node->getOperand(1));
1542 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1543 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman26455c42007-06-13 15:12:02 +00001544 break;
1545
Chris Lattner462505f2006-02-13 09:18:02 +00001546 case ISD::CALLSEQ_START: {
1547 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1548
1549 // Recursively Legalize all of the inputs of the call end that do not lead
1550 // to this call start. This ensures that any libcalls that need be inserted
1551 // are inserted *before* the CALLSEQ_START.
Chris Lattnerebeb48d2007-02-04 00:27:56 +00001552 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner462505f2006-02-13 09:18:02 +00001553 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Chris Lattner4488f0c2006-07-26 23:55:56 +00001554 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node,
1555 NodesLeadingTo);
1556 }
Chris Lattner462505f2006-02-13 09:18:02 +00001557
1558 // Now that we legalized all of the inputs (which may have inserted
1559 // libcalls) create the new CALLSEQ_START node.
1560 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1561
1562 // Merge in the last call, to ensure that this call start after the last
1563 // call ended.
Chris Lattner62f1b832006-05-17 18:00:08 +00001564 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnera1cec012006-05-17 17:55:45 +00001565 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1566 Tmp1 = LegalizeOp(Tmp1);
1567 }
Chris Lattner462505f2006-02-13 09:18:02 +00001568
1569 // Do not try to legalize the target-specific arguments (#1+).
1570 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001571 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner462505f2006-02-13 09:18:02 +00001572 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001573 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner462505f2006-02-13 09:18:02 +00001574 }
1575
1576 // Remember that the CALLSEQ_START is legalized.
Chris Lattner8e2ee732006-02-14 00:55:02 +00001577 AddLegalizedOperand(Op.getValue(0), Result);
1578 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1579 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1580
Chris Lattner462505f2006-02-13 09:18:02 +00001581 // Now that the callseq_start and all of the non-call nodes above this call
1582 // sequence have been legalized, legalize the call itself. During this
1583 // process, no libcalls can/will be inserted, guaranteeing that no calls
1584 // can overlap.
1585 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
1586 SDOperand InCallSEQ = LastCALLSEQ_END;
1587 // Note that we are selecting this call!
1588 LastCALLSEQ_END = SDOperand(CallEnd, 0);
1589 IsLegalizingCall = true;
1590
1591 // Legalize the call, starting from the CALLSEQ_END.
1592 LegalizeOp(LastCALLSEQ_END);
1593 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1594 return Result;
1595 }
Chris Lattner2dce7032005-05-12 23:24:06 +00001596 case ISD::CALLSEQ_END:
Chris Lattner462505f2006-02-13 09:18:02 +00001597 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1598 // will cause this node to be legalized as well as handling libcalls right.
1599 if (LastCALLSEQ_END.Val != Node) {
1600 LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0));
Chris Lattnered39c862007-02-04 00:50:02 +00001601 DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
Chris Lattner462505f2006-02-13 09:18:02 +00001602 assert(I != LegalizedNodes.end() &&
1603 "Legalizing the call start should have legalized this node!");
1604 return I->second;
1605 }
1606
1607 // Otherwise, the call start has been legalized and everything is going
1608 // according to plan. Just legalize ourselves normally here.
Chris Lattnerdc750592005-01-07 07:47:09 +00001609 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerccb44762006-01-29 07:58:15 +00001610 // Do not try to legalize the target-specific arguments (#1+), except for
1611 // an optional flag input.
1612 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1613 if (Tmp1 != Node->getOperand(0)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001614 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001615 Ops[0] = Tmp1;
Chris Lattner97af9d52006-08-08 01:09:31 +00001616 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001617 }
1618 } else {
1619 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1620 if (Tmp1 != Node->getOperand(0) ||
1621 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Chris Lattner97af9d52006-08-08 01:09:31 +00001622 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattnerccb44762006-01-29 07:58:15 +00001623 Ops[0] = Tmp1;
1624 Ops.back() = Tmp2;
Chris Lattner97af9d52006-08-08 01:09:31 +00001625 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerccb44762006-01-29 07:58:15 +00001626 }
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001627 }
Chris Lattner8e2ee732006-02-14 00:55:02 +00001628 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner462505f2006-02-13 09:18:02 +00001629 // This finishes up call legalization.
1630 IsLegalizingCall = false;
Chris Lattner8e2ee732006-02-14 00:55:02 +00001631
1632 // If the CALLSEQ_END node has a flag, remember that we legalized it.
1633 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
1634 if (Node->getNumValues() == 2)
1635 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1636 return Result.getValue(Op.ResNo);
Evan Cheng7f4ec822006-01-11 22:14:47 +00001637 case ISD::DYNAMIC_STACKALLOC: {
Evan Cheng631ccc62007-08-16 23:50:06 +00001638 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerec26b482005-01-09 19:03:49 +00001639 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1640 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1641 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001642 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +00001643
Chris Lattnerd02b0542006-01-28 10:58:55 +00001644 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +00001645 Tmp2 = Result.getValue(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001646 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Cheng7f4ec822006-01-11 22:14:47 +00001647 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +00001648 case TargetLowering::Expand: {
1649 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1650 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1651 " not tell us which reg is the stack pointer!");
1652 SDOperand Chain = Tmp1.getOperand(0);
Bill Wendlingf359fed2007-11-13 00:44:25 +00001653
1654 // Chain the dynamic stack allocation so that it doesn't modify the stack
1655 // pointer when other instructions are using the stack.
1656 Chain = DAG.getCALLSEQ_START(Chain,
1657 DAG.getConstant(0, TLI.getPointerTy()));
1658
Chris Lattner59b82f92006-01-15 08:54:32 +00001659 SDOperand Size = Tmp2.getOperand(1);
Evan Cheng631ccc62007-08-16 23:50:06 +00001660 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT);
1661 Chain = SP.getValue(1);
1662 unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue();
1663 unsigned StackAlign =
1664 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1665 if (Align > StackAlign)
Evan Chengcb6d65e2007-08-17 18:02:22 +00001666 SP = DAG.getNode(ISD::AND, VT, SP,
1667 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng631ccc62007-08-16 23:50:06 +00001668 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendlingf359fed2007-11-13 00:44:25 +00001669 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1670
1671 Tmp2 =
1672 DAG.getCALLSEQ_END(Chain,
1673 DAG.getConstant(0, TLI.getPointerTy()),
1674 DAG.getConstant(0, TLI.getPointerTy()),
1675 SDOperand());
1676
Chris Lattnerd02b0542006-01-28 10:58:55 +00001677 Tmp1 = LegalizeOp(Tmp1);
1678 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001679 break;
1680 }
1681 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +00001682 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
1683 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001684 Tmp1 = LegalizeOp(Tmp3);
1685 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +00001686 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001687 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001688 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +00001689 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001690 }
Chris Lattner59b82f92006-01-15 08:54:32 +00001691 // Since this op produce two values, make sure to remember that we
1692 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001693 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1694 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +00001695 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +00001696 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001697 case ISD::INLINEASM: {
Chris Lattner97af9d52006-08-08 01:09:31 +00001698 SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001699 bool Changed = false;
1700 // Legalize all of the operands of the inline asm, in case they are nodes
1701 // that need to be expanded or something. Note we skip the asm string and
1702 // all of the TargetConstant flags.
1703 SDOperand Op = LegalizeOp(Ops[0]);
1704 Changed = Op != Ops[0];
1705 Ops[0] = Op;
1706
1707 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1708 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
1709 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3;
1710 for (++i; NumVals; ++i, --NumVals) {
1711 SDOperand Op = LegalizeOp(Ops[i]);
1712 if (Op != Ops[i]) {
1713 Changed = true;
1714 Ops[i] = Op;
1715 }
1716 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001717 }
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001718
1719 if (HasInFlag) {
1720 Op = LegalizeOp(Ops.back());
1721 Changed |= Op != Ops.back();
1722 Ops.back() = Op;
1723 }
1724
1725 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00001726 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner476e67b2006-01-26 22:24:51 +00001727
1728 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001729 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +00001730 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1731 return Result.getValue(Op.ResNo);
Chris Lattner1b8ea1f2006-07-11 01:40:09 +00001732 }
Chris Lattner68a12142005-01-07 22:12:08 +00001733 case ISD::BR:
1734 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001735 // Ensure that libcalls are emitted before a branch.
1736 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1737 Tmp1 = LegalizeOp(Tmp1);
1738 LastCALLSEQ_END = DAG.getEntryNode();
1739
Chris Lattnerd02b0542006-01-28 10:58:55 +00001740 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +00001741 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001742 case ISD::BRIND:
1743 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1744 // Ensure that libcalls are emitted before a branch.
1745 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1746 Tmp1 = LegalizeOp(Tmp1);
1747 LastCALLSEQ_END = DAG.getEntryNode();
1748
1749 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1750 default: assert(0 && "Indirect target must be legal type (pointer)!");
1751 case Legal:
1752 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1753 break;
1754 }
1755 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1756 break;
Evan Cheng84a28d42006-10-30 08:00:44 +00001757 case ISD::BR_JT:
1758 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1759 // Ensure that libcalls are emitted before a branch.
1760 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1761 Tmp1 = LegalizeOp(Tmp1);
1762 LastCALLSEQ_END = DAG.getEntryNode();
1763
1764 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1765 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1766
1767 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1768 default: assert(0 && "This action is not supported yet!");
1769 case TargetLowering::Legal: break;
1770 case TargetLowering::Custom:
1771 Tmp1 = TLI.LowerOperation(Result, DAG);
1772 if (Tmp1.Val) Result = Tmp1;
1773 break;
1774 case TargetLowering::Expand: {
1775 SDOperand Chain = Result.getOperand(0);
1776 SDOperand Table = Result.getOperand(1);
1777 SDOperand Index = Result.getOperand(2);
1778
1779 MVT::ValueType PTy = TLI.getPointerTy();
Jim Laskey70323a82006-12-14 19:17:33 +00001780 MachineFunction &MF = DAG.getMachineFunction();
1781 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng84a28d42006-10-30 08:00:44 +00001782 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
1783 SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskey70323a82006-12-14 19:17:33 +00001784
1785 SDOperand LD;
1786 switch (EntrySize) {
1787 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman2d489b52008-02-06 22:27:42 +00001788 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001789 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman2d489b52008-02-06 22:27:42 +00001790 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00001791 PseudoSourceValue::getJumpTable(), 0); break;
Jim Laskey70323a82006-12-14 19:17:33 +00001792 }
1793
Evan Cheng797d56f2007-11-09 01:32:10 +00001794 Addr = LD;
Jim Laskey70323a82006-12-14 19:17:33 +00001795 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng84a28d42006-10-30 08:00:44 +00001796 // For PIC, the sequence is:
1797 // BRIND(load(Jumptable + index) + RelocBase)
Evan Cheng797d56f2007-11-09 01:32:10 +00001798 // RelocBase can be JumpTable, GOT or some sort of global base.
1799 if (PTy != MVT::i32)
1800 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1801 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1802 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng84a28d42006-10-30 08:00:44 +00001803 }
Evan Cheng797d56f2007-11-09 01:32:10 +00001804 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng84a28d42006-10-30 08:00:44 +00001805 }
1806 }
1807 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001808 case ISD::BRCOND:
1809 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001810 // Ensure that libcalls are emitted before a return.
1811 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1812 Tmp1 = LegalizeOp(Tmp1);
1813 LastCALLSEQ_END = DAG.getEntryNode();
1814
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001815 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1816 case Expand: assert(0 && "It's impossible to expand bools");
1817 case Legal:
1818 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1819 break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00001820 case Promote: {
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001821 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerdb189382006-11-27 04:39:56 +00001822
1823 // The top bits of the promoted condition are not necessarily zero, ensure
1824 // that the value is properly zero extended.
Dan Gohman1f372ed2008-02-25 21:11:39 +00001825 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00001826 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman1f372ed2008-02-25 21:11:39 +00001827 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerdb189382006-11-27 04:39:56 +00001828 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001829 break;
1830 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00001831 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001832
1833 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001834 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +00001835
1836 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1837 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001838 case TargetLowering::Legal: break;
1839 case TargetLowering::Custom:
1840 Tmp1 = TLI.LowerOperation(Result, DAG);
1841 if (Tmp1.Val) Result = Tmp1;
1842 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001843 case TargetLowering::Expand:
1844 // Expand brcond's setcc into its constituent parts and create a BR_CC
1845 // Node.
1846 if (Tmp2.getOpcode() == ISD::SETCC) {
1847 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1848 Tmp2.getOperand(0), Tmp2.getOperand(1),
1849 Node->getOperand(2));
1850 } else {
1851 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1852 DAG.getCondCode(ISD::SETNE), Tmp2,
1853 DAG.getConstant(0, Tmp2.getValueType()),
1854 Node->getOperand(2));
1855 }
1856 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001857 }
1858 break;
1859 case ISD::BR_CC:
1860 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00001861 // Ensure that libcalls are emitted before a branch.
1862 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1863 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman7e7f4392006-02-01 07:19:44 +00001864 Tmp2 = Node->getOperand(2); // LHS
1865 Tmp3 = Node->getOperand(3); // RHS
1866 Tmp4 = Node->getOperand(1); // CC
1867
1868 LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4);
Evan Chengadc80f92006-12-18 22:55:34 +00001869 LastCALLSEQ_END = DAG.getEntryNode();
1870
Nate Begeman7e7f4392006-02-01 07:19:44 +00001871 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
1872 // the LHS is a legal SETCC itself. In this case, we need to compare
1873 // the result against zero to select between true and false values.
1874 if (Tmp3.Val == 0) {
1875 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
1876 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001877 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00001878
1879 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
1880 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001881
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001882 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
1883 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001884 case TargetLowering::Legal: break;
1885 case TargetLowering::Custom:
1886 Tmp4 = TLI.LowerOperation(Result, DAG);
1887 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +00001888 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001889 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +00001890 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001891 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00001892 LoadSDNode *LD = cast<LoadSDNode>(Node);
1893 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
1894 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001895
Evan Chenge71fe34d2006-10-09 20:57:25 +00001896 ISD::LoadExtType ExtType = LD->getExtensionType();
1897 if (ExtType == ISD::NON_EXTLOAD) {
1898 MVT::ValueType VT = Node->getValueType(0);
1899 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
1900 Tmp3 = Result.getValue(0);
1901 Tmp4 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001902
Evan Chenge71fe34d2006-10-09 20:57:25 +00001903 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1904 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001905 case TargetLowering::Legal:
1906 // If this is an unaligned load and the target doesn't support it,
1907 // expand it.
1908 if (!TLI.allowsUnalignedMemoryAccesses()) {
1909 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001910 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001911 if (LD->getAlignment() < ABIAlignment){
1912 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
1913 TLI);
1914 Tmp3 = Result.getOperand(0);
1915 Tmp4 = Result.getOperand(1);
Dale Johannesen29e6ac42007-09-08 19:29:23 +00001916 Tmp3 = LegalizeOp(Tmp3);
1917 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00001918 }
1919 }
1920 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00001921 case TargetLowering::Custom:
1922 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
1923 if (Tmp1.Val) {
1924 Tmp3 = LegalizeOp(Tmp1);
1925 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001926 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001927 break;
1928 case TargetLowering::Promote: {
1929 // Only promote a load of vector type to another.
1930 assert(MVT::isVector(VT) && "Cannot promote this load!");
1931 // Change base type to a different vector type.
1932 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
1933
1934 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00001935 LD->getSrcValueOffset(),
1936 LD->isVolatile(), LD->getAlignment());
Evan Chenge71fe34d2006-10-09 20:57:25 +00001937 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
1938 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001939 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +00001940 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00001941 }
1942 // Since loads produce two values, make sure to remember that we
1943 // legalized both of them.
1944 AddLegalizedOperand(SDOperand(Node, 0), Tmp3);
1945 AddLegalizedOperand(SDOperand(Node, 1), Tmp4);
1946 return Op.ResNo ? Tmp4 : Tmp3;
1947 } else {
Dan Gohman47a7d6f2008-01-30 00:15:11 +00001948 MVT::ValueType SrcVT = LD->getMemoryVT();
Duncan Sands95d46ef2008-01-23 20:39:46 +00001949 unsigned SrcWidth = MVT::getSizeInBits(SrcVT);
1950 int SVOffset = LD->getSrcValueOffset();
1951 unsigned Alignment = LD->getAlignment();
1952 bool isVolatile = LD->isVolatile();
1953
1954 if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) &&
1955 // Some targets pretend to have an i1 loading operation, and actually
1956 // load an i8. This trick is correct for ZEXTLOAD because the top 7
1957 // bits are guaranteed to be zero; it helps the optimizers understand
1958 // that these bits are zero. It is also useful for EXTLOAD, since it
1959 // tells the optimizers that those bits are undefined. It would be
1960 // nice to have an effective generic way of getting these benefits...
1961 // Until such a way is found, don't insist on promoting i1 here.
1962 (SrcVT != MVT::i1 ||
1963 TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
1964 // Promote to a byte-sized load if not loading an integral number of
1965 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
1966 unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT);
1967 MVT::ValueType NVT = MVT::getIntegerType(NewWidth);
1968 SDOperand Ch;
1969
1970 // The extra bits are guaranteed to be zero, since we stored them that
1971 // way. A zext load from NVT thus automatically gives zext from SrcVT.
1972
1973 ISD::LoadExtType NewExtType =
1974 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
1975
1976 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
1977 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
1978 NVT, isVolatile, Alignment);
1979
1980 Ch = Result.getValue(1); // The chain.
1981
1982 if (ExtType == ISD::SEXTLOAD)
1983 // Having the top bits zero doesn't help when sign extending.
1984 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1985 Result, DAG.getValueType(SrcVT));
1986 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
1987 // All the top bits are guaranteed to be zero - inform the optimizers.
1988 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
1989 DAG.getValueType(SrcVT));
1990
1991 Tmp1 = LegalizeOp(Result);
1992 Tmp2 = LegalizeOp(Ch);
1993 } else if (SrcWidth & (SrcWidth - 1)) {
1994 // If not loading a power-of-2 number of bits, expand as two loads.
1995 assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) &&
1996 "Unsupported extload!");
1997 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
1998 assert(RoundWidth < SrcWidth);
1999 unsigned ExtraWidth = SrcWidth - RoundWidth;
2000 assert(ExtraWidth < RoundWidth);
2001 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2002 "Load size not an integral number of bytes!");
2003 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2004 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2005 SDOperand Lo, Hi, Ch;
2006 unsigned IncrementSize;
2007
2008 if (TLI.isLittleEndian()) {
2009 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2010 // Load the bottom RoundWidth bits.
2011 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2012 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2013 Alignment);
2014
2015 // Load the remaining ExtraWidth bits.
2016 IncrementSize = RoundWidth / 8;
2017 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2018 DAG.getIntPtrConstant(IncrementSize));
2019 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2020 LD->getSrcValue(), SVOffset + IncrementSize,
2021 ExtraVT, isVolatile,
2022 MinAlign(Alignment, IncrementSize));
2023
2024 // Build a factor node to remember that this load is independent of the
2025 // other one.
2026 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2027 Hi.getValue(1));
2028
2029 // Move the top bits to the right place.
2030 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2031 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2032
2033 // Join the hi and lo parts.
2034 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002035 } else {
Duncan Sands95d46ef2008-01-23 20:39:46 +00002036 // Big endian - avoid unaligned loads.
2037 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2038 // Load the top RoundWidth bits.
2039 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2040 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2041 Alignment);
2042
2043 // Load the remaining ExtraWidth bits.
2044 IncrementSize = RoundWidth / 8;
2045 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2046 DAG.getIntPtrConstant(IncrementSize));
2047 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2048 LD->getSrcValue(), SVOffset + IncrementSize,
2049 ExtraVT, isVolatile,
2050 MinAlign(Alignment, IncrementSize));
2051
2052 // Build a factor node to remember that this load is independent of the
2053 // other one.
2054 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2055 Hi.getValue(1));
2056
2057 // Move the top bits to the right place.
2058 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2059 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2060
2061 // Join the hi and lo parts.
2062 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2063 }
2064
2065 Tmp1 = LegalizeOp(Result);
2066 Tmp2 = LegalizeOp(Ch);
2067 } else {
2068 switch (TLI.getLoadXAction(ExtType, SrcVT)) {
2069 default: assert(0 && "This action is not supported yet!");
2070 case TargetLowering::Custom:
2071 isCustom = true;
2072 // FALLTHROUGH
2073 case TargetLowering::Legal:
2074 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2075 Tmp1 = Result.getValue(0);
2076 Tmp2 = Result.getValue(1);
2077
2078 if (isCustom) {
2079 Tmp3 = TLI.LowerOperation(Result, DAG);
2080 if (Tmp3.Val) {
2081 Tmp1 = LegalizeOp(Tmp3);
2082 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2083 }
2084 } else {
2085 // If this is an unaligned load and the target doesn't support it,
2086 // expand it.
2087 if (!TLI.allowsUnalignedMemoryAccesses()) {
2088 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002089 getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT()));
Duncan Sands95d46ef2008-01-23 20:39:46 +00002090 if (LD->getAlignment() < ABIAlignment){
2091 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG,
2092 TLI);
2093 Tmp1 = Result.getOperand(0);
2094 Tmp2 = Result.getOperand(1);
2095 Tmp1 = LegalizeOp(Tmp1);
2096 Tmp2 = LegalizeOp(Tmp2);
2097 }
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002098 }
2099 }
Duncan Sands95d46ef2008-01-23 20:39:46 +00002100 break;
2101 case TargetLowering::Expand:
2102 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2103 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
2104 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
2105 LD->getSrcValueOffset(),
2106 LD->isVolatile(), LD->getAlignment());
2107 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2108 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2109 Tmp2 = LegalizeOp(Load.getValue(1));
2110 break;
2111 }
2112 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2113 // Turn the unsupported load into an EXTLOAD followed by an explicit
2114 // zero/sign extend inreg.
2115 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2116 Tmp1, Tmp2, LD->getSrcValue(),
2117 LD->getSrcValueOffset(), SrcVT,
2118 LD->isVolatile(), LD->getAlignment());
2119 SDOperand ValRes;
2120 if (ExtType == ISD::SEXTLOAD)
2121 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2122 Result, DAG.getValueType(SrcVT));
2123 else
2124 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2125 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2126 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002127 break;
2128 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00002129 }
Duncan Sands95d46ef2008-01-23 20:39:46 +00002130
Evan Chenge71fe34d2006-10-09 20:57:25 +00002131 // Since loads produce two values, make sure to remember that we legalized
2132 // both of them.
2133 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2134 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2135 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +00002136 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +00002137 }
Nate Begeman5172ce62005-10-19 00:06:56 +00002138 case ISD::EXTRACT_ELEMENT: {
2139 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
2140 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002141 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +00002142 case Legal:
2143 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
2144 // 1 -> Hi
2145 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
2146 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
2147 TLI.getShiftAmountTy()));
2148 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2149 } else {
2150 // 0 -> Lo
2151 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2152 Node->getOperand(0));
2153 }
Nate Begeman5172ce62005-10-19 00:06:56 +00002154 break;
2155 case Expand:
2156 // Get both the low and high parts.
2157 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2158 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
2159 Result = Tmp2; // 1 -> Hi
2160 else
2161 Result = Tmp1; // 0 -> Lo
2162 break;
2163 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002164 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00002165 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002166
2167 case ISD::CopyToReg:
2168 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00002169
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002170 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00002171 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +00002172 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +00002173 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002174 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002175 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00002176 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002177 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +00002178 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +00002179 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002180 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2181 Tmp3);
2182 } else {
2183 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +00002184 }
2185
2186 // Since this produces two values, make sure to remember that we legalized
2187 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002188 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +00002189 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002190 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +00002191 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002192 break;
2193
2194 case ISD::RET:
2195 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner462505f2006-02-13 09:18:02 +00002196
2197 // Ensure that libcalls are emitted before a return.
2198 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2199 Tmp1 = LegalizeOp(Tmp1);
2200 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002201
Chris Lattnerdc750592005-01-07 07:47:09 +00002202 switch (Node->getNumOperands()) {
Evan Chenga2e99532006-05-26 23:09:09 +00002203 case 3: // ret val
Evan Cheng7256b0a2006-04-11 06:33:39 +00002204 Tmp2 = Node->getOperand(1);
Evan Chenga2e99532006-05-26 23:09:09 +00002205 Tmp3 = Node->getOperand(2); // Signness
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002206 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattnerdc750592005-01-07 07:47:09 +00002207 case Legal:
Evan Chenga2e99532006-05-26 23:09:09 +00002208 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattnerdc750592005-01-07 07:47:09 +00002209 break;
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002210 case Expand:
Dan Gohmana8665142007-06-25 16:23:39 +00002211 if (!MVT::isVector(Tmp2.getValueType())) {
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002212 SDOperand Lo, Hi;
2213 ExpandOp(Tmp2, Lo, Hi);
Chris Lattner13780ac2007-03-06 20:01:06 +00002214
2215 // Big endian systems want the hi reg first.
Duncan Sands7377f5f2008-02-11 10:37:04 +00002216 if (TLI.isBigEndian())
Chris Lattner13780ac2007-03-06 20:01:06 +00002217 std::swap(Lo, Hi);
2218
Evan Cheng3432ab92006-12-11 19:27:14 +00002219 if (Hi.Val)
2220 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2221 else
2222 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattner451b0992006-08-21 20:24:53 +00002223 Result = LegalizeOp(Result);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002224 } else {
2225 SDNode *InVal = Tmp2.Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00002226 int InIx = Tmp2.ResNo;
2227 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
2228 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002229
Dan Gohmana8665142007-06-25 16:23:39 +00002230 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002231 // type. If so, convert to the vector type.
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002232 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002233 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002234 // Turn this into a return of the vector type.
Dan Gohmana8665142007-06-25 16:23:39 +00002235 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002236 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002237 } else if (NumElems == 1) {
2238 // Turn this into a return of the scalar type.
Dan Gohmana8665142007-06-25 16:23:39 +00002239 Tmp2 = ScalarizeVectorOp(Tmp2);
2240 Tmp2 = LegalizeOp(Tmp2);
Evan Chenga2e99532006-05-26 23:09:09 +00002241 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002242
2243 // FIXME: Returns of gcc generic vectors smaller than a legal type
2244 // should be returned in integer registers!
2245
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002246 // The scalarized value type may not be legal, e.g. it might require
2247 // promotion or expansion. Relegalize the return.
2248 Result = LegalizeOp(Result);
2249 } else {
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002250 // FIXME: Returns of gcc generic vectors larger than a legal vector
2251 // type should be returned by reference!
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002252 SDOperand Lo, Hi;
2253 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattner296a83c2007-02-01 04:55:59 +00002254 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattner2eb22ee2006-04-11 01:31:51 +00002255 Result = LegalizeOp(Result);
2256 }
2257 }
Misha Brukman835702a2005-04-21 22:36:52 +00002258 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002259 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002260 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Chenga2e99532006-05-26 23:09:09 +00002261 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002262 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002263 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002264 }
2265 break;
2266 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00002267 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00002268 break;
2269 default: { // ret <values>
Chris Lattner97af9d52006-08-08 01:09:31 +00002270 SmallVector<SDOperand, 8> NewValues;
Chris Lattnerdc750592005-01-07 07:47:09 +00002271 NewValues.push_back(Tmp1);
Evan Chenga2e99532006-05-26 23:09:09 +00002272 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattnerdc750592005-01-07 07:47:09 +00002273 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2274 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002275 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Chenga2e99532006-05-26 23:09:09 +00002276 NewValues.push_back(Node->getOperand(i+1));
Chris Lattnerdc750592005-01-07 07:47:09 +00002277 break;
2278 case Expand: {
2279 SDOperand Lo, Hi;
Dan Gohman3b62d722007-06-27 16:08:04 +00002280 assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) &&
Chris Lattner6cf3bbb2006-04-11 02:00:08 +00002281 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002282 ExpandOp(Node->getOperand(i), Lo, Hi);
2283 NewValues.push_back(Lo);
Evan Chenga2e99532006-05-26 23:09:09 +00002284 NewValues.push_back(Node->getOperand(i+1));
Evan Cheng3432ab92006-12-11 19:27:14 +00002285 if (Hi.Val) {
2286 NewValues.push_back(Hi);
2287 NewValues.push_back(Node->getOperand(i+1));
2288 }
Misha Brukman835702a2005-04-21 22:36:52 +00002289 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002290 }
2291 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00002292 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002293 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002294
2295 if (NewValues.size() == Node->getNumOperands())
Chris Lattner97af9d52006-08-08 01:09:31 +00002296 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerd02b0542006-01-28 10:58:55 +00002297 else
Chris Lattner97af9d52006-08-08 01:09:31 +00002298 Result = DAG.getNode(ISD::RET, MVT::Other,
2299 &NewValues[0], NewValues.size());
Chris Lattnerdc750592005-01-07 07:47:09 +00002300 break;
2301 }
2302 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002303
Chris Lattner4d1ea712006-01-29 21:02:23 +00002304 if (Result.getOpcode() == ISD::RET) {
2305 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2306 default: assert(0 && "This action is not supported yet!");
2307 case TargetLowering::Legal: break;
2308 case TargetLowering::Custom:
2309 Tmp1 = TLI.LowerOperation(Result, DAG);
2310 if (Tmp1.Val) Result = Tmp1;
2311 break;
2312 }
Evan Chengf35b1c82006-01-06 00:41:43 +00002313 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002314 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002315 case ISD::STORE: {
Evan Chengab51cf22006-10-13 21:14:26 +00002316 StoreSDNode *ST = cast<StoreSDNode>(Node);
2317 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2318 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohman2af30632007-07-09 22:18:38 +00002319 int SVOffset = ST->getSrcValueOffset();
2320 unsigned Alignment = ST->getAlignment();
2321 bool isVolatile = ST->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00002322
Evan Chengab51cf22006-10-13 21:14:26 +00002323 if (!ST->isTruncatingStore()) {
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002324 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2325 // FIXME: We shouldn't do this for TargetConstantFP's.
2326 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2327 // to phase ordering between legalized code and the dag combiner. This
2328 // probably means that we need to integrate dag combiner and legalizer
2329 // together.
Dale Johannesen98d3a082007-09-14 22:26:36 +00002330 // We generally can't do this one for long doubles.
Chris Lattner5e6fe052007-10-13 06:35:54 +00002331 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002332 if (CFP->getValueType(0) == MVT::f32 &&
2333 getTypeAction(MVT::i32) == Legal) {
Dale Johannesen028084e2007-09-12 03:30:33 +00002334 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
2335 convertToAPInt().getZExtValue(),
Dale Johannesen245dceb2007-09-11 18:32:33 +00002336 MVT::i32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00002337 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2338 SVOffset, isVolatile, Alignment);
2339 break;
2340 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattnerb193517e2007-10-15 05:46:06 +00002341 // If this target supports 64-bit registers, do a single 64-bit store.
2342 if (getTypeAction(MVT::i64) == Legal) {
2343 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
2344 getZExtValue(), MVT::i64);
2345 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2346 SVOffset, isVolatile, Alignment);
2347 break;
2348 } else if (getTypeAction(MVT::i32) == Legal) {
2349 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2350 // stores. If the target supports neither 32- nor 64-bits, this
2351 // xform is certainly not worth it.
2352 uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue();
2353 SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32);
2354 SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32);
Duncan Sands7377f5f2008-02-11 10:37:04 +00002355 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattnerb193517e2007-10-15 05:46:06 +00002356
2357 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2358 SVOffset, isVolatile, Alignment);
2359 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner72733e52008-01-17 07:00:52 +00002360 DAG.getIntPtrConstant(4));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002361 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sands1826ded2007-10-28 12:59:45 +00002362 isVolatile, MinAlign(Alignment, 4U));
Chris Lattnerb193517e2007-10-15 05:46:06 +00002363
2364 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2365 break;
2366 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002367 }
Chris Lattner6ba11fb2006-12-12 04:18:56 +00002368 }
2369
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002370 switch (getTypeAction(ST->getMemoryVT())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002371 case Legal: {
2372 Tmp3 = LegalizeOp(ST->getValue());
2373 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2374 ST->getOffset());
Evan Cheng31d15fa2005-12-23 07:29:34 +00002375
Evan Chengab51cf22006-10-13 21:14:26 +00002376 MVT::ValueType VT = Tmp3.getValueType();
2377 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2378 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002379 case TargetLowering::Legal:
2380 // If this is an unaligned store and the target doesn't support it,
2381 // expand it.
2382 if (!TLI.allowsUnalignedMemoryAccesses()) {
2383 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002384 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002385 if (ST->getAlignment() < ABIAlignment)
2386 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2387 TLI);
2388 }
2389 break;
Evan Chengab51cf22006-10-13 21:14:26 +00002390 case TargetLowering::Custom:
2391 Tmp1 = TLI.LowerOperation(Result, DAG);
2392 if (Tmp1.Val) Result = Tmp1;
2393 break;
2394 case TargetLowering::Promote:
2395 assert(MVT::isVector(VT) && "Unknown legal promote case!");
2396 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2397 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2398 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohman2af30632007-07-09 22:18:38 +00002399 ST->getSrcValue(), SVOffset, isVolatile,
2400 Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002401 break;
2402 }
2403 break;
2404 }
2405 case Promote:
2406 // Truncate the value and store the result.
2407 Tmp3 = PromoteOp(ST->getValue());
2408 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002409 SVOffset, ST->getMemoryVT(),
Dan Gohman2af30632007-07-09 22:18:38 +00002410 isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002411 break;
2412
2413 case Expand:
2414 unsigned IncrementSize = 0;
2415 SDOperand Lo, Hi;
2416
2417 // If this is a vector type, then we have to calculate the increment as
2418 // the product of the element size in bytes, and the number of elements
2419 // in the high half of the vector.
Dan Gohmana8665142007-06-25 16:23:39 +00002420 if (MVT::isVector(ST->getValue().getValueType())) {
Evan Chengab51cf22006-10-13 21:14:26 +00002421 SDNode *InVal = ST->getValue().Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00002422 int InIx = ST->getValue().ResNo;
Chris Lattner72733e52008-01-17 07:00:52 +00002423 MVT::ValueType InVT = InVal->getValueType(InIx);
2424 unsigned NumElems = MVT::getVectorNumElements(InVT);
2425 MVT::ValueType EVT = MVT::getVectorElementType(InVT);
Evan Chengab51cf22006-10-13 21:14:26 +00002426
Dan Gohmana8665142007-06-25 16:23:39 +00002427 // Figure out if there is a simple type corresponding to this Vector
Reid Spencer09575ba2007-02-15 03:39:18 +00002428 // type. If so, convert to the vector type.
Evan Chengab51cf22006-10-13 21:14:26 +00002429 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
Dan Gohmana8665142007-06-25 16:23:39 +00002430 if (TLI.isTypeLegal(TVT)) {
Reid Spencer09575ba2007-02-15 03:39:18 +00002431 // Turn this into a normal store of the vector type.
Dan Gohmana36ade52008-02-15 18:11:59 +00002432 Tmp3 = LegalizeOp(ST->getValue());
Evan Chengab51cf22006-10-13 21:14:26 +00002433 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002434 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002435 Result = LegalizeOp(Result);
2436 break;
2437 } else if (NumElems == 1) {
2438 // Turn this into a normal store of the scalar type.
Dan Gohmana36ade52008-02-15 18:11:59 +00002439 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Chengab51cf22006-10-13 21:14:26 +00002440 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002441 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002442 // The scalarized value type may not be legal, e.g. it might require
2443 // promotion or expansion. Relegalize the scalar store.
2444 Result = LegalizeOp(Result);
2445 break;
2446 } else {
Dan Gohmana36ade52008-02-15 18:11:59 +00002447 SplitVectorOp(ST->getValue(), Lo, Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00002448 IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) *
2449 MVT::getSizeInBits(EVT)/8;
Evan Chengab51cf22006-10-13 21:14:26 +00002450 }
2451 } else {
Dan Gohmana36ade52008-02-15 18:11:59 +00002452 ExpandOp(ST->getValue(), Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00002453 IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0;
Evan Chengab51cf22006-10-13 21:14:26 +00002454
Duncan Sands7377f5f2008-02-11 10:37:04 +00002455 if (TLI.isBigEndian())
Evan Chengab51cf22006-10-13 21:14:26 +00002456 std::swap(Lo, Hi);
2457 }
2458
2459 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002460 SVOffset, isVolatile, Alignment);
Evan Cheng0076ca02006-12-12 19:53:13 +00002461
2462 if (Hi.Val == NULL) {
2463 // Must be int <-> float one-to-one expansion.
2464 Result = Lo;
2465 break;
2466 }
2467
Evan Chengab51cf22006-10-13 21:14:26 +00002468 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner72733e52008-01-17 07:00:52 +00002469 DAG.getIntPtrConstant(IncrementSize));
Evan Chengab51cf22006-10-13 21:14:26 +00002470 assert(isTypeLegal(Tmp2.getValueType()) &&
2471 "Pointers must be legal!");
Dan Gohman2af30632007-07-09 22:18:38 +00002472 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00002473 Alignment = MinAlign(Alignment, IncrementSize);
Evan Chengab51cf22006-10-13 21:14:26 +00002474 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00002475 SVOffset, isVolatile, Alignment);
Evan Chengab51cf22006-10-13 21:14:26 +00002476 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2477 break;
2478 }
2479 } else {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00002480 switch (getTypeAction(ST->getValue().getValueType())) {
2481 case Legal:
2482 Tmp3 = LegalizeOp(ST->getValue());
2483 break;
2484 case Promote:
2485 // We can promote the value, the truncstore will still take care of it.
2486 Tmp3 = PromoteOp(ST->getValue());
2487 break;
2488 case Expand:
2489 // Just store the low part. This may become a non-trunc store, so make
2490 // sure to use getTruncStore, not UpdateNodeOperands below.
2491 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2492 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2493 SVOffset, MVT::i8, isVolatile, Alignment);
2494 }
Evan Chengab51cf22006-10-13 21:14:26 +00002495
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002496 MVT::ValueType StVT = ST->getMemoryVT();
Duncan Sands88de26c2008-01-22 07:17:34 +00002497 unsigned StWidth = MVT::getSizeInBits(StVT);
2498
2499 if (StWidth != MVT::getStoreSizeInBits(StVT)) {
2500 // Promote to a byte-sized store with upper bits zero if not
2501 // storing an integral number of bytes. For example, promote
2502 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
2503 MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT));
2504 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2505 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2506 SVOffset, NVT, isVolatile, Alignment);
2507 } else if (StWidth & (StWidth - 1)) {
2508 // If not storing a power-of-2 number of bits, expand as two stores.
2509 assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) &&
2510 "Unsupported truncstore!");
2511 unsigned RoundWidth = 1 << Log2_32(StWidth);
2512 assert(RoundWidth < StWidth);
2513 unsigned ExtraWidth = StWidth - RoundWidth;
2514 assert(ExtraWidth < RoundWidth);
2515 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2516 "Store size not an integral number of bytes!");
2517 MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth);
2518 MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth);
2519 SDOperand Lo, Hi;
2520 unsigned IncrementSize;
2521
2522 if (TLI.isLittleEndian()) {
2523 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2524 // Store the bottom RoundWidth bits.
2525 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2526 SVOffset, RoundVT,
2527 isVolatile, Alignment);
2528
2529 // Store the remaining ExtraWidth bits.
2530 IncrementSize = RoundWidth / 8;
2531 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2532 DAG.getIntPtrConstant(IncrementSize));
2533 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2534 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2535 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2536 SVOffset + IncrementSize, ExtraVT, isVolatile,
2537 MinAlign(Alignment, IncrementSize));
2538 } else {
2539 // Big endian - avoid unaligned stores.
2540 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2541 // Store the top RoundWidth bits.
2542 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2543 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2544 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2545 RoundVT, isVolatile, Alignment);
2546
2547 // Store the remaining ExtraWidth bits.
2548 IncrementSize = RoundWidth / 8;
2549 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2550 DAG.getIntPtrConstant(IncrementSize));
2551 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2552 SVOffset + IncrementSize, ExtraVT, isVolatile,
2553 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venancio0db44182007-08-01 19:34:21 +00002554 }
Duncan Sands88de26c2008-01-22 07:17:34 +00002555
2556 // The order of the stores doesn't matter.
2557 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2558 } else {
2559 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2560 Tmp2 != ST->getBasePtr())
2561 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2562 ST->getOffset());
2563
2564 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2565 default: assert(0 && "This action is not supported yet!");
2566 case TargetLowering::Legal:
2567 // If this is an unaligned store and the target doesn't support it,
2568 // expand it.
2569 if (!TLI.allowsUnalignedMemoryAccesses()) {
2570 unsigned ABIAlignment = TLI.getTargetData()->
Dan Gohman47a7d6f2008-01-30 00:15:11 +00002571 getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT()));
Duncan Sands88de26c2008-01-22 07:17:34 +00002572 if (ST->getAlignment() < ABIAlignment)
2573 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG,
2574 TLI);
2575 }
2576 break;
2577 case TargetLowering::Custom:
2578 Result = TLI.LowerOperation(Result, DAG);
2579 break;
2580 case Expand:
2581 // TRUNCSTORE:i16 i32 -> STORE i16
2582 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2583 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2584 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2585 isVolatile, Alignment);
2586 break;
2587 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00002588 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002589 }
2590 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00002591 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00002592 case ISD::PCMARKER:
2593 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002594 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00002595 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002596 case ISD::STACKSAVE:
2597 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002598 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2599 Tmp1 = Result.getValue(0);
2600 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002601
Chris Lattnerb3266452006-01-13 02:50:02 +00002602 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2603 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002604 case TargetLowering::Legal: break;
2605 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002606 Tmp3 = TLI.LowerOperation(Result, DAG);
2607 if (Tmp3.Val) {
2608 Tmp1 = LegalizeOp(Tmp3);
2609 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00002610 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002611 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002612 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002613 // Expand to CopyFromReg if the target set
2614 // StackPointerRegisterToSaveRestore.
2615 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002616 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00002617 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002618 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002619 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002620 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2621 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00002622 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002623 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00002624 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002625
2626 // Since stacksave produce two values, make sure to remember that we
2627 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002628 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2629 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
2630 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002631
Chris Lattnerb3266452006-01-13 02:50:02 +00002632 case ISD::STACKRESTORE:
2633 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2634 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00002635 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00002636
2637 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2638 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002639 case TargetLowering::Legal: break;
2640 case TargetLowering::Custom:
2641 Tmp1 = TLI.LowerOperation(Result, DAG);
2642 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00002643 break;
2644 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00002645 // Expand to CopyToReg if the target set
2646 // StackPointerRegisterToSaveRestore.
2647 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2648 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2649 } else {
2650 Result = Tmp1;
2651 }
Chris Lattnerb3266452006-01-13 02:50:02 +00002652 break;
2653 }
2654 break;
2655
Andrew Lenharth01aa5632005-11-11 16:47:30 +00002656 case ISD::READCYCLECOUNTER:
2657 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00002658 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng69739932006-11-29 08:26:18 +00002659 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2660 Node->getValueType(0))) {
2661 default: assert(0 && "This action is not supported yet!");
Evan Chenga743fad2006-11-29 19:13:47 +00002662 case TargetLowering::Legal:
2663 Tmp1 = Result.getValue(0);
2664 Tmp2 = Result.getValue(1);
2665 break;
Evan Cheng69739932006-11-29 08:26:18 +00002666 case TargetLowering::Custom:
2667 Result = TLI.LowerOperation(Result, DAG);
Evan Chenga743fad2006-11-29 19:13:47 +00002668 Tmp1 = LegalizeOp(Result.getValue(0));
2669 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Cheng69739932006-11-29 08:26:18 +00002670 break;
2671 }
Andrew Lenharth73420b32005-12-02 04:56:24 +00002672
2673 // Since rdcc produce two values, make sure to remember that we legalized
2674 // both of them.
Evan Chenga743fad2006-11-29 19:13:47 +00002675 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
2676 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattnerd02b0542006-01-28 10:58:55 +00002677 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00002678
Chris Lattner39c67442005-01-14 22:08:15 +00002679 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002680 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2681 case Expand: assert(0 && "It's impossible to expand bools");
2682 case Legal:
2683 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2684 break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00002685 case Promote: {
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002686 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattner3abb6362006-11-28 01:03:30 +00002687 // Make sure the condition is either zero or one.
Dan Gohman1f372ed2008-02-25 21:11:39 +00002688 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002689 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman1f372ed2008-02-25 21:11:39 +00002690 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattner3abb6362006-11-28 01:03:30 +00002691 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002692 break;
2693 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00002694 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002695 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00002696 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00002697
Chris Lattnerd02b0542006-01-28 10:58:55 +00002698 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002699
Nate Begeman987121a2005-08-23 04:29:48 +00002700 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00002701 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002702 case TargetLowering::Legal: break;
2703 case TargetLowering::Custom: {
2704 Tmp1 = TLI.LowerOperation(Result, DAG);
2705 if (Tmp1.Val) Result = Tmp1;
2706 break;
2707 }
Nate Begemane5b86d72005-08-10 20:51:12 +00002708 case TargetLowering::Expand:
2709 if (Tmp1.getOpcode() == ISD::SETCC) {
2710 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2711 Tmp2, Tmp3,
2712 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2713 } else {
2714 Result = DAG.getSelectCC(Tmp1,
2715 DAG.getConstant(0, Tmp1.getValueType()),
2716 Tmp2, Tmp3, ISD::SETNE);
2717 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002718 break;
2719 case TargetLowering::Promote: {
2720 MVT::ValueType NVT =
2721 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2722 unsigned ExtOp, TruncOp;
Evan Chengbe8a8932006-04-12 16:33:18 +00002723 if (MVT::isVector(Tmp2.getValueType())) {
2724 ExtOp = ISD::BIT_CONVERT;
2725 TruncOp = ISD::BIT_CONVERT;
2726 } else if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002727 ExtOp = ISD::ANY_EXTEND;
2728 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002729 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002730 ExtOp = ISD::FP_EXTEND;
2731 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002732 }
2733 // Promote each of the values to the new type.
2734 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2735 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2736 // Perform the larger operation, then round down.
2737 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner72733e52008-01-17 07:00:52 +00002738 if (TruncOp != ISD::FP_ROUND)
2739 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2740 else
2741 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2742 DAG.getIntPtrConstant(0));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002743 break;
2744 }
2745 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002746 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002747 case ISD::SELECT_CC: {
2748 Tmp1 = Node->getOperand(0); // LHS
2749 Tmp2 = Node->getOperand(1); // RHS
Nate Begemane5b86d72005-08-10 20:51:12 +00002750 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2751 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Nate Begeman7e7f4392006-02-01 07:19:44 +00002752 SDOperand CC = Node->getOperand(4);
Nate Begemane5b86d72005-08-10 20:51:12 +00002753
Nate Begeman7e7f4392006-02-01 07:19:44 +00002754 LegalizeSetCCOperands(Tmp1, Tmp2, CC);
2755
2756 // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands,
2757 // the LHS is a legal SETCC itself. In this case, we need to compare
2758 // the result against zero to select between true and false values.
2759 if (Tmp2.Val == 0) {
2760 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2761 CC = DAG.getCondCode(ISD::SETNE);
2762 }
2763 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2764
2765 // Everything is legal, see if we should expand this op or something.
2766 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2767 default: assert(0 && "This action is not supported yet!");
2768 case TargetLowering::Legal: break;
2769 case TargetLowering::Custom:
2770 Tmp1 = TLI.LowerOperation(Result, DAG);
2771 if (Tmp1.Val) Result = Tmp1;
Nate Begemane5b86d72005-08-10 20:51:12 +00002772 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002773 }
2774 break;
Nate Begeman7e7f4392006-02-01 07:19:44 +00002775 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002776 case ISD::SETCC:
Nate Begeman7e7f4392006-02-01 07:19:44 +00002777 Tmp1 = Node->getOperand(0);
2778 Tmp2 = Node->getOperand(1);
2779 Tmp3 = Node->getOperand(2);
2780 LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3);
2781
2782 // If we had to Expand the SetCC operands into a SELECT node, then it may
2783 // not always be possible to return a true LHS & RHS. In this case, just
2784 // return the value we legalized, returned in the LHS
2785 if (Tmp2.Val == 0) {
2786 Result = Tmp1;
Chris Lattnerdc750592005-01-07 07:47:09 +00002787 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002788 }
Nate Begeman987121a2005-08-23 04:29:48 +00002789
Chris Lattnerf263a232006-01-30 22:43:50 +00002790 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002791 default: assert(0 && "Cannot handle this action for SETCC yet!");
2792 case TargetLowering::Custom:
2793 isCustom = true;
2794 // FALLTHROUGH.
2795 case TargetLowering::Legal:
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002796 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002797 if (isCustom) {
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002798 Tmp4 = TLI.LowerOperation(Result, DAG);
2799 if (Tmp4.Val) Result = Tmp4;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002800 }
Nate Begeman987121a2005-08-23 04:29:48 +00002801 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002802 case TargetLowering::Promote: {
2803 // First step, figure out the appropriate operation to use.
2804 // Allow SETCC to not be supported for all legal data types
2805 // Mostly this targets FP
2806 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
Chris Lattnerebeb48d2007-02-04 00:27:56 +00002807 MVT::ValueType OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002808
2809 // Scan for the appropriate larger type to use.
2810 while (1) {
2811 NewInTy = (MVT::ValueType)(NewInTy+1);
2812
2813 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
2814 "Fell off of the edge of the integer world");
2815 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
2816 "Fell off of the edge of the floating point world");
2817
2818 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00002819 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002820 break;
2821 }
2822 if (MVT::isInteger(NewInTy))
2823 assert(0 && "Cannot promote Legal Integer SETCC yet");
2824 else {
2825 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2826 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2827 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002828 Tmp1 = LegalizeOp(Tmp1);
2829 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002830 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng6f86a7d2006-01-17 19:47:13 +00002831 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00002832 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00002833 }
Nate Begeman987121a2005-08-23 04:29:48 +00002834 case TargetLowering::Expand:
2835 // Expand a setcc node into a select_cc of the same condition, lhs, and
2836 // rhs that selects between const 1 (true) and const 0 (false).
2837 MVT::ValueType VT = Node->getValueType(0);
2838 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2839 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng35fdd5f2006-12-15 02:59:56 +00002840 Tmp3);
Nate Begeman987121a2005-08-23 04:29:48 +00002841 break;
2842 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002843 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00002844 case ISD::MEMSET:
2845 case ISD::MEMCPY:
2846 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00002847 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002848 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
2849
2850 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
2851 switch (getTypeAction(Node->getOperand(2).getValueType())) {
2852 case Expand: assert(0 && "Cannot expand a byte!");
2853 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002854 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002855 break;
2856 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00002857 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002858 break;
2859 }
2860 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00002861 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002862 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00002863
2864 SDOperand Tmp4;
2865 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00002866 case Expand: {
2867 // Length is too big, just take the lo-part of the length.
2868 SDOperand HiPart;
Chris Lattner94c231f2006-11-07 04:11:44 +00002869 ExpandOp(Node->getOperand(3), Tmp4, HiPart);
Chris Lattnerba08a332005-07-13 01:42:45 +00002870 break;
2871 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002872 case Legal:
2873 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002874 break;
2875 case Promote:
2876 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00002877 break;
2878 }
2879
2880 SDOperand Tmp5;
2881 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
2882 case Expand: assert(0 && "Cannot expand this yet!");
2883 case Legal:
2884 Tmp5 = LegalizeOp(Node->getOperand(4));
2885 break;
2886 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00002887 Tmp5 = PromoteOp(Node->getOperand(4));
2888 break;
2889 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002890
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002891 SDOperand Tmp6;
2892 switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool
2893 case Expand: assert(0 && "Cannot expand this yet!");
2894 case Legal:
2895 Tmp6 = LegalizeOp(Node->getOperand(5));
2896 break;
2897 case Promote:
2898 Tmp6 = PromoteOp(Node->getOperand(5));
2899 break;
2900 }
2901
Chris Lattner3c0dd462005-01-16 07:29:19 +00002902 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
2903 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002904 case TargetLowering::Custom:
2905 isCustom = true;
2906 // FALLTHROUGH
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002907 case TargetLowering::Legal: {
2908 SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 };
2909 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002910 if (isCustom) {
2911 Tmp1 = TLI.LowerOperation(Result, DAG);
2912 if (Tmp1.Val) Result = Tmp1;
2913 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002914 break;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00002915 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002916 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00002917 // Otherwise, the target does not support this operation. Lower the
2918 // operation to an explicit libcall as appropriate.
2919 MVT::ValueType IntPtr = TLI.getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00002920 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00002921 TargetLowering::ArgListTy Args;
2922 TargetLowering::ArgListEntry Entry;
Chris Lattner85d70c62005-01-11 05:57:22 +00002923
Reid Spencer6dced922005-01-12 14:53:45 +00002924 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00002925 if (Node->getOpcode() == ISD::MEMSET) {
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002926 Entry.Node = Tmp2; Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00002927 Args.push_back(Entry);
Chris Lattner486d1bc2006-02-20 06:38:35 +00002928 // Extend the (previously legalized) ubyte argument to be an int value
2929 // for the call.
2930 if (Tmp3.getValueType() > MVT::i32)
2931 Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3);
2932 else
2933 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002934 Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
Reid Spencere63b6512006-12-31 05:55:36 +00002935 Args.push_back(Entry);
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00002936 Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false;
Reid Spencere63b6512006-12-31 05:55:36 +00002937 Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002938
2939 FnName = "memset";
2940 } else if (Node->getOpcode() == ISD::MEMCPY ||
2941 Node->getOpcode() == ISD::MEMMOVE) {
Anton Korobeynikov1b4e6012007-02-01 08:39:52 +00002942 Entry.Ty = IntPtrTy;
Reid Spencer791864c2007-01-03 04:22:32 +00002943 Entry.Node = Tmp2; Args.push_back(Entry);
2944 Entry.Node = Tmp3; Args.push_back(Entry);
2945 Entry.Node = Tmp4; Args.push_back(Entry);
Chris Lattner85d70c62005-01-11 05:57:22 +00002946 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
2947 } else {
2948 assert(0 && "Unknown op!");
2949 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002950
Chris Lattner85d70c62005-01-11 05:57:22 +00002951 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands4c95dbd2008-02-14 17:28:50 +00002952 TLI.LowerCallTo(Tmp1, Type::VoidTy,
2953 false, false, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00002954 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002955 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00002956 break;
2957 }
Chris Lattner85d70c62005-01-11 05:57:22 +00002958 }
2959 break;
2960 }
Chris Lattner5385db52005-05-09 20:23:03 +00002961
Chris Lattner4157c412005-04-02 04:00:59 +00002962 case ISD::SHL_PARTS:
2963 case ISD::SRA_PARTS:
2964 case ISD::SRL_PARTS: {
Chris Lattner97af9d52006-08-08 01:09:31 +00002965 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002966 bool Changed = false;
2967 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2968 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2969 Changed |= Ops.back() != Node->getOperand(i);
2970 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002971 if (Changed)
Chris Lattner97af9d52006-08-08 01:09:31 +00002972 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner13fe99c2005-04-02 05:00:07 +00002973
Evan Cheng870e4f82006-01-09 18:31:59 +00002974 switch (TLI.getOperationAction(Node->getOpcode(),
2975 Node->getValueType(0))) {
2976 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002977 case TargetLowering::Legal: break;
2978 case TargetLowering::Custom:
2979 Tmp1 = TLI.LowerOperation(Result, DAG);
2980 if (Tmp1.Val) {
2981 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00002982 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002983 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00002984 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
2985 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00002986 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00002987 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00002988 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00002989 return RetVal;
2990 }
Evan Cheng870e4f82006-01-09 18:31:59 +00002991 break;
2992 }
2993
Chris Lattner13fe99c2005-04-02 05:00:07 +00002994 // Since these produce multiple values, make sure to remember that we
2995 // legalized all of them.
2996 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
2997 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
2998 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002999 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003000
3001 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00003002 case ISD::ADD:
3003 case ISD::SUB:
3004 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00003005 case ISD::MULHS:
3006 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00003007 case ISD::UDIV:
3008 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00003009 case ISD::AND:
3010 case ISD::OR:
3011 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00003012 case ISD::SHL:
3013 case ISD::SRL:
3014 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003015 case ISD::FADD:
3016 case ISD::FSUB:
3017 case ISD::FMUL:
3018 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00003019 case ISD::FPOW:
Chris Lattnerdc750592005-01-07 07:47:09 +00003020 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00003021 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3022 case Expand: assert(0 && "Not possible");
3023 case Legal:
3024 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3025 break;
3026 case Promote:
3027 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3028 break;
3029 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003030
3031 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003032
Andrew Lenharth72594262005-12-24 23:42:32 +00003033 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner87f08092006-04-02 03:57:31 +00003034 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003035 case TargetLowering::Legal: break;
3036 case TargetLowering::Custom:
3037 Tmp1 = TLI.LowerOperation(Result, DAG);
3038 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00003039 break;
Chris Lattner87f08092006-04-02 03:57:31 +00003040 case TargetLowering::Expand: {
Dan Gohmana1603612007-10-08 18:33:35 +00003041 MVT::ValueType VT = Op.getValueType();
3042
3043 // See if multiply or divide can be lowered using two-result operations.
3044 SDVTList VTs = DAG.getVTList(VT, VT);
3045 if (Node->getOpcode() == ISD::MUL) {
3046 // We just need the low half of the multiply; try both the signed
3047 // and unsigned forms. If the target supports both SMUL_LOHI and
3048 // UMUL_LOHI, form a preference by checking which forms of plain
3049 // MULH it supports.
3050 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3051 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3052 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3053 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3054 unsigned OpToUse = 0;
3055 if (HasSMUL_LOHI && !HasMULHS) {
3056 OpToUse = ISD::SMUL_LOHI;
3057 } else if (HasUMUL_LOHI && !HasMULHU) {
3058 OpToUse = ISD::UMUL_LOHI;
3059 } else if (HasSMUL_LOHI) {
3060 OpToUse = ISD::SMUL_LOHI;
3061 } else if (HasUMUL_LOHI) {
3062 OpToUse = ISD::UMUL_LOHI;
3063 }
3064 if (OpToUse) {
3065 Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0);
3066 break;
3067 }
3068 }
3069 if (Node->getOpcode() == ISD::MULHS &&
3070 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
3071 Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3072 break;
3073 }
3074 if (Node->getOpcode() == ISD::MULHU &&
3075 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
3076 Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1);
3077 break;
3078 }
3079 if (Node->getOpcode() == ISD::SDIV &&
3080 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3081 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3082 break;
3083 }
3084 if (Node->getOpcode() == ISD::UDIV &&
3085 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3086 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0);
3087 break;
3088 }
3089
Dan Gohman2a7de412007-10-11 23:57:53 +00003090 // Check to see if we have a libcall for this operator.
3091 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3092 bool isSigned = false;
3093 switch (Node->getOpcode()) {
3094 case ISD::UDIV:
3095 case ISD::SDIV:
3096 if (VT == MVT::i32) {
3097 LC = Node->getOpcode() == ISD::UDIV
Evan Cheng31cbddf2007-01-12 02:11:51 +00003098 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman2a7de412007-10-11 23:57:53 +00003099 isSigned = Node->getOpcode() == ISD::SDIV;
3100 }
3101 break;
3102 case ISD::FPOW:
Duncan Sands53c954f2008-01-10 10:28:30 +00003103 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3104 RTLIB::POW_PPCF128);
Dan Gohman2a7de412007-10-11 23:57:53 +00003105 break;
3106 default: break;
3107 }
3108 if (LC != RTLIB::UNKNOWN_LIBCALL) {
3109 SDOperand Dummy;
3110 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003111 break;
3112 }
3113
Chris Lattner87f08092006-04-02 03:57:31 +00003114 assert(MVT::isVector(Node->getValueType(0)) &&
3115 "Cannot expand this binary operator!");
3116 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman2a7de412007-10-11 23:57:53 +00003117 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattner87f08092006-04-02 03:57:31 +00003118 break;
3119 }
Evan Cheng119266e2006-04-12 21:20:24 +00003120 case TargetLowering::Promote: {
3121 switch (Node->getOpcode()) {
3122 default: assert(0 && "Do not know how to promote this BinOp!");
3123 case ISD::AND:
3124 case ISD::OR:
3125 case ISD::XOR: {
3126 MVT::ValueType OVT = Node->getValueType(0);
3127 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3128 assert(MVT::isVector(OVT) && "Cannot promote this BinOp!");
3129 // Bit convert each of the values to the new type.
3130 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3131 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3132 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3133 // Bit convert the result back the original type.
3134 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3135 break;
3136 }
3137 }
3138 }
Andrew Lenharth72594262005-12-24 23:42:32 +00003139 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003140 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003141
Dan Gohman12334ac2007-10-05 14:17:22 +00003142 case ISD::SMUL_LOHI:
3143 case ISD::UMUL_LOHI:
3144 case ISD::SDIVREM:
3145 case ISD::UDIVREM:
3146 // These nodes will only be produced by target-specific lowering, so
3147 // they shouldn't be here if they aren't legal.
Duncan Sands052c8432007-10-16 09:07:20 +00003148 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohman12334ac2007-10-05 14:17:22 +00003149 "This must be legal!");
Dan Gohmana1603612007-10-08 18:33:35 +00003150
3151 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3152 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3153 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman12334ac2007-10-05 14:17:22 +00003154 break;
3155
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003156 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3157 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3158 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3159 case Expand: assert(0 && "Not possible");
3160 case Legal:
3161 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3162 break;
3163 case Promote:
3164 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3165 break;
3166 }
3167
3168 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3169
3170 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3171 default: assert(0 && "Operation not supported");
3172 case TargetLowering::Custom:
3173 Tmp1 = TLI.LowerOperation(Result, DAG);
3174 if (Tmp1.Val) Result = Tmp1;
Chris Lattner994d8e62006-03-13 06:08:38 +00003175 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003176 case TargetLowering::Legal: break;
Evan Cheng003feb02007-01-04 21:56:39 +00003177 case TargetLowering::Expand: {
Evan Cheng5f80c452007-01-05 23:33:44 +00003178 // If this target supports fabs/fneg natively and select is cheap,
3179 // do this efficiently.
3180 if (!TLI.isSelectExpensive() &&
3181 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3182 TargetLowering::Legal &&
Evan Cheng003feb02007-01-04 21:56:39 +00003183 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng5f80c452007-01-05 23:33:44 +00003184 TargetLowering::Legal) {
Chris Lattner994d8e62006-03-13 06:08:38 +00003185 // Get the sign bit of the RHS.
3186 MVT::ValueType IVT =
3187 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
3188 SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michela6729e82008-03-10 15:42:14 +00003189 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Chris Lattner994d8e62006-03-13 06:08:38 +00003190 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3191 // Get the absolute value of the result.
3192 SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
3193 // Select between the nabs and abs value based on the sign bit of
3194 // the input.
3195 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3196 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3197 AbsVal),
3198 AbsVal);
3199 Result = LegalizeOp(Result);
3200 break;
3201 }
3202
3203 // Otherwise, do bitwise ops!
Evan Cheng003feb02007-01-04 21:56:39 +00003204 MVT::ValueType NVT =
3205 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3206 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3207 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3208 Result = LegalizeOp(Result);
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003209 break;
3210 }
Evan Cheng003feb02007-01-04 21:56:39 +00003211 }
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00003212 break;
3213
Nate Begeman5965bd12006-02-17 05:43:56 +00003214 case ISD::ADDC:
3215 case ISD::SUBC:
3216 Tmp1 = LegalizeOp(Node->getOperand(0));
3217 Tmp2 = LegalizeOp(Node->getOperand(1));
3218 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3219 // Since this produces two values, make sure to remember that we legalized
3220 // both of them.
3221 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3222 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3223 return Result;
Misha Brukman835702a2005-04-21 22:36:52 +00003224
Nate Begeman5965bd12006-02-17 05:43:56 +00003225 case ISD::ADDE:
3226 case ISD::SUBE:
3227 Tmp1 = LegalizeOp(Node->getOperand(0));
3228 Tmp2 = LegalizeOp(Node->getOperand(1));
3229 Tmp3 = LegalizeOp(Node->getOperand(2));
3230 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3231 // Since this produces two values, make sure to remember that we legalized
3232 // both of them.
3233 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
3234 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
3235 return Result;
Nate Begeman5965bd12006-02-17 05:43:56 +00003236
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003237 case ISD::BUILD_PAIR: {
3238 MVT::ValueType PairTy = Node->getValueType(0);
3239 // TODO: handle the case where the Lo and Hi operands are not of legal type
3240 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3241 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3242 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003243 case TargetLowering::Promote:
3244 case TargetLowering::Custom:
3245 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003246 case TargetLowering::Legal:
3247 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3248 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3249 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003250 case TargetLowering::Expand:
3251 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3252 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3253 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
3254 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
3255 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003256 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00003257 break;
3258 }
3259 break;
3260 }
3261
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003262 case ISD::UREM:
3263 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00003264 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003265 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3266 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003267
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003268 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003269 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3270 case TargetLowering::Custom:
3271 isCustom = true;
3272 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003273 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003274 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003275 if (isCustom) {
3276 Tmp1 = TLI.LowerOperation(Result, DAG);
3277 if (Tmp1.Val) Result = Tmp1;
3278 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003279 break;
Dan Gohmana1603612007-10-08 18:33:35 +00003280 case TargetLowering::Expand: {
Evan Cheng1fc7c362006-09-18 23:28:33 +00003281 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencere63b6512006-12-31 05:55:36 +00003282 bool isSigned = DivOpc == ISD::SDIV;
Dan Gohmana1603612007-10-08 18:33:35 +00003283 MVT::ValueType VT = Node->getValueType(0);
3284
3285 // See if remainder can be lowered using two-result operations.
3286 SDVTList VTs = DAG.getVTList(VT, VT);
3287 if (Node->getOpcode() == ISD::SREM &&
3288 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
3289 Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3290 break;
3291 }
3292 if (Node->getOpcode() == ISD::UREM &&
3293 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
3294 Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1);
3295 break;
3296 }
3297
3298 if (MVT::isInteger(VT)) {
3299 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003300 TargetLowering::Legal) {
3301 // X % Y -> X-X/Y*Y
Evan Cheng1fc7c362006-09-18 23:28:33 +00003302 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003303 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3304 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Dan Gohman08143e32007-11-05 23:35:22 +00003305 } else if (MVT::isVector(VT)) {
3306 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003307 } else {
Dan Gohmana1603612007-10-08 18:33:35 +00003308 assert(VT == MVT::i32 &&
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003309 "Cannot expand this binary operator!");
Evan Cheng31cbddf2007-01-12 02:11:51 +00003310 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3311 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003312 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003313 Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy);
Evan Cheng4bfaf0b2006-09-18 21:49:04 +00003314 }
Dan Gohmanccfc0282007-11-06 22:11:54 +00003315 } else {
3316 assert(MVT::isFloatingPoint(VT) &&
3317 "remainder op must have integer or floating-point type");
Dan Gohman08143e32007-11-05 23:35:22 +00003318 if (MVT::isVector(VT)) {
3319 Result = LegalizeOp(UnrollVectorOp(Op));
3320 } else {
3321 // Floating point mod -> fmod libcall.
Duncan Sands53c954f2008-01-10 10:28:30 +00003322 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3323 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman08143e32007-11-05 23:35:22 +00003324 SDOperand Dummy;
3325 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3326 false/*sign irrelevant*/, Dummy);
3327 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003328 }
3329 break;
3330 }
Dan Gohmana1603612007-10-08 18:33:35 +00003331 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00003332 break;
Nate Begemane74795c2006-01-25 18:21:52 +00003333 case ISD::VAARG: {
3334 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3335 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3336
Chris Lattner364b89a2006-01-28 07:42:08 +00003337 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00003338 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3339 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003340 case TargetLowering::Custom:
3341 isCustom = true;
3342 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003343 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003344 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3345 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003346 Tmp1 = Result.getValue(1);
3347
3348 if (isCustom) {
3349 Tmp2 = TLI.LowerOperation(Result, DAG);
3350 if (Tmp2.Val) {
3351 Result = LegalizeOp(Tmp2);
3352 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3353 }
3354 }
Nate Begemane74795c2006-01-25 18:21:52 +00003355 break;
3356 case TargetLowering::Expand: {
Dan Gohman2d489b52008-02-06 22:27:42 +00003357 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
3358 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003359 // Increment the pointer, VAList, to the next vaarg
3360 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3361 DAG.getConstant(MVT::getSizeInBits(VT)/8,
3362 TLI.getPointerTy()));
3363 // Store the incremented VAList to the legalized pointer
Dan Gohman2d489b52008-02-06 22:27:42 +00003364 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003365 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00003366 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003367 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00003368 Result = LegalizeOp(Result);
3369 break;
3370 }
3371 }
3372 // Since VAARG produces two values, make sure to remember that we
3373 // legalized both of them.
3374 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003375 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
3376 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00003377 }
3378
3379 case ISD::VACOPY:
3380 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3381 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3382 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3383
3384 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3385 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003386 case TargetLowering::Custom:
3387 isCustom = true;
3388 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003389 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003390 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3391 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003392 if (isCustom) {
3393 Tmp1 = TLI.LowerOperation(Result, DAG);
3394 if (Tmp1.Val) Result = Tmp1;
3395 }
Nate Begemane74795c2006-01-25 18:21:52 +00003396 break;
3397 case TargetLowering::Expand:
3398 // This defaults to loading a pointer from the input and storing it to the
3399 // output, returning the chain.
Dan Gohman2d489b52008-02-06 22:27:42 +00003400 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3401 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
3402 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VD, 0);
3403 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VS, 0);
Nate Begemane74795c2006-01-25 18:21:52 +00003404 break;
3405 }
3406 break;
3407
3408 case ISD::VAEND:
3409 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3410 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3411
3412 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3413 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003414 case TargetLowering::Custom:
3415 isCustom = true;
3416 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00003417 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003418 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003419 if (isCustom) {
3420 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
3421 if (Tmp1.Val) Result = Tmp1;
3422 }
Nate Begemane74795c2006-01-25 18:21:52 +00003423 break;
3424 case TargetLowering::Expand:
3425 Result = Tmp1; // Default to a no-op, return the chain
3426 break;
3427 }
3428 break;
3429
3430 case ISD::VASTART:
3431 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3432 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3433
Chris Lattnerd02b0542006-01-28 10:58:55 +00003434 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3435
Nate Begemane74795c2006-01-25 18:21:52 +00003436 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3437 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003438 case TargetLowering::Legal: break;
3439 case TargetLowering::Custom:
3440 Tmp1 = TLI.LowerOperation(Result, DAG);
3441 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00003442 break;
3443 }
3444 break;
3445
Nate Begeman1b8121b2006-01-11 21:21:00 +00003446 case ISD::ROTL:
3447 case ISD::ROTR:
3448 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3449 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00003450 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michel16627a52007-04-02 21:36:32 +00003451 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3452 default:
3453 assert(0 && "ROTL/ROTR legalize operation not supported");
3454 break;
3455 case TargetLowering::Legal:
3456 break;
3457 case TargetLowering::Custom:
3458 Tmp1 = TLI.LowerOperation(Result, DAG);
3459 if (Tmp1.Val) Result = Tmp1;
3460 break;
3461 case TargetLowering::Promote:
3462 assert(0 && "Do not know how to promote ROTL/ROTR");
3463 break;
3464 case TargetLowering::Expand:
3465 assert(0 && "Do not know how to expand ROTL/ROTR");
3466 break;
3467 }
Nate Begeman1b8121b2006-01-11 21:21:00 +00003468 break;
3469
Nate Begeman2fba8a32006-01-14 03:14:10 +00003470 case ISD::BSWAP:
3471 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3472 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003473 case TargetLowering::Custom:
3474 assert(0 && "Cannot custom legalize this yet!");
3475 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003476 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003477 break;
3478 case TargetLowering::Promote: {
3479 MVT::ValueType OVT = Tmp1.getValueType();
3480 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Dan Gohman1796f1f2007-05-18 17:52:13 +00003481 unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00003482
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003483 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3484 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3485 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3486 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3487 break;
3488 }
3489 case TargetLowering::Expand:
3490 Result = ExpandBSWAP(Tmp1);
3491 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00003492 }
3493 break;
3494
Andrew Lenharth5e177822005-05-03 17:19:30 +00003495 case ISD::CTPOP:
3496 case ISD::CTTZ:
3497 case ISD::CTLZ:
3498 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3499 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel34e2d222007-07-30 21:00:31 +00003500 case TargetLowering::Custom:
Andrew Lenharth5e177822005-05-03 17:19:30 +00003501 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003502 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel34e2d222007-07-30 21:00:31 +00003503 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel5b80ecb2007-08-02 02:22:46 +00003504 TargetLowering::Custom) {
3505 Tmp1 = TLI.LowerOperation(Result, DAG);
3506 if (Tmp1.Val) {
3507 Result = Tmp1;
3508 }
Scott Michel34e2d222007-07-30 21:00:31 +00003509 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003510 break;
3511 case TargetLowering::Promote: {
3512 MVT::ValueType OVT = Tmp1.getValueType();
3513 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00003514
3515 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00003516 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3517 // Perform the larger operation, then subtract if needed.
3518 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003519 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00003520 case ISD::CTPOP:
3521 Result = Tmp1;
3522 break;
3523 case ISD::CTTZ:
3524 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michela6729e82008-03-10 15:42:14 +00003525 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003526 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
Chris Lattnerd47675e2005-08-09 20:20:18 +00003527 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003528 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Scott Michel34e2d222007-07-30 21:00:31 +00003529 DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003530 break;
3531 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003532 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003533 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00003534 DAG.getConstant(MVT::getSizeInBits(NVT) -
3535 MVT::getSizeInBits(OVT), NVT));
Andrew Lenharth5e177822005-05-03 17:19:30 +00003536 break;
3537 }
3538 break;
3539 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00003540 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003541 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00003542 break;
3543 }
3544 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003545
Chris Lattner13fe99c2005-04-02 05:00:07 +00003546 // Unary operators
3547 case ISD::FABS:
3548 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00003549 case ISD::FSQRT:
3550 case ISD::FSIN:
3551 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003552 Tmp1 = LegalizeOp(Node->getOperand(0));
3553 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003554 case TargetLowering::Promote:
3555 case TargetLowering::Custom:
Evan Cheng2443ab92006-01-31 18:14:25 +00003556 isCustom = true;
3557 // FALLTHROUGH
Chris Lattner13fe99c2005-04-02 05:00:07 +00003558 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003559 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng2443ab92006-01-31 18:14:25 +00003560 if (isCustom) {
3561 Tmp1 = TLI.LowerOperation(Result, DAG);
3562 if (Tmp1.Val) Result = Tmp1;
3563 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003564 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00003565 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003566 switch (Node->getOpcode()) {
3567 default: assert(0 && "Unreachable!");
3568 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00003569 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3570 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003571 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00003572 break;
Chris Lattner80026402005-04-30 04:43:14 +00003573 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003574 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
3575 MVT::ValueType VT = Node->getValueType(0);
3576 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michela6729e82008-03-10 15:42:14 +00003577 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
3578 ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00003579 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3580 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00003581 break;
3582 }
3583 case ISD::FSQRT:
3584 case ISD::FSIN:
3585 case ISD::FCOS: {
3586 MVT::ValueType VT = Node->getValueType(0);
Dan Gohman2a7de412007-10-11 23:57:53 +00003587
3588 // Expand unsupported unary vector operators by unrolling them.
3589 if (MVT::isVector(VT)) {
3590 Result = LegalizeOp(UnrollVectorOp(Op));
3591 break;
3592 }
3593
Evan Cheng31cbddf2007-01-12 02:11:51 +00003594 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner80026402005-04-30 04:43:14 +00003595 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00003596 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00003597 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3598 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003599 break;
3600 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00003601 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3602 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003603 break;
3604 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00003605 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3606 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00003607 break;
Chris Lattner80026402005-04-30 04:43:14 +00003608 default: assert(0 && "Unreachable!");
3609 }
Nate Begeman77558da2005-08-04 21:43:28 +00003610 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003611 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3612 false/*sign irrelevant*/, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00003613 break;
3614 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00003615 }
3616 break;
3617 }
3618 break;
Chris Lattnerf0359b32006-09-09 06:03:30 +00003619 case ISD::FPOWI: {
Dan Gohman2a7de412007-10-11 23:57:53 +00003620 MVT::ValueType VT = Node->getValueType(0);
3621
3622 // Expand unsupported unary vector operators by unrolling them.
3623 if (MVT::isVector(VT)) {
3624 Result = LegalizeOp(UnrollVectorOp(Op));
3625 break;
3626 }
3627
3628 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands53c954f2008-01-10 10:28:30 +00003629 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3630 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003631 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003632 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3633 false/*sign irrelevant*/, Dummy);
Chris Lattnerf0359b32006-09-09 06:03:30 +00003634 break;
3635 }
Chris Lattner36e663d2005-12-23 00:16:34 +00003636 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00003637 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner87bc3e72008-01-16 07:45:30 +00003638 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3639 Node->getValueType(0));
Dan Gohmana8665142007-06-25 16:23:39 +00003640 } else if (MVT::isVector(Op.getOperand(0).getValueType())) {
3641 // The input has to be a vector type, we have to either scalarize it, pack
3642 // it, or convert it based on whether the input vector type is legal.
3643 SDNode *InVal = Node->getOperand(0).Val;
Dale Johannesen771188c2007-10-20 00:07:52 +00003644 int InIx = Node->getOperand(0).ResNo;
3645 unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx));
3646 MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx));
Dan Gohmana8665142007-06-25 16:23:39 +00003647
3648 // Figure out if there is a simple type corresponding to this Vector
3649 // type. If so, convert to the vector type.
3650 MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems);
3651 if (TLI.isTypeLegal(TVT)) {
Dan Gohman06c60b62007-07-16 14:29:03 +00003652 // Turn this into a bit convert of the vector input.
Dan Gohmana8665142007-06-25 16:23:39 +00003653 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3654 LegalizeOp(Node->getOperand(0)));
3655 break;
3656 } else if (NumElems == 1) {
3657 // Turn this into a bit convert of the scalar input.
3658 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3659 ScalarizeVectorOp(Node->getOperand(0)));
3660 break;
3661 } else {
3662 // FIXME: UNIMP! Store then reload
3663 assert(0 && "Cast from unsupported vector type not implemented yet!");
3664 }
Chris Lattner763dfd72006-01-23 07:30:46 +00003665 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00003666 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3667 Node->getOperand(0).getValueType())) {
3668 default: assert(0 && "Unknown operation action!");
3669 case TargetLowering::Expand:
Chris Lattner87bc3e72008-01-16 07:45:30 +00003670 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3671 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00003672 break;
3673 case TargetLowering::Legal:
3674 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003675 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00003676 break;
3677 }
3678 }
3679 break;
Chris Lattnera4f68052006-03-24 02:26:29 +00003680
Chris Lattner13fe99c2005-04-02 05:00:07 +00003681 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00003682 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003683 case ISD::UINT_TO_FP: {
3684 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00003685 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3686 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00003687 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003688 Node->getOperand(0).getValueType())) {
3689 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003690 case TargetLowering::Custom:
3691 isCustom = true;
3692 // FALLTHROUGH
3693 case TargetLowering::Legal:
3694 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003695 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003696 if (isCustom) {
3697 Tmp1 = TLI.LowerOperation(Result, DAG);
3698 if (Tmp1.Val) Result = Tmp1;
3699 }
3700 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003701 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00003702 Result = ExpandLegalINT_TO_FP(isSigned,
3703 LegalizeOp(Node->getOperand(0)),
3704 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003705 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003706 case TargetLowering::Promote:
3707 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
3708 Node->getValueType(0),
3709 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003710 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00003711 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003712 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00003713 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003714 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
3715 Node->getValueType(0), Node->getOperand(0));
3716 break;
3717 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003718 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003719 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003720 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
3721 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003722 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00003723 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
3724 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00003725 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003726 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3727 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003728 break;
3729 }
3730 break;
3731 }
3732 case ISD::TRUNCATE:
3733 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3734 case Legal:
3735 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00003736 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003737 break;
3738 case Expand:
3739 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3740
3741 // Since the result is legal, we should just be able to truncate the low
3742 // part of the source.
3743 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3744 break;
3745 case Promote:
3746 Result = PromoteOp(Node->getOperand(0));
3747 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3748 break;
3749 }
3750 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003751
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003752 case ISD::FP_TO_SINT:
3753 case ISD::FP_TO_UINT:
3754 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3755 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00003756 Tmp1 = LegalizeOp(Node->getOperand(0));
3757
Chris Lattner44fe26f2005-07-29 00:11:56 +00003758 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3759 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003760 case TargetLowering::Custom:
3761 isCustom = true;
3762 // FALLTHROUGH
3763 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003764 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003765 if (isCustom) {
3766 Tmp1 = TLI.LowerOperation(Result, DAG);
3767 if (Tmp1.Val) Result = Tmp1;
3768 }
3769 break;
3770 case TargetLowering::Promote:
3771 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3772 Node->getOpcode() == ISD::FP_TO_SINT);
3773 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00003774 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00003775 if (Node->getOpcode() == ISD::FP_TO_UINT) {
3776 SDOperand True, False;
3777 MVT::ValueType VT = Node->getOperand(0).getValueType();
3778 MVT::ValueType NVT = Node->getValueType(0);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003779 const uint64_t zero[] = {0, 0};
3780 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Dan Gohman837a6dc2008-02-29 01:44:25 +00003781 APInt x = APInt::getSignBit(MVT::getSizeInBits(NVT));
3782 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00003783 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michela6729e82008-03-10 15:42:14 +00003784 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Nate Begeman36853ee2005-08-14 01:20:53 +00003785 Node->getOperand(0), Tmp2, ISD::SETLT);
3786 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3787 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00003788 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00003789 Tmp2));
3790 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohman837a6dc2008-02-29 01:44:25 +00003791 DAG.getConstant(x, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003792 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3793 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00003794 } else {
3795 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3796 }
3797 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003798 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003799 break;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003800 case Expand: {
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003801 MVT::ValueType VT = Op.getValueType();
Dale Johannesen666323e2007-10-10 01:01:31 +00003802 MVT::ValueType OVT = Node->getOperand(0).getValueType();
Dale Johannesen6472eb62007-10-11 23:32:15 +00003803 // Convert ppcf128 to i32
Dale Johannesen666323e2007-10-10 01:01:31 +00003804 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner72733e52008-01-17 07:00:52 +00003805 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3806 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3807 Node->getOperand(0), DAG.getValueType(MVT::f64));
3808 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3809 DAG.getIntPtrConstant(1));
3810 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3811 } else {
Dale Johannesen6472eb62007-10-11 23:32:15 +00003812 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3813 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3814 Tmp2 = DAG.getConstantFP(apf, OVT);
3815 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3816 // FIXME: generated code sucks.
3817 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3818 DAG.getNode(ISD::ADD, MVT::i32,
3819 DAG.getNode(ISD::FP_TO_SINT, VT,
3820 DAG.getNode(ISD::FSUB, OVT,
3821 Node->getOperand(0), Tmp2)),
3822 DAG.getConstant(0x80000000, MVT::i32)),
3823 DAG.getNode(ISD::FP_TO_SINT, VT,
3824 Node->getOperand(0)),
3825 DAG.getCondCode(ISD::SETGE));
3826 }
Dale Johannesen666323e2007-10-10 01:01:31 +00003827 break;
3828 }
Dale Johannesen6472eb62007-10-11 23:32:15 +00003829 // Convert f32 / f64 to i32 / i64.
Evan Cheng31cbddf2007-01-12 02:11:51 +00003830 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003831 switch (Node->getOpcode()) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003832 case ISD::FP_TO_SINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003833 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003834 LC = (VT == MVT::i32)
3835 ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003836 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003837 LC = (VT == MVT::i32)
3838 ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003839 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003840 assert(VT == MVT::i64);
Dale Johannesenc0154c02007-10-05 20:04:43 +00003841 LC = RTLIB::FPTOSINT_F80_I64;
3842 }
3843 else if (OVT == MVT::ppcf128) {
3844 assert(VT == MVT::i64);
3845 LC = RTLIB::FPTOSINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003846 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003847 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003848 }
3849 case ISD::FP_TO_UINT: {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003850 if (OVT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003851 LC = (VT == MVT::i32)
3852 ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003853 else if (OVT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00003854 LC = (VT == MVT::i32)
3855 ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00003856 else if (OVT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00003857 LC = (VT == MVT::i32)
Dale Johannesenc0154c02007-10-05 20:04:43 +00003858 ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64;
3859 }
3860 else if (OVT == MVT::ppcf128) {
3861 assert(VT == MVT::i64);
3862 LC = RTLIB::FPTOUINT_PPCF128_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003863 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003864 break;
Dale Johannesen7d67e542007-09-19 23:55:34 +00003865 }
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003866 default: assert(0 && "Unreachable!");
3867 }
3868 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00003869 Result = ExpandLibCall(TLI.getLibcallName(LC), Node,
3870 false/*sign irrelevant*/, Dummy);
Evan Cheng0a5b805f2006-12-13 01:57:55 +00003871 break;
3872 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003873 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003874 Tmp1 = PromoteOp(Node->getOperand(0));
3875 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3876 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003877 break;
3878 }
3879 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003880
Chris Lattner91d86242008-01-16 06:57:07 +00003881 case ISD::FP_EXTEND: {
Chris Lattner72733e52008-01-17 07:00:52 +00003882 MVT::ValueType DstVT = Op.getValueType();
3883 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3884 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3885 // The only other way we can lower this is to turn it into a STORE,
3886 // LOAD pair, targetting a temporary location (a stack slot).
3887 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3888 break;
Chris Lattner91d86242008-01-16 06:57:07 +00003889 }
3890 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3891 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3892 case Legal:
3893 Tmp1 = LegalizeOp(Node->getOperand(0));
3894 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3895 break;
3896 case Promote:
3897 Tmp1 = PromoteOp(Node->getOperand(0));
3898 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3899 break;
3900 }
3901 break;
Chris Lattner72733e52008-01-17 07:00:52 +00003902 }
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003903 case ISD::FP_ROUND: {
Chris Lattner72733e52008-01-17 07:00:52 +00003904 MVT::ValueType DstVT = Op.getValueType();
3905 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3906 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3907 if (SrcVT == MVT::ppcf128) {
Dale Johannesen949e5a22008-01-20 01:18:38 +00003908 SDOperand Lo;
3909 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner72733e52008-01-17 07:00:52 +00003910 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen949e5a22008-01-20 01:18:38 +00003911 if (DstVT!=MVT::f64)
3912 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner72733e52008-01-17 07:00:52 +00003913 break;
Dale Johannesenba1a98a2007-08-09 01:04:01 +00003914 }
Chris Lattner72733e52008-01-17 07:00:52 +00003915 // The only other way we can lower this is to turn it into a STORE,
3916 // LOAD pair, targetting a temporary location (a stack slot).
3917 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3918 break;
Dale Johannesena2b3c172007-07-03 00:53:03 +00003919 }
Chris Lattner91d86242008-01-16 06:57:07 +00003920 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3921 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3922 case Legal:
3923 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner72733e52008-01-17 07:00:52 +00003924 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner91d86242008-01-16 06:57:07 +00003925 break;
3926 case Promote:
3927 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner72733e52008-01-17 07:00:52 +00003928 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3929 Node->getOperand(1));
Chris Lattner91d86242008-01-16 06:57:07 +00003930 break;
3931 }
3932 break;
Chris Lattner72733e52008-01-17 07:00:52 +00003933 }
Chris Lattner7753f172005-09-02 00:18:10 +00003934 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003935 case ISD::ZERO_EXTEND:
3936 case ISD::SIGN_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003937 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003938 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003939 case Legal:
3940 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michela3cefea2008-02-15 23:05:48 +00003941 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3942 TargetLowering::Custom) {
3943 Tmp2 = TLI.LowerOperation(Result, DAG);
3944 if (Tmp2.Val) {
3945 Tmp1 = Tmp2;
3946 }
3947 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00003948 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00003949 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003950 case Promote:
3951 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00003952 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003953 Tmp1 = PromoteOp(Node->getOperand(0));
3954 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00003955 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003956 case ISD::ZERO_EXTEND:
3957 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003958 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00003959 Result = DAG.getZeroExtendInReg(Result,
3960 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003961 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003962 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003963 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00003964 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003965 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00003966 Result,
3967 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00003968 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00003969 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003970 }
3971 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003972 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00003973 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00003974 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00003975 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00003976
3977 // If this operation is not supported, convert it to a shl/shr or load/store
3978 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00003979 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
3980 default: assert(0 && "This action not supported for this op yet!");
3981 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00003982 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00003983 break;
3984 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00003985 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00003986 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00003987 // NOTE: we could fall back on load/store here too for targets without
3988 // SAR. However, it is doubtful that any exist.
3989 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
3990 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00003991 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00003992 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
3993 Node->getOperand(0), ShiftCst);
3994 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
3995 Result, ShiftCst);
3996 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey6a4c6d32006-10-11 17:52:19 +00003997 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner99222f72005-01-15 07:15:18 +00003998 // EXTLOAD pair, targetting a temporary location (a stack slot).
3999
4000 // NOTE: there is a choice here between constantly creating new stack
4001 // slots and always reusing the same one. We currently always create
4002 // new ones, as reuse may inhibit scheduling.
Chris Lattner7ca4d5b2008-01-16 07:51:34 +00004003 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4004 Node->getValueType(0));
Chris Lattner99222f72005-01-15 07:15:18 +00004005 } else {
4006 assert(0 && "Unknown op");
4007 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00004008 break;
Chris Lattner99222f72005-01-15 07:15:18 +00004009 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004010 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00004011 }
Duncan Sands644f9172007-07-27 12:58:54 +00004012 case ISD::TRAMPOLINE: {
4013 SDOperand Ops[6];
4014 for (unsigned i = 0; i != 6; ++i)
4015 Ops[i] = LegalizeOp(Node->getOperand(i));
4016 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4017 // The only option for this node is to custom lower it.
4018 Result = TLI.LowerOperation(Result, DAG);
4019 assert(Result.Val && "Should always custom lower!");
Duncan Sands86e01192007-09-11 14:10:23 +00004020
4021 // Since trampoline produces two values, make sure to remember that we
4022 // legalized both of them.
4023 Tmp1 = LegalizeOp(Result.getValue(1));
4024 Result = LegalizeOp(Result);
4025 AddLegalizedOperand(SDOperand(Node, 0), Result);
4026 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
4027 return Op.ResNo ? Tmp1 : Result;
Duncan Sands644f9172007-07-27 12:58:54 +00004028 }
Dan Gohman9ba4d762008-01-31 00:41:03 +00004029 case ISD::FLT_ROUNDS_: {
Anton Korobeynikov66b91e66e2007-11-15 23:25:33 +00004030 MVT::ValueType VT = Node->getValueType(0);
4031 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4032 default: assert(0 && "This action not supported for this op yet!");
4033 case TargetLowering::Custom:
4034 Result = TLI.LowerOperation(Op, DAG);
4035 if (Result.Val) break;
4036 // Fall Thru
4037 case TargetLowering::Legal:
4038 // If this operation is not supported, lower it to constant 1
4039 Result = DAG.getConstant(1, VT);
4040 break;
4041 }
4042 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00004043 case ISD::TRAP: {
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004044 MVT::ValueType VT = Node->getValueType(0);
4045 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4046 default: assert(0 && "This action not supported for this op yet!");
Chris Lattneree8df1f2008-01-15 21:58:08 +00004047 case TargetLowering::Legal:
4048 Tmp1 = LegalizeOp(Node->getOperand(0));
4049 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4050 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004051 case TargetLowering::Custom:
4052 Result = TLI.LowerOperation(Op, DAG);
4053 if (Result.Val) break;
4054 // Fall Thru
Chris Lattneree8df1f2008-01-15 21:58:08 +00004055 case TargetLowering::Expand:
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004056 // If this operation is not supported, lower it to 'abort()' call
Chris Lattneree8df1f2008-01-15 21:58:08 +00004057 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004058 TargetLowering::ArgListTy Args;
4059 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sands4c95dbd2008-02-14 17:28:50 +00004060 TLI.LowerCallTo(Tmp1, Type::VoidTy,
4061 false, false, false, CallingConv::C, false,
Chris Lattnerec224882008-01-15 22:09:33 +00004062 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
4063 Args, DAG);
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004064 Result = CallResult.second;
4065 break;
4066 }
Chris Lattneree8df1f2008-01-15 21:58:08 +00004067 break;
Anton Korobeynikov6bbbc4c2008-01-15 07:02:33 +00004068 }
Chris Lattner99222f72005-01-15 07:15:18 +00004069 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004070
Chris Lattner101ea662006-04-08 04:13:17 +00004071 assert(Result.getValueType() == Op.getValueType() &&
4072 "Bad legalization!");
4073
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004074 // Make sure that the generated code is itself legal.
4075 if (Result != Op)
4076 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00004077
Chris Lattnerb5a78e02005-05-12 16:53:42 +00004078 // Note that LegalizeOp may be reentered even from single-use nodes, which
4079 // means that we always must cache transformed nodes.
4080 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00004081 return Result;
4082}
4083
Chris Lattner4d978642005-01-15 22:16:26 +00004084/// PromoteOp - Given an operation that produces a value in an invalid type,
4085/// promote it to compute the value into a larger type. The produced value will
4086/// have the correct bits for the low portion of the register, but no guarantee
4087/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004088SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
4089 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00004090 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004091 assert(getTypeAction(VT) == Promote &&
4092 "Caller should expand or legalize operands that are not promotable!");
4093 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
4094 "Cannot promote to smaller type!");
4095
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004096 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004097 SDOperand Result;
4098 SDNode *Node = Op.Val;
4099
Chris Lattner4b0ddb22007-02-04 01:17:38 +00004100 DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
Chris Lattner1a570f12005-09-02 20:32:45 +00004101 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00004102
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004103 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00004104 case ISD::CopyFromReg:
4105 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004106 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004107#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00004108 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00004109#endif
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004110 assert(0 && "Do not know how to promote this operator!");
4111 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00004112 case ISD::UNDEF:
4113 Result = DAG.getNode(ISD::UNDEF, NVT);
4114 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004115 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00004116 if (VT != MVT::i1)
4117 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4118 else
4119 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004120 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4121 break;
4122 case ISD::ConstantFP:
4123 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4124 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4125 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00004126
Chris Lattner2cb338d2005-01-18 02:59:52 +00004127 case ISD::SETCC:
Scott Michela6729e82008-03-10 15:42:14 +00004128 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
4129 && "SetCC type is not legal??");
4130 Result = DAG.getNode(ISD::SETCC,
4131 TLI.getSetCCResultType(Node->getOperand(0)),
4132 Node->getOperand(0), Node->getOperand(1),
4133 Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00004134 break;
Chris Lattnerd3b504a2006-04-12 16:20:43 +00004135
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004136 case ISD::TRUNCATE:
4137 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4138 case Legal:
4139 Result = LegalizeOp(Node->getOperand(0));
4140 assert(Result.getValueType() >= NVT &&
4141 "This truncation doesn't make sense!");
4142 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
4143 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4144 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00004145 case Promote:
4146 // The truncation is not required, because we don't guarantee anything
4147 // about high bits anyway.
4148 Result = PromoteOp(Node->getOperand(0));
4149 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004150 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00004151 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4152 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00004153 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004154 }
4155 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004156 case ISD::SIGN_EXTEND:
4157 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00004158 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00004159 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4160 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4161 case Legal:
4162 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004163 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00004164 break;
4165 case Promote:
4166 // Promote the reg if it's smaller.
4167 Result = PromoteOp(Node->getOperand(0));
4168 // The high bits are not guaranteed to be anything. Insert an extend.
4169 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00004170 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00004171 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00004172 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00004173 Result = DAG.getZeroExtendInReg(Result,
4174 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00004175 break;
4176 }
4177 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00004178 case ISD::BIT_CONVERT:
Chris Lattner87bc3e72008-01-16 07:45:30 +00004179 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4180 Node->getValueType(0));
Chris Lattner36e663d2005-12-23 00:16:34 +00004181 Result = PromoteOp(Result);
4182 break;
4183
Chris Lattner4d978642005-01-15 22:16:26 +00004184 case ISD::FP_EXTEND:
4185 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4186 case ISD::FP_ROUND:
4187 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4188 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4189 case Promote: assert(0 && "Unreachable with 2 FP types!");
4190 case Legal:
Chris Lattner72733e52008-01-17 07:00:52 +00004191 if (Node->getConstantOperandVal(1) == 0) {
4192 // Input is legal? Do an FP_ROUND_INREG.
4193 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4194 DAG.getValueType(VT));
4195 } else {
4196 // Just remove the truncate, it isn't affecting the value.
4197 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4198 Node->getOperand(1));
4199 }
Chris Lattner4d978642005-01-15 22:16:26 +00004200 break;
4201 }
4202 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004203 case ISD::SINT_TO_FP:
4204 case ISD::UINT_TO_FP:
4205 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4206 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00004207 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004208 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00004209 break;
4210
4211 case Promote:
4212 Result = PromoteOp(Node->getOperand(0));
4213 if (Node->getOpcode() == ISD::SINT_TO_FP)
4214 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00004215 Result,
4216 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00004217 else
Chris Lattner0e852af2005-04-13 02:38:47 +00004218 Result = DAG.getZeroExtendInReg(Result,
4219 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00004220 // No extra round required here.
4221 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00004222 break;
4223 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00004224 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4225 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00004226 // Round if we cannot tolerate excess precision.
4227 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004228 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4229 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00004230 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004231 }
Chris Lattner4d978642005-01-15 22:16:26 +00004232 break;
4233
Chris Lattner268d4572005-12-09 17:32:47 +00004234 case ISD::SIGN_EXTEND_INREG:
4235 Result = PromoteOp(Node->getOperand(0));
4236 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4237 Node->getOperand(1));
4238 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004239 case ISD::FP_TO_SINT:
4240 case ISD::FP_TO_UINT:
4241 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4242 case Legal:
Evan Cheng86000462006-12-16 02:10:30 +00004243 case Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004244 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00004245 break;
4246 case Promote:
4247 // The input result is prerounded, so we don't have to do anything
4248 // special.
4249 Tmp1 = PromoteOp(Node->getOperand(0));
4250 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004251 }
Nate Begeman36853ee2005-08-14 01:20:53 +00004252 // If we're promoting a UINT to a larger size, check to see if the new node
4253 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4254 // we can use that instead. This allows us to generate better code for
4255 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4256 // legal, such as PowerPC.
4257 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00004258 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00004259 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4260 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00004261 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4262 } else {
4263 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4264 }
Chris Lattner4d978642005-01-15 22:16:26 +00004265 break;
4266
Chris Lattner13fe99c2005-04-02 05:00:07 +00004267 case ISD::FABS:
4268 case ISD::FNEG:
4269 Tmp1 = PromoteOp(Node->getOperand(0));
4270 assert(Tmp1.getValueType() == NVT);
4271 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4272 // NOTE: we do not have to do any extra rounding here for
4273 // NoExcessFPPrecision, because we know the input will have the appropriate
4274 // precision, and these operations don't modify precision at all.
4275 break;
4276
Chris Lattner9d6fa982005-04-28 21:44:33 +00004277 case ISD::FSQRT:
4278 case ISD::FSIN:
4279 case ISD::FCOS:
4280 Tmp1 = PromoteOp(Node->getOperand(0));
4281 assert(Tmp1.getValueType() == NVT);
4282 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004283 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004284 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4285 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00004286 break;
4287
Chris Lattnerca401aa2007-03-03 23:43:21 +00004288 case ISD::FPOWI: {
4289 // Promote f32 powi to f64 powi. Note that this could insert a libcall
4290 // directly as well, which may be better.
4291 Tmp1 = PromoteOp(Node->getOperand(0));
4292 assert(Tmp1.getValueType() == NVT);
4293 Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1));
4294 if (NoExcessFPPrecision)
4295 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4296 DAG.getValueType(VT));
4297 break;
4298 }
4299
Andrew Lenharth95528942008-02-21 06:45:13 +00004300 case ISD::ATOMIC_LCS: {
4301 Tmp2 = PromoteOp(Node->getOperand(2));
4302 Tmp3 = PromoteOp(Node->getOperand(3));
4303 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4304 Node->getOperand(1), Tmp2, Tmp3,
4305 cast<AtomicSDNode>(Node)->getVT());
4306 // Remember that we legalized the chain.
4307 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4308 break;
4309 }
4310 case ISD::ATOMIC_LAS:
4311 case ISD::ATOMIC_SWAP: {
4312 Tmp2 = PromoteOp(Node->getOperand(2));
4313 Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0),
4314 Node->getOperand(1), Tmp2,
4315 cast<AtomicSDNode>(Node)->getVT());
4316 // Remember that we legalized the chain.
4317 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4318 break;
4319 }
4320
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004321 case ISD::AND:
4322 case ISD::OR:
4323 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004324 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00004325 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004326 case ISD::MUL:
4327 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00004328 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004329 // that too is okay if they are integer operations.
4330 Tmp1 = PromoteOp(Node->getOperand(0));
4331 Tmp2 = PromoteOp(Node->getOperand(1));
4332 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4333 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00004334 break;
4335 case ISD::FADD:
4336 case ISD::FSUB:
4337 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00004338 Tmp1 = PromoteOp(Node->getOperand(0));
4339 Tmp2 = PromoteOp(Node->getOperand(1));
4340 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4341 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4342
4343 // Floating point operations will give excess precision that we may not be
4344 // able to tolerate. If we DO allow excess precision, just leave it,
4345 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00004346 // FIXME: Why would we need to round FP ops more than integer ones?
4347 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00004348 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004349 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4350 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00004351 break;
4352
Chris Lattner4d978642005-01-15 22:16:26 +00004353 case ISD::SDIV:
4354 case ISD::SREM:
4355 // These operators require that their input be sign extended.
4356 Tmp1 = PromoteOp(Node->getOperand(0));
4357 Tmp2 = PromoteOp(Node->getOperand(1));
4358 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00004359 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4360 DAG.getValueType(VT));
4361 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4362 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004363 }
4364 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4365
4366 // Perform FP_ROUND: this is probably overly pessimistic.
4367 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00004368 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4369 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00004370 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00004371 case ISD::FDIV:
4372 case ISD::FREM:
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004373 case ISD::FCOPYSIGN:
Chris Lattner6f3b5772005-09-28 22:28:18 +00004374 // These operators require that their input be fp extended.
Nate Begeman1a225d22006-05-09 18:20:51 +00004375 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner72733e52008-01-17 07:00:52 +00004376 case Expand: assert(0 && "not implemented");
4377 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4378 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begeman1a225d22006-05-09 18:20:51 +00004379 }
4380 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner72733e52008-01-17 07:00:52 +00004381 case Expand: assert(0 && "not implemented");
4382 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4383 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begeman1a225d22006-05-09 18:20:51 +00004384 }
Chris Lattner6f3b5772005-09-28 22:28:18 +00004385 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4386
4387 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00004388 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner6f3b5772005-09-28 22:28:18 +00004389 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4390 DAG.getValueType(VT));
4391 break;
Chris Lattner4d978642005-01-15 22:16:26 +00004392
4393 case ISD::UDIV:
4394 case ISD::UREM:
4395 // These operators require that their input be zero extended.
4396 Tmp1 = PromoteOp(Node->getOperand(0));
4397 Tmp2 = PromoteOp(Node->getOperand(1));
4398 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00004399 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4400 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00004401 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4402 break;
4403
4404 case ISD::SHL:
4405 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004406 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004407 break;
4408 case ISD::SRA:
4409 // The input value must be properly sign extended.
4410 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00004411 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4412 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004413 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004414 break;
4415 case ISD::SRL:
4416 // The input value must be properly zero extended.
4417 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00004418 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004419 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00004420 break;
Nate Begeman595ec732006-01-28 03:14:31 +00004421
4422 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004423 Tmp1 = Node->getOperand(0); // Get the chain.
4424 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00004425 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4426 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
4427 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
4428 } else {
Dan Gohman2d489b52008-02-06 22:27:42 +00004429 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
4430 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman595ec732006-01-28 03:14:31 +00004431 // Increment the pointer, VAList, to the next vaarg
4432 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
4433 DAG.getConstant(MVT::getSizeInBits(VT)/8,
4434 TLI.getPointerTy()));
4435 // Store the incremented VAList to the legalized pointer
Dan Gohman2d489b52008-02-06 22:27:42 +00004436 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman595ec732006-01-28 03:14:31 +00004437 // Load the actual argument out of the pointer VAList
Evan Chenge71fe34d2006-10-09 20:57:25 +00004438 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman595ec732006-01-28 03:14:31 +00004439 }
4440 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004441 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00004442 break;
4443
Evan Chenge71fe34d2006-10-09 20:57:25 +00004444 case ISD::LOAD: {
4445 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Chengdc6a3aa2006-10-10 07:51:21 +00004446 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4447 ? ISD::EXTLOAD : LD->getExtensionType();
4448 Result = DAG.getExtLoad(ExtType, NVT,
4449 LD->getChain(), LD->getBasePtr(),
Chris Lattner84384292006-10-10 18:54:19 +00004450 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohman47a7d6f2008-01-30 00:15:11 +00004451 LD->getMemoryVT(),
Dan Gohman2af30632007-07-09 22:18:38 +00004452 LD->isVolatile(),
4453 LD->getAlignment());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004454 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004455 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004456 break;
Evan Chenge71fe34d2006-10-09 20:57:25 +00004457 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004458 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004459 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4460 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004461 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004462 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00004463 case ISD::SELECT_CC:
4464 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4465 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4466 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004467 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00004468 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00004469 case ISD::BSWAP:
4470 Tmp1 = Node->getOperand(0);
4471 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4472 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4473 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004474 DAG.getConstant(MVT::getSizeInBits(NVT) -
4475 MVT::getSizeInBits(VT),
Nate Begeman2fba8a32006-01-14 03:14:10 +00004476 TLI.getShiftAmountTy()));
4477 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004478 case ISD::CTPOP:
4479 case ISD::CTTZ:
4480 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004481 // Zero extend the argument
4482 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004483 // Perform the larger operation, then subtract if needed.
4484 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004485 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004486 case ISD::CTPOP:
4487 Result = Tmp1;
4488 break;
4489 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004490 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michela6729e82008-03-10 15:42:14 +00004491 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004492 DAG.getConstant(MVT::getSizeInBits(NVT), NVT),
4493 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004494 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004495 DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004496 break;
4497 case ISD::CTLZ:
4498 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004499 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Dan Gohman1796f1f2007-05-18 17:52:13 +00004500 DAG.getConstant(MVT::getSizeInBits(NVT) -
4501 MVT::getSizeInBits(VT), NVT));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00004502 break;
4503 }
4504 break;
Dan Gohmana8665142007-06-25 16:23:39 +00004505 case ISD::EXTRACT_SUBVECTOR:
4506 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman26455c42007-06-13 15:12:02 +00004507 break;
Chris Lattner42a5fca2006-04-02 05:06:04 +00004508 case ISD::EXTRACT_VECTOR_ELT:
4509 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4510 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004511 }
4512
4513 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004514
4515 // Make sure the result is itself legal.
4516 Result = LegalizeOp(Result);
4517
4518 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00004519 AddPromotedOperand(Op, Result);
4520 return Result;
4521}
Chris Lattnerdc750592005-01-07 07:47:09 +00004522
Dan Gohmana8665142007-06-25 16:23:39 +00004523/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4524/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4525/// based on the vector type. The return type of this matches the element type
4526/// of the vector, which may not be legal for the target.
4527SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) {
Chris Lattner6f423252006-03-31 17:55:51 +00004528 // We know that operand #0 is the Vec vector. If the index is a constant
4529 // or if the invec is a supported hardware type, we can use it. Otherwise,
4530 // lower to a store then an indexed load.
4531 SDOperand Vec = Op.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00004532 SDOperand Idx = Op.getOperand(1);
Chris Lattner6f423252006-03-31 17:55:51 +00004533
Dan Gohman60028182007-09-24 15:54:53 +00004534 MVT::ValueType TVT = Vec.getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +00004535 unsigned NumElems = MVT::getVectorNumElements(TVT);
Chris Lattner6f423252006-03-31 17:55:51 +00004536
Dan Gohmana8665142007-06-25 16:23:39 +00004537 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4538 default: assert(0 && "This action is not supported yet!");
4539 case TargetLowering::Custom: {
4540 Vec = LegalizeOp(Vec);
4541 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
4542 SDOperand Tmp3 = TLI.LowerOperation(Op, DAG);
4543 if (Tmp3.Val)
4544 return Tmp3;
4545 break;
4546 }
4547 case TargetLowering::Legal:
4548 if (isTypeLegal(TVT)) {
4549 Vec = LegalizeOp(Vec);
4550 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb3fead962007-07-26 03:33:13 +00004551 return Op;
Dan Gohmana8665142007-06-25 16:23:39 +00004552 }
4553 break;
4554 case TargetLowering::Expand:
4555 break;
4556 }
4557
4558 if (NumElems == 1) {
Chris Lattner6f423252006-03-31 17:55:51 +00004559 // This must be an access of the only element. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004560 Op = ScalarizeVectorOp(Vec);
4561 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begemanef337672008-01-29 02:24:00 +00004562 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohmana8665142007-06-25 16:23:39 +00004563 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Chris Lattner6f423252006-03-31 17:55:51 +00004564 SDOperand Lo, Hi;
4565 SplitVectorOp(Vec, Lo, Hi);
Nate Begemanef337672008-01-29 02:24:00 +00004566 if (CIdx->getValue() < NumLoElts) {
Chris Lattner6f423252006-03-31 17:55:51 +00004567 Vec = Lo;
4568 } else {
4569 Vec = Hi;
Nate Begemanef337672008-01-29 02:24:00 +00004570 Idx = DAG.getConstant(CIdx->getValue() - NumLoElts,
Dan Gohmana8665142007-06-25 16:23:39 +00004571 Idx.getValueType());
Chris Lattner6f423252006-03-31 17:55:51 +00004572 }
Dan Gohmana8665142007-06-25 16:23:39 +00004573
Chris Lattner6f423252006-03-31 17:55:51 +00004574 // It's now an extract from the appropriate high or low part. Recurse.
4575 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004576 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner6f423252006-03-31 17:55:51 +00004577 } else {
Dan Gohmana8665142007-06-25 16:23:39 +00004578 // Store the value to a temporary stack slot, then LOAD the scalar
4579 // element back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004580 SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
Dan Gohmana8665142007-06-25 16:23:39 +00004581 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
4582
4583 // Add the offset to the index.
4584 unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8;
4585 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4586 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling070aca52007-10-18 08:32:37 +00004587
4588 if (MVT::getSizeInBits(Idx.getValueType()) >
4589 MVT::getSizeInBits(TLI.getPointerTy()))
Chris Lattner064c31e2007-10-19 16:47:35 +00004590 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004591 else
Chris Lattner064c31e2007-10-19 16:47:35 +00004592 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling070aca52007-10-18 08:32:37 +00004593
Dan Gohmana8665142007-06-25 16:23:39 +00004594 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4595
4596 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner6f423252006-03-31 17:55:51 +00004597 }
Dan Gohmana8665142007-06-25 16:23:39 +00004598 return Op;
Chris Lattner6f423252006-03-31 17:55:51 +00004599}
4600
Dan Gohmana8665142007-06-25 16:23:39 +00004601/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman26455c42007-06-13 15:12:02 +00004602/// we assume the operation can be split if it is not already legal.
Dan Gohmana8665142007-06-25 16:23:39 +00004603SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) {
Dan Gohman26455c42007-06-13 15:12:02 +00004604 // We know that operand #0 is the Vec vector. For now we assume the index
4605 // is a constant and that the extracted result is a supported hardware type.
4606 SDOperand Vec = Op.getOperand(0);
4607 SDOperand Idx = LegalizeOp(Op.getOperand(1));
4608
Dan Gohmana8665142007-06-25 16:23:39 +00004609 unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType());
Dan Gohman26455c42007-06-13 15:12:02 +00004610
4611 if (NumElems == MVT::getVectorNumElements(Op.getValueType())) {
4612 // This must be an access of the desired vector length. Return it.
Dan Gohmana8665142007-06-25 16:23:39 +00004613 return Vec;
Dan Gohman26455c42007-06-13 15:12:02 +00004614 }
4615
4616 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
4617 SDOperand Lo, Hi;
4618 SplitVectorOp(Vec, Lo, Hi);
4619 if (CIdx->getValue() < NumElems/2) {
4620 Vec = Lo;
4621 } else {
4622 Vec = Hi;
4623 Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType());
4624 }
4625
4626 // It's now an extract from the appropriate high or low part. Recurse.
4627 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohmana8665142007-06-25 16:23:39 +00004628 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman26455c42007-06-13 15:12:02 +00004629}
4630
Nate Begeman7e7f4392006-02-01 07:19:44 +00004631/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4632/// with condition CC on the current target. This usually involves legalizing
4633/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4634/// there may be no choice but to create a new SetCC node to represent the
4635/// legalized value of setcc lhs, rhs. In this case, the value is returned in
4636/// LHS, and the SDOperand returned in RHS has a nil SDNode value.
4637void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS,
4638 SDOperand &RHS,
4639 SDOperand &CC) {
Dale Johannesenf864ac92007-10-06 01:24:11 +00004640 SDOperand Tmp1, Tmp2, Tmp3, Result;
Nate Begeman7e7f4392006-02-01 07:19:44 +00004641
4642 switch (getTypeAction(LHS.getValueType())) {
4643 case Legal:
4644 Tmp1 = LegalizeOp(LHS); // LHS
4645 Tmp2 = LegalizeOp(RHS); // RHS
4646 break;
4647 case Promote:
4648 Tmp1 = PromoteOp(LHS); // LHS
4649 Tmp2 = PromoteOp(RHS); // RHS
4650
4651 // If this is an FP compare, the operands have already been extended.
4652 if (MVT::isInteger(LHS.getValueType())) {
4653 MVT::ValueType VT = LHS.getValueType();
4654 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
4655
4656 // Otherwise, we have to insert explicit sign or zero extends. Note
4657 // that we could insert sign extends for ALL conditions, but zero extend
4658 // is cheaper on many machines (an AND instead of two shifts), so prefer
4659 // it.
4660 switch (cast<CondCodeSDNode>(CC)->get()) {
4661 default: assert(0 && "Unknown integer comparison!");
4662 case ISD::SETEQ:
4663 case ISD::SETNE:
4664 case ISD::SETUGE:
4665 case ISD::SETUGT:
4666 case ISD::SETULE:
4667 case ISD::SETULT:
4668 // ALL of these operations will work if we either sign or zero extend
4669 // the operands (including the unsigned comparisons!). Zero extend is
4670 // usually a simpler/cheaper operation, so prefer it.
4671 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4672 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4673 break;
4674 case ISD::SETGE:
4675 case ISD::SETGT:
4676 case ISD::SETLT:
4677 case ISD::SETLE:
4678 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4679 DAG.getValueType(VT));
4680 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4681 DAG.getValueType(VT));
4682 break;
4683 }
4684 }
4685 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004686 case Expand: {
4687 MVT::ValueType VT = LHS.getValueType();
4688 if (VT == MVT::f32 || VT == MVT::f64) {
4689 // Expand into one or more soft-fp libcall(s).
Evan Cheng31cbddf2007-01-12 02:11:51 +00004690 RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004691 switch (cast<CondCodeSDNode>(CC)->get()) {
4692 case ISD::SETEQ:
4693 case ISD::SETOEQ:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004694 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004695 break;
4696 case ISD::SETNE:
4697 case ISD::SETUNE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004698 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004699 break;
4700 case ISD::SETGE:
4701 case ISD::SETOGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004702 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004703 break;
4704 case ISD::SETLT:
4705 case ISD::SETOLT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004706 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004707 break;
4708 case ISD::SETLE:
4709 case ISD::SETOLE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004710 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004711 break;
4712 case ISD::SETGT:
4713 case ISD::SETOGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004714 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004715 break;
4716 case ISD::SETUO:
Evan Cheng53026f12007-01-31 09:29:11 +00004717 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4718 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004719 case ISD::SETO:
Evan Chengf309d132007-02-03 00:43:46 +00004720 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004721 break;
4722 default:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004723 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004724 switch (cast<CondCodeSDNode>(CC)->get()) {
4725 case ISD::SETONE:
4726 // SETONE = SETOLT | SETOGT
Evan Cheng31cbddf2007-01-12 02:11:51 +00004727 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004728 // Fallthrough
4729 case ISD::SETUGT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004730 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004731 break;
4732 case ISD::SETUGE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004733 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004734 break;
4735 case ISD::SETULT:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004736 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004737 break;
4738 case ISD::SETULE:
Evan Cheng31cbddf2007-01-12 02:11:51 +00004739 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004740 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004741 case ISD::SETUEQ:
4742 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004743 break;
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004744 default: assert(0 && "Unsupported FP setcc!");
4745 }
4746 }
4747
4748 SDOperand Dummy;
Evan Cheng31cbddf2007-01-12 02:11:51 +00004749 Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1),
Reid Spencere63b6512006-12-31 05:55:36 +00004750 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004751 false /*sign irrelevant*/, Dummy);
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004752 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Cheng53026f12007-01-31 09:29:11 +00004753 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng31cbddf2007-01-12 02:11:51 +00004754 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michela6729e82008-03-10 15:42:14 +00004755 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
4756 CC);
Evan Cheng31cbddf2007-01-12 02:11:51 +00004757 LHS = ExpandLibCall(TLI.getLibcallName(LC2),
Reid Spencere63b6512006-12-31 05:55:36 +00004758 DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val,
Reid Spencer791864c2007-01-03 04:22:32 +00004759 false /*sign irrelevant*/, Dummy);
Scott Michela6729e82008-03-10 15:42:14 +00004760 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Evan Cheng53026f12007-01-31 09:29:11 +00004761 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004762 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4763 Tmp2 = SDOperand();
4764 }
4765 LHS = Tmp1;
4766 RHS = Tmp2;
4767 return;
4768 }
4769
Nate Begeman7e7f4392006-02-01 07:19:44 +00004770 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
4771 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004772 ExpandOp(RHS, RHSLo, RHSHi);
4773 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4774
4775 if (VT==MVT::ppcf128) {
4776 // FIXME: This generated code sucks. We want to generate
4777 // FCMP crN, hi1, hi2
4778 // BNE crN, L:
4779 // FCMP crN, lo1, lo2
4780 // The following can be improved, but not that much.
Scott Michela6729e82008-03-10 15:42:14 +00004781 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
4782 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004783 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Scott Michela6729e82008-03-10 15:42:14 +00004784 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
4785 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesenf864ac92007-10-06 01:24:11 +00004786 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4787 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
4788 Tmp2 = SDOperand();
4789 break;
4790 }
4791
4792 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004793 case ISD::SETEQ:
4794 case ISD::SETNE:
4795 if (RHSLo == RHSHi)
4796 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4797 if (RHSCST->isAllOnesValue()) {
4798 // Comparison to -1.
4799 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4800 Tmp2 = RHSLo;
4801 break;
4802 }
4803
4804 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4805 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4806 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4807 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4808 break;
4809 default:
4810 // If this is a comparison of the sign bit, just look at the top part.
4811 // X > -1, x < 0
4812 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4813 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
4814 CST->getValue() == 0) || // X < 0
4815 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4816 CST->isAllOnesValue())) { // X > -1
4817 Tmp1 = LHSHi;
4818 Tmp2 = RHSHi;
4819 break;
4820 }
4821
4822 // FIXME: This generated code sucks.
4823 ISD::CondCode LowCC;
Evan Cheng93049452007-02-08 22:16:19 +00004824 switch (CCCode) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004825 default: assert(0 && "Unknown integer setcc!");
4826 case ISD::SETLT:
4827 case ISD::SETULT: LowCC = ISD::SETULT; break;
4828 case ISD::SETGT:
4829 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4830 case ISD::SETLE:
4831 case ISD::SETULE: LowCC = ISD::SETULE; break;
4832 case ISD::SETGE:
4833 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4834 }
4835
4836 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4837 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4838 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4839
4840 // NOTE: on targets without efficient SELECT of bools, we can always use
4841 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng93049452007-02-08 22:16:19 +00004842 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michela6729e82008-03-10 15:42:14 +00004843 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
4844 LowCC, false, DagCombineInfo);
Evan Cheng93049452007-02-08 22:16:19 +00004845 if (!Tmp1.Val)
Scott Michela6729e82008-03-10 15:42:14 +00004846 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
4847 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng93049452007-02-08 22:16:19 +00004848 CCCode, false, DagCombineInfo);
4849 if (!Tmp2.Val)
Scott Michela6729e82008-03-10 15:42:14 +00004850 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
4851 RHSHi,CC);
Evan Cheng93049452007-02-08 22:16:19 +00004852
4853 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
4854 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
4855 if ((Tmp1C && Tmp1C->getValue() == 0) ||
4856 (Tmp2C && Tmp2C->getValue() == 0 &&
4857 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4858 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
4859 (Tmp2C && Tmp2C->getValue() == 1 &&
4860 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4861 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4862 // low part is known false, returns high part.
4863 // For LE / GE, if high part is known false, ignore the low part.
4864 // For LT / GT, if high part is known true, ignore the low part.
4865 Tmp1 = Tmp2;
4866 Tmp2 = SDOperand();
4867 } else {
Scott Michela6729e82008-03-10 15:42:14 +00004868 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng93049452007-02-08 22:16:19 +00004869 ISD::SETEQ, false, DagCombineInfo);
4870 if (!Result.Val)
Scott Michela6729e82008-03-10 15:42:14 +00004871 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
4872 ISD::SETEQ);
Evan Cheng93049452007-02-08 22:16:19 +00004873 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
4874 Result, Tmp1, Tmp2));
4875 Tmp1 = Result;
4876 Tmp2 = SDOperand();
4877 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004878 }
4879 }
Evan Cheng35fdd5f2006-12-15 02:59:56 +00004880 }
Nate Begeman7e7f4392006-02-01 07:19:44 +00004881 LHS = Tmp1;
4882 RHS = Tmp2;
4883}
4884
Chris Lattner87bc3e72008-01-16 07:45:30 +00004885/// EmitStackConvert - Emit a store/load combination to the stack. This stores
4886/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
4887/// a load from the stack slot to DestVT, extending it if needed.
4888/// The resultant code need not be legal.
4889SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp,
4890 MVT::ValueType SlotVT,
4891 MVT::ValueType DestVT) {
Chris Lattner36e663d2005-12-23 00:16:34 +00004892 // Create the stack frame object.
Chris Lattner87bc3e72008-01-16 07:45:30 +00004893 SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT);
4894
Dan Gohman54d3b5a2008-02-11 18:58:42 +00004895 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman2d489b52008-02-06 22:27:42 +00004896 int SPFI = StackPtrFI->getIndex();
4897
Chris Lattner87bc3e72008-01-16 07:45:30 +00004898 unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType());
4899 unsigned SlotSize = MVT::getSizeInBits(SlotVT);
4900 unsigned DestSize = MVT::getSizeInBits(DestVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004901
Chris Lattner87bc3e72008-01-16 07:45:30 +00004902 // Emit a store to the stack slot. Use a truncstore if the input value is
4903 // later than DestVT.
4904 SDOperand Store;
4905 if (SrcSize > SlotSize)
Dan Gohman2d489b52008-02-06 22:27:42 +00004906 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004907 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00004908 SPFI, SlotVT);
Chris Lattner87bc3e72008-01-16 07:45:30 +00004909 else {
4910 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman2d489b52008-02-06 22:27:42 +00004911 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004912 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00004913 SPFI, SlotVT);
Chris Lattner87bc3e72008-01-16 07:45:30 +00004914 }
4915
Chris Lattner36e663d2005-12-23 00:16:34 +00004916 // Result is a load from the stack slot.
Chris Lattner87bc3e72008-01-16 07:45:30 +00004917 if (SlotSize == DestSize)
4918 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0);
4919
4920 assert(SlotSize < DestSize && "Unknown extension!");
4921 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT);
Chris Lattner36e663d2005-12-23 00:16:34 +00004922}
4923
Chris Lattner6be79822006-04-04 17:23:26 +00004924SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
4925 // Create a vector sized/aligned stack slot, store the value to element #0,
4926 // then load the whole vector back out.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00004927 SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman2d489b52008-02-06 22:27:42 +00004928
Dan Gohman54d3b5a2008-02-11 18:58:42 +00004929 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman2d489b52008-02-06 22:27:42 +00004930 int SPFI = StackPtrFI->getIndex();
4931
Evan Chengdf9ac472006-10-05 23:01:46 +00004932 SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004933 PseudoSourceValue::getFixedStack(), SPFI);
Dan Gohman2d489b52008-02-06 22:27:42 +00004934 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00004935 PseudoSourceValue::getFixedStack(), SPFI);
Chris Lattner6be79822006-04-04 17:23:26 +00004936}
4937
4938
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004939/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman06c60b62007-07-16 14:29:03 +00004940/// support the operation, but do support the resultant vector type.
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004941SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
4942
4943 // If the only non-undef value is the low element, turn this into a
Chris Lattner21e68c82006-03-20 01:52:29 +00004944 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004945 unsigned NumElems = Node->getNumOperands();
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004946 bool isOnlyLowElement = true;
Chris Lattner21e68c82006-03-20 01:52:29 +00004947 SDOperand SplatValue = Node->getOperand(0);
Chris Lattner322c8262008-03-09 00:29:42 +00004948
4949 // FIXME: it would be far nicer to change this into map<SDOperand,uint64_t>
4950 // and use a bitmask instead of a list of elements.
Evan Cheng1d2e9952006-03-24 01:17:21 +00004951 std::map<SDOperand, std::vector<unsigned> > Values;
4952 Values[SplatValue].push_back(0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004953 bool isConstant = true;
4954 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
4955 SplatValue.getOpcode() != ISD::UNDEF)
4956 isConstant = false;
4957
Evan Cheng1d2e9952006-03-24 01:17:21 +00004958 for (unsigned i = 1; i < NumElems; ++i) {
4959 SDOperand V = Node->getOperand(i);
Chris Lattner73eb58e2006-04-19 23:17:50 +00004960 Values[V].push_back(i);
Evan Cheng1d2e9952006-03-24 01:17:21 +00004961 if (V.getOpcode() != ISD::UNDEF)
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004962 isOnlyLowElement = false;
Evan Cheng1d2e9952006-03-24 01:17:21 +00004963 if (SplatValue != V)
Chris Lattner21e68c82006-03-20 01:52:29 +00004964 SplatValue = SDOperand(0,0);
Chris Lattner77e271c2006-03-24 07:29:17 +00004965
4966 // If this isn't a constant element or an undef, we can't use a constant
4967 // pool load.
4968 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
4969 V.getOpcode() != ISD::UNDEF)
4970 isConstant = false;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00004971 }
4972
4973 if (isOnlyLowElement) {
4974 // If the low element is an undef too, then this whole things is an undef.
4975 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
4976 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
4977 // Otherwise, turn this into a scalar_to_vector node.
4978 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
4979 Node->getOperand(0));
4980 }
4981
Chris Lattner77e271c2006-03-24 07:29:17 +00004982 // If all elements are constants, create a load from the constant pool.
4983 if (isConstant) {
4984 MVT::ValueType VT = Node->getValueType(0);
4985 const Type *OpNTy =
4986 MVT::getTypeForValueType(Node->getOperand(0).getValueType());
4987 std::vector<Constant*> CV;
4988 for (unsigned i = 0, e = NumElems; i != e; ++i) {
4989 if (ConstantFPSDNode *V =
4990 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dale Johannesend246b2c2007-08-30 00:23:21 +00004991 CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004992 } else if (ConstantSDNode *V =
4993 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00004994 CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
Chris Lattner77e271c2006-03-24 07:29:17 +00004995 } else {
4996 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
4997 CV.push_back(UndefValue::get(OpNTy));
4998 }
4999 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00005000 Constant *CP = ConstantVector::get(CV);
Chris Lattner77e271c2006-03-24 07:29:17 +00005001 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman2d489b52008-02-06 22:27:42 +00005002 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005003 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner77e271c2006-03-24 07:29:17 +00005004 }
5005
Chris Lattner21e68c82006-03-20 01:52:29 +00005006 if (SplatValue.Val) { // Splat of one value?
5007 // Build the shuffle constant vector: <0, 0, 0, 0>
5008 MVT::ValueType MaskVT =
Evan Cheng1d2e9952006-03-24 01:17:21 +00005009 MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00005010 SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT));
Evan Cheng1d2e9952006-03-24 01:17:21 +00005011 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005012 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
5013 &ZeroVec[0], ZeroVec.size());
Chris Lattner21e68c82006-03-20 01:52:29 +00005014
5015 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00005016 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner21e68c82006-03-20 01:52:29 +00005017 // Get the splatted value into the low element of a vector register.
5018 SDOperand LowValVec =
5019 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5020
5021 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5022 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5023 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5024 SplatMask);
5025 }
5026 }
5027
Evan Cheng1d2e9952006-03-24 01:17:21 +00005028 // If there are only two unique elements, we may be able to turn this into a
5029 // vector shuffle.
5030 if (Values.size() == 2) {
Chris Lattner322c8262008-03-09 00:29:42 +00005031 // Get the two values in deterministic order.
5032 SDOperand Val1 = Node->getOperand(1);
5033 SDOperand Val2;
5034 std::map<SDOperand, std::vector<unsigned> >::iterator MI = Values.begin();
5035 if (MI->first != Val1)
5036 Val2 = MI->first;
5037 else
5038 Val2 = (++MI)->first;
5039
5040 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5041 // vector shuffle has the undef vector on the RHS.
5042 if (Val1.getOpcode() == ISD::UNDEF)
5043 std::swap(Val1, Val2);
5044
Evan Cheng1d2e9952006-03-24 01:17:21 +00005045 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Chris Lattner322c8262008-03-09 00:29:42 +00005046 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5047 MVT::ValueType MaskEltVT = MVT::getVectorElementType(MaskVT);
Evan Cheng1d2e9952006-03-24 01:17:21 +00005048 std::vector<SDOperand> MaskVec(NumElems);
Chris Lattner322c8262008-03-09 00:29:42 +00005049
5050 // Set elements of the shuffle mask for Val1.
5051 std::vector<unsigned> &Val1Elts = Values[Val1];
5052 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5053 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5054
5055 // Set elements of the shuffle mask for Val2.
5056 std::vector<unsigned> &Val2Elts = Values[Val2];
5057 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5058 if (Val2.getOpcode() != ISD::UNDEF)
5059 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5060 else
5061 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5062
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005063 SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
5064 &MaskVec[0], MaskVec.size());
Evan Cheng1d2e9952006-03-24 01:17:21 +00005065
Chris Lattner322c8262008-03-09 00:29:42 +00005066 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner6be79822006-04-04 17:23:26 +00005067 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5068 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattner322c8262008-03-09 00:29:42 +00005069 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5070 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
5071 SDOperand Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng1d2e9952006-03-24 01:17:21 +00005072
5073 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattner322c8262008-03-09 00:29:42 +00005074 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng1d2e9952006-03-24 01:17:21 +00005075 }
5076 }
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005077
5078 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5079 // aligned object on the stack, store each element into it, then load
5080 // the result as a vector.
5081 MVT::ValueType VT = Node->getValueType(0);
5082 // Create the stack frame object.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00005083 SDOperand FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005084
5085 // Emit a store of each element to the stack slot.
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005086 SmallVector<SDOperand, 8> Stores;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005087 unsigned TypeByteSize =
5088 MVT::getSizeInBits(Node->getOperand(0).getValueType())/8;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005089 // Store (in the right endianness) the elements to memory.
5090 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5091 // Ignore undef elements.
5092 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5093
Chris Lattner8fa445a2006-03-22 01:46:54 +00005094 unsigned Offset = TypeByteSize*i;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005095
5096 SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType());
5097 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5098
Evan Chengdf9ac472006-10-05 23:01:46 +00005099 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Chengab51cf22006-10-13 21:14:26 +00005100 NULL, 0));
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005101 }
5102
5103 SDOperand StoreChain;
5104 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005105 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5106 &Stores[0], Stores.size());
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005107 else
5108 StoreChain = DAG.getEntryNode();
5109
5110 // Result is a load from the stack slot.
Evan Chenge71fe34d2006-10-09 20:57:25 +00005111 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00005112}
5113
Chris Lattner4157c412005-04-02 04:00:59 +00005114void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
5115 SDOperand Op, SDOperand Amt,
5116 SDOperand &Lo, SDOperand &Hi) {
5117 // Expand the subcomponents.
5118 SDOperand LHSL, LHSH;
5119 ExpandOp(Op, LHSL, LHSH);
5120
Chris Lattnerc24a1d32006-08-08 02:23:42 +00005121 SDOperand Ops[] = { LHSL, LHSH, Amt };
Chris Lattnerbd887772006-08-14 23:53:35 +00005122 MVT::ValueType VT = LHSL.getValueType();
5123 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner4157c412005-04-02 04:00:59 +00005124 Hi = Lo.getValue(1);
5125}
5126
5127
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005128/// ExpandShift - Try to find a clever way to expand this shift operation out to
5129/// smaller elements. If we can't find a way that is more efficient than a
5130/// libcall on this target, return false. Otherwise, return true with the
5131/// low-parts expanded into Lo and Hi.
5132bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
5133 SDOperand &Lo, SDOperand &Hi) {
5134 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5135 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00005136
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005137 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00005138 SDOperand ShAmt = LegalizeOp(Amt);
5139 MVT::ValueType ShTy = ShAmt.getValueType();
Dan Gohman34fc7db2008-02-20 16:57:27 +00005140 unsigned ShBits = MVT::getSizeInBits(ShTy);
Nate Begemanb0674922005-04-06 21:13:14 +00005141 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
5142 unsigned NVTBits = MVT::getSizeInBits(NVT);
5143
Chris Lattnerfbbe5702007-10-14 20:35:12 +00005144 // Handle the case when Amt is an immediate.
Nate Begemanb0674922005-04-06 21:13:14 +00005145 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
5146 unsigned Cst = CN->getValue();
5147 // Expand the incoming operand to be shifted, so that we have its parts
5148 SDOperand InL, InH;
5149 ExpandOp(Op, InL, InH);
5150 switch(Opc) {
5151 case ISD::SHL:
5152 if (Cst > VTBits) {
5153 Lo = DAG.getConstant(0, NVT);
5154 Hi = DAG.getConstant(0, NVT);
5155 } else if (Cst > NVTBits) {
5156 Lo = DAG.getConstant(0, NVT);
5157 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00005158 } else if (Cst == NVTBits) {
5159 Lo = DAG.getConstant(0, NVT);
5160 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00005161 } else {
5162 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5163 Hi = DAG.getNode(ISD::OR, NVT,
5164 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5165 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5166 }
5167 return true;
5168 case ISD::SRL:
5169 if (Cst > VTBits) {
5170 Lo = DAG.getConstant(0, NVT);
5171 Hi = DAG.getConstant(0, NVT);
5172 } else if (Cst > NVTBits) {
5173 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5174 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00005175 } else if (Cst == NVTBits) {
5176 Lo = InH;
5177 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00005178 } else {
5179 Lo = DAG.getNode(ISD::OR, NVT,
5180 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5181 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5182 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5183 }
5184 return true;
5185 case ISD::SRA:
5186 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00005187 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005188 DAG.getConstant(NVTBits-1, ShTy));
5189 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00005190 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005191 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00005192 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00005193 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00005194 } else if (Cst == NVTBits) {
5195 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00005196 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00005197 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00005198 } else {
5199 Lo = DAG.getNode(ISD::OR, NVT,
5200 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5201 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5202 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5203 }
5204 return true;
5205 }
5206 }
Chris Lattner875ea0c2006-09-20 03:38:48 +00005207
5208 // Okay, the shift amount isn't constant. However, if we can tell that it is
5209 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman34fc7db2008-02-20 16:57:27 +00005210 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5211 APInt KnownZero, KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00005212 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner875ea0c2006-09-20 03:38:48 +00005213
Dan Gohmanf3057a92008-02-22 01:12:31 +00005214 // If we know that if any of the high bits of the shift amount are one, then
5215 // we can do this as a couple of simple shifts.
Dan Gohman34fc7db2008-02-20 16:57:27 +00005216 if (KnownOne.intersects(Mask)) {
Chris Lattner875ea0c2006-09-20 03:38:48 +00005217 // Mask out the high bit, which we know is set.
5218 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman34fc7db2008-02-20 16:57:27 +00005219 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner875ea0c2006-09-20 03:38:48 +00005220
5221 // Expand the incoming operand to be shifted, so that we have its parts
5222 SDOperand InL, InH;
5223 ExpandOp(Op, InL, InH);
5224 switch(Opc) {
5225 case ISD::SHL:
5226 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5227 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5228 return true;
5229 case ISD::SRL:
5230 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5231 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5232 return true;
5233 case ISD::SRA:
5234 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5235 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5236 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5237 return true;
5238 }
5239 }
5240
Dan Gohmanf3057a92008-02-22 01:12:31 +00005241 // If we know that the high bits of the shift amount are all zero, then we can
5242 // do this as a couple of simple shifts.
5243 if ((KnownZero & Mask) == Mask) {
Chris Lattner875ea0c2006-09-20 03:38:48 +00005244 // Compute 32-amt.
5245 SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
5246 DAG.getConstant(NVTBits, Amt.getValueType()),
5247 Amt);
5248
5249 // Expand the incoming operand to be shifted, so that we have its parts
5250 SDOperand InL, InH;
5251 ExpandOp(Op, InL, InH);
5252 switch(Opc) {
5253 case ISD::SHL:
5254 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5255 Hi = DAG.getNode(ISD::OR, NVT,
5256 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5257 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5258 return true;
5259 case ISD::SRL:
5260 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5261 Lo = DAG.getNode(ISD::OR, NVT,
5262 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5263 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5264 return true;
5265 case ISD::SRA:
5266 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5267 Lo = DAG.getNode(ISD::OR, NVT,
5268 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5269 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5270 return true;
5271 }
5272 }
5273
Nate Begemanb0674922005-04-06 21:13:14 +00005274 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005275}
Chris Lattneraac464e2005-01-21 06:05:23 +00005276
Chris Lattner4add7e32005-01-23 04:42:50 +00005277
Chris Lattneraac464e2005-01-21 06:05:23 +00005278// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5279// does not fit into a register, return the lo part and set the hi part to the
5280// by-reg argument. If it does fit into a single register, return the result
5281// and leave the Hi part unset.
5282SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
Reid Spencere63b6512006-12-31 05:55:36 +00005283 bool isSigned, SDOperand &Hi) {
Chris Lattner462505f2006-02-13 09:18:02 +00005284 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5285 // The input chain to this libcall is the entry node of the function.
5286 // Legalizing the call will automatically add the previous call to the
5287 // dependence.
5288 SDOperand InChain = DAG.getEntryNode();
5289
Chris Lattneraac464e2005-01-21 06:05:23 +00005290 TargetLowering::ArgListTy Args;
Reid Spencere63b6512006-12-31 05:55:36 +00005291 TargetLowering::ArgListEntry Entry;
Chris Lattneraac464e2005-01-21 06:05:23 +00005292 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5293 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
5294 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Reid Spencere63b6512006-12-31 05:55:36 +00005295 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikoved4b3032007-03-07 16:25:09 +00005296 Entry.isSExt = isSigned;
Duncan Sands4c95dbd2008-02-14 17:28:50 +00005297 Entry.isZExt = !isSigned;
Reid Spencere63b6512006-12-31 05:55:36 +00005298 Args.push_back(Entry);
Chris Lattneraac464e2005-01-21 06:05:23 +00005299 }
5300 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00005301
Chris Lattner06bbeb62005-05-11 19:02:11 +00005302 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00005303 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00005304 std::pair<SDOperand,SDOperand> CallInfo =
Duncan Sands4c95dbd2008-02-14 17:28:50 +00005305 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C,
5306 false, Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00005307
Chris Lattner462505f2006-02-13 09:18:02 +00005308 // Legalize the call sequence, starting with the chain. This will advance
5309 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5310 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5311 LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00005312 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00005313 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00005314 default: assert(0 && "Unknown thing");
5315 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00005316 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00005317 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00005318 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00005319 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00005320 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00005321 }
Chris Lattner63022662005-09-02 20:26:58 +00005322 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00005323}
5324
Chris Lattner4add7e32005-01-23 04:42:50 +00005325
Evan Chengbf535fc2007-04-27 07:33:31 +00005326/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5327///
Chris Lattneraac464e2005-01-21 06:05:23 +00005328SDOperand SelectionDAGLegalize::
5329ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Dan Gohmand9d874b2008-03-05 01:08:17 +00005330 MVT::ValueType SourceVT = Source.getValueType();
5331 assert(getTypeAction(SourceVT) == Expand &&
Chris Lattneraac464e2005-01-21 06:05:23 +00005332 "This is not an expansion!");
Chris Lattneraac464e2005-01-21 06:05:23 +00005333
Chris Lattner06bbeb62005-05-11 19:02:11 +00005334 if (!isSigned) {
Dan Gohmanda7897c2008-03-05 02:07:31 +00005335 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005336 // incoming integer is set. To handle this, we dynamically test to see if
5337 // it is set, and, if so, add a fudge factor.
5338 SDOperand Lo, Hi;
5339 ExpandOp(Source, Lo, Hi);
5340
Chris Lattner2a4f7312005-05-13 04:45:13 +00005341 // If this is unsigned, and not supported, first perform the conversion to
5342 // signed, then adjust the result if the sign bit is set.
5343 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
Dan Gohmand9d874b2008-03-05 01:08:17 +00005344 DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi));
Chris Lattner2a4f7312005-05-13 04:45:13 +00005345
Scott Michela6729e82008-03-10 15:42:14 +00005346 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Chris Lattnerd47675e2005-08-09 20:20:18 +00005347 DAG.getConstant(0, Hi.getValueType()),
5348 ISD::SETLT);
Chris Lattner72733e52008-01-17 07:00:52 +00005349 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005350 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5351 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00005352 uint64_t FF = 0x5f800000ULL;
5353 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohmanda7897c2008-03-05 02:07:31 +00005354 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005355
Chris Lattnerc30405e2005-08-26 17:15:30 +00005356 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00005357 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5358 SDOperand FudgeInReg;
5359 if (DestTy == MVT::f32)
Dan Gohman2d489b52008-02-06 22:27:42 +00005360 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005361 PseudoSourceValue::getConstantPool(), 0);
Dale Johannesen7f724e92007-09-16 16:51:49 +00005362 else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
Evan Chengbf535fc2007-04-27 07:33:31 +00005363 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen7f724e92007-09-16 16:51:49 +00005364 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman2d489b52008-02-06 22:27:42 +00005365 CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005366 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman2d489b52008-02-06 22:27:42 +00005367 MVT::f32);
Dale Johannesen98d3a082007-09-14 22:26:36 +00005368 else
5369 assert(0 && "Unexpected conversion");
5370
Evan Chengbf535fc2007-04-27 07:33:31 +00005371 MVT::ValueType SCVT = SignedConv.getValueType();
5372 if (SCVT != DestTy) {
5373 // Destination type needs to be expanded as well. The FADD now we are
5374 // constructing will be expanded into a libcall.
5375 if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) {
Dan Gohmand9d874b2008-03-05 01:08:17 +00005376 assert(MVT::getSizeInBits(SCVT) * 2 == MVT::getSizeInBits(DestTy));
5377 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Chengbf535fc2007-04-27 07:33:31 +00005378 SignedConv, SignedConv.getValue(1));
5379 }
5380 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5381 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00005382 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00005383 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00005384
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005385 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand9d874b2008-03-05 01:08:17 +00005386 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005387 default: assert(0 && "This action not implemented for this operation!");
5388 case TargetLowering::Legal:
5389 case TargetLowering::Expand:
5390 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00005391 case TargetLowering::Custom: {
5392 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
5393 Source), DAG);
5394 if (NV.Val)
5395 return LegalizeOp(NV);
5396 break; // The target decided this was legal after all
5397 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00005398 }
5399
Chris Lattner153587e2005-05-12 07:00:44 +00005400 // Expand the source, then glue it back together for the call. We must expand
5401 // the source in case it is shared (this pass of legalize must traverse it).
5402 SDOperand SrcLo, SrcHi;
5403 ExpandOp(Source, SrcLo, SrcHi);
Dan Gohmand9d874b2008-03-05 01:08:17 +00005404 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
Chris Lattner153587e2005-05-12 07:00:44 +00005405
Evan Cheng31cbddf2007-01-12 02:11:51 +00005406 RTLIB::Libcall LC;
Dan Gohmand9d874b2008-03-05 01:08:17 +00005407 if (SourceVT == MVT::i64) {
5408 if (DestTy == MVT::f32)
5409 LC = RTLIB::SINTTOFP_I64_F32;
5410 else {
5411 assert(DestTy == MVT::f64 && "Unknown fp value type!");
5412 LC = RTLIB::SINTTOFP_I64_F64;
5413 }
5414 } else if (SourceVT == MVT::i128) {
5415 if (DestTy == MVT::f32)
5416 LC = RTLIB::SINTTOFP_I128_F32;
5417 else if (DestTy == MVT::f64)
5418 LC = RTLIB::SINTTOFP_I128_F64;
5419 else if (DestTy == MVT::f80)
5420 LC = RTLIB::SINTTOFP_I128_F80;
5421 else {
5422 assert(DestTy == MVT::ppcf128 && "Unknown fp value type!");
5423 LC = RTLIB::SINTTOFP_I128_PPCF128;
5424 }
5425 } else {
5426 assert(0 && "Unknown int value type");
Chris Lattner06bbeb62005-05-11 19:02:11 +00005427 }
Chris Lattner462505f2006-02-13 09:18:02 +00005428
Evan Chengbf535fc2007-04-27 07:33:31 +00005429 assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
Chris Lattner462505f2006-02-13 09:18:02 +00005430 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
5431 SDOperand UnusedHiPart;
Evan Cheng31cbddf2007-01-12 02:11:51 +00005432 return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned,
5433 UnusedHiPart);
Chris Lattneraac464e2005-01-21 06:05:23 +00005434}
Misha Brukman835702a2005-04-21 22:36:52 +00005435
Chris Lattner689bdcc2006-01-28 08:25:58 +00005436/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5437/// INT_TO_FP operation of the specified operand when the target requests that
5438/// we expand it. At this point, we know that the result and operand types are
5439/// legal for the target.
5440SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5441 SDOperand Op0,
5442 MVT::ValueType DestVT) {
5443 if (Op0.getValueType() == MVT::i32) {
5444 // simple 32-bit [signed|unsigned] integer to float/double expansion
5445
Chris Lattnera2c7ff32008-01-16 07:03:22 +00005446 // Get the stack frame index of a 8 byte buffer.
5447 SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
5448
Chris Lattner689bdcc2006-01-28 08:25:58 +00005449 // word offset constant for Hi/Lo address computation
5450 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
5451 // set up Hi and Lo (into buffer) address based on endian
Chris Lattner9ea1b3f2006-03-23 05:29:04 +00005452 SDOperand Hi = StackSlot;
5453 SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
5454 if (TLI.isLittleEndian())
5455 std::swap(Hi, Lo);
5456
Chris Lattner689bdcc2006-01-28 08:25:58 +00005457 // if signed map to unsigned space
5458 SDOperand Op0Mapped;
5459 if (isSigned) {
5460 // constant used to invert sign bit (signed to unsigned mapping)
5461 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
5462 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5463 } else {
5464 Op0Mapped = Op0;
5465 }
5466 // store the lo of the constructed double - based on integer input
Evan Chengdf9ac472006-10-05 23:01:46 +00005467 SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Chengab51cf22006-10-13 21:14:26 +00005468 Op0Mapped, Lo, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005469 // initial hi portion of constructed double
5470 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
5471 // store the hi of the constructed double - biased exponent
Evan Chengab51cf22006-10-13 21:14:26 +00005472 SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005473 // load the constructed double
Evan Chenge71fe34d2006-10-09 20:57:25 +00005474 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005475 // FP constant to bias correct the final result
5476 SDOperand Bias = DAG.getConstantFP(isSigned ?
5477 BitsToDouble(0x4330000080000000ULL)
5478 : BitsToDouble(0x4330000000000000ULL),
5479 MVT::f64);
5480 // subtract the bias
5481 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
5482 // final result
5483 SDOperand Result;
5484 // handle final rounding
5485 if (DestVT == MVT::f64) {
5486 // do nothing
5487 Result = Sub;
Dale Johannesen7f724e92007-09-16 16:51:49 +00005488 } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) {
Chris Lattner72733e52008-01-17 07:00:52 +00005489 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5490 DAG.getIntPtrConstant(0));
Dale Johannesen7f724e92007-09-16 16:51:49 +00005491 } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) {
5492 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005493 }
5494 return Result;
5495 }
5496 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
5497 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
5498
Scott Michela6729e82008-03-10 15:42:14 +00005499 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Chris Lattner689bdcc2006-01-28 08:25:58 +00005500 DAG.getConstant(0, Op0.getValueType()),
5501 ISD::SETLT);
Chris Lattner72733e52008-01-17 07:00:52 +00005502 SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005503 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
5504 SignSet, Four, Zero);
5505
5506 // If the sign bit of the integer is set, the large number will be treated
5507 // as a negative number. To counteract this, the dynamic code adds an
5508 // offset depending on the data type.
5509 uint64_t FF;
5510 switch (Op0.getValueType()) {
5511 default: assert(0 && "Unsupported integer type!");
5512 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5513 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5514 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5515 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5516 }
5517 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencere63b6512006-12-31 05:55:36 +00005518 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005519
5520 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
5521 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
5522 SDOperand FudgeInReg;
5523 if (DestVT == MVT::f32)
Dan Gohman2d489b52008-02-06 22:27:42 +00005524 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005525 PseudoSourceValue::getConstantPool(), 0);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005526 else {
Dan Gohman2d489b52008-02-06 22:27:42 +00005527 FudgeInReg =
5528 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5529 DAG.getEntryNode(), CPIdx,
Dan Gohman16d4bc32008-02-07 18:41:25 +00005530 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman2d489b52008-02-06 22:27:42 +00005531 MVT::f32));
Chris Lattner689bdcc2006-01-28 08:25:58 +00005532 }
5533
5534 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5535}
5536
5537/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5538/// *INT_TO_FP operation of the specified operand when the target requests that
5539/// we promote it. At this point, we know that the result and operand types are
5540/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5541/// operation that takes a larger input.
5542SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
5543 MVT::ValueType DestVT,
5544 bool isSigned) {
5545 // First step, figure out the appropriate *INT_TO_FP operation to use.
5546 MVT::ValueType NewInTy = LegalOp.getValueType();
5547
5548 unsigned OpToUse = 0;
5549
5550 // Scan for the appropriate larger type to use.
5551 while (1) {
5552 NewInTy = (MVT::ValueType)(NewInTy+1);
5553 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
5554
5555 // If the target supports SINT_TO_FP of this type, use it.
5556 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5557 default: break;
5558 case TargetLowering::Legal:
5559 if (!TLI.isTypeLegal(NewInTy))
5560 break; // Can't use this datatype.
5561 // FALL THROUGH.
5562 case TargetLowering::Custom:
5563 OpToUse = ISD::SINT_TO_FP;
5564 break;
5565 }
5566 if (OpToUse) break;
5567 if (isSigned) continue;
5568
5569 // If the target supports UINT_TO_FP of this type, use it.
5570 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5571 default: break;
5572 case TargetLowering::Legal:
5573 if (!TLI.isTypeLegal(NewInTy))
5574 break; // Can't use this datatype.
5575 // FALL THROUGH.
5576 case TargetLowering::Custom:
5577 OpToUse = ISD::UINT_TO_FP;
5578 break;
5579 }
5580 if (OpToUse) break;
5581
5582 // Otherwise, try a larger type.
5583 }
5584
5585 // Okay, we found the operation and type to use. Zero extend our input to the
5586 // desired type then run the operation on it.
5587 return DAG.getNode(OpToUse, DestVT,
5588 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5589 NewInTy, LegalOp));
5590}
5591
5592/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5593/// FP_TO_*INT operation of the specified operand when the target requests that
5594/// we promote it. At this point, we know that the result and operand types are
5595/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5596/// operation that returns a larger result.
5597SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
5598 MVT::ValueType DestVT,
5599 bool isSigned) {
5600 // First step, figure out the appropriate FP_TO*INT operation to use.
5601 MVT::ValueType NewOutTy = DestVT;
5602
5603 unsigned OpToUse = 0;
5604
5605 // Scan for the appropriate larger type to use.
5606 while (1) {
5607 NewOutTy = (MVT::ValueType)(NewOutTy+1);
5608 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
5609
5610 // If the target supports FP_TO_SINT returning this type, use it.
5611 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5612 default: break;
5613 case TargetLowering::Legal:
5614 if (!TLI.isTypeLegal(NewOutTy))
5615 break; // Can't use this datatype.
5616 // FALL THROUGH.
5617 case TargetLowering::Custom:
5618 OpToUse = ISD::FP_TO_SINT;
5619 break;
5620 }
5621 if (OpToUse) break;
5622
5623 // If the target supports FP_TO_UINT of this type, use it.
5624 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5625 default: break;
5626 case TargetLowering::Legal:
5627 if (!TLI.isTypeLegal(NewOutTy))
5628 break; // Can't use this datatype.
5629 // FALL THROUGH.
5630 case TargetLowering::Custom:
5631 OpToUse = ISD::FP_TO_UINT;
5632 break;
5633 }
5634 if (OpToUse) break;
5635
5636 // Otherwise, try a larger type.
5637 }
5638
Chris Lattnerf81d5882007-11-24 07:07:01 +00005639
5640 // Okay, we found the operation and type to use.
5641 SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
5642
5643 // If the operation produces an invalid type, it must be custom lowered. Use
5644 // the target lowering hooks to expand it. Just keep the low part of the
5645 // expanded operation, we know that we're truncating anyway.
5646 if (getTypeAction(NewOutTy) == Expand) {
5647 Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0);
5648 assert(Operation.Val && "Didn't return anything");
5649 }
5650
5651 // Truncate the result of the extended FP_TO_*INT operation to the desired
5652 // size.
5653 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005654}
5655
5656/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5657///
5658SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
5659 MVT::ValueType VT = Op.getValueType();
5660 MVT::ValueType SHVT = TLI.getShiftAmountTy();
5661 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
5662 switch (VT) {
5663 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5664 case MVT::i16:
5665 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5666 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5667 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5668 case MVT::i32:
5669 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5670 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5671 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5672 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5673 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5674 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5675 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5676 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5677 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5678 case MVT::i64:
5679 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5680 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5681 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5682 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5683 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5684 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5685 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5686 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5687 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5688 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5689 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5690 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5691 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5692 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5693 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5694 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5695 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5696 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5697 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5698 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5699 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5700 }
5701}
5702
5703/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5704///
5705SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
5706 switch (Opc) {
5707 default: assert(0 && "Cannot expand this yet!");
5708 case ISD::CTPOP: {
5709 static const uint64_t mask[6] = {
5710 0x5555555555555555ULL, 0x3333333333333333ULL,
5711 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5712 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5713 };
5714 MVT::ValueType VT = Op.getValueType();
5715 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005716 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005717 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5718 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
5719 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
5720 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5721 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5722 DAG.getNode(ISD::AND, VT,
5723 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
5724 }
5725 return Op;
5726 }
5727 case ISD::CTLZ: {
5728 // for now, we do this:
5729 // x = x | (x >> 1);
5730 // x = x | (x >> 2);
5731 // ...
5732 // x = x | (x >>16);
5733 // x = x | (x >>32); // for 64-bit input
5734 // return popcount(~x);
5735 //
5736 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
5737 MVT::ValueType VT = Op.getValueType();
5738 MVT::ValueType ShVT = TLI.getShiftAmountTy();
Dan Gohman1796f1f2007-05-18 17:52:13 +00005739 unsigned len = MVT::getSizeInBits(VT);
Chris Lattner689bdcc2006-01-28 08:25:58 +00005740 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5741 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
5742 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
5743 }
5744 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
5745 return DAG.getNode(ISD::CTPOP, VT, Op);
5746 }
5747 case ISD::CTTZ: {
5748 // for now, we use: { return popcount(~x & (x - 1)); }
5749 // unless the target has ctlz but not ctpop, in which case we use:
5750 // { return 32 - nlz(~x & (x-1)); }
5751 // see also http://www.hackersdelight.org/HDcode/ntz.cc
5752 MVT::ValueType VT = Op.getValueType();
5753 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
5754 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
5755 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
5756 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
5757 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
5758 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
5759 TLI.isOperationLegal(ISD::CTLZ, VT))
5760 return DAG.getNode(ISD::SUB, VT,
Dan Gohman1796f1f2007-05-18 17:52:13 +00005761 DAG.getConstant(MVT::getSizeInBits(VT), VT),
Chris Lattner689bdcc2006-01-28 08:25:58 +00005762 DAG.getNode(ISD::CTLZ, VT, Tmp3));
5763 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
5764 }
5765 }
5766}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00005767
Chris Lattnerdc750592005-01-07 07:47:09 +00005768/// ExpandOp - Expand the specified SDOperand into its two component pieces
5769/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
5770/// LegalizeNodes map is filled in for any results that are not expanded, the
5771/// ExpandedNodes map is filled in for any results that are expanded, and the
5772/// Lo/Hi values are returned.
5773void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
5774 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00005775 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00005776 SDNode *Node = Op.Val;
5777 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Evan Cheng4eee7242006-12-09 02:42:38 +00005778 assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) ||
Dan Gohmana8665142007-06-25 16:23:39 +00005779 MVT::isVector(VT)) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00005780 "Cannot expand to FP value or to larger int value!");
5781
Chris Lattner1a570f12005-09-02 20:32:45 +00005782 // See if we already expanded it.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00005783 DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
Chris Lattner1a570f12005-09-02 20:32:45 +00005784 = ExpandedNodes.find(Op);
5785 if (I != ExpandedNodes.end()) {
5786 Lo = I->second.first;
5787 Hi = I->second.second;
5788 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00005789 }
5790
Chris Lattnerdc750592005-01-07 07:47:09 +00005791 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00005792 case ISD::CopyFromReg:
5793 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen666323e2007-10-10 01:01:31 +00005794 case ISD::FP_ROUND_INREG:
5795 if (VT == MVT::ppcf128 &&
5796 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
5797 TargetLowering::Custom) {
Dale Johannesen6472eb62007-10-11 23:32:15 +00005798 SDOperand SrcLo, SrcHi, Src;
5799 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
5800 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
5801 SDOperand Result = TLI.LowerOperation(
5802 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Dale Johannesen666323e2007-10-10 01:01:31 +00005803 assert(Result.Val->getOpcode() == ISD::BUILD_PAIR);
5804 Lo = Result.Val->getOperand(0);
5805 Hi = Result.Val->getOperand(1);
5806 break;
5807 }
5808 // fall through
Chris Lattner44cab002006-01-21 04:27:00 +00005809 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005810#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00005811 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00005812#endif
Chris Lattnerdc750592005-01-07 07:47:09 +00005813 assert(0 && "Do not know how to expand this operator!");
5814 abort();
Dan Gohman66272a52008-02-27 01:52:30 +00005815 case ISD::EXTRACT_ELEMENT:
5816 ExpandOp(Node->getOperand(0), Lo, Hi);
5817 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
5818 return ExpandOp(Hi, Lo, Hi);
Dan Gohmane5e32ec2008-02-27 19:44:57 +00005819 return ExpandOp(Lo, Lo, Hi);
Dale Johannesenb066c1f2007-10-31 00:32:36 +00005820 case ISD::EXTRACT_VECTOR_ELT:
5821 assert(VT==MVT::i64 && "Do not know how to expand this operator!");
5822 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
5823 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
5824 return ExpandOp(Lo, Lo, Hi);
Nate Begemancda9aa72005-04-01 22:34:39 +00005825 case ISD::UNDEF:
Evan Cheng851e5892006-12-16 02:20:50 +00005826 NVT = TLI.getTypeToExpandTo(VT);
Nate Begemancda9aa72005-04-01 22:34:39 +00005827 Lo = DAG.getNode(ISD::UNDEF, NVT);
5828 Hi = DAG.getNode(ISD::UNDEF, NVT);
5829 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00005830 case ISD::Constant: {
Dan Gohmanf2bbfa32008-03-03 22:20:46 +00005831 unsigned NVTBits = MVT::getSizeInBits(NVT);
5832 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
5833 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
5834 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattnerdc750592005-01-07 07:47:09 +00005835 break;
5836 }
Evan Cheng47833a12006-12-12 21:32:44 +00005837 case ISD::ConstantFP: {
5838 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesen007aa372007-10-11 18:07:22 +00005839 if (CFP->getValueType(0) == MVT::ppcf128) {
5840 APInt api = CFP->getValueAPF().convertToAPInt();
5841 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
5842 MVT::f64);
5843 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
5844 MVT::f64);
5845 break;
5846 }
Evan Cheng3766fc602006-12-12 22:19:28 +00005847 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng22cf8992006-12-13 20:57:08 +00005848 if (getTypeAction(Lo.getValueType()) == Expand)
5849 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005850 break;
5851 }
Chris Lattner32e08b72005-03-28 22:03:13 +00005852 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005853 // Return the operands.
5854 Lo = Node->getOperand(0);
5855 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00005856 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00005857
5858 case ISD::MERGE_VALUES:
Chris Lattnercab915f2007-11-24 19:12:15 +00005859 if (Node->getNumValues() == 1) {
5860 ExpandOp(Op.getOperand(0), Lo, Hi);
5861 break;
5862 }
Chris Lattnerf81d5882007-11-24 07:07:01 +00005863 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
5864 assert(Op.ResNo == 0 && Node->getNumValues() == 2 &&
5865 Op.getValue(1).getValueType() == MVT::Other &&
5866 "unhandled MERGE_VALUES");
5867 ExpandOp(Op.getOperand(0), Lo, Hi);
5868 // Remember that we legalized the chain.
5869 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
5870 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005871
5872 case ISD::SIGN_EXTEND_INREG:
5873 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnerf5839a02006-10-06 17:34:12 +00005874 // sext_inreg the low part if needed.
5875 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
5876
5877 // The high part gets the sign extension from the lo-part. This handles
5878 // things like sextinreg V:i64 from i8.
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005879 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
5880 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
5881 TLI.getShiftAmountTy()));
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00005882 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00005883
Nate Begeman2fba8a32006-01-14 03:14:10 +00005884 case ISD::BSWAP: {
5885 ExpandOp(Node->getOperand(0), Lo, Hi);
5886 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
5887 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
5888 Lo = TempLo;
5889 break;
5890 }
5891
Chris Lattner55e9cde2005-05-11 04:51:16 +00005892 case ISD::CTPOP:
5893 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00005894 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
5895 DAG.getNode(ISD::CTPOP, NVT, Lo),
5896 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00005897 Hi = DAG.getConstant(0, NVT);
5898 break;
5899
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005900 case ISD::CTLZ: {
5901 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005902 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005903 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5904 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Scott Michela6729e82008-03-10 15:42:14 +00005905 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Chris Lattnerd47675e2005-08-09 20:20:18 +00005906 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005907 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
5908 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
5909
5910 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
5911 Hi = DAG.getConstant(0, NVT);
5912 break;
5913 }
5914
5915 case ISD::CTTZ: {
5916 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00005917 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005918 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
5919 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Scott Michela6729e82008-03-10 15:42:14 +00005920 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Chris Lattnerd47675e2005-08-09 20:20:18 +00005921 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00005922 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
5923 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
5924
5925 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
5926 Hi = DAG.getConstant(0, NVT);
5927 break;
5928 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00005929
Nate Begemane74795c2006-01-25 18:21:52 +00005930 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005931 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
5932 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00005933 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
5934 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
5935
5936 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00005937 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00005938 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands7377f5f2008-02-11 10:37:04 +00005939 if (TLI.isBigEndian())
Nate Begemane74795c2006-01-25 18:21:52 +00005940 std::swap(Lo, Hi);
5941 break;
5942 }
5943
Chris Lattnerdc750592005-01-07 07:47:09 +00005944 case ISD::LOAD: {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005945 LoadSDNode *LD = cast<LoadSDNode>(Node);
5946 SDOperand Ch = LD->getChain(); // Legalize the chain.
5947 SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer.
5948 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman2af30632007-07-09 22:18:38 +00005949 int SVOffset = LD->getSrcValueOffset();
5950 unsigned Alignment = LD->getAlignment();
5951 bool isVolatile = LD->isVolatile();
Chris Lattnerdc750592005-01-07 07:47:09 +00005952
Evan Chenge71fe34d2006-10-09 20:57:25 +00005953 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman2af30632007-07-09 22:18:38 +00005954 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5955 isVolatile, Alignment);
Evan Cheng47833a12006-12-12 21:32:44 +00005956 if (VT == MVT::f32 || VT == MVT::f64) {
5957 // f32->i32 or f64->i64 one to one expansion.
5958 // Remember that we legalized the chain.
5959 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng22cf8992006-12-13 20:57:08 +00005960 // Recursively expand the new load.
5961 if (getTypeAction(NVT) == Expand)
5962 ExpandOp(Lo, Lo, Hi);
Evan Cheng47833a12006-12-12 21:32:44 +00005963 break;
5964 }
Chris Lattner0d03eb42005-01-19 18:02:17 +00005965
Evan Chenge71fe34d2006-10-09 20:57:25 +00005966 // Increment the pointer to the other half.
5967 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
5968 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner72733e52008-01-17 07:00:52 +00005969 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00005970 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00005971 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman2af30632007-07-09 22:18:38 +00005972 Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset,
5973 isVolatile, Alignment);
Misha Brukman835702a2005-04-21 22:36:52 +00005974
Evan Chenge71fe34d2006-10-09 20:57:25 +00005975 // Build a factor node to remember that this load is independent of the
5976 // other one.
5977 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
5978 Hi.getValue(1));
5979
5980 // Remember that we legalized the chain.
5981 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands7377f5f2008-02-11 10:37:04 +00005982 if (TLI.isBigEndian())
Evan Chenge71fe34d2006-10-09 20:57:25 +00005983 std::swap(Lo, Hi);
5984 } else {
Dan Gohman47a7d6f2008-01-30 00:15:11 +00005985 MVT::ValueType EVT = LD->getMemoryVT();
Evan Chenge370e0e2006-12-13 03:19:57 +00005986
Dale Johannesen6802d0c2007-10-19 20:29:00 +00005987 if ((VT == MVT::f64 && EVT == MVT::f32) ||
5988 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Chenge370e0e2006-12-13 03:19:57 +00005989 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
5990 SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00005991 SVOffset, isVolatile, Alignment);
Evan Chenge370e0e2006-12-13 03:19:57 +00005992 // Remember that we legalized the chain.
5993 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1)));
5994 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
5995 break;
5996 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00005997
5998 if (EVT == NVT)
5999 Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00006000 SVOffset, isVolatile, Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00006001 else
6002 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(),
Dan Gohman2af30632007-07-09 22:18:38 +00006003 SVOffset, EVT, isVolatile,
6004 Alignment);
Evan Chenge71fe34d2006-10-09 20:57:25 +00006005
6006 // Remember that we legalized the chain.
6007 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
6008
6009 if (ExtType == ISD::SEXTLOAD) {
6010 // The high part is obtained by SRA'ing all but one of the bits of the
6011 // lo part.
6012 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
6013 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6014 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6015 } else if (ExtType == ISD::ZEXTLOAD) {
6016 // The high part is just a zero.
6017 Hi = DAG.getConstant(0, NVT);
6018 } else /* if (ExtType == ISD::EXTLOAD) */ {
6019 // The high part is undefined.
6020 Hi = DAG.getNode(ISD::UNDEF, NVT);
6021 }
6022 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006023 break;
6024 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006025 case ISD::AND:
6026 case ISD::OR:
6027 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
6028 SDOperand LL, LH, RL, RH;
6029 ExpandOp(Node->getOperand(0), LL, LH);
6030 ExpandOp(Node->getOperand(1), RL, RH);
6031 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6032 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6033 break;
6034 }
6035 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00006036 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00006037 ExpandOp(Node->getOperand(1), LL, LH);
6038 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng884bc092006-12-15 22:42:55 +00006039 if (getTypeAction(NVT) == Expand)
6040 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00006041 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng884bc092006-12-15 22:42:55 +00006042 if (VT != MVT::f32)
6043 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00006044 break;
6045 }
Nate Begemane5b86d72005-08-10 20:51:12 +00006046 case ISD::SELECT_CC: {
6047 SDOperand TL, TH, FL, FH;
6048 ExpandOp(Node->getOperand(2), TL, TH);
6049 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng884bc092006-12-15 22:42:55 +00006050 if (getTypeAction(NVT) == Expand)
6051 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begemane5b86d72005-08-10 20:51:12 +00006052 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6053 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng884bc092006-12-15 22:42:55 +00006054 if (VT != MVT::f32)
6055 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6056 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00006057 break;
6058 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006059 case ISD::ANY_EXTEND:
6060 // The low part is any extension of the input (which degenerates to a copy).
6061 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6062 // The high part is undefined.
6063 Hi = DAG.getNode(ISD::UNDEF, NVT);
6064 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00006065 case ISD::SIGN_EXTEND: {
6066 // The low part is just a sign extension of the input (which degenerates to
6067 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006068 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00006069
Chris Lattnerdc750592005-01-07 07:47:09 +00006070 // The high part is obtained by SRA'ing all but one of the bits of the lo
6071 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00006072 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006073 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6074 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00006075 break;
6076 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006077 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00006078 // The low part is just a zero extension of the input (which degenerates to
6079 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006080 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00006081
Chris Lattnerdc750592005-01-07 07:47:09 +00006082 // The high part is just a zero.
6083 Hi = DAG.getConstant(0, NVT);
6084 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00006085
Chris Lattner59b27fa372007-02-13 23:55:16 +00006086 case ISD::TRUNCATE: {
6087 // The input value must be larger than this value. Expand *it*.
6088 SDOperand NewLo;
6089 ExpandOp(Node->getOperand(0), NewLo, Hi);
6090
6091 // The low part is now either the right size, or it is closer. If not the
6092 // right size, make an illegal truncate so we recursively expand it.
6093 if (NewLo.getValueType() != Node->getValueType(0))
6094 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6095 ExpandOp(NewLo, Lo, Hi);
6096 break;
6097 }
6098
Chris Lattner36e663d2005-12-23 00:16:34 +00006099 case ISD::BIT_CONVERT: {
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006100 SDOperand Tmp;
6101 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6102 // If the target wants to, allow it to lower this itself.
6103 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6104 case Expand: assert(0 && "cannot expand FP!");
6105 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6106 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6107 }
6108 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6109 }
6110
Evan Cheng4eee7242006-12-09 02:42:38 +00006111 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng3432ab92006-12-11 19:27:14 +00006112 if (VT == MVT::f32 || VT == MVT::f64) {
6113 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng884bc092006-12-15 22:42:55 +00006114 if (getTypeAction(NVT) == Expand)
6115 ExpandOp(Lo, Lo, Hi);
Evan Cheng0076ca02006-12-12 19:53:13 +00006116 break;
6117 }
6118
6119 // If source operand will be expanded to the same type as VT, i.e.
6120 // i64 <- f64, i32 <- f32, expand the source operand instead.
6121 MVT::ValueType VT0 = Node->getOperand(0).getValueType();
6122 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6123 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006124 break;
6125 }
6126
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006127 // Turn this into a load/store pair by default.
6128 if (Tmp.Val == 0)
Chris Lattner87bc3e72008-01-16 07:45:30 +00006129 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnere4bbb6c2006-09-09 00:20:27 +00006130
Chris Lattner36e663d2005-12-23 00:16:34 +00006131 ExpandOp(Tmp, Lo, Hi);
6132 break;
6133 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006134
Chris Lattnerf81d5882007-11-24 07:07:01 +00006135 case ISD::READCYCLECOUNTER: {
Chris Lattner44c28c22005-11-20 22:56:56 +00006136 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6137 TargetLowering::Custom &&
6138 "Must custom expand ReadCycleCounter");
Chris Lattnerf81d5882007-11-24 07:07:01 +00006139 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6140 assert(Tmp.Val && "Node must be custom expanded!");
6141 ExpandOp(Tmp.getValue(0), Lo, Hi);
Chris Lattner44c28c22005-11-20 22:56:56 +00006142 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerf81d5882007-11-24 07:07:01 +00006143 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006144 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006145 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00006146
Andrew Lenharth357061a2008-03-05 01:15:49 +00006147 case ISD::ATOMIC_LCS: {
6148 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
6149 assert(Tmp.Val && "Node must be custom expanded!");
6150 ExpandOp(Tmp.getValue(0), Lo, Hi);
6151 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
6152 LegalizeOp(Tmp.getValue(1)));
6153 break;
6154 }
6155
6156
6157
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006158 // These operators cannot be expanded directly, emit them as calls to
6159 // library functions.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006160 case ISD::FP_TO_SINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00006161 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00006162 SDOperand Op;
6163 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6164 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006165 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6166 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00006167 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006168
Chris Lattner941d84a2005-07-30 01:40:57 +00006169 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6170
Chris Lattnerfe68d752005-07-29 00:33:32 +00006171 // Now that the custom expander is done, expand the result, which is still
6172 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00006173 if (Op.Val) {
6174 ExpandOp(Op, Lo, Hi);
6175 break;
6176 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00006177 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006178
Dale Johannesenc0154c02007-10-05 20:04:43 +00006179 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006180 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006181 LC = RTLIB::FPTOSINT_F32_I64;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006182 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006183 LC = RTLIB::FPTOSINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00006184 else if (Node->getOperand(0).getValueType() == MVT::f80)
6185 LC = RTLIB::FPTOSINT_F80_I64;
6186 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6187 LC = RTLIB::FPTOSINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006188 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6189 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006190 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006191 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006192
Evan Cheng31cbddf2007-01-12 02:11:51 +00006193 case ISD::FP_TO_UINT: {
Chris Lattnerfe68d752005-07-29 00:33:32 +00006194 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006195 SDOperand Op;
6196 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6197 case Expand: assert(0 && "cannot expand FP!");
6198 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6199 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6200 }
6201
6202 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6203
6204 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00006205 if (Op.Val) {
6206 ExpandOp(Op, Lo, Hi);
6207 break;
6208 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00006209 }
Jeff Cohen546fd592005-07-30 18:33:25 +00006210
Evan Chengfd11ef42007-10-05 01:09:32 +00006211 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006212 if (Node->getOperand(0).getValueType() == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006213 LC = RTLIB::FPTOUINT_F32_I64;
Dale Johannesen789b5a52007-09-28 18:44:17 +00006214 else if (Node->getOperand(0).getValueType() == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006215 LC = RTLIB::FPTOUINT_F64_I64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00006216 else if (Node->getOperand(0).getValueType() == MVT::f80)
6217 LC = RTLIB::FPTOUINT_F80_I64;
6218 else if (Node->getOperand(0).getValueType() == MVT::ppcf128)
6219 LC = RTLIB::FPTOUINT_PPCF128_I64;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006220 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node,
6221 false/*sign irrelevant*/, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006222 break;
Evan Cheng31cbddf2007-01-12 02:11:51 +00006223 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00006224
Evan Cheng870e4f82006-01-09 18:31:59 +00006225 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006226 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00006227 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006228 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006229 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006230 Op = TLI.LowerOperation(Op, DAG);
6231 if (Op.Val) {
6232 // Now that the custom expander is done, expand the result, which is
6233 // still VT.
6234 ExpandOp(Op, Lo, Hi);
6235 break;
6236 }
6237 }
6238
Chris Lattner72b503b2006-09-13 03:50:39 +00006239 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6240 // this X << 1 as X+X.
6241 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
6242 if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
6243 TLI.isOperationLegal(ISD::ADDE, NVT)) {
6244 SDOperand LoOps[2], HiOps[3];
6245 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6246 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6247 LoOps[1] = LoOps[0];
6248 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6249
6250 HiOps[1] = HiOps[0];
6251 HiOps[2] = Lo.getValue(1);
6252 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6253 break;
6254 }
6255 }
6256
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006257 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006258 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006259 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006260
6261 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006262 TargetLowering::LegalizeAction Action =
6263 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6264 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6265 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006266 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006267 break;
6268 }
6269
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006270 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006271 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node,
6272 false/*left shift=unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006273 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006274 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006275
Evan Cheng870e4f82006-01-09 18:31:59 +00006276 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006277 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00006278 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006279 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006280 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006281 Op = TLI.LowerOperation(Op, DAG);
6282 if (Op.Val) {
6283 // Now that the custom expander is done, expand the result, which is
6284 // still VT.
6285 ExpandOp(Op, Lo, Hi);
6286 break;
6287 }
6288 }
6289
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006290 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006291 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006292 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006293
6294 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006295 TargetLowering::LegalizeAction Action =
6296 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6297 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6298 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006299 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006300 break;
6301 }
6302
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006303 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006304 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node,
6305 true/*ashr is signed*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006306 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006307 }
6308
6309 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006310 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00006311 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006312 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006313 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00006314 Op = TLI.LowerOperation(Op, DAG);
6315 if (Op.Val) {
6316 // Now that the custom expander is done, expand the result, which is
6317 // still VT.
6318 ExpandOp(Op, Lo, Hi);
6319 break;
6320 }
6321 }
6322
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006323 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00006324 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006325 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00006326
6327 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00006328 TargetLowering::LegalizeAction Action =
6329 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6330 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6331 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00006332 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00006333 break;
6334 }
6335
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006336 // Otherwise, emit a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006337 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node,
6338 false/*lshr is unsigned*/, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006339 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00006340 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00006341
Misha Brukman835702a2005-04-21 22:36:52 +00006342 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006343 case ISD::SUB: {
6344 // If the target wants to custom expand this, let them.
6345 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6346 TargetLowering::Custom) {
6347 Op = TLI.LowerOperation(Op, DAG);
6348 if (Op.Val) {
6349 ExpandOp(Op, Lo, Hi);
6350 break;
6351 }
6352 }
6353
6354 // Expand the subcomponents.
6355 SDOperand LHSL, LHSH, RHSL, RHSH;
6356 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6357 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner72b503b2006-09-13 03:50:39 +00006358 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6359 SDOperand LoOps[2], HiOps[3];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006360 LoOps[0] = LHSL;
6361 LoOps[1] = RHSL;
6362 HiOps[0] = LHSH;
6363 HiOps[1] = RHSH;
Nate Begeman5965bd12006-02-17 05:43:56 +00006364 if (Node->getOpcode() == ISD::ADD) {
Chris Lattner72b503b2006-09-13 03:50:39 +00006365 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006366 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00006367 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00006368 } else {
Chris Lattner72b503b2006-09-13 03:50:39 +00006369 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006370 HiOps[2] = Lo.getValue(1);
Chris Lattner72b503b2006-09-13 03:50:39 +00006371 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
Nate Begeman5965bd12006-02-17 05:43:56 +00006372 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +00006373 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00006374 }
Chris Lattner2135bc02007-05-17 18:15:41 +00006375
6376 case ISD::ADDC:
6377 case ISD::SUBC: {
6378 // Expand the subcomponents.
6379 SDOperand LHSL, LHSH, RHSL, RHSH;
6380 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6381 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6382 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6383 SDOperand LoOps[2] = { LHSL, RHSL };
6384 SDOperand HiOps[3] = { LHSH, RHSH };
6385
6386 if (Node->getOpcode() == ISD::ADDC) {
6387 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6388 HiOps[2] = Lo.getValue(1);
6389 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6390 } else {
6391 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6392 HiOps[2] = Lo.getValue(1);
6393 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6394 }
6395 // Remember that we legalized the flag.
6396 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6397 break;
6398 }
6399 case ISD::ADDE:
6400 case ISD::SUBE: {
6401 // Expand the subcomponents.
6402 SDOperand LHSL, LHSH, RHSL, RHSH;
6403 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6404 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6405 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
6406 SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6407 SDOperand HiOps[3] = { LHSH, RHSH };
6408
6409 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6410 HiOps[2] = Lo.getValue(1);
6411 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6412
6413 // Remember that we legalized the flag.
6414 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6415 break;
6416 }
Nate Begemanadd0c632005-04-11 03:01:51 +00006417 case ISD::MUL: {
Chris Lattnerfbadbda2006-09-16 00:09:24 +00006418 // If the target wants to custom expand this, let them.
6419 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Chris Lattnere50f5d12006-09-16 05:08:34 +00006420 SDOperand New = TLI.LowerOperation(Op, DAG);
6421 if (New.Val) {
6422 ExpandOp(New, Lo, Hi);
Chris Lattnerfbadbda2006-09-16 00:09:24 +00006423 break;
6424 }
6425 }
6426
Evan Chenge93762d2006-09-01 18:17:58 +00006427 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6428 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohmana1603612007-10-08 18:33:35 +00006429 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6430 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6431 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Nate Begemanadd0c632005-04-11 03:01:51 +00006432 SDOperand LL, LH, RL, RH;
6433 ExpandOp(Node->getOperand(0), LL, LH);
6434 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman1f372ed2008-02-25 21:11:39 +00006435 unsigned OuterBitSize = Op.getValueSizeInBits();
6436 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohmana1603612007-10-08 18:33:35 +00006437 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6438 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman1f372ed2008-02-25 21:11:39 +00006439 if (DAG.MaskedValueIsZero(Op.getOperand(0),
6440 APInt::getHighBitsSet(OuterBitSize, LHSSB)) &&
6441 DAG.MaskedValueIsZero(Op.getOperand(1),
6442 APInt::getHighBitsSet(OuterBitSize, RHSSB))) {
Dan Gohmana1603612007-10-08 18:33:35 +00006443 // The inputs are both zero-extended.
6444 if (HasUMUL_LOHI) {
6445 // We can emit a umul_lohi.
6446 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6447 Hi = SDOperand(Lo.Val, 1);
6448 break;
6449 }
6450 if (HasMULHU) {
6451 // We can emit a mulhu+mul.
6452 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6453 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6454 break;
6455 }
Dan Gohmana1603612007-10-08 18:33:35 +00006456 }
Dan Gohman1f372ed2008-02-25 21:11:39 +00006457 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohmana1603612007-10-08 18:33:35 +00006458 // The input values are both sign-extended.
6459 if (HasSMUL_LOHI) {
6460 // We can emit a smul_lohi.
6461 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
6462 Hi = SDOperand(Lo.Val, 1);
6463 break;
6464 }
6465 if (HasMULHS) {
6466 // We can emit a mulhs+mul.
6467 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6468 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6469 break;
6470 }
6471 }
6472 if (HasUMUL_LOHI) {
6473 // Lo,Hi = umul LHS, RHS.
6474 SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
6475 DAG.getVTList(NVT, NVT), LL, RL);
6476 Lo = UMulLOHI;
6477 Hi = UMulLOHI.getValue(1);
Nate Begeman43144a22005-08-30 02:44:00 +00006478 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6479 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6480 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6481 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattner1b633912006-09-16 00:21:44 +00006482 break;
Nate Begeman43144a22005-08-30 02:44:00 +00006483 }
Dale Johannesena4a972e2007-10-24 22:26:08 +00006484 if (HasMULHU) {
6485 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6486 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6487 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6488 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6489 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6490 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6491 break;
6492 }
Nate Begemanadd0c632005-04-11 03:01:51 +00006493 }
Evan Chenge93762d2006-09-01 18:17:58 +00006494
Dan Gohmana1603612007-10-08 18:33:35 +00006495 // If nothing else, we can make a libcall.
Evan Cheng31cbddf2007-01-12 02:11:51 +00006496 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node,
6497 false/*sign irrelevant*/, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00006498 break;
6499 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006500 case ISD::SDIV:
6501 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi);
6502 break;
6503 case ISD::UDIV:
6504 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi);
6505 break;
6506 case ISD::SREM:
6507 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi);
6508 break;
6509 case ISD::UREM:
6510 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi);
6511 break;
Evan Cheng97a750f2006-12-12 21:51:17 +00006512
Evan Cheng4eee7242006-12-09 02:42:38 +00006513 case ISD::FADD:
Duncan Sands53c954f2008-01-10 10:28:30 +00006514 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32,
6515 RTLIB::ADD_F64,
6516 RTLIB::ADD_F80,
6517 RTLIB::ADD_PPCF128)),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006518 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006519 break;
6520 case ISD::FSUB:
Duncan Sands53c954f2008-01-10 10:28:30 +00006521 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32,
6522 RTLIB::SUB_F64,
6523 RTLIB::SUB_F80,
6524 RTLIB::SUB_PPCF128)),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006525 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006526 break;
6527 case ISD::FMUL:
Duncan Sands53c954f2008-01-10 10:28:30 +00006528 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32,
6529 RTLIB::MUL_F64,
6530 RTLIB::MUL_F80,
6531 RTLIB::MUL_PPCF128)),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006532 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006533 break;
6534 case ISD::FDIV:
Duncan Sands53c954f2008-01-10 10:28:30 +00006535 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32,
6536 RTLIB::DIV_F64,
6537 RTLIB::DIV_F80,
6538 RTLIB::DIV_PPCF128)),
Evan Cheng31cbddf2007-01-12 02:11:51 +00006539 Node, false, Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006540 break;
6541 case ISD::FP_EXTEND:
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006542 if (VT == MVT::ppcf128) {
6543 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6544 Node->getOperand(0).getValueType()==MVT::f64);
6545 const uint64_t zero = 0;
6546 if (Node->getOperand(0).getValueType()==MVT::f32)
6547 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6548 else
6549 Hi = Node->getOperand(0);
6550 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6551 break;
6552 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006553 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006554 break;
6555 case ISD::FP_ROUND:
Evan Cheng31cbddf2007-01-12 02:11:51 +00006556 Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi);
Evan Cheng4eee7242006-12-09 02:42:38 +00006557 break;
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006558 case ISD::FPOWI:
Duncan Sands53c954f2008-01-10 10:28:30 +00006559 Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32,
6560 RTLIB::POWI_F64,
6561 RTLIB::POWI_F80,
6562 RTLIB::POWI_PPCF128)),
Lauro Ramos Venancioa392cd22007-08-15 22:13:27 +00006563 Node, false, Hi);
6564 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006565 case ISD::FSQRT:
6566 case ISD::FSIN:
6567 case ISD::FCOS: {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006568 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Chengf3a80c62006-12-13 02:38:13 +00006569 switch(Node->getOpcode()) {
Evan Cheng31cbddf2007-01-12 02:11:51 +00006570 case ISD::FSQRT:
Duncan Sands53c954f2008-01-10 10:28:30 +00006571 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6572 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006573 break;
6574 case ISD::FSIN:
Duncan Sands53c954f2008-01-10 10:28:30 +00006575 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6576 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006577 break;
6578 case ISD::FCOS:
Duncan Sands53c954f2008-01-10 10:28:30 +00006579 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6580 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng31cbddf2007-01-12 02:11:51 +00006581 break;
Evan Chengf3a80c62006-12-13 02:38:13 +00006582 default: assert(0 && "Unreachable!");
6583 }
Evan Cheng31cbddf2007-01-12 02:11:51 +00006584 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi);
Evan Chengf3a80c62006-12-13 02:38:13 +00006585 break;
6586 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006587 case ISD::FABS: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006588 if (VT == MVT::ppcf128) {
6589 SDOperand Tmp;
6590 ExpandOp(Node->getOperand(0), Lo, Tmp);
6591 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6592 // lo = hi==fabs(hi) ? lo : -lo;
6593 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6594 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6595 DAG.getCondCode(ISD::SETEQ));
6596 break;
6597 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006598 SDOperand Mask = (VT == MVT::f64)
6599 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6600 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6601 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6602 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6603 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6604 if (getTypeAction(NVT) == Expand)
6605 ExpandOp(Lo, Lo, Hi);
6606 break;
6607 }
6608 case ISD::FNEG: {
Dale Johannesen61c574f2007-10-12 19:02:17 +00006609 if (VT == MVT::ppcf128) {
6610 ExpandOp(Node->getOperand(0), Lo, Hi);
6611 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6612 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6613 break;
6614 }
Evan Cheng388cbbf2006-12-16 00:52:40 +00006615 SDOperand Mask = (VT == MVT::f64)
6616 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6617 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6618 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6619 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6620 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6621 if (getTypeAction(NVT) == Expand)
6622 ExpandOp(Lo, Lo, Hi);
6623 break;
6624 }
Evan Cheng003feb02007-01-04 21:56:39 +00006625 case ISD::FCOPYSIGN: {
6626 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
6627 if (getTypeAction(NVT) == Expand)
6628 ExpandOp(Lo, Lo, Hi);
6629 break;
6630 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006631 case ISD::SINT_TO_FP:
6632 case ISD::UINT_TO_FP: {
6633 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
6634 MVT::ValueType SrcVT = Node->getOperand(0).getValueType();
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006635 if (VT == MVT::ppcf128 && SrcVT != MVT::i64) {
Dan Gohman432e4a62008-02-25 21:39:34 +00006636 static const uint64_t zero = 0;
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006637 if (isSigned) {
6638 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6639 Node->getOperand(0)));
6640 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6641 } else {
Dan Gohman432e4a62008-02-25 21:39:34 +00006642 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006643 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
6644 Node->getOperand(0)));
6645 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6646 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006647 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesen05ff9e82007-10-12 01:37:08 +00006648 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6649 DAG.getConstant(0, MVT::i32),
6650 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6651 DAG.getConstantFP(
6652 APFloat(APInt(128, 2, TwoE32)),
6653 MVT::ppcf128)),
6654 Hi,
6655 DAG.getCondCode(ISD::SETLT)),
6656 Lo, Hi);
6657 }
6658 break;
6659 }
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006660 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
6661 // si64->ppcf128 done by libcall, below
Dan Gohman432e4a62008-02-25 21:39:34 +00006662 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesena1a4a9e2007-10-12 17:52:03 +00006663 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
6664 Lo, Hi);
6665 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
6666 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
6667 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
6668 DAG.getConstant(0, MVT::i64),
6669 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
6670 DAG.getConstantFP(
6671 APFloat(APInt(128, 2, TwoE64)),
6672 MVT::ppcf128)),
6673 Hi,
6674 DAG.getCondCode(ISD::SETLT)),
6675 Lo, Hi);
6676 break;
6677 }
Evan Cheng75439b32007-09-27 07:35:39 +00006678 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006679 if (Node->getOperand(0).getValueType() == MVT::i64) {
6680 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006681 LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006682 else if (VT == MVT::f64)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006683 LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64;
Dale Johannesenc0154c02007-10-05 20:04:43 +00006684 else if (VT == MVT::f80) {
Dale Johannesen7d67e542007-09-19 23:55:34 +00006685 assert(isSigned);
Dale Johannesenc0154c02007-10-05 20:04:43 +00006686 LC = RTLIB::SINTTOFP_I64_F80;
6687 }
6688 else if (VT == MVT::ppcf128) {
6689 assert(isSigned);
6690 LC = RTLIB::SINTTOFP_I64_PPCF128;
Dale Johannesen7d67e542007-09-19 23:55:34 +00006691 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006692 } else {
6693 if (VT == MVT::f32)
Evan Cheng31cbddf2007-01-12 02:11:51 +00006694 LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006695 else
Evan Cheng31cbddf2007-01-12 02:11:51 +00006696 LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64;
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006697 }
6698
6699 // Promote the operand if needed.
6700 if (getTypeAction(SrcVT) == Promote) {
6701 SDOperand Tmp = PromoteOp(Node->getOperand(0));
6702 Tmp = isSigned
6703 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
6704 DAG.getValueType(SrcVT))
6705 : DAG.getZeroExtendInReg(Tmp, SrcVT);
6706 Node = DAG.UpdateNodeOperands(Op, Tmp).Val;
6707 }
Evan Chengbf535fc2007-04-27 07:33:31 +00006708
6709 const char *LibCall = TLI.getLibcallName(LC);
6710 if (LibCall)
6711 Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi);
6712 else {
6713 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
6714 Node->getOperand(0));
6715 if (getTypeAction(Lo.getValueType()) == Expand)
6716 ExpandOp(Lo, Lo, Hi);
6717 }
Evan Cheng9ad6edf2006-12-19 01:44:04 +00006718 break;
6719 }
Evan Chengf3a80c62006-12-13 02:38:13 +00006720 }
Chris Lattnerdc750592005-01-07 07:47:09 +00006721
Chris Lattnerac12f682005-12-21 18:02:52 +00006722 // Make sure the resultant values have been legalized themselves, unless this
6723 // is a type that requires multi-step expansion.
6724 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
6725 Lo = LegalizeOp(Lo);
Evan Cheng3432ab92006-12-11 19:27:14 +00006726 if (Hi.Val)
6727 // Don't legalize the high part if it is expanded to a single node.
6728 Hi = LegalizeOp(Hi);
Chris Lattnerac12f682005-12-21 18:02:52 +00006729 }
Evan Cheng870e4f82006-01-09 18:31:59 +00006730
6731 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006732 bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi)));
Evan Cheng870e4f82006-01-09 18:31:59 +00006733 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00006734}
6735
Dan Gohmana8665142007-06-25 16:23:39 +00006736/// SplitVectorOp - Given an operand of vector type, break it down into
6737/// two smaller values, still of vector type.
Chris Lattner32206f52006-03-18 01:44:44 +00006738void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo,
6739 SDOperand &Hi) {
Dan Gohmana8665142007-06-25 16:23:39 +00006740 assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!");
Chris Lattner32206f52006-03-18 01:44:44 +00006741 SDNode *Node = Op.Val;
Dan Gohman60028182007-09-24 15:54:53 +00006742 unsigned NumElements = MVT::getVectorNumElements(Op.getValueType());
Chris Lattner32206f52006-03-18 01:44:44 +00006743 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begemanbd117f02007-11-15 21:15:26 +00006744
Dan Gohman60028182007-09-24 15:54:53 +00006745 MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType());
Nate Begemanbd117f02007-11-15 21:15:26 +00006746
6747 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
6748 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
6749
6750 MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
6751 MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
6752
Chris Lattner32206f52006-03-18 01:44:44 +00006753 // See if we already split it.
6754 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
6755 = SplitNodes.find(Op);
6756 if (I != SplitNodes.end()) {
6757 Lo = I->second.first;
6758 Hi = I->second.second;
6759 return;
6760 }
6761
6762 switch (Node->getOpcode()) {
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006763 default:
6764#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00006765 Node->dump(&DAG);
Jim Laskeyc3d341e2006-07-11 17:58:07 +00006766#endif
6767 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattner67d77942007-11-19 20:21:32 +00006768 case ISD::UNDEF:
6769 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
6770 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
6771 break;
Dan Gohmana8665142007-06-25 16:23:39 +00006772 case ISD::BUILD_PAIR:
6773 Lo = Node->getOperand(0);
6774 Hi = Node->getOperand(1);
6775 break;
Dan Gohmana90183e2007-09-28 23:53:40 +00006776 case ISD::INSERT_VECTOR_ELT: {
6777 SplitVectorOp(Node->getOperand(0), Lo, Hi);
6778 unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
6779 SDOperand ScalarOp = Node->getOperand(1);
Nate Begemanbd117f02007-11-15 21:15:26 +00006780 if (Index < NewNumElts_Lo)
6781 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
Dan Gohmana90183e2007-09-28 23:53:40 +00006782 DAG.getConstant(Index, TLI.getPointerTy()));
6783 else
Nate Begemanbd117f02007-11-15 21:15:26 +00006784 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
6785 DAG.getConstant(Index - NewNumElts_Lo,
6786 TLI.getPointerTy()));
Dan Gohmana90183e2007-09-28 23:53:40 +00006787 break;
6788 }
Chris Lattner6fa95ec2007-11-19 21:16:54 +00006789 case ISD::VECTOR_SHUFFLE: {
6790 // Build the low part.
6791 SDOperand Mask = Node->getOperand(2);
6792 SmallVector<SDOperand, 8> Ops;
6793 MVT::ValueType PtrVT = TLI.getPointerTy();
6794
6795 // Insert all of the elements from the input that are needed. We use
6796 // buildvector of extractelement here because the input vectors will have
6797 // to be legalized, so this makes the code simpler.
6798 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
6799 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6800 SDOperand InVec = Node->getOperand(0);
6801 if (Idx >= NumElements) {
6802 InVec = Node->getOperand(1);
6803 Idx -= NumElements;
6804 }
6805 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6806 DAG.getConstant(Idx, PtrVT)));
6807 }
6808 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6809 Ops.clear();
6810
6811 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
6812 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue();
6813 SDOperand InVec = Node->getOperand(0);
6814 if (Idx >= NumElements) {
6815 InVec = Node->getOperand(1);
6816 Idx -= NumElements;
6817 }
6818 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
6819 DAG.getConstant(Idx, PtrVT)));
6820 }
6821 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
6822 break;
6823 }
Dan Gohmana8665142007-06-25 16:23:39 +00006824 case ISD::BUILD_VECTOR: {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00006825 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
Nate Begemanbd117f02007-11-15 21:15:26 +00006826 Node->op_begin()+NewNumElts_Lo);
6827 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006828
Nate Begemanbd117f02007-11-15 21:15:26 +00006829 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohmana8665142007-06-25 16:23:39 +00006830 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00006831 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattner32206f52006-03-18 01:44:44 +00006832 break;
6833 }
Dan Gohmana8665142007-06-25 16:23:39 +00006834 case ISD::CONCAT_VECTORS: {
Nate Begemanbd117f02007-11-15 21:15:26 +00006835 // FIXME: Handle non-power-of-two vectors?
Dan Gohmana8665142007-06-25 16:23:39 +00006836 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
6837 if (NewNumSubvectors == 1) {
6838 Lo = Node->getOperand(0);
6839 Hi = Node->getOperand(1);
6840 } else {
6841 SmallVector<SDOperand, 8> LoOps(Node->op_begin(),
6842 Node->op_begin()+NewNumSubvectors);
Nate Begemanbd117f02007-11-15 21:15:26 +00006843 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman26455c42007-06-13 15:12:02 +00006844
Dan Gohmana8665142007-06-25 16:23:39 +00006845 SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors,
6846 Node->op_end());
Nate Begemanbd117f02007-11-15 21:15:26 +00006847 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohmana8665142007-06-25 16:23:39 +00006848 }
Dan Gohman26455c42007-06-13 15:12:02 +00006849 break;
6850 }
Dan Gohman8f518b982007-10-17 14:48:28 +00006851 case ISD::SELECT: {
6852 SDOperand Cond = Node->getOperand(0);
6853
6854 SDOperand LL, LH, RL, RH;
6855 SplitVectorOp(Node->getOperand(1), LL, LH);
6856 SplitVectorOp(Node->getOperand(2), RL, RH);
6857
6858 if (MVT::isVector(Cond.getValueType())) {
6859 // Handle a vector merge.
6860 SDOperand CL, CH;
6861 SplitVectorOp(Cond, CL, CH);
Nate Begemanbd117f02007-11-15 21:15:26 +00006862 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
6863 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00006864 } else {
6865 // Handle a simple select with vector operands.
Nate Begemanbd117f02007-11-15 21:15:26 +00006866 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
6867 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohman8f518b982007-10-17 14:48:28 +00006868 }
6869 break;
6870 }
Dan Gohmana8665142007-06-25 16:23:39 +00006871 case ISD::ADD:
6872 case ISD::SUB:
6873 case ISD::MUL:
6874 case ISD::FADD:
6875 case ISD::FSUB:
6876 case ISD::FMUL:
6877 case ISD::SDIV:
6878 case ISD::UDIV:
6879 case ISD::FDIV:
Dan Gohman2a7de412007-10-11 23:57:53 +00006880 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00006881 case ISD::AND:
6882 case ISD::OR:
Dan Gohman36347a22007-11-19 15:15:03 +00006883 case ISD::XOR:
6884 case ISD::UREM:
6885 case ISD::SREM:
6886 case ISD::FREM: {
Chris Lattner32206f52006-03-18 01:44:44 +00006887 SDOperand LL, LH, RL, RH;
6888 SplitVectorOp(Node->getOperand(0), LL, LH);
6889 SplitVectorOp(Node->getOperand(1), RL, RH);
6890
Nate Begemanbd117f02007-11-15 21:15:26 +00006891 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
6892 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattner32206f52006-03-18 01:44:44 +00006893 break;
6894 }
Dan Gohman2a7de412007-10-11 23:57:53 +00006895 case ISD::FPOWI: {
6896 SDOperand L, H;
6897 SplitVectorOp(Node->getOperand(0), L, H);
6898
Nate Begemanbd117f02007-11-15 21:15:26 +00006899 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
6900 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman2a7de412007-10-11 23:57:53 +00006901 break;
6902 }
6903 case ISD::CTTZ:
6904 case ISD::CTLZ:
6905 case ISD::CTPOP:
6906 case ISD::FNEG:
6907 case ISD::FABS:
6908 case ISD::FSQRT:
6909 case ISD::FSIN:
Nate Begemand4d45c22007-11-17 03:58:34 +00006910 case ISD::FCOS:
6911 case ISD::FP_TO_SINT:
6912 case ISD::FP_TO_UINT:
6913 case ISD::SINT_TO_FP:
6914 case ISD::UINT_TO_FP: {
Dan Gohman2a7de412007-10-11 23:57:53 +00006915 SDOperand L, H;
6916 SplitVectorOp(Node->getOperand(0), L, H);
6917
Nate Begemanbd117f02007-11-15 21:15:26 +00006918 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
6919 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman2a7de412007-10-11 23:57:53 +00006920 break;
6921 }
Dan Gohmana8665142007-06-25 16:23:39 +00006922 case ISD::LOAD: {
6923 LoadSDNode *LD = cast<LoadSDNode>(Node);
6924 SDOperand Ch = LD->getChain();
6925 SDOperand Ptr = LD->getBasePtr();
6926 const Value *SV = LD->getSrcValue();
6927 int SVOffset = LD->getSrcValueOffset();
6928 unsigned Alignment = LD->getAlignment();
6929 bool isVolatile = LD->isVolatile();
6930
Nate Begemanbd117f02007-11-15 21:15:26 +00006931 Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
6932 unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8;
Chris Lattner32206f52006-03-18 01:44:44 +00006933 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner72733e52008-01-17 07:00:52 +00006934 DAG.getIntPtrConstant(IncrementSize));
Dan Gohmana8665142007-06-25 16:23:39 +00006935 SVOffset += IncrementSize;
Duncan Sands1826ded2007-10-28 12:59:45 +00006936 Alignment = MinAlign(Alignment, IncrementSize);
Nate Begemanbd117f02007-11-15 21:15:26 +00006937 Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
Chris Lattner32206f52006-03-18 01:44:44 +00006938
6939 // Build a factor node to remember that this load is independent of the
6940 // other one.
6941 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
6942 Hi.getValue(1));
6943
6944 // Remember that we legalized the chain.
6945 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattner32206f52006-03-18 01:44:44 +00006946 break;
6947 }
Dan Gohmana8665142007-06-25 16:23:39 +00006948 case ISD::BIT_CONVERT: {
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006949 // We know the result is a vector. The input may be either a vector or a
6950 // scalar value.
Dan Gohman0de76942007-06-29 00:09:08 +00006951 SDOperand InOp = Node->getOperand(0);
6952 if (!MVT::isVector(InOp.getValueType()) ||
6953 MVT::getVectorNumElements(InOp.getValueType()) == 1) {
6954 // The input is a scalar or single-element vector.
6955 // Lower to a store/load so that it can be split.
6956 // FIXME: this could be improved probably.
Chris Lattnerd6f7d442007-10-15 17:48:57 +00006957 SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType());
Dan Gohman54d3b5a2008-02-11 18:58:42 +00006958 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val);
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006959
Evan Chengdf9ac472006-10-05 23:01:46 +00006960 SDOperand St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman2d489b52008-02-06 22:27:42 +00006961 InOp, Ptr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00006962 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00006963 FI->getIndex());
6964 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohman16d4bc32008-02-07 18:41:25 +00006965 PseudoSourceValue::getFixedStack(),
Dan Gohman2d489b52008-02-06 22:27:42 +00006966 FI->getIndex());
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006967 }
Dan Gohman0de76942007-06-29 00:09:08 +00006968 // Split the vector and convert each of the pieces now.
6969 SplitVectorOp(InOp, Lo, Hi);
Nate Begemanbd117f02007-11-15 21:15:26 +00006970 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
6971 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman0de76942007-06-29 00:09:08 +00006972 break;
Chris Lattnerd7c4e7d2006-03-23 21:16:34 +00006973 }
Chris Lattner32206f52006-03-18 01:44:44 +00006974 }
6975
6976 // Remember in a map if the values will be reused later.
Chris Lattner4b0ddb22007-02-04 01:17:38 +00006977 bool isNew =
Chris Lattner32206f52006-03-18 01:44:44 +00006978 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman0de76942007-06-29 00:09:08 +00006979 assert(isNew && "Value already split?!?");
Chris Lattner32206f52006-03-18 01:44:44 +00006980}
6981
6982
Dan Gohmanf4e86da2007-06-27 14:06:22 +00006983/// ScalarizeVectorOp - Given an operand of single-element vector type
6984/// (e.g. v1f32), convert it into the equivalent operation that returns a
6985/// scalar (e.g. f32) value.
Dan Gohmana8665142007-06-25 16:23:39 +00006986SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) {
6987 assert(MVT::isVector(Op.getValueType()) &&
6988 "Bad ScalarizeVectorOp invocation!");
Chris Lattner32206f52006-03-18 01:44:44 +00006989 SDNode *Node = Op.Val;
Dan Gohmana8665142007-06-25 16:23:39 +00006990 MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType());
6991 assert(MVT::getVectorNumElements(Op.getValueType()) == 1);
Chris Lattner32206f52006-03-18 01:44:44 +00006992
Dan Gohmana8665142007-06-25 16:23:39 +00006993 // See if we already scalarized it.
6994 std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op);
6995 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattner32206f52006-03-18 01:44:44 +00006996
6997 SDOperand Result;
6998 switch (Node->getOpcode()) {
Chris Lattner29b23012006-03-19 01:17:20 +00006999 default:
Jim Laskeyc3d341e2006-07-11 17:58:07 +00007000#ifndef NDEBUG
Dan Gohmanb4c26902007-06-04 16:17:33 +00007001 Node->dump(&DAG); cerr << "\n";
Jim Laskeyc3d341e2006-07-11 17:58:07 +00007002#endif
Dan Gohmana8665142007-06-25 16:23:39 +00007003 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7004 case ISD::ADD:
7005 case ISD::FADD:
7006 case ISD::SUB:
7007 case ISD::FSUB:
7008 case ISD::MUL:
7009 case ISD::FMUL:
7010 case ISD::SDIV:
7011 case ISD::UDIV:
7012 case ISD::FDIV:
7013 case ISD::SREM:
7014 case ISD::UREM:
7015 case ISD::FREM:
Dan Gohman2a7de412007-10-11 23:57:53 +00007016 case ISD::FPOW:
Dan Gohmana8665142007-06-25 16:23:39 +00007017 case ISD::AND:
7018 case ISD::OR:
7019 case ISD::XOR:
7020 Result = DAG.getNode(Node->getOpcode(),
Chris Lattner32206f52006-03-18 01:44:44 +00007021 NewVT,
Dan Gohmana8665142007-06-25 16:23:39 +00007022 ScalarizeVectorOp(Node->getOperand(0)),
7023 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattner32206f52006-03-18 01:44:44 +00007024 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007025 case ISD::FNEG:
7026 case ISD::FABS:
7027 case ISD::FSQRT:
7028 case ISD::FSIN:
7029 case ISD::FCOS:
7030 Result = DAG.getNode(Node->getOpcode(),
7031 NewVT,
7032 ScalarizeVectorOp(Node->getOperand(0)));
7033 break;
Dan Gohman4f056f32007-10-12 14:13:46 +00007034 case ISD::FPOWI:
7035 Result = DAG.getNode(Node->getOpcode(),
7036 NewVT,
7037 ScalarizeVectorOp(Node->getOperand(0)),
7038 Node->getOperand(1));
7039 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007040 case ISD::LOAD: {
7041 LoadSDNode *LD = cast<LoadSDNode>(Node);
7042 SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7043 SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Chris Lattner32206f52006-03-18 01:44:44 +00007044
Dan Gohmana8665142007-06-25 16:23:39 +00007045 const Value *SV = LD->getSrcValue();
7046 int SVOffset = LD->getSrcValueOffset();
7047 Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset,
7048 LD->isVolatile(), LD->getAlignment());
7049
Chris Lattner32206f52006-03-18 01:44:44 +00007050 // Remember that we legalized the chain.
7051 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7052 break;
7053 }
Dan Gohmana8665142007-06-25 16:23:39 +00007054 case ISD::BUILD_VECTOR:
7055 Result = Node->getOperand(0);
Chris Lattner32206f52006-03-18 01:44:44 +00007056 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007057 case ISD::INSERT_VECTOR_ELT:
7058 // Returning the inserted scalar element.
7059 Result = Node->getOperand(1);
7060 break;
7061 case ISD::CONCAT_VECTORS:
Dan Gohman26455c42007-06-13 15:12:02 +00007062 assert(Node->getOperand(0).getValueType() == NewVT &&
7063 "Concat of non-legal vectors not yet supported!");
7064 Result = Node->getOperand(0);
7065 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007066 case ISD::VECTOR_SHUFFLE: {
7067 // Figure out if the scalar is the LHS or RHS and return it.
7068 SDOperand EltNum = Node->getOperand(2).getOperand(0);
7069 if (cast<ConstantSDNode>(EltNum)->getValue())
7070 Result = ScalarizeVectorOp(Node->getOperand(1));
7071 else
7072 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner29b23012006-03-19 01:17:20 +00007073 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007074 }
7075 case ISD::EXTRACT_SUBVECTOR:
7076 Result = Node->getOperand(0);
Dan Gohman26455c42007-06-13 15:12:02 +00007077 assert(Result.getValueType() == NewVT);
7078 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007079 case ISD::BIT_CONVERT:
7080 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0));
Chris Lattnerf6f94d32006-03-28 20:24:43 +00007081 break;
Dan Gohmana8665142007-06-25 16:23:39 +00007082 case ISD::SELECT:
Chris Lattner02274a52006-04-08 22:22:57 +00007083 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohmana8665142007-06-25 16:23:39 +00007084 ScalarizeVectorOp(Op.getOperand(1)),
7085 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattner02274a52006-04-08 22:22:57 +00007086 break;
Chris Lattner32206f52006-03-18 01:44:44 +00007087 }
7088
7089 if (TLI.isTypeLegal(NewVT))
7090 Result = LegalizeOp(Result);
Dan Gohmana8665142007-06-25 16:23:39 +00007091 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7092 assert(isNew && "Value already scalarized?");
Chris Lattner32206f52006-03-18 01:44:44 +00007093 return Result;
7094}
7095
Chris Lattnerdc750592005-01-07 07:47:09 +00007096
7097// SelectionDAG::Legalize - This is the entry point for the file.
7098//
Chris Lattner4add7e32005-01-23 04:42:50 +00007099void SelectionDAG::Legalize() {
Chris Lattneref598052006-04-02 03:07:27 +00007100 if (ViewLegalizeDAGs) viewGraph();
7101
Chris Lattnerdc750592005-01-07 07:47:09 +00007102 /// run - This is the main entry point to this class.
7103 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00007104 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00007105}
7106